我试着测试Webpack的摇树特性,但它似乎不起作用。
这是我的档案:
index.tsimport { good } from './exports';
console.log(good);exports.tsexport const good = 'good';
export const secret = 'iamasecret';tsconfig.json{}webpack.config.tsimport { Configuration } from 'webpack';
import * as TerserPlugin from "terser-webpack-plugin";
const config: Configuration = {
mode: 'production',
entry: './index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [ '.tsx', '.ts', '.js' ]
},
optimization: {
usedExports: true,
minimizer: [new TerserPlugin()],
}
}
export default config;package.json{
"name": "webpacktest",
"version": "1.0.0",
"description": "",
"main": "index.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/terser-webpack-plugin": "^2.2.0",
"@types/webpack": "^4.41.11",
"terser-webpack-plugin": "^2.3.5",
"ts-loader": "^7.0.0",
"ts-node": "^8.8.2",
"typescript": "^3.8.3",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11"
},
"sideEffects": false
}当我运行npx webpack时,它会将文件打包到dist/main.js中。当我打开那个文件时,秘密字符串就在里面,尽管它是一个未使用的导出。有什么方法可以阻止它被包含在最后的包中吗?
发布于 2020-04-16 15:35:30
好吧,所以我想出来了。我需要将包@babel/core、@babel/preset-env和babel-loader安装为开发依赖项,并将处理TypeScript文件的Webpack配置规则更改为:
{
test: /\.tsx?$/,
use: ['babel-loader','ts-loader'],
exclude: /node_modules/,
},接下来,我创建了一个具有以下内容的.babelrc文件:
{
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
]
}最后,我在compilerOptions下更改/添加了以下行到我的compilerOptions中
"module": "es6",
"moduleResolution": "node",使用babel-loader、设置.babelrc配置和使用"module": "es6",可以使我的TypeScript代码受到树的震动。"moduleResolution": "node"修复了一个问题,当我收到某些模块无法解决的错误时。
https://stackoverflow.com/questions/61236997
复制相似问题