webpack/lib/dependencies/ModuleDecoratorDependency.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const InitFragment = require("../InitFragment");
2018-11-17 01:18:44 +08:00
const RuntimeGlobals = require("../RuntimeGlobals");
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("../util/createHash").Hash} Hash */
class ModuleDecoratorDependency extends ModuleDependency {
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {ChunkGraph} chunkGraph chunk graph
* @returns {void}
*/
updateHash(hash, chunkGraph) {
super.updateHash(hash, chunkGraph);
hash.update("module decorator");
}
}
2018-10-09 20:30:59 +08:00
makeSerializable(
ModuleDecoratorDependency,
"webpack/lib/dependencies/ModuleDecoratorDependency"
);
ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends ModuleDependency.Template {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(
dependency,
source,
2018-11-17 01:18:44 +08:00
{
runtimeTemplate,
moduleGraph,
chunkGraph,
initFragments,
runtimeRequirements
}
) {
const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
const originModule = moduleGraph.getOrigin(dep);
2018-11-17 01:18:44 +08:00
runtimeRequirements.add(RuntimeGlobals.module);
initFragments.push(
new InitFragment(
`/* module decorator */ ${
originModule.moduleArgument
} = ${runtimeTemplate.moduleExports({
module: moduleGraph.getModule(dep),
chunkGraph,
2018-11-17 01:18:44 +08:00
request: dep.request,
runtimeRequirements
})}(${originModule.moduleArgument});\n`,
InitFragment.STAGE_PROVIDES,
0,
`module decorator ${chunkGraph.getModuleId(originModule)}`
)
);
}
};
module.exports = ModuleDecoratorDependency;