Skip to content

Commit

Permalink
Use yargs for webpack environment mode
Browse files Browse the repository at this point in the history
  • Loading branch information
greenberga committed Sep 30, 2017
1 parent 528d07c commit 33ae910
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 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 @@ -90,24 +86,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
26 changes: 18 additions & 8 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ const map = require('lodash/map');
const includes = require('lodash/includes');
const git = require('git-rev-sync');
const babel = require('babel-core');
const yargs = require('yargs');
const babelLoaderVersion =
require('./node_modules/babel-loader/package.json').version;

const isProduction = yargs.argv.production;

let targets;
if (process.env.DEBUG === 'true') {
targets = {browsers: 'last 1 Chrome version'};
Expand All @@ -39,7 +42,7 @@ const babelrc = {
babel: babel.version,
'babel-loader': babelLoaderVersion,
debug: process.env.DEBUG,
env: process.env.NODE_ENV || 'development',
env: isProduction ? 'production' : 'development',
}),
};

Expand Down Expand Up @@ -70,12 +73,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 +260,17 @@ module.exports = {
],
ServiceWorker: {navigateFallbackURL: '/'},
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(isProduction ? 'production' : '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 +283,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

0 comments on commit 33ae910

Please sign in to comment.