Skip to content

Commit fcc5019

Browse files
tsriramKyleAMathews
authored andcommitted
Flag to disable uglify for production builds (#4755)
* add no-uglify option to build command * uglify js bundles based on cli arg * fix formatting
1 parent 84e7b7f commit fcc5019

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

packages/gatsby-cli/src/create-cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ function buildLocalCommands(cli, isLocalSite) {
124124
type: `boolean`,
125125
default: false,
126126
describe: `Build site with link paths prefixed (set prefix in your config).`,
127+
}).option(`no-uglify`, {
128+
type: `boolean`,
129+
default: false,
130+
describe: `Build site without uglifying JS bundles (for debugging).`,
127131
}),
128132
handler: handlerP(
129133
getCommandHandler(`build`, (args, cmd) => {

packages/gatsby/src/commands/build.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type BuildArgs = {
1818
sitePackageJson: object,
1919
browserslist: string[],
2020
prefixPaths: boolean,
21+
noUglify: boolean,
2122
}
2223

2324
module.exports = async function build(program: BuildArgs) {

packages/gatsby/src/utils/babel-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function findBabelPackage(directory) {
128128
* the paths will be absolute so that Babel behaves as expected.
129129
*/
130130
module.exports = async function babelConfig(program, stage) {
131-
const { directory } = program
131+
const { directory, noUglify } = program
132132

133133
let babelrc = findBabelrc(directory) || findBabelPackage(directory)
134134

@@ -149,7 +149,7 @@ module.exports = async function babelConfig(program, stage) {
149149
require.resolve(`babel-preset-env`),
150150
{
151151
loose: true,
152-
uglify: true,
152+
uglify: !noUglify,
153153
modules: `commonjs`,
154154
targets: {
155155
browsers: program.browserslist,

packages/gatsby/src/utils/webpack.config.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = async (
4545
// webpack config.
4646
const stage = suppliedStage
4747
const babelConfig = await genBabelConfig(program, suppliedStage)
48+
const { noUglify } = program
4849

4950
function processEnv(stage, defaultNodeEnv) {
5051
debug(`Building env for "${stage}"`)
@@ -235,7 +236,7 @@ module.exports = async (
235236
.getState()
236237
.pages.map(page => page.componentChunkName)
237238
components = uniq(components)
238-
return [
239+
const plugins = [
239240
// Moment.js includes 100s of KBs of extra localization data by
240241
// default in Webpack that most sites don't want. This line disables
241242
// loading locale modules. This is a practical solution that requires
@@ -315,26 +316,31 @@ module.exports = async (
315316
filename: `chunk-manifest.json`,
316317
manifestVariable: `webpackManifest`,
317318
}),
318-
// Minify Javascript.
319-
new webpack.optimize.UglifyJsPlugin({
320-
compress: {
321-
screw_ie8: true, // React doesn't support IE8
322-
warnings: false,
323-
},
324-
mangle: {
325-
screw_ie8: true,
326-
},
327-
output: {
328-
comments: false,
329-
screw_ie8: true,
330-
},
331-
}),
332319
// Ensure module order stays the same. Supposibly fixed in webpack 2.0.
333320
new webpack.optimize.OccurenceOrderPlugin(),
334321
new GatsbyModulePlugin(),
335322
// new WebpackStableModuleIdAndHash({ seed: 9, hashSize: 47 }),
336323
new HashedChunkIdsPlugin(),
337324
]
325+
if (!noUglify) {
326+
// Minify JavaScript.
327+
plugins.push(
328+
new webpack.optimize.UglifyJsPlugin({
329+
compress: {
330+
screw_ie8: true, // React doesn't support IE8
331+
warnings: false,
332+
},
333+
mangle: {
334+
screw_ie8: true,
335+
},
336+
output: {
337+
comments: false,
338+
screw_ie8: true,
339+
},
340+
})
341+
)
342+
}
343+
return plugins
338344
}
339345
default:
340346
throw new Error(`The state requested ${stage} doesn't exist.`)

0 commit comments

Comments
 (0)