我尝试使用bundle.js
构建生产webpack.config.prod.js
,但是当我运行npm run build
命令时,它会执行prebuild
脚本并停在build
脚本上甚至没有试图运行它。因此,它根本不会创建bundle.js
。
我关注webpack v1
的示例,我尝试迁移到v2
并了解为什么build.js
的运行不起作用。
这些是我的文件:
{
"scripts": {
"lint": "esw webpack.config.* src buildScripts",
"test": "mocha --reporter progress buildScripts/testSetup.js \"src/**/*.test.js\"",
"clean-dist": "rimraf ./dist && mkdir dist",
// Executes prebuild script
"prebuild": "npm-run-all clean-dist test lint",
// Stops on build script
"build": "babel-node buildScripts/build.js",
"postbuild": "babel-node buildScripts/distServer.js"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-latest": "^6.24.1",
"babel-register": "^6.24.1",
"chai": "^4.1.0",
"cheerio": "^1.0.0-rc.2",
"eslint": "^4.2.0",
"eslint-plugin-import": "^2.7.0",
"eslint-watch": "^3.1.2",
"express": "^4.15.3",
"jsdom": "^11.1.0",
"json-server": "^0.11.2",
"npm-run-all": "^4.0.2",
"rimraf": "^2.6.1",
"webpack": "^3.3.0"
}
}
import path from 'path';
import webpack from 'webpack';
module.exports = {
devtool: "source-map",
entry: [
path.resolve(__dirname, "src/index.js")
],
target: "web",
output: {
path: path.resolve(__dirname, "dist"),
publicPath: "/",
filename: "bundle.js"
},
devServer: {
noInfo: false
},
plugins: [
new webpack.LoaderOptionsPlugin({
debug: true
}),
//Minify JS
new webpack.optimize.UglifyJsPlugin()
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["babel-loader"]
}
]
}
};
import webpack from 'webpack';
import webpackConfig from '../webpack.config.prod';
process.env.NODE_ENV = 'production';
console.log('Generating minified bundle for production. This will take a moment...');
webpack(webpackConfig).run((err, stats) => {
if (err) {
console.log(err);
return 1;
}
return 0;
});