我不确定如何uglify我的server.js
文件并将其保存到dist
文件夹下的server
文件夹中。现在我只使用CopyWebpackPlugin
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
},
{
from: path.resolve(__dirname, '../src/server'),
to: config.build.assetsServerDirectory,
ignore: ['*.sql']
}
]),
这很有效,但只是简单的复制和粘贴。
答案 0 :(得分:4)
您可以使用uglify-es + copy-webpack-plugin's transform()
:
安装包:
npm install uglify-es --save-dev
将其添加到您的来源:
const UglifyJS = require("uglify-es"); // add the require
new CopyWebpackPlugin([
{
from: path.resolve(__dirname, '../static'),
to: config.build.assetsSubDirectory,
ignore: ['.*']
},
{
from: path.resolve(__dirname, '../src/server'),
to: config.build.assetsServerDirectory,
transform: function (content, path) { // add transform()
return UglifyJS.minify(content.toString()).code; // use uglify
}, // (no args = mangle+compress)
ignore: ['*.sql']
}
]),
注意:UglifyJS.minify(content.toString()).code
与UglifyJS.minify(content.toString('utf8')).code
相同。 There are options in case of different encodings
答案 1 :(得分:0)
有一个图书馆已经准备好了。您可以使用 uglifyjs-webpack-plugin
然后将捆绑的文件输出到所需的目录
如果您不喜欢使用该插件。您可以使用Webpack的默认uglifier。
const webpack = require("webpack");
module.exports = {
entry: {
"bundle": "./server/server.js",
}
output: {
path: "./dist",
filename: "[name].js"
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
include: /\.js$/,
minimize: true
})
]
};