Skip to content

Commit

Permalink
Feat: Drop webpack 3 support (#1023)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

webpack 3 is no longer supported, webpack 4 is the minimum required version
  • Loading branch information
Stepan Mikhaylyuk authored and sapegin committed Oct 18, 2018
1 parent 608c582 commit 621c2c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 58 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -5,7 +5,6 @@ node_js:
- 6
env:
- WEBPACK_VERSION= # Current, from package.json
- WEBPACK_VERSION=3
install:
# Use npm 5.7.x since it has introduced `npm ci`
- if [[ `npm -v` != 5.7* ]]; then npm install -g npm@'>=5.7.1'; fi
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 28 additions & 18 deletions scripts/__tests__/make-webpack-config.spec.js
@@ -1,7 +1,12 @@
import webpack, { validate } from 'webpack';
import getWebpackVersion from '../utils/getWebpackVersion';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import makeWebpackConfig from '../make-webpack-config';

jest.mock('copy-webpack-plugin', () => {
const RealCopyWebpackPluginModule = require.requireActual('copy-webpack-plugin');
return jest.fn(RealCopyWebpackPluginModule);
});

const theme = 'hl-theme';
const styleguideConfig = {
styleguideDir: __dirname,
Expand All @@ -16,7 +21,10 @@ const getClasses = (plugins, name) => plugins.filter(x => x.constructor.name ===
const getClassNames = plugins => plugins.map(x => x.constructor.name);

const process$env$nodeEnv = process.env.NODE_ENV;
const isWebpack4 = getWebpackVersion() >= 4;

beforeEach(() => {
CopyWebpackPlugin.mockClear();
});

afterEach(() => {
process.env.NODE_ENV = process$env$nodeEnv;
Expand All @@ -32,14 +40,10 @@ it('should return a development config', () => {
const plugins = getClassNames(config.plugins);
expect(plugins).toContain('HotModuleReplacementPlugin');

if (isWebpack4) {
expect(config).toMatchObject({
mode: env,
});
expect(config).not.toHaveProperty('optimization');
} else {
expect(plugins).not.toContain('UglifyJsPlugin');
}
expect(config).toMatchObject({
mode: env,
});
expect(config).not.toHaveProperty('optimization');
});

it('should return a production config', () => {
Expand All @@ -59,14 +63,10 @@ it('should return a production config', () => {
},
});

if (isWebpack4) {
expect(config).toMatchObject({
mode: env,
});
expect(getClasses(config.optimization.minimizer, 'UglifyJsPlugin')).toHaveLength(1);
} else {
expect(plugins).toContain('UglifyJsPlugin');
}
expect(config).toMatchObject({
mode: env,
});
expect(getClasses(config.optimization.minimizer, 'UglifyJsPlugin')).toHaveLength(1);
});

it('should set aliases', () => {
Expand Down Expand Up @@ -102,6 +102,16 @@ it('should enable verbose mode in CleanWebpackPlugin', () => {
expect(getClasses(result.plugins, 'CleanWebpackPlugin')).toMatchSnapshot();
});

it('should set from with assetsDir in CopyWebpackPlugin', () => {
makeWebpackConfig({ ...styleguideConfig, assetsDir: '/assets/' }, 'production');
expect(CopyWebpackPlugin).toHaveBeenCalledWith([{ from: '/assets/' }]); //([
});

it('should add CopyWebpackPlugin to plugins in production', () => {
makeWebpackConfig({ ...styleguideConfig }, 'production');
expect(CopyWebpackPlugin).toHaveBeenCalledWith([]);
});

it('should merge user webpack config', () => {
const result = makeWebpackConfig(
{ ...styleguideConfig, webpackConfig: { resolve: { alias: { foo: 'bar' } } } },
Expand Down
61 changes: 24 additions & 37 deletions scripts/make-webpack-config.js
Expand Up @@ -10,11 +10,9 @@ const forEach = require('lodash/forEach');
const isFunction = require('lodash/isFunction');
const mergeWebpackConfig = require('./utils/mergeWebpackConfig');
const StyleguidistOptionsPlugin = require('./utils/StyleguidistOptionsPlugin');
const getWebpackVersion = require('./utils/getWebpackVersion');

const RENDERER_REGEXP = /Renderer$/;

const isWebpack4 = getWebpackVersion() >= 4;
const sourceDir = path.resolve(__dirname, '../lib');

module.exports = function(config, env) {
Expand All @@ -35,6 +33,7 @@ module.exports = function(config, env) {

let webpackConfig = {
entry: config.require.concat([path.resolve(sourceDir, 'index')]),
mode: env,
output: {
path: config.styleguideDir,
filename: 'build/[name].bundle.js',
Expand All @@ -59,34 +58,7 @@ module.exports = function(config, env) {
},
};

/* istanbul ignore if */
if (isWebpack4) {
webpackConfig.mode = env;
}

if (isProd) {
webpackConfig = merge(webpackConfig, {
output: {
filename: 'build/bundle.[chunkhash:8].js',
chunkFilename: 'build/[name].[chunkhash:8].js',
},
plugins: [
new CleanWebpackPlugin(['build'], {
root: config.styleguideDir,
verbose: config.verbose === true,
}),
new CopyWebpackPlugin(
config.assetsDir
? [
{
from: config.assetsDir,
},
]
: []
),
],
});

const uglifier = new UglifyJSPlugin({
parallel: true,
cache: true,
Expand All @@ -110,15 +82,30 @@ module.exports = function(config, env) {
},
},
});

/* istanbul ignore if */
if (isWebpack4) {
webpackConfig.optimization = {
webpackConfig = merge(webpackConfig, {
output: {
filename: 'build/bundle.[chunkhash:8].js',
chunkFilename: 'build/[name].[chunkhash:8].js',
},
plugins: [
new CleanWebpackPlugin(['build'], {
root: config.styleguideDir,
verbose: config.verbose === true,
}),
new CopyWebpackPlugin(
config.assetsDir
? [
{
from: config.assetsDir,
},
]
: []
),
],
optimization: {
minimizer: [uglifier],
};
} else {
webpackConfig.plugins.unshift(uglifier);
}
},
});
} else {
webpackConfig = merge(webpackConfig, {
entry: [require.resolve('react-dev-utils/webpackHotDevClient')],
Expand Down
2 changes: 1 addition & 1 deletion scripts/utils/ensureWebpack.js
Expand Up @@ -7,7 +7,7 @@ const getWebpackVersion = require('./getWebpackVersion');
const StyleguidistError = require('./error');
const consts = require('../consts');

const MIN_WEBPACK_VERSION = 2;
const MIN_WEBPACK_VERSION = 4;
const webpackVersion = getWebpackVersion();

if (!webpackVersion) {
Expand Down

0 comments on commit 621c2c6

Please sign in to comment.