1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Parser = require("../Parser");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | class AssetParser extends Parser {
|
15 | |
16 |
|
17 |
|
18 | constructor(dataUrlCondition) {
|
19 | super();
|
20 | this.dataUrlCondition = dataUrlCondition;
|
21 | }
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 | parse(source, state) {
|
29 | if (typeof source === "object" && !Buffer.isBuffer(source)) {
|
30 | throw new Error("AssetParser doesn't accept preparsed AST");
|
31 | }
|
32 | state.module.buildInfo.strict = true;
|
33 | state.module.buildMeta.exportsType = "default";
|
34 |
|
35 | if (typeof this.dataUrlCondition === "function") {
|
36 | state.module.buildInfo.dataUrl = this.dataUrlCondition(source, {
|
37 | filename: state.module.matchResource || state.module.resource,
|
38 | module: state.module
|
39 | });
|
40 | } else if (typeof this.dataUrlCondition === "boolean") {
|
41 | state.module.buildInfo.dataUrl = this.dataUrlCondition;
|
42 | } else if (
|
43 | this.dataUrlCondition &&
|
44 | typeof this.dataUrlCondition === "object"
|
45 | ) {
|
46 | state.module.buildInfo.dataUrl =
|
47 | Buffer.byteLength(source) <= this.dataUrlCondition.maxSize;
|
48 | } else {
|
49 | throw new Error("Unexpected dataUrlCondition type");
|
50 | }
|
51 |
|
52 | return state;
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = AssetParser;
|