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("../util/Hash")} Hash */
|
---|
22 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
23 |
|
---|
24 | class 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 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @returns {string} a display name for the type of dependency
|
---|
37 | */
|
---|
38 | get type() {
|
---|
39 | return "module decorator";
|
---|
40 | }
|
---|
41 |
|
---|
42 | get category() {
|
---|
43 | return "self";
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @returns {string | null} an identifier to merge equal requests
|
---|
48 | */
|
---|
49 | getResourceIdentifier() {
|
---|
50 | return `self`;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Returns list of exports referenced by this dependency
|
---|
55 | * @param {ModuleGraph} moduleGraph module graph
|
---|
56 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
57 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
58 | */
|
---|
59 | getReferencedExports(moduleGraph, runtime) {
|
---|
60 | return this.allowExportsAccess
|
---|
61 | ? Dependency.EXPORTS_OBJECT_REFERENCED
|
---|
62 | : Dependency.NO_EXPORTS_REFERENCED;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Update the hash
|
---|
67 | * @param {Hash} hash hash to be updated
|
---|
68 | * @param {UpdateHashContext} context context
|
---|
69 | * @returns {void}
|
---|
70 | */
|
---|
71 | updateHash(hash, context) {
|
---|
72 | hash.update(this.decorator);
|
---|
73 | hash.update(`${this.allowExportsAccess}`);
|
---|
74 | }
|
---|
75 |
|
---|
76 | serialize(context) {
|
---|
77 | const { write } = context;
|
---|
78 | write(this.decorator);
|
---|
79 | write(this.allowExportsAccess);
|
---|
80 | super.serialize(context);
|
---|
81 | }
|
---|
82 |
|
---|
83 | deserialize(context) {
|
---|
84 | const { read } = context;
|
---|
85 | this.decorator = read();
|
---|
86 | this.allowExportsAccess = read();
|
---|
87 | super.deserialize(context);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | makeSerializable(
|
---|
92 | ModuleDecoratorDependency,
|
---|
93 | "webpack/lib/dependencies/ModuleDecoratorDependency"
|
---|
94 | );
|
---|
95 |
|
---|
96 | ModuleDecoratorDependency.Template = class ModuleDecoratorDependencyTemplate extends (
|
---|
97 | NullDependency.Template
|
---|
98 | ) {
|
---|
99 | /**
|
---|
100 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
101 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
102 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
103 | * @returns {void}
|
---|
104 | */
|
---|
105 | apply(
|
---|
106 | dependency,
|
---|
107 | source,
|
---|
108 | { module, chunkGraph, initFragments, runtimeRequirements }
|
---|
109 | ) {
|
---|
110 | const dep = /** @type {ModuleDecoratorDependency} */ (dependency);
|
---|
111 | runtimeRequirements.add(RuntimeGlobals.moduleLoaded);
|
---|
112 | runtimeRequirements.add(RuntimeGlobals.moduleId);
|
---|
113 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
114 | runtimeRequirements.add(dep.decorator);
|
---|
115 | initFragments.push(
|
---|
116 | new InitFragment(
|
---|
117 | `/* module decorator */ ${module.moduleArgument} = ${dep.decorator}(${module.moduleArgument});\n`,
|
---|
118 | InitFragment.STAGE_PROVIDES,
|
---|
119 | 0,
|
---|
120 | `module decorator ${chunkGraph.getModuleId(module)}`
|
---|
121 | )
|
---|
122 | );
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | module.exports = ModuleDecoratorDependency;
|
---|