Skip to content

Commit

Permalink
Move production toggle into webpack.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
greenberga committed Sep 24, 2017
1 parent e3a62a9 commit 12e8c0e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
23 changes: 1 addition & 22 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const fs = require('fs');
const path = require('path');
const https = require('https');
const gulp = require('gulp');
const yargs = require('yargs');
const concat = require('gulp-concat');
const sourcemaps = require('gulp-sourcemaps');
const cssnano = require('cssnano');
Expand Down Expand Up @@ -45,9 +44,6 @@ forOwn(supportedBrowsers, (version, browser) => {

gulp.task('env', () => {
process.env.GIT_REVISION = git.short();
if (yargs.argv.production) {
process.env.NODE_ENV = 'production';
}
});

gulp.task('static', () => gulp.
Expand Down Expand Up @@ -93,24 +89,7 @@ gulp.task('css', () => {
pipe(browserSync.stream());
});

gulp.task('js', ['env'], () => {
const productionWebpackConfig = Object.create(webpackConfiguration);
productionWebpackConfig.plugins = productionWebpackConfig.plugins.concat(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
},
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {warnings: false},
output: {comments: false},
sourceMap: true,
}),
);

return pify(webpack)(productionWebpackConfig);
});
gulp.task('js', ['env'], () => pify(webpack)(webpackConfiguration));

gulp.task('build', ['static', 'fonts', 'css', 'js']);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
"postinstall": "bower install",
"pretest": "yarn run lint",
"dev": "yarn install && gulp dev",
"prod": "yarn install && NODE_ENV=production gulp build",
"test": "karma start --single-run --no-auto-watch",
"autotest": "karma start --no-single-run --auto-watch",
"lint": "eslint --ext .js,.jsx --plugin react src test *.js && stylelint src/**/*.css",
Expand Down Expand Up @@ -249,8 +250,7 @@
"webpack": "^3.1.0",
"webpack-chunk-hash": "^0.4.0",
"webpack-dev-middleware": "^1.8.2",
"webpack-hot-middleware": "^2.12.2",
"yargs": "^8.0.1"
"webpack-hot-middleware": "^2.12.2"
},
"optionalDependencies": {
"fsevents": "*"
Expand Down
23 changes: 16 additions & 7 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const babel = require('babel-core');
const babelLoaderVersion =
require('./node_modules/babel-loader/package.json').version;

const isProduction = process.env.NODE_ENV === 'production';

let targets;
if (process.env.DEBUG === 'true') {
targets = {browsers: 'last 1 Chrome version'};
Expand Down Expand Up @@ -70,12 +72,8 @@ module.exports = {
entry: './src/application.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: process.env.NODE_ENV === 'production' ?
'[name].[chunkhash].js' :
'[name].js',
chunkFilename: process.env.NODE_ENV === 'production' ?
'[name].[chunkhash].js' :
'[name].js',
filename: isProduction ? '[name].[chunkhash].js' : '[name].js',
chunkFilename: isProduction ? '[name].[chunkhash].js' : '[name].js',
},
module: {
rules: [
Expand Down Expand Up @@ -261,6 +259,17 @@ module.exports = {
],
ServiceWorker: {navigateFallbackURL: '/'},
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
},
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {warnings: false},
output: {comments: false},
sourceMap: true,
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks({context}) {
Expand All @@ -273,7 +282,7 @@ module.exports = {
},
}),
new webpack.optimize.CommonsChunkPlugin({name: 'manifest'}),
process.env.NODE_ENV === 'production' ?
isProduction ?
new webpack.HashedModuleIdsPlugin() :
new webpack.NamedModulesPlugin(),
new MD5ChunkHash(),
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9529,7 +9529,7 @@ [email protected]:
y18n "^3.2.1"
yargs-parser "^4.1.0"

yargs@^8.0.1, yargs@^8.0.2:
yargs@^8.0.2:
version "8.0.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-8.0.2.tgz#6299a9055b1cefc969ff7e79c1d918dceb22c360"
dependencies:
Expand Down

0 comments on commit 12e8c0e

Please sign in to comment.