-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
define doesn't work with keys starting with $ #5956
Comments
I'd say this is working as intended, so this would be a feature request. (to change how TL;DR:
So at the start and the end of your replacement string there needs to be a word boundary in order for it to be replaced. A word boundary is a point where a non-word character meets a word character. Word characters are anything that match You usually want to have the key replaced when it is surrounded by non-word characters: // { and }
let msg = `you are in ${_ENV_}`;
// ( and )
console.log(_ENV_);
// space and ;
let env = _ENV_;
// ... At the same, you don't want it replaced when surrounded by word characters: let secretKey = "sshABC_5353jf62_ENV_Pf3g_h"; // don't replace it here
const SOME_OTHER_ENV_DATA = []; // also not here! So, your replacement key must start and end with a word character! In your case, the // `($` isn't a word boundary and doesn't match the `\b` in the RegExp
console.log($build.foo) This however will be replaced: // `o$` in the beginning and `o'` in the end are both word boundaries so the key is surrounded by them
console.log('hello$build.foo') |
Thanks for explanation, I still believe this can be a feature request since the only missing character comparing to JavaScript Identifier syntax is |
Unfortunately not, since JS Identifiers allow Unicode letters and digits, but RegExp Nonetheless, I agree that an exception is probably a good idea here! Edit: Just checked, ES2018 comes with everything we'd need to replace |
Describe the bug
define
config doesn't work with keys starting with$
like$foo.bar
Reproduction
From vite vanilla template
Add following to
vite.config.ts
:change
main.js
to following:Run
vite build
Open
dist/assets/index.xxx.js
and findconsole.log
callsWe expect both
console.log
arguments to be replaced to"bar"
, however the actual build asset is (formatted):Keys starts with
$
is not replaced.By digging a little deeper, we simplified the regex pattern generated to this:
Without
$
prefix regex works as expected:Maybe this is an issue with the regex generation.
System Info
System: OS: macOS 12.0.1 CPU: (8) arm64 Apple M1 Memory: 162.14 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 16.13.0 - /var/folders/9v/sx18bwn925b3f2rp2jfg952w0000gn/T/fnm_multishells/5569_1638539624158/bin/node Yarn: 1.22.17 - /var/folders/9v/sx18bwn925b3f2rp2jfg952w0000gn/T/fnm_multishells/5569_1638539624158/bin/yarn npm: 8.1.0 - /var/folders/9v/sx18bwn925b3f2rp2jfg952w0000gn/T/fnm_multishells/5569_1638539624158/bin/npm Browsers: Chrome: 96.0.4664.55 Safari: 15.1 npmPackages: vite: ^2.6.4 => 2.6.14
Used Package Manager
yarn
Logs
Validations
The text was updated successfully, but these errors were encountered: