Skip to content

Commit

Permalink
feat(build): make webpack config more flexible by exporting a function
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
The webpack.config.js file has been changed to export a function instead of an object. You will need to change the last line from `module.exports = config;` to `module.exports.mergeConfig = mergeConfig;`. This change allows the prod/dev/test configs to inject some metadata into this file, allowing them to be simpler and have more control over the output.

ISSUES CLOSED:
#101
  • Loading branch information
Brett Uglow committed Dec 28, 2016
1 parent 397759d commit 8e42256
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 172 deletions.
83 changes: 15 additions & 68 deletions lib/buildTool/webpack/buildBrowser/templates/dev.webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,24 @@
'use strict';

// START_CONFIT_GENERATED_CONTENT
<%
var configPath = paths.config.configDir + 'webpack/';
var relativePath = configPath.replace(/([^/]+)/g, '..');
-%>
const path = require('path');
const webpack = require('webpack');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');

let config = require('./webpack.config.js');
const helpers = require('./webpackHelpers')(path.resolve(__dirname, '<%- relativePath %>'));

const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
const HMR = helpers.hasProcessFlag('hot');

/**
* Devtool
* Reference: https://webpack.js.org/configuration/devtool/
* Type of sourcemap to use per build type
*/
Object.assign(config, {
devtool: 'cheap-module-source-map',
});

config.node.process = true; // Not sure why we do this for development?


/**
* Add additional plugins to the compiler.
*
* See: http://webpack.github.io/docs/configuration.html#plugins
*/

/**
* Plugin: DefinePlugin
* Description: Define free variables.
* Useful for having development builds with debug logging or adding global constants.
*
* Environment helpers
*
* See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin
*/
// NOTE: when adding more properties make sure you include them in custom-typings.d.ts
config.plugins.push(
new DefinePlugin({
'ENV': JSON.stringify(ENV),
'HMR': HMR,
'process.env': {
'ENV': JSON.stringify(ENV),
'NODE_ENV': JSON.stringify(ENV),
'HMR': HMR,
}
})
);


/**
* Plugin LoaderOptionsPlugin (experimental)
*
* See: https://gist.github.com/sokra/27b24881210b56bbaff7
*/
let loaderOptions = {
debug: true,
options: Object.assign({}, config.loaderOptions)
let configOptions = {
metaData: {
title: 'Confit Sample Project',
baseUrl: '/',
ENV: process.env.ENV = process.env.NODE_ENV = 'development',
jsSourceMap: 'cheap-module-source-map',
cssSourceMap: false,
},
loaderOptions: {
debug: true
}
};
// END_CONFIT_GENERATED_CONTENT

delete config.loaderOptions; // Remove the property so that config is valid

let loaderOptionsPlugin = new LoaderOptionsPlugin(loaderOptions);

config.plugins.push(loaderOptionsPlugin);
// START_CONFIT_GENERATED_CONTENT
let config = require('./webpack.config.js').mergeConfig(configOptions);
config.node.process = true; // Not sure why we do this for development?
// END_CONFIT_GENERATED_CONTENT

module.exports = config;
82 changes: 15 additions & 67 deletions lib/buildTool/webpack/buildBrowser/templates/prod.webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,24 @@
'use strict';

// START_CONFIT_GENERATED_CONTENT
<%
var configPath = paths.config.configDir + 'webpack/';
var relativePath = configPath.replace(/([^/]+)/g, '..');
-%>
const path = require('path');
const webpack = require('webpack');
const DefinePlugin = require('webpack/lib/DefinePlugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');

let config = require('./webpack.config.js');
const helpers = require('./webpackHelpers')(path.resolve(__dirname, '<%- relativePath %>'));

const ENV = process.env.ENV = process.env.NODE_ENV = 'production';
const HMR = helpers.hasProcessFlag('hot');

/**
* Devtool
* Reference: https://webpack.js.org/configuration/devtool/
* Type of sourcemap to use per build type
*/
Object.assign(config, {
devtool: 'source-map'
});


/**
* Add additional plugins to the compiler.
*
* See: http://webpack.github.io/docs/configuration.html#plugins
*/

/**
* Plugin: DefinePlugin
* Description: Define free variables.
* Useful for having development builds with debug logging or adding global constants.
*
* Environment helpers
*
* See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin
*/
// NOTE: when adding more properties make sure you include them in custom-typings.d.ts
config.plugins.push(
new DefinePlugin({
'ENV': JSON.stringify(ENV),
'HMR': HMR,
'process.env': {
'ENV': JSON.stringify(ENV),
'NODE_ENV': JSON.stringify(ENV),
'HMR': HMR,
}
})
);

/**
* Plugin LoaderOptionsPlugin (experimental)
*
* See: https://gist.github.com/sokra/27b24881210b56bbaff7
*/
let loaderOptions = {
debug: false,
minimize: true,
options: Object.assign({}, config.loaderOptions)
let configOptions = {
metaData: {
title: 'Confit Sample Project',
baseUrl: '/',
ENV: process.env.ENV = process.env.NODE_ENV = 'production',
jsSourceMap: 'source-map',
cssSourceMap: false,
},
loaderOptions: {
debug: false,
minimize: true
}
};
// END_CONFIT_GENERATED_CONTENT

delete config.loaderOptions; // Remove the property so that config is valid

let loaderOptionsPlugin = new LoaderOptionsPlugin(loaderOptions);

config.plugins.push(loaderOptionsPlugin);

// START_CONFIT_GENERATED_CONTENT
let config = require('./webpack.config.js').mergeConfig(configOptions);
// END_CONFIT_GENERATED_CONTENT

module.exports = config;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** Build START */

<%
var configPath = paths.config.configDir + 'webpack/';
var relativePath = configPath.replace(/([^/]+)/g, '..');
Expand All @@ -14,13 +13,18 @@ const helpers = require('./webpackHelpers')(path.resolve(__dirname, '<%- relativ
* Webpack Constants
*/
const HMR = helpers.hasProcessFlag('hot');
const METADATA = {
title: 'Confit Sample Project',
baseUrl: '/',
isDevServer: helpers.isWebpackDevServer(),
HMR: HMR
};

const METADATA = Object.assign(
{
title: 'Confit Sample Project',
baseUrl: '/',
isDevServer: helpers.isWebpackDevServer(),
HMR: HMR,
jsSourceMap: 'cheap-source-map', // See https://webpack.js.org/configuration/devtool/#devtool
cssSourceMap: false, // There are issues with this option for the css-loader: https://github.com/webpack/css-loader#sourcemaps
performanceHints: 'warning'
},
mergeOptions.metaData
);


// https://gist.github.com/sokra/27b24881210b56bbaff7#resolving-options
Expand All @@ -35,13 +39,19 @@ let moduleDirectories = ['node_modules', 'bower_components']; // Only needed to
let config = {
context: helpers.root('<%= paths.input.srcDir.slice(0, -1) %>'), // The baseDir for resolving the entry option and the HTML-Webpack-Plugin
/**
* Performance settings - https://webpack.js.org/configuration/performance/#performance
*/
performance: {
hints: METADATA.performanceHints
},

/**
* Devtool
* Reference: https://webpack.js.org/configuration/devtool/
* Type of sourcemap to use per build type
*/
devtool: 'source-map',
devtool: METADATA.jsSourceMap,

/**
* Options affecting the output of the compilation.
Expand Down Expand Up @@ -76,7 +86,6 @@ let config = {
* See: http://webpack.github.io/docs/configuration.html#resolve
*/
resolve: {
/*
* An array of extensions that should be used to resolve modules.
*
Expand All @@ -101,7 +110,7 @@ let config = {
* Include polyfills or mocks for various node stuff
* Description: Node configuration
*
* See: https://webpack.github.io/docs/configuration.html#node
* See: https://webpack.js.org/configuration/node/
*/
node: {
global: true,
Expand All @@ -110,8 +119,19 @@ let config = {
module: false,
clearImmediate: false,
setImmediate: false
},

loaderOptions: {} // Temporary property to allow multiple builders to add their options, then the dev/prod builds will add the LoaderOptionsPlugin
}
};


/*
* Loader options (for backwards compatibility)
* See https://webpack.js.org/guides/migrating/#loaderoptionsplugin-context
* E.g. https://github.com/jtangelder/sass-loader/issues/285
*/
let LOADER_OPTIONS = Object.assign(
{options: {
context: config.context
}},
mergeOptions.loaderOptions
);
/* **/
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

// START_CONFIT_GENERATED_CONTENT
function mergeConfig(mergeOptions) {
<% include ../../buildBrowser/templates/webpack.build.config.js.tpl %>
<% include ../../entryPoint/templates/webpack.entryPoint.config.js.tpl %>
<% include ../../buildJS/templates/webpack.buildJS.config.js.tpl %>
Expand All @@ -9,9 +10,13 @@
<% include ../../buildCSS/templates/webpack.buildCSS.config.js.tpl %>
<% include ../../buildHTML/templates/webpack.buildHTML.config.js.tpl %>
<% include ../../serverDev/templates/webpack.serverDev.config.js.tpl %>
<% include ../../buildBrowser/templates/webpack.finalize.config.js.tpl %>
return config;
}

// To remove content hashes, call helpers.removeHash(config.prop.parent, propertyName, regExMatcher (optional));
// For example helpers.removeHash(config.output, 'fileName', /\[(contentHash|hash).*?\]/)
// END_CONFIT_GENERATED_CONTENT

module.exports = config;
module.exports.mergeConfig = mergeConfig;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/** Finalise Config START */
/**
* Add additional plugins to the compiler.
*
* See: http://webpack.github.io/docs/configuration.html#plugins
*/

/**
* Plugin: DefinePlugin
* Description: Define free variables.
* Useful for having development builds with debug logging or adding global constants.
*
* Environment helpers
*
* See: https://webpack.github.io/docs/list-of-plugins.html#defineplugin
*/
// NOTE: when adding more properties make sure you include them in custom-typings.d.ts
const DefinePlugin = require('webpack/lib/DefinePlugin');

let definePlugin = new DefinePlugin({
'ENV': JSON.stringify(METADATA.ENV),
'HMR': METADATA.HMR,
'process.env': {
'ENV': JSON.stringify(METADATA.ENV),
'NODE_ENV': JSON.stringify(METADATA.ENV),
'HMR': METADATA.HMR,
}
});

config.plugins.push(definePlugin);

/**
* Plugin LoaderOptionsPlugin (experimental)
*
* See: https://gist.github.com/sokra/27b24881210b56bbaff7
*/
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');

let loaderOptionsPlugin = new LoaderOptionsPlugin(LOADER_OPTIONS);

config.plugins.push(loaderOptionsPlugin);
/* **/
Loading

0 comments on commit 8e42256

Please sign in to comment.