-
-
Notifications
You must be signed in to change notification settings - Fork 206
/
index.js
62 lines (49 loc) · 1.48 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
import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
import { sourcePlugin, minimizerPlugin } from './plugins';
import {
pluginRunner,
normalizeOptions,
getImportCode,
getModuleCode,
getExportCode,
} from './utils';
import schema from './options.json';
export default async function loader(content) {
const rawOptions = getOptions(this);
validateOptions(schema, rawOptions, {
name: 'HTML Loader',
baseDataPath: 'options',
});
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 = [];
if (options.attributes) {
plugins.push(
sourcePlugin({
attributes: options.attributes,
resourcePath: this.resourcePath,
imports,
errors,
replacements,
})
);
}
if (options.minimize) {
plugins.push(minimizerPlugin({ minimize: options.minimize, errors }));
}
const { html } = pluginRunner(plugins).process(content);
for (const error of errors) {
this.emitError(error instanceof Error ? error : new Error(error));
}
const importCode = getImportCode(html, this, imports, options);
const moduleCode = getModuleCode(html, replacements, options);
const exportCode = getExportCode(html, options);
return `${importCode}${moduleCode}${exportCode}`;
}