Skip to content
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

vite build --mode=production rollup syntax error when undefined import.meta.env variable #9369

Closed
7 tasks done
jschaf opened this issue Jul 25, 2022 · 1 comment
Closed
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@jschaf
Copy link

jschaf commented Jul 25, 2022

Describe the bug

Running vite build --mode=production on a TypeScript file that includes import.meta.env with an unset variable errors with Error: Unexpected token on import.meta.env.

I hit this bug attempting to upgrade Vite from 2 to 3.

TypeScript file

export const isFooEnabled = (): boolean =>
  import.meta.env.VITE_FOO === undefined || import.meta.env.VITE_FOO === 'enabled';

console.log('isFooEnabled', isFooEnabled());

Error

vite build --mode=production
vite v3.0.3 building for production...
✓ 3 modules transformed.
Unexpected token (Note that you need plugins to import files that are not JavaScript)
file: /home/projects/vitejs-vite-rw3kdr/src/main.ts:2:14
1: export const isFooEnabled = (): boolean =>
2:   import.meta.env.VITE_FOO === undefined || import.meta.env.VITE_FOO === 'enabled';
                 ^
3: 
4: console.log('isFooEnabled', isFooEnabled());
error during build:
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:1888:30)
    at Module.error (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12441:16)
    at Module.tryParse (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12818:25)
    at Module.setSource (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12723:24)
    at ModuleLoader.addModuleSource (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:22165:20)

Workaround

Ensure that every import.meta.env variable is defined, like so:

 VITE_FOO=enabled vite build --mode=production

Related issues:

Reproduction

https://stackblitz.com/edit/vitejs-vite-rw3kdr?file=src/main.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.10 - /bin/yarn
    npm: 7.17.0 - /bin/npm
  npmPackages:
    vite: ^3.0.2 => 3.0.3

Used Package Manager

npm

Logs

Command:

vite build --mode=production --debug

# Workaround (note the env var goes on the vite command, not tsc)
VITE_FOO=enabled vite build --mode=production --debug
Log output (click to expand)
  vite:config no config file found. +0ms
  vite:config using resolved config: {
  vite:config   root: '/home/projects/vitejs-vite-rw3kdr',
  vite:config   base: '/',
  vite:config   mode: 'production',
  vite:config   configFile: undefined,
  vite:config   logLevel: undefined,
  vite:config   clearScreen: undefined,
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: undefined }
  vite:config   },
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  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   configFileDependencies: [],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: 'production',
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     optimizeDeps: { force: undefined },
  vite:config     build: {}
  vite:config   },
  vite:config   resolve: { alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/home/projects/vitejs-vite-rw3kdr/public',
  vite:config   cacheDir: '/home/projects/vitejs-vite-rw3kdr/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   ssr: {
  vite:config     format: 'esm',
  vite:config     target: 'node',
  vite:config     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     middlewareMode: false,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  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   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false }
  vite:config } +4ms
vite v3.0.3 building for production...
✓ 3 modules transformed.
Unexpected token (Note that you need plugins to import files that are not JavaScript)
file: /home/projects/vitejs-vite-rw3kdr/src/main.ts:2:14
1: export const isFooEnabled = (): boolean =>
2:   import.meta.env.VITE_FOO === undefined || import.meta.env.VITE_FOO === 'enabled';
                 ^
3: 
4: console.log('isFooEnabled', isFooEnabled());
error during build:
Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:1888:30)
    at Module.error (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12441:16)
    at Module.tryParse (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12818:25)
    at Module.setSource (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:12723:24)
    at ModuleLoader.addModuleSource (file:///home/projects/vitejs-vite-rw3kdr/node_modules/rollup/dist/es/shared/rollup.js:22165:20)

Validations

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) and removed p3-minor-bug An edge case that only affects very specific usage (priority) labels Jul 26, 2022
@sapphi-red sapphi-red added pending triage p2-edge-case Bug, but has workaround or limited in scope (priority) labels Jul 26, 2022
@bluwy
Copy link
Member

bluwy commented Jul 27, 2022

You're right with #8663, and it's a duplicate of it. What happens is that Vite is incorrectly replacing that line of code into:

export const isFooEnabled = () => {}.VITE_FOO === undefined || {}.VITE_FOO === 'enabled';

which doesn't work if you paste it in your editor or browser console. A workaround would be to do:

export const isFooEnabled = (): boolean => {
  return import.meta.env.VITE_FOO === undefined || import.meta.env.VITE_FOO === 'enabled';
}

instead. Closing to keep the tracking issue at #8663.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

3 participants