-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
SystemJSPublicPathWebpackPlugin.js
74 lines (63 loc) · 2.2 KB
/
SystemJSPublicPathWebpackPlugin.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
const webpack = require("webpack");
const path = require("path");
const isWebpack5 = webpack.version && webpack.version.startsWith("5.");
class SystemJSPublicPathWebpackPlugin {
constructor(options) {
this.options = options || {};
if (!isWebpack5 && !this.options.systemjsModuleName) {
throw Error(
`SystemJSPublicPathWebpackPlugin: When using webpack@<5, you must provide 'systemjsModuleName' as an option.`
);
}
}
apply(compiler) {
const additionalEntries = [];
const rootDirectoryLevel = this.options.rootDirectoryLevel || 1;
if (isWebpack5) {
additionalEntries.push(
path.resolve(__dirname, `auto-public-path/${rootDirectoryLevel}`)
);
} else {
additionalEntries.push(
path.resolve(
__dirname,
`resource-query-public-path?systemjsModuleName=${encodeURIComponent(this.options.systemjsModuleName)}&rootDirectoryLevel=${rootDirectoryLevel}`
)
);
}
compiler.options.entry = prependEntry(
compiler.options.entry,
additionalEntries
);
}
}
// This function was copied from https://github.com/webpack/webpack-dev-server/blob/b0161e9852cdf41730e82aa43efe7e88f44a4f9d/lib/utils/DevServerPlugin.js#L72
function prependEntry(originalEntry, additionalEntries) {
if (typeof originalEntry === "function") {
return () =>
Promise.resolve(originalEntry()).then(entry =>
prependEntry(entry, additionalEntries)
);
}
if (typeof originalEntry === "object" && !Array.isArray(originalEntry)) {
/** @type {Object<string,string>} */
const clone = {};
Object.keys(originalEntry).forEach(key => {
// entry[key] should be a string here
const entryDescription = originalEntry[key];
clone[key] = prependEntry(entryDescription, additionalEntries);
});
return clone;
}
// in this case, entry is a string or an array.
// make sure that we do not add duplicates.
/** @type {Entry} */
const entriesClone = additionalEntries.slice(0);
[].concat(originalEntry).forEach(newEntry => {
if (!entriesClone.includes(newEntry)) {
entriesClone.push(newEntry);
}
});
return entriesClone;
}
module.exports = SystemJSPublicPathWebpackPlugin;