From a3054cb393069ecd309d85e5ca76c5076f47d55b Mon Sep 17 00:00:00 2001 From: Sriram Thiagarajan Date: Thu, 29 Mar 2018 09:23:29 +0530 Subject: [PATCH 1/3] add no-uglify option to build command --- packages/gatsby-cli/src/create-cli.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/gatsby-cli/src/create-cli.js b/packages/gatsby-cli/src/create-cli.js index b3645263d4b1a..fa4f2bcad93e5 100644 --- a/packages/gatsby-cli/src/create-cli.js +++ b/packages/gatsby-cli/src/create-cli.js @@ -124,6 +124,10 @@ function buildLocalCommands(cli, isLocalSite) { type: `boolean`, default: false, describe: `Build site with link paths prefixed (set prefix in your config).`, + }).option(`no-uglify`, { + type: `boolean`, + default: false, + describe: `Build site without uglifying JS bundles (for debugging).` }), handler: handlerP( getCommandHandler(`build`, (args, cmd) => { From c5b9e7c12aa1af6f8c9c1d0f259bca41a7b805e0 Mon Sep 17 00:00:00 2001 From: Sriram Thiagarajan Date: Thu, 29 Mar 2018 09:27:13 +0530 Subject: [PATCH 2/3] uglify js bundles based on cli arg --- packages/gatsby/src/commands/build.js | 1 + packages/gatsby/src/utils/babel-config.js | 4 ++-- packages/gatsby/src/utils/webpack.config.js | 24 ++++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/gatsby/src/commands/build.js b/packages/gatsby/src/commands/build.js index 8a4fb4c008b2b..1966dee326251 100644 --- a/packages/gatsby/src/commands/build.js +++ b/packages/gatsby/src/commands/build.js @@ -18,6 +18,7 @@ type BuildArgs = { sitePackageJson: object, browserslist: string[], prefixPaths: boolean, + noUglify: boolean } module.exports = async function build(program: BuildArgs) { diff --git a/packages/gatsby/src/utils/babel-config.js b/packages/gatsby/src/utils/babel-config.js index 88f89dbd71fba..0c1b18e81edb7 100644 --- a/packages/gatsby/src/utils/babel-config.js +++ b/packages/gatsby/src/utils/babel-config.js @@ -128,7 +128,7 @@ function findBabelPackage(directory) { * the paths will be absolute so that Babel behaves as expected. */ module.exports = async function babelConfig(program, stage) { - const { directory } = program + const { directory, noUglify } = program let babelrc = findBabelrc(directory) || findBabelPackage(directory) @@ -149,7 +149,7 @@ module.exports = async function babelConfig(program, stage) { require.resolve(`babel-preset-env`), { loose: true, - uglify: true, + uglify: !noUglify, modules: `commonjs`, targets: { browsers: program.browserslist, diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 349cbbf6ee035..55d2aaad90c75 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -45,6 +45,7 @@ module.exports = async ( // webpack config. const stage = suppliedStage const babelConfig = await genBabelConfig(program, suppliedStage) + const { noUglify } = program function processEnv(stage, defaultNodeEnv) { debug(`Building env for "${stage}"`) @@ -235,7 +236,7 @@ module.exports = async ( .getState() .pages.map(page => page.componentChunkName) components = uniq(components) - return [ + const plugins = [ // Moment.js includes 100s of KBs of extra localization data by // default in Webpack that most sites don't want. This line disables // loading locale modules. This is a practical solution that requires @@ -315,8 +316,15 @@ module.exports = async ( filename: `chunk-manifest.json`, manifestVariable: `webpackManifest`, }), - // Minify Javascript. - new webpack.optimize.UglifyJsPlugin({ + // Ensure module order stays the same. Supposibly fixed in webpack 2.0. + new webpack.optimize.OccurenceOrderPlugin(), + new GatsbyModulePlugin(), + // new WebpackStableModuleIdAndHash({ seed: 9, hashSize: 47 }), + new HashedChunkIdsPlugin(), + ]; + if(!noUglify) { + // Minify JavaScript. + plugins.push(new webpack.optimize.UglifyJsPlugin({ compress: { screw_ie8: true, // React doesn't support IE8 warnings: false, @@ -328,13 +336,9 @@ module.exports = async ( comments: false, screw_ie8: true, }, - }), - // Ensure module order stays the same. Supposibly fixed in webpack 2.0. - new webpack.optimize.OccurenceOrderPlugin(), - new GatsbyModulePlugin(), - // new WebpackStableModuleIdAndHash({ seed: 9, hashSize: 47 }), - new HashedChunkIdsPlugin(), - ] + })) + } + return plugins } default: throw new Error(`The state requested ${stage} doesn't exist.`) From 1c07193c005a78b5f8f961a983c9fe737ac70f7c Mon Sep 17 00:00:00 2001 From: Sriram Thiagarajan Date: Thu, 29 Mar 2018 16:06:15 +0530 Subject: [PATCH 3/3] fix formatting --- packages/gatsby-cli/src/create-cli.js | 2 +- packages/gatsby/src/commands/build.js | 2 +- packages/gatsby/src/utils/webpack.config.js | 32 +++++++++++---------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/gatsby-cli/src/create-cli.js b/packages/gatsby-cli/src/create-cli.js index fa4f2bcad93e5..0cdf6c0626b6c 100644 --- a/packages/gatsby-cli/src/create-cli.js +++ b/packages/gatsby-cli/src/create-cli.js @@ -127,7 +127,7 @@ function buildLocalCommands(cli, isLocalSite) { }).option(`no-uglify`, { type: `boolean`, default: false, - describe: `Build site without uglifying JS bundles (for debugging).` + describe: `Build site without uglifying JS bundles (for debugging).`, }), handler: handlerP( getCommandHandler(`build`, (args, cmd) => { diff --git a/packages/gatsby/src/commands/build.js b/packages/gatsby/src/commands/build.js index 1966dee326251..05533d41a32b6 100644 --- a/packages/gatsby/src/commands/build.js +++ b/packages/gatsby/src/commands/build.js @@ -18,7 +18,7 @@ type BuildArgs = { sitePackageJson: object, browserslist: string[], prefixPaths: boolean, - noUglify: boolean + noUglify: boolean, } module.exports = async function build(program: BuildArgs) { diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 55d2aaad90c75..fe161630f9a42 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -321,22 +321,24 @@ module.exports = async ( new GatsbyModulePlugin(), // new WebpackStableModuleIdAndHash({ seed: 9, hashSize: 47 }), new HashedChunkIdsPlugin(), - ]; - if(!noUglify) { + ] + if (!noUglify) { // Minify JavaScript. - plugins.push(new webpack.optimize.UglifyJsPlugin({ - compress: { - screw_ie8: true, // React doesn't support IE8 - warnings: false, - }, - mangle: { - screw_ie8: true, - }, - output: { - comments: false, - screw_ie8: true, - }, - })) + plugins.push( + new webpack.optimize.UglifyJsPlugin({ + compress: { + screw_ie8: true, // React doesn't support IE8 + warnings: false, + }, + mangle: { + screw_ie8: true, + }, + output: { + comments: false, + screw_ie8: true, + }, + }) + ) } return plugins }