| [9af201e] | 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 JsonGenerator = require("./JsonGenerator");
|
|---|
| 10 | const JsonParser = require("./JsonParser");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("../Compiler")} Compiler */
|
|---|
| 13 |
|
|---|
| 14 | const PLUGIN_NAME = "JsonModulesPlugin";
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
|
|---|
| 18 | * It adds the json module type to the compiler and registers the json parser and generator.
|
|---|
| 19 | */
|
|---|
| 20 | class JsonModulesPlugin {
|
|---|
| 21 | /**
|
|---|
| 22 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 23 | * @param {Compiler} compiler the compiler instance
|
|---|
| 24 | * @returns {void}
|
|---|
| 25 | */
|
|---|
| 26 | apply(compiler) {
|
|---|
| 27 | compiler.hooks.compilation.tap(
|
|---|
| 28 | PLUGIN_NAME,
|
|---|
| 29 | (compilation, { normalModuleFactory }) => {
|
|---|
| 30 | normalModuleFactory.hooks.createParser
|
|---|
| 31 | .for(JSON_MODULE_TYPE)
|
|---|
| 32 | .tap(PLUGIN_NAME, (parserOptions) => {
|
|---|
| 33 | compiler.validate(
|
|---|
| 34 | () =>
|
|---|
| 35 | require("../../schemas/plugins/json/JsonModulesPluginParser.json"),
|
|---|
| 36 | parserOptions,
|
|---|
| 37 | {
|
|---|
| 38 | name: "Json Modules Plugin",
|
|---|
| 39 | baseDataPath: "parser"
|
|---|
| 40 | },
|
|---|
| 41 | (options) =>
|
|---|
| 42 | require("../../schemas/plugins/json/JsonModulesPluginParser.check")(
|
|---|
| 43 | options
|
|---|
| 44 | )
|
|---|
| 45 | );
|
|---|
| 46 |
|
|---|
| 47 | return new JsonParser(parserOptions);
|
|---|
| 48 | });
|
|---|
| 49 | normalModuleFactory.hooks.createGenerator
|
|---|
| 50 | .for(JSON_MODULE_TYPE)
|
|---|
| 51 | .tap(PLUGIN_NAME, (generatorOptions) => {
|
|---|
| 52 | compiler.validate(
|
|---|
| 53 | () =>
|
|---|
| 54 | require("../../schemas/plugins/json/JsonModulesPluginGenerator.json"),
|
|---|
| 55 | generatorOptions,
|
|---|
| 56 | {
|
|---|
| 57 | name: "Json Modules Plugin",
|
|---|
| 58 | baseDataPath: "generator"
|
|---|
| 59 | },
|
|---|
| 60 | (options) =>
|
|---|
| 61 | require("../../schemas/plugins/json/JsonModulesPluginGenerator.check")(
|
|---|
| 62 | options
|
|---|
| 63 | )
|
|---|
| 64 | );
|
|---|
| 65 |
|
|---|
| 66 | return new JsonGenerator(generatorOptions);
|
|---|
| 67 | });
|
|---|
| 68 | }
|
|---|
| 69 | );
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | module.exports = JsonModulesPlugin;
|
|---|