Skip to content

Commit

Permalink
Using devtools with react-stylegudist styleguidist#456
Browse files Browse the repository at this point in the history
The `devtool` setting in your webpack config will now get passed through for non-production environments, e.g., running the hot-reloading server.

It will still get ignored for production builds.

Docs updated, and added a test.
  • Loading branch information
Ryan Oriecuia authored and Ryan Oriecuia committed Aug 9, 2017
1 parent df1a321 commit 0a52cf3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ module.exports = {

> **Warning:** This option disables config load from `webpack.config.js`, load your config [manually](Webpack.md#reusing-your-projects-webpack-config).
> **Note:** `entry`, `externals`, `output`, `watch`, `stats` and `devtool` options will be ignored.
> **Note:** `entry`, `externals`, `output`, `watch`, and `stats` options will be ignored. For production builds, `devtool` will also be ignored.
> **Note:** `CommonsChunkPlugins`, `HtmlWebpackPlugin`, `UglifyJsPlugin`, `HotModuleReplacementPlugin` plugins will be ignored because Styleguidist already includes them or they may break Styleguidist.
Expand Down
4 changes: 2 additions & 2 deletions docs/Webpack.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
};
```

> **Note:** `entry`, `externals`, `output`, `watch`, `stats` and `devtool` options will be ignored.
> **Note:** `entry`, `externals`, `output`, `watch`, and `stats` options will be ignored. For production builds, `devtool` will also be ignored.
> **Note:** `CommonsChunkPlugins`, `HtmlWebpackPlugin`, `UglifyJsPlugin`, `HotModuleReplacementPlugin` plugins will be ignored because Styleguidist already includes them or they may break Styleguidist.
Expand Down Expand Up @@ -69,7 +69,7 @@ module.exports = {

> **Warning:** This option disables config load from `webpack.config.js`, see above how to load your config manually.
> **Note:** `entry`, `externals`, `output`, `watch`, `stats` and `devtool` options will be ignored.
> **Note:** `entry`, `externals`, `output`, `watch`, and `stats` options will be ignored. For production builds, `devtool` will also be ignored.
> **Note:** `CommonsChunkPlugins`, `HtmlWebpackPlugin`, `UglifyJsPlugin`, `HotModuleReplacementPlugin` plugins will be ignored because Styleguidist already includes them or they may break Styleguidist.
Expand Down
10 changes: 10 additions & 0 deletions scripts/utils/__tests__/mergeWebpackConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ it('should ignore certain Webpack plugins', () => {
expect(result.plugins[0].constructor.name).toBe('UglifyJsPlugin');
expect(result.plugins[1].constructor.name).toBe('MyPlugin');
});

[
{ env: 'development', action: 'pass', expected: 'source-map' },
{ env: 'production', action: 'ignore', expected: false },
].forEach(t => {
it(`should ${t.action} devtool settings in ${t.env}`, () => {
const result = mergeWebpackConfig({ devtool: false }, () => ({ devtool: 'source-map' }), t.env);
expect(result).toEqual({ devtool: t.expected });
});
});
17 changes: 7 additions & 10 deletions scripts/utils/mergeWebpackConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@ const isFunction = require('lodash/isFunction');
const omit = require('lodash/omit');
const mergeBase = require('webpack-merge');

const IGNORE_SECTIONS = [
'entry',
'externals',
'output',
'watch',
'stats',
'devtool',
'styleguidist',
];
const IGNORE_SECTIONS = ['entry', 'externals', 'output', 'watch', 'stats', 'styleguidist'];

const IGNORE_PLUGINS = [
'CommonsChunkPlugins',
Expand Down Expand Up @@ -46,6 +38,11 @@ const merge = mergeBase({
*/
module.exports = function mergeWebpackConfig(baseConfig, userConfig, env) {
const userConfigObject = isFunction(userConfig) ? userConfig(env) : userConfig;
const safeUserConfig = omit(userConfigObject, IGNORE_SECTIONS);
let safeUserConfig = omit(userConfigObject, IGNORE_SECTIONS);
// For production builds, we'll ignore devtool settings to avoid
// source mapping bloat.
if (env === 'production') {
safeUserConfig = omit(safeUserConfig, ['devtool']);
}
return merge(baseConfig, safeUserConfig);
};

0 comments on commit 0a52cf3

Please sign in to comment.