UNPKG

3.86 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const InitFragment = require("../InitFragment");
10const RuntimeGlobals = require("../RuntimeGlobals");
11const makeSerializable = require("../util/makeSerializable");
12const NullDependency = require("./NullDependency");
13
14/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
15/** @typedef {import("../ChunkGraph")} ChunkGraph */
16/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
17/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
18/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
19/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
20/** @typedef {import("../ModuleGraph")} ModuleGraph */
21/** @typedef {import("../util/Hash")} Hash */
22/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
23
24class ModuleDecoratorDependency extends NullDependency {
25 /**
26 * @param {string} decorator the decorator requirement
27 * @param {boolean} allowExportsAccess allow to access exports from module
28 */
29 constructor(decorator, allowExportsAccess) {
30 super();
31 this.decorator = decorator;
32 this.allowExportsAccess = allowExportsAccess;
33 this._hashUpdate = undefined;
34 }
35
36 /**
37 * @returns {string} a display name for the type of dependency
38 */
39 get type() {
40 return "module decorator";
41 }
42
43 get category() {
44 return "self";
45 }
46
47 /**
48 * @returns {string | null} an identifier to merge equal requests
49 */
50 getResourceIdentifier() {
51 return `self`;
52 }
53
54 /**
55 * Returns list of exports referenced by this dependency
56 * @param {ModuleGraph} moduleGraph module graph
57 * @param {RuntimeSpec} runtime the runtime for which the module is analysed
58 * @returns {(string[] | ReferencedExport)[]} referenced exports
59 */
60 getReferencedExports(moduleGraph, runtime) {
61 return this.allowExportsAccess
62 ? Dependency.EXPORTS_OBJECT_REFERENCED
63 : Dependency.NO_EXPORTS_REFERENCED;
64 }
65
66 /**
67 * Update the hash
68 * @param {Hash} hash hash to be updated
69 * @param {UpdateHashContext} context context
70 * @returns {void}
71 */
72 updateHash(hash, context) {
73 if (this._hashUpdate === undefined) {
74 this._hashUpdate = `${this.decorator}${this.allowExportsAccess}`;
75 }
76 hash.update(this._hashUpdate);
77 }
78
79 serialize(context) {
80 const { write } = context;
81 write(this.decorator);
82 write(this.allowExportsAccess);
83 super.serialize(context);
84 }
85
86 deserialize(context) {
87 const { read } = context;
88 this.decorator = read();
89 this.allowExportsAccess = read();
90 super.deserialize(context);
91 }
92}
93
94makeSerializable(
95 ModuleDecoratorDependency,
96 "webpack/lib/dependencies/ModuleDecoratorDependency"
97);
98
99ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends (
100 NullDependency.Template
101) {
102 /**
103 * @param {Dependency} dependency the dependency for which the template should be applied
104 * @param {ReplaceSource} source the current replace source which can be modified
105 * @param {DependencyTemplateContext} templateContext the context object
106 * @returns {void}
107 */
108 apply(
109 dependency,
110 source,
111 { module, chunkGraph, initFragments, runtimeRequirements }
112 ) {
113 const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
114 runtimeRequirements.add(RuntimeGlobals.moduleLoaded);
115 runtimeRequirements.add(RuntimeGlobals.moduleId);
116 runtimeRequirements.add(RuntimeGlobals.module);
117 runtimeRequirements.add(dep.decorator);
118 initFragments.push(
119 new InitFragment(
120 `/* module decorator */ ${module.moduleArgument} = ${dep.decorator}(${module.moduleArgument});\n`,
121 InitFragment.STAGE_PROVIDES,
122 0,
123 `module decorator ${chunkGraph.getModuleId(module)}`
124 )
125 );
126 }
127};
128
129module.exports = ModuleDecoratorDependency;