-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
334 lines (286 loc) · 14.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
const path = require('path');
const fs = require('fs');
const loaderUtils = require('loader-utils');
class MiniCssThemesWebpackPlugin {
constructor(config) {
this.validate(config);
this.init(config);
}
validate({themes, defaultTheme}) {
if (!themes) {
throw new Error(`You must provide the list of themes.`);
}
if (!defaultTheme) {
throw new Error(`You must provide the default theme key.`);
}
if (!themes[defaultTheme]) {
throw new Error(`Default theme '${defaultTheme}' missing from themes definition.`);
}
const areThemesSingleEntry = typeof themes[defaultTheme] === 'string';
const themeEntryKeys = areThemesSingleEntry ? [] : Object.keys(themes[defaultTheme]);
const validateThemeFile = (themePath, themeKey, entry = null) => {
const fullPath = path.resolve(themePath);
if (!fs.existsSync(fullPath)) {
throw new Error(`Theme '${entry ? `${themeKey}.${entry}` : themeKey}' file not found: ${fullPath}`);
}
};
Object.entries(themes).forEach(([themeKey, fileOrFilesObject]) => {
if (areThemesSingleEntry) {
if (typeof fileOrFilesObject !== 'string') {
throw new Error(`All themes value must be a string (path to theme file) as the default theme is also string.`);
}
return validateThemeFile(fileOrFilesObject, themeKey);
}
if (typeof fileOrFilesObject !== 'object') {
throw new Error(`Themes value must be either object or string.`);
}
const currentThemeEntryKeys = Object.keys(fileOrFilesObject);
if (currentThemeEntryKeys.length !== themeEntryKeys.length || themeEntryKeys.filter((k) => !currentThemeEntryKeys.includes(k)).length) {
throw new Error(`Missing or additional theme entries in '${themeKey}'. All themes must match schema of default theme.`);
}
Object.entries(fileOrFilesObject).forEach(([entry, path]) => {
validateThemeFile(path, themeKey, entry);
});
});
}
init({themes, defaultTheme, chunkPrefix}) {
this.pluginName = this.constructor.name;
this.themes = themes;
this.defaultTheme = defaultTheme;
this.chunkPrefix = chunkPrefix || 'theme__';
this.nonDefaultThemeKeys = Object.keys(this.themes).filter(t => t !== this.defaultTheme);
this.themeChunkNames = this.nonDefaultThemeKeys.map(theme => `${this.chunkPrefix}${theme}`);
const areThemesSingleEntry = typeof themes[defaultTheme] === 'string';
this.absoluteThemePaths = {};
Object.entries(themes).forEach(([key, fileOrFilesObject]) => {
if (areThemesSingleEntry) {
this.absoluteThemePaths[key] = [path.resolve(fileOrFilesObject)];
} else {
this.absoluteThemePaths[key] = Object.entries(fileOrFilesObject)
.sort((a, b) => a.key > b.key ? -1 : 1)
.reduce((reduced, [,current]) => reduced.push(current) && reduced, []);
}
});
this.defaultImportFilenames = this.absoluteThemePaths[this.defaultTheme].map((themePath) => path.basename(themePath).replace(/\.[a-zA-Z0-9]+$/, ''));
}
apply(compiler) {
const filterThemeChunks = chunks => chunks.filter(chunk =>
this.themeChunkNames.find(c => chunk.chunkReason && chunk.chunkReason.indexOf(`(cache group: ${c})`) !== -1)
);
this.themedModules = [];
const findSassDependencies = (module) => module.dependencies.filter(dep => dep.request && dep.request.match(/\.scss$/));
const cloneDependencyForTheme = (theme, dep) => {
const cloneDependency = Object.assign(Object.create(Object.getPrototypeOf(dep)), dep);
// Mark for which theme we are cloning the sass dependency.
cloneDependency.themed = theme;
// Need to modify the identifier so that it is recorded as a separate dependency.
// Otherwise webpack will consider them the same deps and not attempt creating
// different modules for them.
const oldGetResourceIdentifier = cloneDependency.getResourceIdentifier.bind(cloneDependency);
cloneDependency.getResourceIdentifier = () => oldGetResourceIdentifier() + `?theme=${theme}`;
return cloneDependency;
};
const themeSassDependencies = (module) => {
findSassDependencies(module).forEach((dep) => {
this.nonDefaultThemeKeys.forEach(theme => {
const clone = cloneDependencyForTheme(theme, dep);
module.dependencies.push(clone);
});
this.themedModules.push(path.join(module.context, dep.request));
});
}
const transformSassDependencies = (module, theme) => {
findSassDependencies(module).forEach((dep) => {
const clone = cloneDependencyForTheme(theme, dep);
module.dependencies.splice(module.dependencies.indexOf(dep), 1);
module.dependencies.push(clone);
});
};
compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
// Finds import dependencies targeted to sass files and duplicates them for each theme.
compilation.hooks.succeedModule.tap(this.pluginName, module => {
// For modules that have been marked for theming we need to ensure to make their dependencies unique,
// since otherwise webpack will not include these dependencies in the themed version as well.
let theme;
if (module.request && (theme = module.request.match(/\?\?.*theme:([^:?!]+)/))) {
transformSassDependencies(module, theme[1]);
return;
}
// Prevent theming dependencies of already themed modules. Since the root module is already themed, the theme
// will apply accordingly in dependencies as well (?).
if (module.issuer && this.themedModules.indexOf(module.issuer.resource) !== -1) {
this.themedModules.push(module.resource);
return;
}
themeSassDependencies(module);
});
});
// Makes sure that modules contain theme information and that they are
// duplicated for compilation as well.
compiler.hooks.beforeCompile.tap(this.pluginName, (params) => {
params.normalModuleFactory.hooks.beforeResolve.tap(this.pluginName, (module) => {
const theme = module.dependencies[0].themed;
const isSassModuleAndIsThemed = module.request.match(/\.scss$/) && theme;
if (isSassModuleAndIsThemed) {
// Need to update the request as otherwise webpack will consider it the same
// module and will reuse the same compilation of it.
module.request = `${module.request}??theme:${theme}`;
}
});
});
// Create special loaders for css modules that were marked for themes so that
// imports can be switched to the different theme one.
compiler.hooks.beforeCompile.tap(this.pluginName, (params) => {
params.normalModuleFactory.hooks.module.tap(this.pluginName, (module) => {
const theme = module.request.match(/\?\?.*theme:([^:?!]+)/);
const loaderPath = require.resolve('./loader.js');
if (theme && !module.loaders.find(l => l.loader === loaderPath)) {
module.themed = theme[1];
module.loaders.push({
loader: require.resolve('./loader.js'),
options: {
defaultImportFilenames: this.defaultImportFilenames,
defaultImportPaths: this.absoluteThemePaths[this.defaultTheme],
targetImportPaths: this.absoluteThemePaths[theme[1]],
}
});
}
});
});
// Make sure to not generate different hashes for classes loaded from
// theme files as composes.
compiler.hooks.environment.tap(this.pluginName, () => {
const fileToThemeFileTypeMap = {};
const classNamesPerThemeFileTypes = {};
Object.entries(this.themes).forEach(([_, fileOrFiles]) => {
if (typeof fileOrFiles === 'string') {
fileToThemeFileTypeMap[fileOrFiles] = 'default';
} else {
Object.entries(fileOrFiles).forEach(([type, file]) => {
fileToThemeFileTypeMap[path.resolve(file)] = type;
});
}
});
compiler.options.module.rules.forEach((rule) => {
if (!rule.use) {
// @TODO: In certain cases css loader can be nested in mini css extract?
return;
}
(Array.isArray(rule.use) ? rule.use : [rule.use])
.filter((loader) => loader.loader === 'css-loader' && loader.options && loader.options.modules)
.forEach((loader) => {
const isCssLoaderOldVersion = typeof loader.options.modules === 'boolean';
let originalGetLocalIdent;
if (isCssLoaderOldVersion) {
originalGetLocalIdent = loader.options.getLocalIdent || require('css-loader/dist/utils').getLocalIdent;
} else {
originalGetLocalIdent = loader.options.modules.getLocalIdent || ((loaderContext, localIdentName, localName, options) =>{
const {context, hashPrefix} = options;
const {resourcePath} = loaderContext;
const request = path.relative(context, resourcePath);
options.content = `${hashPrefix + request}\x00${localName}`;
return loaderUtils.interpolateName(loaderContext, localIdentName, options)
.replace(/\[local]/gi, localName);
});
}
const getLocalIdent = (...args) => {
const [module,,className] = args;
const themeFileType = fileToThemeFileTypeMap[module.resourcePath];
const hasClassName = themeFileType
&& classNamesPerThemeFileTypes[themeFileType]
&& classNamesPerThemeFileTypes[themeFileType][className];
if (hasClassName) {
return classNamesPerThemeFileTypes[themeFileType][className];
}
const ident = originalGetLocalIdent(...args);
if (!hasClassName && themeFileType) {
classNamesPerThemeFileTypes[themeFileType] = classNamesPerThemeFileTypes[themeFileType] || {};
classNamesPerThemeFileTypes[themeFileType][className] = ident;
}
return ident;
};
if (isCssLoaderOldVersion) {
loader.options.getLocalIdent = getLocalIdent;
} else {
loader.options.modules.getLocalIdent = getLocalIdent;
}
});
});
});
// Separate theme based css modules in their own chunks.
compiler.hooks.entryOption.tap(this.pluginName, () => {
compiler.options.optimization.splitChunks = compiler.options.optimization.splitChunks || {};
compiler.options.optimization.splitChunks.cacheGroups = compiler.options.optimization.splitChunks.cacheGroups || {};
const hasReasonTheme = (module, theme) => {
const actualTheme = module._identifier.match(/\?\?theme:([^:?!]+)/);
return actualTheme ? actualTheme[1] === theme : false;
};
const addThemeChunk = (themeKey) => {
compiler.options.optimization.splitChunks.cacheGroups[`${this.chunkPrefix}${themeKey}`] = {
test: (m) => m.constructor.name === 'CssModule' && hasReasonTheme(m, themeKey),
chunks: 'all',
name: (_module, chunks, name) => `${name}~${chunks.map(c => c.name).join('~')}`,
enforce: true,
};
};
this.nonDefaultThemeKeys.forEach(theme => addThemeChunk(theme));
});
let moduleDependencyHistory = [];
let moduleChunkHistory = [];
// Required cleanup for functional final compilation.
compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
compilation.hooks.optimizeChunkModules.tap(this.pluginName, chunks => {
chunks.forEach(chunk => {
chunk.getModules().forEach(module => {
// Remove duplicate dependency links for the same css module as otherwise
// webpack will concatenate IDs on the imports of these modules which will
// produce an id to a module that doesn't in fact exist.
if (module.dependencies && module.dependencies.find(dep => !dep.themed)) {
moduleDependencyHistory.push({module, dependencies: module.dependencies});
module.dependencies = module.dependencies.filter(dep => !dep.themed);
}
// Remove from built JS duplicates of css modules classes generated by themes.
if (module.themed) {
moduleChunkHistory.push({module, chunk});
module.removeChunk(chunk);
}
});
});
// Remove theme chunks from the entry points so that they are not recognized in
// a dependency tree and thus we can safely remove the empty JS files generated
// by these chunks.
filterThemeChunks(chunks).forEach(chunk => chunk._groups.forEach(entry => entry.removeChunk(chunk)));
});
});
// Remove assets generated by theme chunks that are not css.
compiler.hooks.thisCompilation.tap(this.pluginName, (compilation) => {
// @TODO: Deprecated hook in latest webpack version.
compilation.hooks.optimizeChunkAssets.tap(this.pluginName, chunks => {
filterThemeChunks(chunks).forEach(chunk => {
const nonCssAssets = chunk.files.filter(file => !file.match(/\.css$/));
nonCssAssets.forEach(file => delete compilation.assets[file]);
if (chunk.files instanceof Set) {
chunk.files = new Set(chunk.files.filter(file => file.match(/\.css$/)));
} else {
chunk.files = chunk.files.filter(file => file.match(/\.css$/));
}
});
});
});
// When in watch mode, we need to revert previous cleanup as otherwise dependency
// links will be lost and the themes won't be included in future compilations.
compiler.hooks.watchRun.tap(this.pluginName, () => {
compiler.hooks.afterCompile.tap(this.pluginName, () => {
if (moduleDependencyHistory.length) {
moduleDependencyHistory.forEach((history) => history.module.dependencies = history.dependencies);
moduleDependencyHistory = [];
}
if (moduleChunkHistory.length) {
moduleChunkHistory.forEach((history) => history.chunk.addModule(history.module));
moduleChunkHistory = [];
}
});
});
}
}
module.exports = MiniCssThemesWebpackPlugin;