简体   繁体   English

如何防止在 webpack 构建期间丢弃未使用的代码?

[英]How to prevent unused code from being dropped during webpack build?

I am trying to use webpack (v 5.6.0) to bundle some JavaScript into an ESM (aka. ES6 module, ECMAScript Module) which doesn't run on its own, but rather can be used as a library for another app.我正在尝试使用 webpack (v 5.6.0) 将一些 JavaScript 捆绑到一个 ESM(又名 ES6 模块,ECMAScript 模块)中,它不能自行运行,而是可以用作另一个应用程序的库。 I want the other app to have the option to include this library simply using <script src="..." type="module"> without having to have its own build step and worry about dependencies.我希望其他应用程序可以选择仅使用<script src="..." type="module">来包含此库,而不必拥有自己的构建步骤并担心依赖关系。

In the library's project I've added "type": "module" to my package.json to let it know it's an ESM , and I have a src folder with an index.js and a dist folder for the output of webpack. In the library's project I've added "type": "module" to my package.json to let it know it's an ESM , and I have a src folder with an index.js and a dist folder for the output of webpack.

My webpack.config.cjs file looks like:我的webpack.config.cjs文件如下所示:

const path = require('path');

module.exports = {
    entry: {
        index: './src/index.js',
        // plan to have more here for code spliting
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].bundle.min.js',
    },
    optimization: {
        // removeEmptyChunks: false, // Tried this, didn't seem to help
    },
};

And a simplified version of my index.js is:我的index.js的简化版本是:

const hello = () => { console.log('Hello world'); };
export default { hello };

Eventually there will be actual code here, many different files, dependencies, etc.最终这里会有实际的代码、许多不同的文件、依赖项等。

When I run webpack, a index.bundle.min.js file is created, but it is completely blank .当我运行 webpack 时,创建一个index.bundle.min.js文件,但它是完全空白的。 For this simple example, I would expect it to look very much like the source file.对于这个简单的示例,我希望它看起来非常像源文件。 It appears that Webpack is dropping code that isn't being run, which might be useful optimization in some cases, it is not what I want.似乎 Webpack 正在丢弃未运行的代码,这在某些情况下可能是有用的优化,这不是我想要的。

If I add hello();如果我添加hello(); to my index.js then I do get some code in my built file, but I don't want to run any code, and it's still not exposing the methods for later use like I'd like.到我的index.js然后我确实在我的构建文件中得到了一些代码,但我不想运行任何代码,它仍然没有像我想要的那样公开这些方法以供以后使用。

Pytth's solution solves the problem of the code not showing up, but doesn't solve the problem of creating a library file in ESM (ie, using export ). Pytth 的解决方案解决了代码不显示的问题,但没有解决在 ESM 中创建库文件的问题(即使用export )。 Sadly I think Webpack may just be missing the obvious option for libraryTarget: 'esm' .可悲的是,我认为 Webpack 可能只是缺少libraryTarget: 'esm'的明显选项

I was reminded of a solution I (regrettably) used once: Creating one build file with config options that output the library onto the window :我想起了我(遗憾地)使用过一次的解决方案:使用 output 库到window的配置选项创建一个构建文件:

entry: {
    'myLib': './src/index.js',
},
output: {
    library: '[name]',
    libraryExport: 'default',
    libraryTarget: 'window',
    path: path.resolve(__dirname, './dist'),
    filename: '[name].window.js',
},

Then including another (non-generated) file in the dist folder called, for example, myLib.esm.js that just has:然后在dist文件夹中包含另一个(非生成的)文件,例如myLib.esm.js ,它只有:

import './myLib.window.js';
const myLib = window.myLib;
delete window.myLib;
export default myLib;

It's ugly, but it does work.这很丑陋,但它确实有效。

Webpack actually has a setting for creating libraries ? Webpack 实际上有创建库的设置? Have you tried that?你试过吗?

module.exports = {
  //...
  output: {
    library: 'MyLibrary'
  }
};

Here is some additional info. 是一些附加信息。

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

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