本文介绍了webpack打包node.js后端项目的方法,分享给大家,具体如下:
安装依赖
webpack配置
webpack.config.js
let externals = _externals();
module.exports = {
entry: {
app: './app.js',},target: 'node',output: {
path: './build',filename: '[name].js'
},resolve: {
extensions: ['','.js']
},externals: externals,node: {
console: true,global: true,process: true,Buffer: true,filename: true,dirname: true,setImmediate: true
},module: {
loaders: [
{
test: /.js$/,loader: 'babel',query: {
presets: ['es2015','stage-0']
},exclude: /node_modules/
}
]
},plugins: [
new webpack.optimize.UglifyJsPlugin()
]
};
function _externals() {
let manifest = require('./package.json');
let dependencies = manifest.dependencies;
let externals = {};
for (let p in dependencies) {
externals[p] = 'commonjs ' + p;
}
return externals;
}