-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ExposeRuntimeCssAssetsPlugin.cjs
110 lines (98 loc) · 3.4 KB
/
ExposeRuntimeCssAssetsPlugin.cjs
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
const webpack = require("webpack");
const RuntimeModule = require("webpack/lib/RuntimeModule");
const Template = require("webpack/lib/Template");
const { RuntimeGlobals } = require("webpack");
const { MODULE_TYPE } = require("mini-css-extract-plugin/dist/utils");
const pluginName = "SingleSpaExposeRuntimeCssAssetsPlugin";
class ExposedCssRuntimeModule extends RuntimeModule {
constructor() {
super("exposed-css-runtime", 10);
}
generate() {
return Template.asString(
`${RuntimeGlobals.require}.cssAssets = ${JSON.stringify([
this.chunk.id,
])};`
);
}
}
module.exports = class ExposeRuntimeCssAssetsPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.contentHash.tap(pluginName, (chunk) => {
const { outputOptions, chunkGraph } = compilation;
const modules = chunkGraph.getChunkModulesIterableBySourceType(
chunk,
MODULE_TYPE
);
if (modules) {
const { hashFunction, hashDigest, hashDigestLength } = outputOptions;
const hash = webpack.util.createHash(hashFunction);
for (const m of modules) {
m.updateHash(hash, { chunkGraph });
}
// eslint-disable-next-line no-param-reassign
chunk.contentHash[MODULE_TYPE] = hash
.digest(hashDigest)
.substring(0, hashDigestLength);
}
});
compilation.hooks.afterOptimizeChunks.tap(pluginName, (chunks) => {
chunks.forEach((chunk) => {
if (chunk.hasEntryModule()) {
let foundCssModule = false;
for (let module of chunk.getModules()) {
if (module.type === MODULE_TYPE) {
foundCssModule = true;
break;
}
}
if (foundCssModule) {
if (!chunk.contentHash[MODULE_TYPE]) {
const { outputOptions, chunkGraph } = compilation;
const modules = chunkGraph.getChunkModulesIterableBySourceType(
chunk,
MODULE_TYPE
);
if (modules) {
const {
hashFunction,
hashDigest,
hashDigestLength,
} = outputOptions;
const hash = webpack.util.createHash(hashFunction);
for (const m of modules) {
m.updateHash(hash, { chunkGraph });
}
// eslint-disable-next-line no-param-reassign
chunk.contentHash[MODULE_TYPE] = hash
.digest(hashDigest)
.substring(0, hashDigestLength);
}
}
compilation.addRuntimeModule(
chunk,
new webpack.runtime.GetChunkFilenameRuntimeModule(
MODULE_TYPE,
"single-spa-css",
`${webpack.RuntimeGlobals.require}.cssAssetFileName`,
(referencedChunk) => {
return this.options.filename;
},
true
)
);
compilation.addRuntimeModule(
chunk,
new ExposedCssRuntimeModule()
);
}
}
});
});
});
}
};