webpack/lib/dependencies/ModuleDecoratorDependency.js

72 lines
2.1 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 NullDependency = require("./NullDependency");
/** @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("../util/createHash").Hash} Hash */
class ModuleDecoratorDependency extends NullDependency {
/**
* @param {string} decorator the decorator requirement
*/
constructor(decorator) {
super();
this.decorator = decorator;
}
/**
* @returns {string} a display name for the type of dependency
*/
get type() {
return "module decorator";
}
}
2018-10-09 20:30:59 +08:00
makeSerializable(
ModuleDecoratorDependency,
"webpack/lib/dependencies/ModuleDecoratorDependency"
);
ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends NullDependency.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,
{ module, chunkGraph, initFragments, runtimeRequirements }
) {
const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
2018-11-17 01:18:44 +08:00
runtimeRequirements.add(RuntimeGlobals.module);
runtimeRequirements.add(dep.decorator);
initFragments.push(
new InitFragment(
`/* module decorator */ ${RuntimeGlobals.module} = ${dep.decorator}(${
RuntimeGlobals.module
});\n`,
InitFragment.STAGE_PROVIDES,
0,
`module decorator ${chunkGraph.getModuleId(module)}`
)
);
}
};
module.exports = ModuleDecoratorDependency;