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

When using import.meta.glob the build fails, but works with import.meta.globEager #5568

Closed
7 tasks done
4refael opened this issue Nov 6, 2021 · 3 comments
Closed
7 tasks done
Labels
bug: upstream Bug in a dependency of Vite

Comments

@4refael
Copy link

4refael commented Nov 6, 2021

Describe the bug

I have files that contain a single quote in their names, for example: data/y'=0.json
I'm trying to use import.meta.glob for lazy loading all the files as needed.
This works fine in dev mode. But once I try to build for the production it fails.
Should note the build succeeds when using import.meta.globEager instead of import.meta.glob.

I've tried to use the latest stable Vite version and the beta version. Both fail.

Reproduction

Create a directory data and a JSON file data/y'=0.json with some dummy data in it.
Try to load the file using import.meta.glob:

const modules = import.meta.glob('./data/**/*.json');

for (const path in modules) {
  modules[path]().then((mod) => {
    console.log(path, mod);
  });
}

Note npm run dev will work as expected. But npm run build will fail.

System Info

npx envinfo --system --npmPackages '{vite,@vitejs/*}' --binaries --browsers                 3073  10:20:47
npx: installed 1 in 1.733s

  System:
    OS: macOS 12.0.1
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
    Memory: 286.13 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.1 - /usr/local/bin/node
    npm: 6.14.12 - /usr/local/bin/npm
  Browsers:
    Chrome: 95.0.4638.69
    Firefox: 88.0
    Safari: 15.1
    Safari Technology Preview: 15.4
  npmPackages:
    @vitejs/plugin-vue: ^1.10.0-beta.0 => 1.10.0-beta.0 
    vite: ^2.7.0-beta.1 => 2.7.0-beta.1

Used Package Manager

npm

Logs

~/Sites/vite-project  npm run build -- --debug                                                                    3075  10:22:11

> vite-project@0.0.0 build /Users/refael/Sites/vite-project
> vite build "--debug"

  vite:config bundled config file loaded in 208.17ms +0ms
  vite:config using resolved config: {
  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:vue',
  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   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   define: { __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false },
  vite:config   ssr: { external: [ 'vue', '@vue/server-renderer' ] },
  vite:config   configFile: '/Users/refael/Sites/vite-project/vite.config.js',
  vite:config   configFileDependencies: [ 'vite.config.js' ],
  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/refael/Sites/vite-project',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/refael/Sites/vite-project/public',
  vite:config   cacheDir: '/Users/refael/Sites/vite-project/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: true, allow: [Array], deny: [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 } +5ms
vite v2.7.0-beta.1 building for production...
✓ 15 modules transformed.
  vite:esbuild esbuild error with options used:  {
  sourcemap: true,
  sourcefile: 'assets/index.67f80057.js',
  target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  minify: true,
  treeShaking: true,
  format: 'esm',
  loader: 'js',
  tsconfigRaw: { compilerOptions: {} }
} +0ms
[vite:esbuild-transpile] Transform failed with 1 error:
assets/index.67f80057.js:100:234: error: Syntax error "a"

Syntax error "a"
98 |  // This starter template is using Vue 3 <script setup> SFCs
99 |  // Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
100|  const items = { "./data/y'=0.json": () => (true? __vitePreload(()=>import('./y'=0.e072adcd.js'),"__VITE_PRELOAD__"): import('./y'=0.e072adcd.js')),};
   |                                                                                                                                                                                                                                            ^
101|

error during build:
Error: Transform failed with 1 error:
assets/index.67f80057.js:100:234: error: Syntax error "a"
    at failureErrorWithLog (/Users/refael/Sites/vite-project/node_modules/esbuild/lib/main.js:1493:15)
    at /Users/refael/Sites/vite-project/node_modules/esbuild/lib/main.js:1282:29
    at /Users/refael/Sites/vite-project/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/Users/refael/Sites/vite-project/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/Users/refael/Sites/vite-project/node_modules/esbuild/lib/main.js:596:7)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:188:23)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! vite-project@0.0.0 build: `vite build "--debug"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the vite-project@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/refael/.npm/_logs/2021-11-06T08_22_34_155Z-debug.log

Validations

@4refael
Copy link
Author

4refael commented Nov 6, 2021

After looking at the logs, I think this is an escaping issue.

const items = { "./data/y'=0.json": () => (true? __vitePreload(()=>import('./y'=0.e072adcd.js'),"__VITE_PRELOAD__"): import('./y'=0.e072adcd.js')),};

The import statement is compiled as import('./y'=0.e072adcd.js') which is semantically wrong for this file name.
I believe that if backticks were used it would resolve the issue.

Edit:
I'm looking at the source code and seems like JSON.stringify is used to handle proper escaping.

let imp = `import(${JSON.stringify(importee)})`;

This seems OK but somehow it still ends up with incorrect escaping.
Possibly this gets changed somewhere afterward...

@ygj6
Copy link
Member

ygj6 commented Nov 11, 2021

It's a rollup transform problem. The source code generated by vite is valid:

import("./y'=0.e072adcd.js")

After rollup's transform, the code becomes:

import('./y'=0.e072adcd.js')

Here is a Reproduction of Rollup.

@ygj6 ygj6 added bug: upstream Bug in a dependency of Vite and removed pending triage labels Nov 11, 2021
@bluwy
Copy link
Member

bluwy commented Mar 28, 2022

Fixed in rollup/rollup#4286. Pulling in Vite should bring in the new Rollup version, which should resolve this.

@bluwy bluwy closed this as completed Mar 28, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite
Projects
None yet
Development

No branches or pull requests

3 participants