1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const parseJson = require("json-parse-better-errors");
|
9 | const Parser = require("../Parser");
|
10 | const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | class JsonParser extends Parser {
|
17 | |
18 |
|
19 |
|
20 | constructor(options) {
|
21 | super();
|
22 | this.options = options || {};
|
23 | }
|
24 |
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 | parse(source, state) {
|
31 | if (Buffer.isBuffer(source)) {
|
32 | source = source.toString("utf-8");
|
33 | }
|
34 |
|
35 |
|
36 | const parseFn =
|
37 | typeof this.options.parse === "function" ? this.options.parse : parseJson;
|
38 |
|
39 | const data =
|
40 | typeof source === "object"
|
41 | ? source
|
42 | : parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
|
43 |
|
44 | state.module.buildInfo.jsonData = data;
|
45 | state.module.buildInfo.strict = true;
|
46 | state.module.buildMeta.exportsType = "default";
|
47 | state.module.buildMeta.defaultObject =
|
48 | typeof data === "object" ? "redirect-warn" : false;
|
49 | state.module.addDependency(
|
50 | new JsonExportsDependency(JsonExportsDependency.getExportsFromData(data))
|
51 | );
|
52 | return state;
|
53 | }
|
54 | }
|
55 |
|
56 | module.exports = JsonParser;
|