Webpack在ES5中生成箭头功能 [英] Webpack generates arrow function in ES5
问题描述
我想使用TypeScript模块并通过Webpack捆绑它们.这是我的配置文件:
I want to use TypeScript modules and bundle them via Webpack. Here are my config files:
webpack.config.js:
const path = require('path');
module.exports = () => {
return {
entry: './index.ts',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
}],
},
};
};
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es3"
},
"include": ["./**/*"],
"exclude": ["node_modules/**/*", "webpack.config.js"]
}
也许我从文档中弄错了.目的是在ES5(甚至更早)中生成代码.但是,这是我的捆绑文件:
Maybe I got something wrong from the documentation. The aim was to generate code in ES5 (or even earlier). But here is my bundled file:
(()=>{var n=function(n,o){console.warn(o)};n(0,0),n(0,1)})();
它具有箭头功能,该功能已在ES6中添加.我很困惑.我该如何摆脱呢?
It has an arrow function, which was added in ES6. I am confused. How can I get rid of that?
这是我尝试编译的代码:
Here's the code I try to compile:
const func = (foo, bar: number) => {
console.warn(bar);
};
func(0, 0);
func(2, 1);
此外,我在生产模式下运行编译过程.(idk,也许这是有用的信息)
Also, I run the compilation process in production mode. (idk, maybe that's useful information)
推荐答案
决定只是在Webpack配置中添加 target:['web','es5']
.
The decision is simply adding target: ['web', 'es5']
to your webpack configuration.
您还可以设置 target:'es5'
,但是在这种情况下,存在一些问题.至少就我而言,未指定"Web"的情况下,TerserWebpackPlugin拒绝在生产模式下压缩文件.
You could also set target: 'es5'
, but in this case, there are some problems. At least in my case without specifying 'web' TerserWebpackPlugin refused to compress files in the production mode.
这篇关于Webpack在ES5中生成箭头功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!