-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
130 lines (116 loc) · 3.75 KB
/
index.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const fs = require("fs");
const path = require("path");
const packageJson = require("./package.json");
const getLocalIdent = require("./scripts/getLocalIdent.cjs");
const SCRIPT_INJECT_SRC = path.resolve(__dirname, "./scripts/inject/setAttributeDynamic.js");
const SCRIPT_INJECT = SCRIPT_INJECT_SRC.replace(/\.js$/, "_generated.js");
class _class {
static DEFAULT_OPTIONS = {
enabled: true,
entryName: undefined,
localIdentName: "[md4:hash:base64:5]",
attributes: /^(class)$/,
exclusionTags: /(path)/i,
exclusionValues: /^(css|sc|icon)-/i,
scope: "",
inject: {
src: SCRIPT_INJECT_SRC,
out: SCRIPT_INJECT
}
};
constructor(options) {
this.plugin = {
name: packageJson.name,
version: packageJson.version
};
this.options = {
..._class.DEFAULT_OPTIONS,
...options
};
}
apply(compiler) {
const _this = this;
const {options} = _this;
if (options.enabled !== true) {
return;
}
compiler.hooks.afterEnvironment.tap(_this.plugin.name, () => {
const rules = compiler.options.module.rules;
//Inject options into css-loader
const cssRule = rules.find((rule) => rule.test.test(".css"));
if (!cssRule) {
throw new Error(".css rule not found");
}
const cssLoader = cssRule.use.find((loader) => loader.loader === "css-loader");
if (!cssLoader) {
throw new Error("css-loader not found");
}
const cssLoaderOptions = cssLoader.options || {};
const cssLoaderOptionsModules = cssLoaderOptions.modules || {};
cssLoaderOptionsModules.localIdentName = options.localIdentName;
cssLoaderOptionsModules.getLocalIdent = getLocalIdent(options);
cssLoaderOptions.modules = cssLoaderOptionsModules;
cssLoader.options = cssLoaderOptions;
//console.log(rules);
//Inject string-replace-loader to hook into react-dom setAttribute
rules.unshift({
//react-dom.development.js
//react-dom.production.min.js
test: /react-dom\..+\.js$/,
loader: "string-replace-loader",
options: {
search: /(\w+)\.setAttribute\(/g,
replace: `window['${options.scope}setAttributeDynamic'].call($1,`
}
});
//console.log(rules);
//Inject the setAttributeDynamic function into the bundle
const entry = compiler.options.entry;
const entryName = options.entryName || Object.keys(entry)[0];
const entryBundle = entry[entryName];
if (!entryBundle) {
throw new Error("Entry bundle not found");
}
const hasImport = typeof entryBundle.import !== typeof undefined;
let entryBundleImport = hasImport ? entryBundle.import : entryBundle;
if (!Array.isArray(entryBundleImport)) {
entryBundleImport = [entryBundleImport];
}
_this._generateInjectScript(options);
entryBundleImport.unshift(options.inject.out);
if (hasImport) {
entryBundle.import = entryBundleImport;
} else {
entry[entryName] = entryBundleImport;
}
//console.log(entry);
//process.exit(1);
});
}
_generateInjectScript(options) {
const _this = this;
const {src, out} = options.inject;
const data = fs.readFileSync(src, "utf8");
const tokens = _this._computeTokens(options);
const output = _this._replaceTokens(data, tokens);
fs.writeFileSync(out, output, "utf8");
}
_computeTokens(options) {
return {
__PACKAGE_NAME__: packageJson["name"],
__PACKAGE_VERSION__: packageJson["version"],
__LOCAL_IDENT_NAME__: options.localIdentName,
__ATTRIBUTES__: options.attributes,
__EXCLUSION_TAGS__: options.exclusionTags,
__EXCLUSION_VALUES__: options.exclusionValues,
__SCOPE__: options.scope
};
}
_replaceTokens(input, tokens) {
return Object.entries(tokens).reduce((output, [key, value]) => {
const pattern = typeof value === "string" ? key : `["']?${key}["']?`;
return output.replace(new RegExp(pattern, "g"), value);
}, input);
}
}
module.exports = _class;