Seems like a truly stupid question that must have an answer somewhere, but I've searched for hours to no avail. I'm new to ReactJS and building with Webpack, build configs in general. I'm using Webpack to link and bundle my entire project, including ReactJS. It works great, but I cannot figure out any way to get the bundle to come out un-minified so that I can debug issues when they arise.
Here is my Webpack config:
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'public/js');
var APP_DIR = path.resolve(__dirname, 'build-source/js');
var config = {
entry: APP_DIR + '\\main.js',
output: {
path: BUILD_DIR,
filename: 'build.js' // want this output file to end un-minified
},
module: {
loaders: [
{
test: /\.jsx?/,
include: APP_DIR,
loader: 'babel'
}
]
}
};
module.exports = config;
I run the bundling execution with either npm run dev
or npm run build
which call upon the following from my package.json:
{
/* blah blah */,
"scripts": {
"start": "node ./bin/www",
"dev": "webpack -d --watch",
"build": "webpack -p"
},
"dependencies": {
"babel-core": "^6.16.0",
"babel-loader": "^6.2.5",
"babel-preset-react": "^6.16.0",
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"helmet": "^3.1.0",
"morgan": "~1.7.0",
"mysql": "^2.11.1",
"querystring": "^0.2.0",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"request": "^2.75.0",
"serve-favicon": "~2.3.0",
"webpack": "^1.13.2"
}
}
What do I need to change to get un-minified JavaScript bundles out of my Webpack execution?
You can use source maps to preserve the mapping between your source code and the bundled/minified one. Webpack provides the devtool option to enhance debugging in the developer tool just creating a source map of the bundled file for you. This option can be used from the command line or used in your webpack.
In webpack 4, the bundle-level minification is enabled automatically – both in the production mode and without one. It uses the UglifyJS minifier under the hood. (If you ever need to disable minification, just use the development mode or pass false to the optimization.
Enable source maps in your webpack app to debug easier Now rebuild your project and open up in the browser. You will be able to see the original code straight in your browser. From here you can set break points and debug !
When webpack processes your application, it internally builds a dependency graph from one or more entry points and then combines every module your project needs into one or more bundles, which are static assets to serve your content from.
When you use the -p flag for webpack's cli, you are telling webpack to use the UglifyJSPlugin
(behind the scenes)
So instead, I would have a separate build task that runs webpack without the -p flag and instead passes the following in your config instead.
var webpack = require('webpack');
module.exports = {
plugins: [
new webpack.optimize.UglifyJsPlugin({
options: {
compress: {drop_debugger: false}
}
})
]
};
Additionally, you can see that I've passed a custom option to the UglifyJsPlugin (which just corresponds to UglifyJs's compression options).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With