Skip to content

Commit 5b38373

Browse files
committed
More resilient Webpack configuration
Make Webpack configuration less sensitive to the specific layout of `node_modules`. Extracted from #561, which may need to be put on hold until yarn properly handles optional dependencies. Most usefully, changes the logic for loading stylelint rule modules—only load rules that are whitelisted, instead of loading all rules unless they’re blacklisted. Rules themselves often have external library dependencies, so this trims down the gzipped bundle size of the linter bundle from 415K to 355K.
1 parent 62a14f8 commit 5b38373

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

webpack.config.js

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
const path = require('path');
22
const webpack = require('webpack');
33
const escapeRegExp = require('lodash/escapeRegExp');
4+
const startsWith = require('lodash/startsWith');
5+
const map = require('lodash/map');
6+
const includes = require('lodash/includes');
47

58
function matchModule(modulePath) {
6-
return new RegExp(`\\/node_modules\\/${escapeRegExp(modulePath)}`);
9+
const modulePattern = new RegExp(
10+
`\\/node_modules\\/${escapeRegExp(modulePath)}`
11+
);
12+
const moduleDependencyPattern = new RegExp(
13+
`\\/node_modules\\/${escapeRegExp(modulePath)}\\/node_modules`
14+
);
15+
16+
return (filePath) =>
17+
modulePattern.test(filePath) && !moduleDependencyPattern.test(filePath);
18+
}
19+
20+
function directoryContentsExcept(directory, exceptions) {
21+
const fullExceptions = map(
22+
exceptions,
23+
(exception) => path.resolve(directory, exception)
24+
);
25+
26+
return (filePath) =>
27+
startsWith(filePath, path.resolve(directory)) &&
28+
!includes(fullExceptions, filePath);
729
}
830

931
module.exports = {
@@ -22,36 +44,34 @@ module.exports = {
2244
path.resolve(__dirname, 'src'),
2345
path.resolve(__dirname, 'spec'),
2446
],
25-
loaders: ['babel-loader', 'transform/cacheable?brfs-babel'],
47+
loaders: ['babel-loader', 'transform-loader/cacheable?brfs-babel'],
2648
},
2749
{
2850
test: /\.js$/,
2951
include: [
3052
path.resolve(__dirname, 'node_modules/htmllint'),
3153
],
32-
loader: 'transform/cacheable?bulkify',
54+
loader: 'transform-loader/cacheable?bulkify',
3355
},
3456
{
3557
test: /\.js$/,
3658
include: [
3759
path.resolve(__dirname, 'node_modules/PrettyCSS'),
3860
path.resolve(__dirname, 'node_modules/css'),
3961
],
40-
loader: 'transform/cacheable?brfs',
62+
loader: 'transform-loader/cacheable?brfs',
4163
},
4264
{
4365
test: /\.js$/,
4466
include: [
4567
path.resolve(__dirname, 'node_modules/postcss/lib/previous-map'),
46-
path.resolve(__dirname, 'node_modules/stylelint/dist/getPostcssResult'),
47-
path.resolve(__dirname,
48-
'node_modules/stylelint/node_modules/sugarss/node_modules/postcss/lib/previous-map'
49-
),
50-
path.resolve(__dirname,
51-
'node_modules/stylelint/node_modules/postcss-scss/node_modules/postcss/lib/previous-map'
68+
path.resolve(
69+
__dirname,
70+
'node_modules/stylelint/dist/getPostcssResult'
5271
),
72+
matchModule('postcss/lib/previous-map'),
5373
],
54-
loader: 'string-replace',
74+
loader: 'string-replace-loader',
5575
query: {
5676
search: /require\(['"]fs['"]\)/,
5777
replace: '{}',
@@ -65,15 +85,15 @@ module.exports = {
6585
'node_modules/stylelint/dist/utils/isAutoprefixable'
6686
),
6787
],
68-
loader: 'substitute',
88+
loader: 'substitute-loader',
6989
query: {content: '() => false'},
7090
},
7191
{
7292
test: /\.js$/,
7393
include: [
74-
path.resolve(__dirname, 'node_modules/redux'),
75-
path.resolve(__dirname, 'node_modules/lodash-es'),
76-
path.resolve(__dirname, 'node_modules/github-api'),
94+
matchModule('redux'),
95+
matchModule('lodash-es'),
96+
matchModule('github-api'),
7797
],
7898
loader: 'babel-loader',
7999
},
@@ -82,7 +102,7 @@ module.exports = {
82102
include: [
83103
path.resolve(__dirname, 'node_modules/loop-protect'),
84104
],
85-
loader: 'imports?define=>false',
105+
loader: 'imports-loader?define=>false',
86106
},
87107
{
88108
include: [
@@ -91,26 +111,30 @@ module.exports = {
91111
'node_modules/html-inspector/html-inspector.js'
92112
),
93113
],
94-
loader: 'imports?window=>{}!exports?window.HTMLInspector',
114+
loader:
115+
'imports-loader?window=>{}!exports-loader?window.HTMLInspector',
95116
},
96117
{
97118
test: /\.js$/,
98119
include: [
99120
path.resolve(__dirname, 'node_modules/brace/worker'),
100-
path.resolve(
101-
__dirname,
102-
'node_modules/stylelint/dist/rules/no-unsupported-browser-features'
103-
),
104-
path.resolve(
105-
__dirname,
106-
'node_modules/stylelint/dist/rules/no-browser-hacks'
107-
),
108121
matchModule('autoprefixer'),
109122
matchModule('postcss-scss'),
110123
matchModule('postcss-less'),
111124
matchModule('sugarss'),
112125
],
113-
loader: 'null',
126+
loader: 'null-loader',
127+
},
128+
{
129+
test: /\.js$/,
130+
include: directoryContentsExcept(
131+
'node_modules/stylelint/dist/rules',
132+
[
133+
'index.js',
134+
'declaration-block-trailing-semicolon/index.js',
135+
]
136+
),
137+
loader: 'null-loader',
114138
},
115139
{
116140
test: /\.json$/,

0 commit comments

Comments
 (0)