-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathindex.js
93 lines (77 loc) · 2.32 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
import { sourcesPlugin, minimizerPlugin } from "./plugins";
import {
pluginRunner,
normalizeOptions,
getImportCode,
getModuleCode,
getExportCode,
defaultMinimizerOptions,
supportTemplateLiteral,
convertToTemplateLiteral,
} from "./utils";
import schema from "./options.json";
export default async function loader(content) {
const rawOptions = this.getOptions(schema);
const options = normalizeOptions(rawOptions, this);
if (options.preprocessor) {
// eslint-disable-next-line no-param-reassign
content = await options.preprocessor(content, this);
}
const plugins = [];
const errors = [];
const imports = [];
const replacements = [];
let isSupportAbsoluteURL = false;
// TODO enable by default in the next major release
if (
this._compilation &&
this._compilation.options &&
this._compilation.options.experiments &&
this._compilation.options.experiments.buildHttp
) {
isSupportAbsoluteURL = true;
}
if (options.sources) {
plugins.push(
sourcesPlugin({
isSupportAbsoluteURL,
isSupportDataURL: options.esModule,
sources: options.sources,
resourcePath: this.resourcePath,
context: this.context,
imports,
errors,
replacements,
}),
);
}
if (options.minimize) {
plugins.push(minimizerPlugin({ minimize: options.minimize, errors }));
}
let { html } = await pluginRunner(plugins).process(content);
for (const error of errors) {
this.emitError(error instanceof Error ? error : new Error(error));
}
const isTemplateLiteralSupported = supportTemplateLiteral(this);
html = (
isTemplateLiteralSupported
? convertToTemplateLiteral(html)
: JSON.stringify(html)
)
// Invalid in JavaScript but valid HTML
.replace(/[\u2028\u2029]/g, (str) =>
str === "\u2029" ? "\\u2029" : "\\u2028",
);
if (options.postprocessor) {
// eslint-disable-next-line no-param-reassign
html = await options.postprocessor(html, this);
}
const importCode = getImportCode(html, imports, options);
const moduleCode = getModuleCode(html, replacements, this, {
esModule: options.esModule,
isTemplateLiteralSupported,
});
const exportCode = getExportCode(html, options);
return `${importCode}${moduleCode}${exportCode}`;
}
export { defaultMinimizerOptions };