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

lazy imported modules are not bundled correctly on building #4840

Closed
7 tasks done
JounQin opened this issue Sep 4, 2021 · 20 comments
Closed
7 tasks done

lazy imported modules are not bundled correctly on building #4840

JounQin opened this issue Sep 4, 2021 · 20 comments
Labels
feat: css inconsistency Inconsistency between dev & build pending triage

Comments

@JounQin
Copy link
Contributor

JounQin commented Sep 4, 2021

Describe the bug

The following pattern runs very well on development:

const [data, error] = usePromise(() =>
  Promise.all([
    Promise.any([
      import(
        `../../../node_modules/prismjs/themes/prism-${theme}.css`
      ) as Promise<unknown>,
      import(
        `../../../node_modules/prism-themes/themes/prism-${theme}.css`
      ) as Promise<unknown>,
    ]),
    import(
      `../../../node_modules/prismjs/components/prism-${
        lang === 'js' ? 'javascript' : lang
      }.min.js`
    ) as Promise<unknown>,
  ]),
)

But it seems not been bundled at all on building, confirmed with build.rollupOptions.manualChunks

Reproduction

https://github.com/JounQin/test/tree/repro/vite_public_path

yarn dev works fine

image

yarn build && yarn serve does not work

image

And there is no prism related chunk in dist/assets

image

In my true app (it's a bit huge for a reproduction), they are bundled as prism.*.js chunks correctly, but the public path seems broken

image

System Info

System:
    OS: macOS 12.0
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 24.68 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.5 - ~/.nvm/versions/node/v12.22.5/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 6.14.14 - ~/.nvm/versions/node/v12.22.5/bin/npm
  npmPackages:
    vite: ^2.5.3 => 2.5.3

Used Package Manager

yarn

Logs

$ vite build --debug
  vite:config bundled config file loaded in 121ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:react-jsx',
  vite:config     'react-refresh',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  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     'asset-import-meta-url',
  vite:config     'vite:style-import',
  vite:config     'vite:import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'load-fallback'
  vite:config   ],
  vite:config   server: { open: true, fs: { strict: undefined, allow: [Array] } },
  vite:config   css: { preprocessorOptions: { less: [Object] } },
  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     sourcemap: false,
  vite:config     rollupOptions: { output: [Object] },
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     minify: 'terser',
  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     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null
  vite:config   },
  vite:config   resolve: { dedupe: [ 'react', 'react-dom' ], alias: [ [Object], [Object] ] },
  vite:config   configFile: '/Users/JounQin/Workspaces/Local/test/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/JounQin/Workspaces/Local/test',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/JounQin/Workspaces/Local/test/public',
  vite:config   cacheDir: '/Users/JounQin/Workspaces/Local/test/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  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: { esbuildOptions: { keepNames: undefined } }
  vite:config } +11ms
vite v2.5.3 building for production...
transforming (268) node_modules/rc-tooltip/es/index.js  vite:resolve 0ms   ./themes/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/themes/index.less +0ms
  vite:resolve 0ms   ./core/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/index.less +0ms
  vite:resolve 1ms   ./default.less -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/themes/default.less +3ms
  vite:resolve 1ms   ../mixins/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/index.less +0ms
  vite:resolve 1ms   base -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/base.less +0ms
  vite:resolve 1ms   global -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/global.less +0ms
  vite:resolve 1ms   iconfont -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/iconfont.less +0ms
  vite:resolve 1ms   motion -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion.less +0ms
  vite:resolve 9ms   ../color/colors -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/color/colors.less +34ms
  vite:resolve 8ms   size -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/size.less +0ms
  vite:resolve 8ms   compatibility -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/compatibility.less +0ms
  vite:resolve 8ms   clearfix -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/clearfix.less +0ms
  vite:resolve 8ms   iconfont -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/iconfont.less +0ms
  vite:resolve 8ms   motion -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/motion.less +0ms
  vite:resolve 8ms   reset -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/reset.less +0ms
  vite:resolve 8ms   operation-unit -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/operation-unit.less +0ms
  vite:resolve 8ms   typography -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/typography.less +0ms
  vite:resolve 8ms   customize -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/customize.less +0ms
  vite:resolve 8ms   box -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/box.less +0ms
  vite:resolve 8ms   modal-mask -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/modal-mask.less +0ms
  vite:resolve 2ms   ../themes/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/themes/index.less +0ms
  vite:resolve 3ms   ../mixins/iconfont -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/iconfont.less +1ms
  vite:resolve 3ms   ../mixins/motion -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/motion.less +0ms
  vite:resolve 3ms   motion/fade -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion/fade.less +0ms
  vite:resolve 3ms   motion/move -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion/move.less +0ms
  vite:resolve 3ms   motion/other -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion/other.less +0ms
  vite:resolve 3ms   motion/slide -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion/slide.less +0ms
  vite:resolve 3ms   motion/zoom -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/core/motion/zoom.less +0ms
  vite:resolve 14ms  colorPalette -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/color/colorPalette.less +22ms
  vite:resolve 12ms  ../../style/themes/default -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/themes/default.less +0ms
  vite:resolve 0ms   bezierEasing -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/color/bezierEasing.less +2ms
  vite:resolve 0ms   tinyColor -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/color/tinyColor.less +0ms
  vite:resolve 0ms   ../../style/themes/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/themes/index.less +201ms
  vite:resolve 1ms   ../../style/mixins/index -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/style/mixins/index.less +1ms
  vite:resolve 1ms   ./mixin -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/button/style/mixin.less +0ms
  vite:resolve 1ms   ./rtl -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/button/style/rtl.less +0ms
  vite:resolve 0ms   ./rtl -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/result/style/rtl.less +234ms
transforming (571) node_modules/rc-menu/es/SubMenu/PopupTrigger.js  vite:resolve 1ms   ./rtl -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/spin/style/rtl.less +1s
transforming (596) node_modules/rc-rate/es/util.js  vite:resolve 1ms   ./rtl -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/alert/style/rtl.less +371ms
  vite:resolve 1ms   ./rtl -> /Users/JounQin/Workspaces/Local/test/node_modules/antd/es/skeleton/style/rtl.less +108ms

Validations

@JounQin
Copy link
Contributor Author

JounQin commented Sep 4, 2021

In my true app (it's a bit huge for a reproduction), they are bundled as prism.*.js chunks correctly, but the public path seems broken

If I set base: 'http://localhost:3000/' it will result http://localhost:3000/http://localhost:3000/assets/prism-dracula.a4e77483.css...

image

@JounQin
Copy link
Contributor Author

JounQin commented Sep 4, 2021

The problem is here

image

image

It results

image

@JounQin
Copy link
Contributor Author

JounQin commented Sep 4, 2021

I finished up by patch vite for now.

diff --git a/node_modules/vite/dist/node/chunks/dep-1be34a63.js b/node_modules/vite/dist/node/chunks/dep-1be34a63.js
index e0418aa..64f4a95 100644
--- a/node_modules/vite/dist/node/chunks/dep-1be34a63.js
+++ b/node_modules/vite/dist/node/chunks/dep-1be34a63.js
@@ -34720,7 +34720,7 @@ function preload(baseModule, deps) {
     }
     return Promise.all(deps.map((dep) => {
         // @ts-ignore
-        dep = `${base}${dep}`;
+        dep = /^(https?:\/)?\//.test(dep) ? dep : `${base}${dep}`;
         // @ts-ignore
         if (dep in seen)
             return;

But it still does not work in my reproduction, that seems two different problems.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 10, 2021

This is a bug introduced by #3333, I have submitted pr #4740

@JounQin
Copy link
Contributor Author

JounQin commented Sep 11, 2021

@hex-ci Is the reproduction related too? It seems two different issues, because in the reproduction, there is no prism chunks generated at all.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 11, 2021

The bug introduced by #3333 will affect all dynamically imported .css files, because only the dynamically imported .css file part of the code writes a duplicate config.base attribute. Why does this bug occur? I have a simple analysis in #4731.

@sodatea sodatea added inconsistency Inconsistency between dev & build feat: css has pr labels Sep 11, 2021
@hex-ci
Copy link
Contributor

hex-ci commented Sep 13, 2021

Vite 2.5.7 version has been released, you can try to see if this version solves your problem.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 14, 2021

@hex-ci Hi, I tried my local project, it seems working now.

However, it is still broken on the reproduction, as I said before, it seems a different issue.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

Vite 2.5.7 fixes the problem of .css dynamic import failure caused by incorrect base. If you have this problem, it should be fixed. I took a look at the code repository you provided, and it seems that there is no css dynamic import code, it does not seem to have anything to do with this problem, haha.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 14, 2021

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

I took a look, the path of ../../../node_modules/prismjs/themes/prism-${theme}.css in the code you mentioned is wrong, you wrote an extra ../ , Written as ../../node_modules/prismjs/themes/prism-${theme}.css, I just tried it, no problem. There are also several other paths with more ../.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 14, 2021

@hex-ci Hmmm, but it works on development?

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

I tried the development environment, and I found that /node_modules/prismjs/themes/prism-dracula.css this is a 404, but this file does not exist in the file system, so this 404 is normal. Then, I did not find any other problems.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

As for why writing an extra ../ is normal in the development environment, I guess it is because the root directory in the development environment has been restricted to the project directory, so how many ../ you write will not exceed the project Directory, I tried to write multiple ../ and it didn't matter, but if you write less ../, an error will be reported.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

@hex-ci Hmmm, but it works on development?

I think that after modifying the path, the development environment is OK. Vite 2.5.7 solves your problem.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 14, 2021

@hex-ci Hmmm, but it works on development?

I think that after modifying the path, the development environment is OK. Vite 2.5.7 solves your problem.

Without modifying or upgrading to 2.5.7, it is still working on development when using 2.5.6.

@JounQin
Copy link
Contributor Author

JounQin commented Sep 14, 2021

As for why writing an extra ../ is normal in the development environment, I guess it is because the root directory in the development environment has been restricted to the project directory, so how many ../ you write will not exceed the project Directory, I tried to write multiple ../ and it didn't matter, but if you write less ../, an error will be reported.

I think it's the root cause, so vite should warn about incorrect ../ levels, right?

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

As for why writing an extra ../ is normal in the development environment, I guess it is because the root directory in the development environment has been restricted to the project directory, so how many ../ you write will not exceed the project Directory, I tried to write multiple ../ and it didn't matter, but if you write less ../, an error will be reported.

I think it's the root cause, so vite should warn about incorrect ../ levels, right?

Maybe Vite needs to warn that your path exceeds the project root directory.

@hex-ci
Copy link
Contributor

hex-ci commented Sep 14, 2021

@hex-ci Hmmm, but it works on development?

I think that after modifying the path, the development environment is OK. Vite 2.5.7 solves your problem.

Without modifying or upgrading to 2.5.7, it is still working on development when using 2.5.6.

Your problem is actually caused by two aspects. One is a bug in Vite, and the other is that your path is written incorrectly. After both of these problems are fixed, your project will be normal.

@benmccann
Copy link
Collaborator

#4740 was released in 2.5.7 so it sounds like this is fixed https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md#257-2021-09-13

I'll close this as a result. Please file a new issue requesting a warning when an extra .. is provided since that is a separate issue

@github-actions github-actions bot locked and limited conversation to collaborators Oct 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: css inconsistency Inconsistency between dev & build pending triage
Projects
None yet
Development

No branches or pull requests

4 participants