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