Webpack 4中无Babel的摇树

时间:2018-09-10 09:44:45

标签: reactjs webpack antd tree-shaking

Webpack 4中是否可以在不使用Babel的情况下摇动外部模块(如React或Antd)(摇树)?

示例React结构: node_modules-> react文件夹包含/ cjs和/ umd目录。

设置:
项目: https://github.com/webpack/webpack/tree/master/examples/harmony-unused
组态 https://webpack.js.org/guides/tree-shaking/
Typescript-Loader(可选)https://github.com/TypeStrong/ts-loader

src文件将被摇动,所有文件都包含antd之类的node_module或不作反应。

问候里奇

1 个答案:

答案 0 :(得分:0)

您可以使用外部组件从所有程序包中删除react和reactdom或任何其他程序包,并使用页面上的script标记加载一次。

const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
externals: {
react: 'React',
'react-dom': 'ReactDOM'
},
entry:{},//entry files
output: {} // output path and files
plugins: [
new CopyWebpackPlugin([
{
from: path.join('src/Commons/Polyfills/Symbol.js'),
to: 'manifest.js'
},
{
from : path.join('node_modules/react/umd/react.production.min.js'),
to : 'react.js'
},
{
from : path.join('node_modules/react-dom/umd/react-dom.production.min.js'),
to : 'react-dom.js'
}
]),
]
};