123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const {
- JAVASCRIPT_MODULE_TYPE_AUTO,
- JAVASCRIPT_MODULE_TYPE_DYNAMIC
- } = require("../ModuleTypeConstants");
- const RuntimeGlobals = require("../RuntimeGlobals");
- const {
- approve,
- evaluateToIdentifier,
- evaluateToString,
- toConstantDependency
- } = require("../javascript/JavascriptParserHelpers");
- const AMDDefineDependency = require("./AMDDefineDependency");
- const AMDDefineDependencyParserPlugin = require("./AMDDefineDependencyParserPlugin");
- const AMDRequireArrayDependency = require("./AMDRequireArrayDependency");
- const AMDRequireContextDependency = require("./AMDRequireContextDependency");
- const AMDRequireDependenciesBlockParserPlugin = require("./AMDRequireDependenciesBlockParserPlugin");
- const AMDRequireDependency = require("./AMDRequireDependency");
- const AMDRequireItemDependency = require("./AMDRequireItemDependency");
- const {
- AMDDefineRuntimeModule,
- AMDOptionsRuntimeModule
- } = require("./AMDRuntimeModules");
- const ConstDependency = require("./ConstDependency");
- const LocalModuleDependency = require("./LocalModuleDependency");
- const UnsupportedDependency = require("./UnsupportedDependency");
- /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */
- /** @typedef {import("../../declarations/WebpackOptions").ModuleOptionsNormalized} ModuleOptions */
- /** @typedef {import("../Compiler")} Compiler */
- /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */
- /** @typedef {import("../javascript/JavascriptParser")} Parser */
- /** @typedef {import("../javascript/JavascriptParser").Range} Range */
- const PLUGIN_NAME = "AMDPlugin";
- class AMDPlugin {
- /**
- * @param {Record<string, any>} amdOptions the AMD options
- */
- constructor(amdOptions) {
- this.amdOptions = amdOptions;
- }
- /**
- * Apply the plugin
- * @param {Compiler} compiler the compiler instance
- * @returns {void}
- */
- apply(compiler) {
- const amdOptions = this.amdOptions;
- compiler.hooks.compilation.tap(
- PLUGIN_NAME,
- (compilation, { contextModuleFactory, normalModuleFactory }) => {
- compilation.dependencyTemplates.set(
- AMDRequireDependency,
- new AMDRequireDependency.Template()
- );
- compilation.dependencyFactories.set(
- AMDRequireItemDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- AMDRequireItemDependency,
- new AMDRequireItemDependency.Template()
- );
- compilation.dependencyTemplates.set(
- AMDRequireArrayDependency,
- new AMDRequireArrayDependency.Template()
- );
- compilation.dependencyFactories.set(
- AMDRequireContextDependency,
- contextModuleFactory
- );
- compilation.dependencyTemplates.set(
- AMDRequireContextDependency,
- new AMDRequireContextDependency.Template()
- );
- compilation.dependencyTemplates.set(
- AMDDefineDependency,
- new AMDDefineDependency.Template()
- );
- compilation.dependencyTemplates.set(
- UnsupportedDependency,
- new UnsupportedDependency.Template()
- );
- compilation.dependencyTemplates.set(
- LocalModuleDependency,
- new LocalModuleDependency.Template()
- );
- compilation.hooks.runtimeRequirementInModule
- .for(RuntimeGlobals.amdDefine)
- .tap(PLUGIN_NAME, (module, set) => {
- set.add(RuntimeGlobals.require);
- });
- compilation.hooks.runtimeRequirementInModule
- .for(RuntimeGlobals.amdOptions)
- .tap(PLUGIN_NAME, (module, set) => {
- set.add(RuntimeGlobals.requireScope);
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.amdDefine)
- .tap(PLUGIN_NAME, (chunk, set) => {
- compilation.addRuntimeModule(chunk, new AMDDefineRuntimeModule());
- });
- compilation.hooks.runtimeRequirementInTree
- .for(RuntimeGlobals.amdOptions)
- .tap(PLUGIN_NAME, (chunk, set) => {
- compilation.addRuntimeModule(
- chunk,
- new AMDOptionsRuntimeModule(amdOptions)
- );
- });
- /**
- * @param {Parser} parser parser parser
- * @param {JavascriptParserOptions} parserOptions parserOptions
- * @returns {void}
- */
- const handler = (parser, parserOptions) => {
- if (parserOptions.amd !== undefined && !parserOptions.amd) return;
- /**
- * @param {string} optionExpr option expression
- * @param {string} rootName root name
- * @param {function(): TODO} getMembers callback
- */
- const tapOptionsHooks = (optionExpr, rootName, getMembers) => {
- parser.hooks.expression
- .for(optionExpr)
- .tap(
- PLUGIN_NAME,
- toConstantDependency(parser, RuntimeGlobals.amdOptions, [
- RuntimeGlobals.amdOptions
- ])
- );
- parser.hooks.evaluateIdentifier
- .for(optionExpr)
- .tap(
- PLUGIN_NAME,
- evaluateToIdentifier(optionExpr, rootName, getMembers, true)
- );
- parser.hooks.evaluateTypeof
- .for(optionExpr)
- .tap(PLUGIN_NAME, evaluateToString("object"));
- parser.hooks.typeof
- .for(optionExpr)
- .tap(
- PLUGIN_NAME,
- toConstantDependency(parser, JSON.stringify("object"))
- );
- };
- new AMDRequireDependenciesBlockParserPlugin(parserOptions).apply(
- parser
- );
- new AMDDefineDependencyParserPlugin(parserOptions).apply(parser);
- tapOptionsHooks("define.amd", "define", () => "amd");
- tapOptionsHooks("require.amd", "require", () => ["amd"]);
- tapOptionsHooks(
- "__webpack_amd_options__",
- "__webpack_amd_options__",
- () => []
- );
- parser.hooks.expression.for("define").tap(PLUGIN_NAME, expr => {
- const dep = new ConstDependency(
- RuntimeGlobals.amdDefine,
- /** @type {Range} */ (expr.range),
- [RuntimeGlobals.amdDefine]
- );
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
- parser.state.module.addPresentationalDependency(dep);
- return true;
- });
- parser.hooks.typeof
- .for("define")
- .tap(
- PLUGIN_NAME,
- toConstantDependency(parser, JSON.stringify("function"))
- );
- parser.hooks.evaluateTypeof
- .for("define")
- .tap(PLUGIN_NAME, evaluateToString("function"));
- parser.hooks.canRename.for("define").tap(PLUGIN_NAME, approve);
- parser.hooks.rename.for("define").tap(PLUGIN_NAME, expr => {
- const dep = new ConstDependency(
- RuntimeGlobals.amdDefine,
- /** @type {Range} */ (expr.range),
- [RuntimeGlobals.amdDefine]
- );
- dep.loc = /** @type {DependencyLocation} */ (expr.loc);
- parser.state.module.addPresentationalDependency(dep);
- return false;
- });
- parser.hooks.typeof
- .for("require")
- .tap(
- PLUGIN_NAME,
- toConstantDependency(parser, JSON.stringify("function"))
- );
- parser.hooks.evaluateTypeof
- .for("require")
- .tap(PLUGIN_NAME, evaluateToString("function"));
- };
- normalModuleFactory.hooks.parser
- .for(JAVASCRIPT_MODULE_TYPE_AUTO)
- .tap(PLUGIN_NAME, handler);
- normalModuleFactory.hooks.parser
- .for(JAVASCRIPT_MODULE_TYPE_DYNAMIC)
- .tap(PLUGIN_NAME, handler);
- }
- );
- }
- }
- module.exports = AMDPlugin;
|