This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
278 lines (245 loc) · 6.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const chalk = require('chalk');
const incorrectStyleGuidePath = require('./incorrectStyleGuidePath');
const autoprefixerConfig = require('./autoprefixer.config');
const isProduction = () => process.env.NODE_ENV === 'production';
const styleGuidePath = path.dirname(require.resolve('seek-style-guide'));
const styleGuideModules = ['react', 'theme', 'fonts'];
const getIncludePaths = options => {
const extraIncludePaths = options.extraIncludePaths || [];
return {
relative: [
...styleGuideModules.map(module => `seek-style-guide/${module}`),
...extraIncludePaths
],
absolute: [
...styleGuideModules.map(module => path.resolve(styleGuidePath, module)),
...extraIncludePaths.map(include =>
path.dirname(require.resolve(include))
)
]
};
};
const resolveAliases = {
'seek-style-guide': styleGuidePath
};
const singleLine = string =>
string
.replace(/^ +/gm, ' ')
.replace(/\n|\r/gm, '')
.trim();
const warn = message =>
console.warn(
chalk.yellow(`\nSEEK STYLE GUIDE WARNING:\n${singleLine(message)}\n`)
);
const error = message => {
throw new Error(
chalk.red(`\nSEEK STYLE GUIDE ERROR:\n${singleLine(message)}\n`)
);
};
const validateConfig = config => {
if (incorrectStyleGuidePath) {
error(
`
This module has resolved the style-guide path as ${require.resolve(
'seek-style-guide'
)}.
That appears incorrect, did you link this module locally? Please unlink it,
it does not resolve correctly when linked locally.
`
);
}
if (config.module.loaders !== undefined) {
error(
`
You've configured your loaders with the legacy "module.loaders" field.
Please use the newer "module.rules" format instead.
`
);
}
config.module.rules.forEach(rules => {
if (!rules.include) {
error(
`
The following rules config is missing an
"include" value: ${rules.test}. This is required by 'seek-style-guide'
in order to avoid rules clashes. More info:
https://webpack.github.io/docs/configuration.html#module-loaders
`
);
}
});
};
const getLocalIdentName = () =>
isProduction()
? '[hash:base64:7]'
: '__STYLE_GUIDE__[name]__[local]___[hash:base64:7]';
const getCommonLoaders = includes => [
{
test: /\.js$/,
include: includes,
exclude: /\.raw\.js$/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
babelrc: false,
presets: [
[require.resolve('babel-preset-es2015'), { modules: false }],
require.resolve('babel-preset-react')
],
plugins: [
require.resolve('babel-plugin-transform-class-properties'),
require.resolve('babel-plugin-transform-object-rest-spread'),
require.resolve('babel-plugin-add-react-displayname'),
require.resolve('babel-plugin-flow-react-proptypes'),
require.resolve('babel-plugin-transform-flow-strip-types')
]
}
}
]
},
{
test: /\.raw\.js$/,
include: includes,
use: [require.resolve('raw-loader')]
},
{
test: /\.svg$/,
include: includes,
use: [
require.resolve('raw-loader'),
{
loader: require.resolve('svgo-loader'),
options: {
plugins: [
{ removeViewBox: false },
{ addAttributesToSVGElement: { attribute: 'focusable="false"' } }
]
}
}
]
}
];
const decorateConfig = (config, includes, options = {}) => {
const rules = options.rules || [];
const plugins = options.plugins || [];
const externals = options.externals;
// Ensure config meets minimum requirements for decoration
config.module = config.module || {};
config.module.rules = config.module.rules || [];
config.plugins = config.plugins || [];
validateConfig(config);
// Prepend style guide loaders
config.module.rules = getCommonLoaders(includes)
.concat(rules)
.concat(config.module.rules);
// Prepend style guide plugins
config.plugins = plugins.concat(config.plugins);
// Provide externals, if provided
if (externals) {
if (config.externals) {
warn(
`
You've provided "externals" in your Webpack config.
This means that the style guide cannot provide its
own externals for you. It's recommended that you
delete your externals and let the style guide take
care of it, otherwise you will have to manually keep
your own externals in sync with the style guide.
`
);
} else {
config.externals = externals;
}
}
// Add resolve aliases
const consumerAliases =
config.resolve && config.resolve.alias ? config.resolve.alias : {};
for (var alias in resolveAliases) {
if (
consumerAliases[alias] &&
consumerAliases[alias] !== resolveAliases[alias]
) {
error(`Resolve alias '${alias}' is reserved. Please rename it.\n`);
} else {
consumerAliases[alias] = resolveAliases[alias];
}
}
config.resolve = config.resolve || {};
config.resolve.alias = consumerAliases;
return config;
};
const decorateServerConfig = (config, options = {}) => {
const { relative, absolute } = getIncludePaths(options);
return decorateConfig(config, absolute, {
externals: [
nodeExternals({
whitelist: relative
})
],
rules: [
{
test: /\.less$/,
include: absolute,
use: [
{
loader: require.resolve('css-loader/locals'),
options: {
modules: true,
localIdentName: getLocalIdentName()
}
},
require.resolve('less-loader')
]
}
]
});
};
const postcssPlugins = ({ cssSelectorPrefix } = {}) =>
[require('autoprefixer')(autoprefixerConfig)].concat(
!cssSelectorPrefix
? []
: [
require('postcss-prefix-selector')({
prefix: `:global(${cssSelectorPrefix})`
})
]
);
const decorateClientConfig = (config, options = {}) => {
const { absolute } = getIncludePaths(options);
const clientCssLoader = options.cssOutputLoader || 'style-loader';
return decorateConfig(config, absolute, {
rules: [
{
test: /\.less$/,
include: absolute,
use: [
clientCssLoader,
{
loader: require.resolve('css-loader'),
options: {
modules: true,
minimize: isProduction(),
localIdentName: getLocalIdentName(),
importLoaders: 1
}
},
{
loader: require.resolve('postcss-loader'),
options: {
plugins: () => postcssPlugins(options)
}
},
require.resolve('less-loader')
]
}
]
});
};
module.exports = {
decorateServerConfig,
decorateClientConfig,
autoprefixerConfig
};