webpack/lib/json/JsonModulesPlugin.js

70 lines
2.0 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2023-04-01 00:45:36 +08:00
const { JSON_MODULE_TYPE } = require("../ModuleTypeConstants");
const createSchemaValidation = require("../util/create-schema-validation");
const JsonGenerator = require("./JsonGenerator");
2018-07-30 23:08:51 +08:00
const JsonParser = require("./JsonParser");
2017-12-07 03:37:58 +08:00
2019-10-11 21:46:57 +08:00
/** @typedef {import("../Compiler")} Compiler */
2025-04-04 21:38:51 +08:00
/** @typedef {import("../util/fs").JsonArray} JsonArray */
/** @typedef {import("../util/fs").JsonObject} JsonObject */
/** @typedef {import("../util/fs").JsonValue} JsonValue */
2018-11-03 04:05:46 +08:00
const validate = createSchemaValidation(
require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
() => require("../../schemas/plugins/JsonModulesPluginParser.json"),
{
name: "Json Modules Plugin",
baseDataPath: "parser"
}
);
2019-11-29 01:12:11 +08:00
const validateGenerator = createSchemaValidation(
require("../../schemas/plugins/JsonModulesPluginGenerator.check.js"),
() => require("../../schemas/plugins/JsonModulesPluginGenerator.json"),
{
name: "Json Modules Plugin",
baseDataPath: "generator"
}
);
const PLUGIN_NAME = "JsonModulesPlugin";
/**
* The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
* It adds the json module type to the compiler and registers the json parser and generator.
*/
class JsonModulesPlugin {
2018-11-03 04:05:46 +08:00
/**
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap(
PLUGIN_NAME,
2018-02-25 09:00:20 +08:00
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.createParser
.for(JSON_MODULE_TYPE)
.tap(PLUGIN_NAME, parserOptions => {
validate(parserOptions);
return new JsonParser(parserOptions);
2018-02-25 09:00:20 +08:00
});
normalModuleFactory.hooks.createGenerator
.for(JSON_MODULE_TYPE)
.tap(PLUGIN_NAME, generatorOptions => {
validateGenerator(generatorOptions);
return new JsonGenerator(generatorOptions);
});
2018-02-25 09:00:20 +08:00
}
);
}
}
module.exports = JsonModulesPlugin;