[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Florent Cailhol @ooflorent
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const InitFragment = require("../InitFragment");
|
---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 10 | const ModuleDependency = require("./ModuleDependency");
|
---|
| 11 |
|
---|
| 12 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 13 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
---|
| 15 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 16 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 17 | /** @typedef {import("../DependencyTemplates")} DependencyTemplates */
|
---|
| 18 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 19 | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
---|
| 20 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @param {string[]|null} path the property path array
|
---|
| 24 | * @returns {string} the converted path
|
---|
| 25 | */
|
---|
| 26 | const pathToString = path =>
|
---|
| 27 | path !== null && path.length > 0
|
---|
| 28 | ? path.map(part => `[${JSON.stringify(part)}]`).join("")
|
---|
| 29 | : "";
|
---|
| 30 |
|
---|
| 31 | class ProvidedDependency extends ModuleDependency {
|
---|
| 32 | constructor(request, identifier, path, range) {
|
---|
| 33 | super(request);
|
---|
| 34 | this.identifier = identifier;
|
---|
| 35 | this.path = path;
|
---|
| 36 | this.range = range;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | get type() {
|
---|
| 40 | return "provided";
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | get category() {
|
---|
| 44 | return "esm";
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Update the hash
|
---|
| 49 | * @param {Hash} hash hash to be updated
|
---|
| 50 | * @param {UpdateHashContext} context context
|
---|
| 51 | * @returns {void}
|
---|
| 52 | */
|
---|
| 53 | updateHash(hash, context) {
|
---|
| 54 | hash.update(this.identifier);
|
---|
| 55 | hash.update(this.path ? this.path.join(",") : "null");
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | serialize(context) {
|
---|
| 59 | const { write } = context;
|
---|
| 60 | write(this.identifier);
|
---|
| 61 | write(this.path);
|
---|
| 62 | super.serialize(context);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | deserialize(context) {
|
---|
| 66 | const { read } = context;
|
---|
| 67 | this.identifier = read();
|
---|
| 68 | this.path = read();
|
---|
| 69 | super.deserialize(context);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | makeSerializable(
|
---|
| 74 | ProvidedDependency,
|
---|
| 75 | "webpack/lib/dependencies/ProvidedDependency"
|
---|
| 76 | );
|
---|
| 77 |
|
---|
| 78 | class ProvidedDependencyTemplate extends ModuleDependency.Template {
|
---|
| 79 | /**
|
---|
| 80 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 81 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 82 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 83 | * @returns {void}
|
---|
| 84 | */
|
---|
| 85 | apply(
|
---|
| 86 | dependency,
|
---|
| 87 | source,
|
---|
| 88 | {
|
---|
| 89 | runtimeTemplate,
|
---|
| 90 | moduleGraph,
|
---|
| 91 | chunkGraph,
|
---|
| 92 | initFragments,
|
---|
| 93 | runtimeRequirements
|
---|
| 94 | }
|
---|
| 95 | ) {
|
---|
| 96 | const dep = /** @type {ProvidedDependency} */ (dependency);
|
---|
| 97 | initFragments.push(
|
---|
| 98 | new InitFragment(
|
---|
| 99 | `/* provided dependency */ var ${
|
---|
| 100 | dep.identifier
|
---|
| 101 | } = ${runtimeTemplate.moduleExports({
|
---|
| 102 | module: moduleGraph.getModule(dep),
|
---|
| 103 | chunkGraph,
|
---|
| 104 | request: dep.request,
|
---|
| 105 | runtimeRequirements
|
---|
| 106 | })}${pathToString(dep.path)};\n`,
|
---|
| 107 | InitFragment.STAGE_PROVIDES,
|
---|
| 108 | 1,
|
---|
| 109 | `provided ${dep.identifier}`
|
---|
| 110 | )
|
---|
| 111 | );
|
---|
| 112 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | ProvidedDependency.Template = ProvidedDependencyTemplate;
|
---|
| 117 |
|
---|
| 118 | module.exports = ProvidedDependency;
|
---|