Skip to content

Commit

Permalink
feat(webpack): add dangerouslySetWebpackConfig option (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles committed Jul 18, 2017
1 parent c4b2a98 commit da3c67b
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 137 deletions.
7 changes: 6 additions & 1 deletion config/builds.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const buildConfigs = fs.existsSync(skuConfigPath)
? makeArray(require(skuConfigPath))
: [{}];

const defaultDecorator = a => a;

let buildName = args.buildName;

if (!buildName && args.script === 'start' && buildConfigs.length > 1) {
Expand Down Expand Up @@ -38,6 +40,8 @@ const builds = buildConfigs
const env = buildConfig.env || {};
const entry = buildConfig.entry || {};
const locales = buildConfig.locales || [''];
const webpackDecorator =
buildConfig.dangerouslySetWebpackConfig || defaultDecorator;

const paths = {
seekStyleGuide: path.join(cwd, 'node_modules/seek-style-guide'),
Expand All @@ -51,7 +55,8 @@ const builds = buildConfigs
name,
env,
paths,
locales
locales,
webpackDecorator
};
});

Expand Down
274 changes: 138 additions & 136 deletions config/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,148 +94,150 @@ const svgLoaders = [
}
];

const buildWebpackConfigs = builds.map(({ name, paths, env, locales }) => {
const envVars = lodash
.chain(env)
.mapValues((value, key) => {
if (typeof value !== 'object') {
return JSON.stringify(value);
}
const buildWebpackConfigs = builds.map(
({ name, paths, env, locales, webpackDecorator }) => {
const envVars = lodash
.chain(env)
.mapValues((value, key) => {
if (typeof value !== 'object') {
return JSON.stringify(value);
}

const valueForProfile = value[args.profile];
const valueForProfile = value[args.profile];

if (typeof valueForProfile === 'undefined') {
console.log(
`WARNING: Environment variable "${key}" for build "${name}" is missing a value for the "${args.profile}" profile`
);
process.exit(1);
}
if (typeof valueForProfile === 'undefined') {
console.log(
`WARNING: Environment variable "${key}" for build "${name}" is missing a value for the "${args.profile}" profile`
);
process.exit(1);
}

return JSON.stringify(valueForProfile);
})
.mapKeys((value, key) => `process.env.${key}`)
.value();
return JSON.stringify(valueForProfile);
})
.mapKeys((value, key) => `process.env.${key}`)
.value();

return [
{
entry: paths.clientEntry,
output: {
path: paths.dist,
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: thirdPartyModulesRegex,
use: jsLoaders
},
{
test: /\.js$/,
include: thirdPartyModulesRegex,
use: [
{
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [require.resolve('babel-preset-es2015')]
return [
{
entry: paths.clientEntry,
output: {
path: paths.dist,
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: thirdPartyModulesRegex,
use: jsLoaders
},
{
test: /\.js$/,
include: thirdPartyModulesRegex,
use: [
{
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [require.resolve('babel-preset-es2015')]
}
}
}
]
},
{
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: makeCssLoaders()
})
},
{
test: /\.less$/,
include: paths.seekStyleGuide,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: makeCssLoaders({ styleGuide: true })
})
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: makeImageLoaders()
},
{
test: /\.svg$/,
use: svgLoaders
}
]
},
plugins: [
new webpack.DefinePlugin(envVars),
new ExtractTextPlugin('style.css')
].concat(
!isProductionBuild
? []
: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.ModuleConcatenationPlugin()
]
)
},
{
entry: {
render: paths.renderEntry
},
output: {
path: paths.dist,
filename: '[name].js',
libraryTarget: 'umd'
]
},
{
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: makeCssLoaders()
})
},
{
test: /\.less$/,
include: paths.seekStyleGuide,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: makeCssLoaders({ styleGuide: true })
})
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: makeImageLoaders()
},
{
test: /\.svg$/,
use: svgLoaders
}
]
},
plugins: [
new webpack.DefinePlugin(envVars),
new ExtractTextPlugin('style.css')
].concat(
!isProductionBuild
? []
: [
new webpack.optimize.UglifyJsPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.optimize.ModuleConcatenationPlugin()
]
)
},
module: {
rules: [
{
test: /\.js$/,
exclude: thirdPartyModulesRegex,
use: jsLoaders
},
{
test: /\.less$/,
exclude: /node_modules/,
use: makeCssLoaders({ server: true })
},
{
test: /\.less$/,
include: paths.seekStyleGuide,
use: makeCssLoaders({ server: true, styleGuide: true })
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: makeImageLoaders({ server: true })
},
{
test: /\.svg$/,
use: svgLoaders
}
{
entry: {
render: paths.renderEntry
},
output: {
path: paths.dist,
filename: '[name].js',
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /\.js$/,
exclude: thirdPartyModulesRegex,
use: jsLoaders
},
{
test: /\.less$/,
exclude: /node_modules/,
use: makeCssLoaders({ server: true })
},
{
test: /\.less$/,
include: paths.seekStyleGuide,
use: makeCssLoaders({ server: true, styleGuide: true })
},
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
use: makeImageLoaders({ server: true })
},
{
test: /\.svg$/,
use: svgLoaders
}
]
},
plugins: [
new webpack.DefinePlugin(envVars),
...locales.slice(0, isProductionBuild ? locales.length : 1).map(
locale =>
new StaticSiteGeneratorPlugin({
locals: {
locale
},
paths: `index${isProductionBuild && locale
? `-${locale}`
: ''}.html`
})
)
]
},
plugins: [
new webpack.DefinePlugin(envVars),
...locales.slice(0, isProductionBuild ? locales.length : 1).map(
locale =>
new StaticSiteGeneratorPlugin({
locals: {
locale
},
paths: `index${isProductionBuild && locale
? `-${locale}`
: ''}.html`
})
)
]
}
];
});
}
].map(webpackDecorator);
}
);

module.exports = flatten(buildWebpackConfigs);

0 comments on commit da3c67b

Please sign in to comment.