Skip to content

define doesn't work with keys starting with $ #5956

@otakustay

Description

@otakustay

Describe the bug

define config doesn't work with keys starting with $ like $foo.bar

Reproduction

  1. From vite vanilla template

  2. Add following to vite.config.ts:

    import { defineConfig } from 'vite'
    
    // https://vitejs.dev/config/
    export default defineConfig({
      define: {
        '$build.foo': '"bar"',
        'dev.foo': '"bar"',
      },
    })
  3. change main.js to following:

    console.log($build.foo);
    console.log(dev.foo);
  4. Run vite build

  5. Open dist/assets/index.xxx.js and find console.log calls

We expect both console.log arguments to be replaced to "bar", however the actual build asset is (formatted):

console.log($build.foo);
console.log("bar");

Keys starts with $ is not replaced.

By digging a little deeper, we simplified the regex pattern generated to this:

const regex = /\b(\$build\.foo)\b/g
regex.test('$build.foo'); // false

Without $ prefix regex works as expected:

const c = /\b(build\.foo)\b/g;
console.log(c.test('build.foo')); // true

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

vite:config bundled config file loaded in 10.75ms +0ms
  vite:config using resolved config: {
  vite:config   define: { '$build.foo': '"bar"', 'dev.foo': '"bar"' },
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   configFile: '/Users/otakustay/Downloads/vite-issue/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/otakustay/Downloads/vite-issue',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/otakustay/Downloads/vite-issue/public',
  vite:config   cacheDir: '/Users/otakustay/Downloads/vite-issue/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   }
  vite:config } +2ms
vite v2.6.14 building for production...
✓ 3 modules transformed.
dist/assets/favicon.17e50649.svg   1.49 KiB
dist/index.html                    0.39 KiB
dist/assets/index.77d0d5b3.js      0.74 KiB / gzip: 0.42 KiB

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions