reactjs - Webpack treeshaking 不适用于 typescript 和 Babel7
问题描述
当我使用 javascript 进行 treeshaking 时,当我将模式设置为生产时它可以正常工作,但是当我切换到使用 typescript 和 babel 7 时,treeshaking 就不起作用。
我也尝试在 package.json 中设置 "sideEffects": false"
我设置的任何我的 babellrc ["@babel/preset-env", { "modules": false }],
我的 tsconfig
{
"compilerOptions": {
"outDir": "./dist/",
"module": "es6",
"target": "esnext",
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"noEmit": true,
"moduleResolution": "node",
"rootDir": "src",
"isolatedModules": true,
"esModuleInterop": true
},
"include": [
"src"
]
}
我的生产 webpack 配置。请注意,我将优化最小化设置为 false,以便不会最小化输出。我可以清楚地知道它不会摇树
module.exports = merge(common, {
mode: 'production',
output: {
filename: '[name].[chunkhash].js',
chunkFilename: '[name].[chunkhash].chunk.js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.(?:sa|sc|c)ss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
modules: true,
localIdentName: '[path][name]__[local]--[hash:base64:5]',
},
},
'postcss-loader',
'sass-loader',
],
},
],
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
}),
new OptimizeCssAssetsPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html',
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true,
},
}),
],
optimization: {
minimize: false
}
});
常见的 webpack 配置
const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
module.exports = {
entry:["@babel/polyfill", "./src/index.ts"],
output: {
path: path.resolve(__dirname, "dist")
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ]
},
module: {
rules: [
{
test: /\.(ts|js)x?$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
enforce: 'pre',
test: /\.(ts|js)x?$/,
use: {
loader: 'tslint-loader',
options: {
emitErrors: true,
failOnHint: true
}
},
exclude: /node_modules/
},
{
enforce: "pre", test: /\.js$/, loader: "source-map-loader"
},
{
test: /\.(eot|otf|ttf|woff|woff2)$/,
use: 'file-loader'
},
{
test: /\.svg$/,
use: [
{
loader: 'svg-url-loader',
options: {
// Inline files smaller than 10 kB
limit: 10 * 1024,
noquotes: true,
},
},
],
},
{
test: /\.(jpg|png|gif)$/,
use: [
{
loader: 'url-loader',
options: {
// Inline files smaller than 10 kB
limit: 10 * 1024,
},
},
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
enabled: false,
// NOTE: mozjpeg is disabled as it causes errors in some Linux environments
// Try enabling it in your environment by switching the config to:
// enabled: true,
// progressive: true,
},
gifsicle: {
interlaced: false,
},
optipng: {
optimizationLevel: 7,
},
pngquant: {
quality: '65-90',
speed: 4,
},
},
}
]
}
]
},
plugins: [
new CleanWebpackPlugin(['dist'], {
root: path.join(__dirname)
}),
]
}
解决方案
推荐阅读
- java - 当他们在 Spring Boot 应用程序中注册他们的详细信息时,我们可以为不同角色(如买家、卖家)发送电子邮件通知吗?
- arrays - 如何以角度从数组中输出项目
- forms - 如何阻止 Flutter“表单”抛出错误?
- android - Unity 2019.3:“Android 手机崩溃”
- database - 我的用户字体端显示搞砸了!但是如果我使用管理员帐户..它工作正常
- mongodb - 嵌套数组中的 $addToSet
- swift - CoreData 和 CloudKit 同步有效,但 NSPersistentStoreRemoteChange 通知永远不会触发
- typescript - Typescript 是否对变量或定义的特定类型使用类型注释?
- android - Firebase 数据库路径无效。Firebase 数据库路径不得包含“.”、“#”、“$”、“[”或“]”
- solr - 在 solr 中使用 Re-rank 查询解析器时 df 是强制性参数吗