【发布时间】:2020-12-04 19:24:26
【问题描述】:
似乎 Webpack 的 tree shaking 功能有助于从包中删除未使用的代码。但是,Webpack 确实会读取这些不可读的文件。我如何告诉 Webpack 不要阅读它们?
这是一个例子:
index.js
import { bar } from './bar';
bar();
bar/index.js
export { bar } from './bar';
export { foo } from './foo';
bar/foo.js
import fs from 'fs';
export function foo() {}
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
mode: 'production',
module: {
rules: [
{ sideEffects: false }
],
},
optimization: {
usedExports: true
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
运行 Webpack 会导致以下错误:
ERROR in ./src/bar/foo.js Module not found: Error: Can't resolve 'fs' in "/temp/webpack-test/src/bar'
我希望 Webpack 不会读取 foo.js 文件,因为没有从入口点到该文件的路由。
【问题讨论】:
标签: webpack tree-shaking