webpack/lib/json/JsonModulesPlugin.js

51 lines
1.2 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";
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 */
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
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(
"JsonModulesPlugin",
(compilation, { normalModuleFactory }) => {
normalModuleFactory.hooks.createParser
.for("json")
2019-11-29 01:12:11 +08:00
.tap("JsonModulesPlugin", parserOptions => {
validate(parserOptions);
2019-11-29 01:12:11 +08:00
return new JsonParser(parserOptions);
2018-02-25 09:00:20 +08:00
});
normalModuleFactory.hooks.createGenerator
.for("json")
.tap("JsonModulesPlugin", () => {
return new JsonGenerator();
});
}
);
}
}
module.exports = JsonModulesPlugin;