1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { JSON_MODULE_TYPE } = require("../ModuleTypeConstants");
|
---|
9 | const createSchemaValidation = require("../util/create-schema-validation");
|
---|
10 | const JsonGenerator = require("./JsonGenerator");
|
---|
11 | const JsonParser = require("./JsonParser");
|
---|
12 |
|
---|
13 | /** @typedef {import("../Compiler")} Compiler */
|
---|
14 | /** @typedef {Record<string, any>} RawJsonData */
|
---|
15 |
|
---|
16 | const validate = createSchemaValidation(
|
---|
17 | require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
|
---|
18 | () => require("../../schemas/plugins/JsonModulesPluginParser.json"),
|
---|
19 | {
|
---|
20 | name: "Json Modules Plugin",
|
---|
21 | baseDataPath: "parser"
|
---|
22 | }
|
---|
23 | );
|
---|
24 |
|
---|
25 | const PLUGIN_NAME = "JsonModulesPlugin";
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
|
---|
29 | * It adds the json module type to the compiler and registers the json parser and generator.
|
---|
30 | */
|
---|
31 | class JsonModulesPlugin {
|
---|
32 | /**
|
---|
33 | * Apply the plugin
|
---|
34 | * @param {Compiler} compiler the compiler instance
|
---|
35 | * @returns {void}
|
---|
36 | */
|
---|
37 | apply(compiler) {
|
---|
38 | compiler.hooks.compilation.tap(
|
---|
39 | PLUGIN_NAME,
|
---|
40 | (compilation, { normalModuleFactory }) => {
|
---|
41 | normalModuleFactory.hooks.createParser
|
---|
42 | .for(JSON_MODULE_TYPE)
|
---|
43 | .tap(PLUGIN_NAME, parserOptions => {
|
---|
44 | validate(parserOptions);
|
---|
45 |
|
---|
46 | return new JsonParser(parserOptions);
|
---|
47 | });
|
---|
48 | normalModuleFactory.hooks.createGenerator
|
---|
49 | .for(JSON_MODULE_TYPE)
|
---|
50 | .tap(PLUGIN_NAME, () => new JsonGenerator());
|
---|
51 | }
|
---|
52 | );
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | module.exports = JsonModulesPlugin;
|
---|