question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

regex based filter in asset-import-meta-url ignores actual code in some cases

See original GitHub issue

Describe the bug

Hey!

When using new URL(..., import.meta.url), for some cases vite does not transform the URLs to point to bundled assets (only when building though, works fine in dev 🧑‍💻)

Reproduction

Go to repro and in the terminal do

npm run build
npm run preview

Browser console should show (notice the second URL isn’t transformed) image

Why?

It seems these regex filters are at fault https://github.com/vitejs/vite/blob/4433df4c6b278d5a2f4d2ca14f0a7930ed647e80/packages/vite/src/node/plugins/assetImportMetaUrl.ts#L34-L37

// before

import "./App.css";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
console.log("1", new URL("./logo.svg", import.meta.url).href);
console.log("2 //", new URL("./logo.svg", import.meta.url).href);
function App() {
  return /* @__PURE__ */ _jsx(_Fragment, {
    children: /* @__PURE__ */ _jsx("img", {
      src: new URL("./logo.svg", import.meta.url).href
    })
  });
}
export default App;
// after filtering with regexes

import '';
import { jsx as _jsx } from '';
import { Fragment as _Fragment } from '';
console.log('', new URL('', import.meta.url).href);
console.log(''img''./logo.svg", import.meta.url).href
    })
  });
}
export default App;

More importantly, the order in which they are applied seems to be the problem i.e. since singlelineCommentsRE is quite broad, removing line comments before strings messes the code in weird ways 🐞

How do we fix? 🔨

AFAICT removing strings before line comments fixes such problems (shouldn’t create new bugs either?)

 const noCommentsCode = code 
   .replace(multilineCommentsRE, blankReplacer) 
-  .replace(singlelineCommentsRE, blankReplacer)
-  .replace(stringsRE, (m) => `'${'\0'.repeat(m.length - 2)}'`)
+  .replace(stringsRE, (m) => `'${'\0'.repeat(m.length - 2)}'`)
+  .replace(singlelineCommentsRE, blankReplacer) 

PS: I stumbled across this when trying to consume Emscripten’s release build (-Oz) from vite. In release emcc minifies all JS and puts it in single line, that line had // somewhere in the middle, and everything went kaboom 💥 🤣

Reproduction

https://stackblitz.com/edit/vitejs-vite-vc6p2d?file=src%2FApp.tsx&terminal=dev

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 84.89 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.asdf/shims/node
    Yarn: 1.22.17 - ~/.asdf/shims/yarn
    npm: 8.1.2 - ~/.asdf/shims/npm
  Browsers:
    Chrome: 99.0.4844.84
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-react: ^1.0.7 => 1.1.4
    vite: ^2.7.2 => 2.7.10

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jawadsh123commented, Apr 11, 2022

thanks @poyoho!

1reaction
poyohocommented, Apr 7, 2022

Thank you very much for your correction 😊

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regex: ignore case sensitivity - Stack Overflow
Here I am trying to find all the folders starting with characters [a-c] in the current folder only.. to do some more manipulation.....
Read more >
Regular Expression (Regex) Tutorial
To match a character having special meaning in regex, you need to use a escape ... denotes raw strings which ignore escape code,...
Read more >
Regular expressions for customizing and filtering alerts
This operator provides ability to define complex filtering in a single condition rule and reduces your dependency to already-defined condition operators. You ...
Read more >
re — Regular expression operations — Python 3.11.1 ...
Source code : Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be...
Read more >
Regular Expressions :: Eloquent JavaScript
Regular expressions are a way to describe patterns in string data. ... special character codes (like \n ) will be preserved, rather than...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found