我有一个几乎可以正常工作的 webpack 设置,但是当我导入一些用 ES6 编写的第 3 方库时,我看到 UglifyJS 发出错误。这是来自 node-postgres
的示例:
module.exports = {
prepareValue: function prepareValueWrapper (value) {
// this ensures that extra arguments do not get passed into prepareValue
// by accident, eg: from calling values.map(utils.prepareValue)
return prepareValue(value)
},
normalizeQueryConfig,
postgresMd5PasswordHash,
md5
}
这是它在 ES5 中的样子:
module.exports = {
prepareValue: function prepareValueWrapper (value) {
// this ensures that extra arguments do not get passed into prepareValue
// by accident, eg: from calling values.map(utils.prepareValue)
return prepareValue(value)
},
normalizeQueryConfig: normalizeQueryConfig,
postgresMd5PasswordHash: postgresMd5PasswordHash,
md5: md5
}
当原始代码被 UglifyJS 处理时,我看到这个错误:
ERROR in proxy.js from UglifyJs
Unexpected token: punc (,) [proxy.js:382,22]
它指向上面的代码。
当我编译一个 TypeScript 项目时,我怀疑我需要通过 Webpack 流中的一些转译器处理第 3 方库代码,以便在捆绑之前将它们转换为 ES5。
这是我的webpack.config.js
:
const path = require('path');
const root = (dir) => {
return path.resolve(__dirname, dir);
};
module.exports = {
entry: './src/main.ts',
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
},
resolve: {
extensions: [".ts", ".js"],
modules: [root('node_modules'), root('src')]
},
output: {
filename: 'proxy.js',
path: path.resolve(__dirname, 'build')
},
target: 'node'
};
我该怎么做?
最佳答案
您可以使用 uglify-es缩小你的 ES6 代码,或者使用 Babel 和 ES2015 preset将代码转换为 ES5。
关于node.js - 如何为第 3 方库将 ES6 转换为 ES5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46489828/