简体   繁体   English

`export` 或 `import` 在 webpack 生产与开发模式下的行为不同

[英]`export` or `import` behaving different in webpack production than in development mode

I'm trying to figure out why an app (Vuejs3, webpack 5, Babel 7) behaves differently in production than in development mode.我试图弄清楚为什么应用程序(Vuejs3、webpack 5、Babel 7)在生产模式和开发模式下的行为不同。 For some reason, in production mode, the imports and exports are working differently (ie files aren't imported at all, ie their code isn't run during import).出于某种原因,在生产模式下,导入和导出的工作方式不同(即根本不导入文件,即它们的代码在导入期间不运行)。

This is the structure which works in development mode:这是在开发模式下工作的结构:

+- foo/
| +-- index.js
|     
|     export {default as Component} from './comp';
|     export {default as SomethingElse} from './comp2';
|
| +-- comp.vue
|
|     alert('hey there!');     
|     export default {...component here...}
|
+- main.js
   
   import './foo';

This works all fine in development mode, but not at all in production.这在开发模式下一切正常,但在生产中根本不行。 I seems as if the code in comp.vue is never being executed and thus the component isn't imported/registered.我似乎好像comp.vue中的代码从未被执行,因此组件没有被导入/注册。 I tried to use re-exporting/aggregation in index.js instead, but that didn't change anything.我尝试在index.js中使用重新导出/聚合,但这并没有改变任何东西。

If I change main.js to import {Component} from './foo' then everything works fine.如果我将 main.js 更改为import {Component} from './foo' main.js一切正常。 However the import 'file' pattern is used so often that I would struggle to change it (and it works in development mode).然而, import 'file'模式的使用如此频繁,以至于我很难改变它(它在开发模式下工作)。

Another way to make this work is to set the source map to eval-cheap-source-map (instead of source-map ) in production which is the same source map method used as in development.完成这项工作的另一种方法是在生产中将源 map 设置为eval-cheap-source-map (而不是source-map ),这与开发中使用的源 map 方法相同。 Conversely, if I change source map to source-map in development mode, the component is successfully loaded.相反,如果我在开发模式下将 source map 更改为source-map ,则组件加载成功。

Which leads me to the conclusion that webpack is somehow handling export/import different in production that in develop (the source-map observation is just an observation, but worth noting).这使我得出结论,webpack 以某种方式处理生产中与开发中不同的导出/导入(源图观察只是一个观察,但值得注意)。 I'm a bit puzzled as to why this happens, so would like to appreciate any experience that you can share.我对为什么会发生这种情况感到有些困惑,因此希望感谢您可以分享的任何经验。

Note笔记

This may be an issue with some of the production mode optimisation steps.这可能是某些生产模式优化步骤的问题。 Is there any way to disable them (all of them to see whether this is the general issue and then disable/re-enable parts of them to find the "culprit"?)有什么办法可以禁用它们(所有人都看看这是否是一般问题,然后禁用/重新启用它们的一部分以找到“罪魁祸首”?)

After many unsuccessful attempts to fix this in the webpack configuration, I arrived at this configuration:在多次尝试在 webpack 配置中修复此问题后,我得到了以下配置:

optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerPlugin(),
      new TerserPlugin({
        terserOptions: {
          compress: {
            unused: false
          }
        }
      })
    ]
  },

unused (default: true) -- drop unreferenced functions and variables (simple direct variable assignments do not count as references unless set to "keep_assign") unused (默认值:true)- 删除未引用的函数和变量(简单的直接变量赋值不计为引用,除非设置为“keep_assign”)

This seems to avoid side effect code from being culled when using re-exports.这似乎可以避免在使用重新导出时剔除副作用代码。 I'm yet to find out why the code was deemed side effect free though (which is not the default as far as I can tell).我还没有弄清楚为什么代码被认为是无副作用的(据我所知,这不是默认值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM