[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra and Zackary Jackson @ScriptedAlchemy
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
---|
| 9 | const Module = require("../Module");
|
---|
| 10 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 11 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 12 | const ProvideForSharedDependency = require("./ProvideForSharedDependency");
|
---|
| 13 |
|
---|
| 14 | /** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 15 | /** @typedef {import("../Chunk")} Chunk */
|
---|
| 16 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 17 | /** @typedef {import("../ChunkGroup")} ChunkGroup */
|
---|
| 18 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 19 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
---|
| 20 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
---|
| 21 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
---|
| 22 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
---|
| 23 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
---|
| 24 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
| 25 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
| 26 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 27 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
---|
| 28 |
|
---|
| 29 | const TYPES = new Set(["share-init"]);
|
---|
| 30 |
|
---|
| 31 | class ProvideSharedModule extends Module {
|
---|
| 32 | /**
|
---|
| 33 | * @param {string} shareScope shared scope name
|
---|
| 34 | * @param {string} name shared key
|
---|
| 35 | * @param {string | false} version version
|
---|
| 36 | * @param {string} request request to the provided module
|
---|
| 37 | * @param {boolean} eager include the module in sync way
|
---|
| 38 | */
|
---|
| 39 | constructor(shareScope, name, version, request, eager) {
|
---|
| 40 | super("provide-module");
|
---|
| 41 | this._shareScope = shareScope;
|
---|
| 42 | this._name = name;
|
---|
| 43 | this._version = version;
|
---|
| 44 | this._request = request;
|
---|
| 45 | this._eager = eager;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @returns {string} a unique identifier of the module
|
---|
| 50 | */
|
---|
| 51 | identifier() {
|
---|
| 52 | return `provide module (${this._shareScope}) ${this._name}@${this._version} = ${this._request}`;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * @param {RequestShortener} requestShortener the request shortener
|
---|
| 57 | * @returns {string} a user readable identifier of the module
|
---|
| 58 | */
|
---|
| 59 | readableIdentifier(requestShortener) {
|
---|
| 60 | return `provide shared module (${this._shareScope}) ${this._name}@${
|
---|
| 61 | this._version
|
---|
| 62 | } = ${requestShortener.shorten(this._request)}`;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * @param {LibIdentOptions} options options
|
---|
| 67 | * @returns {string | null} an identifier for library inclusion
|
---|
| 68 | */
|
---|
| 69 | libIdent(options) {
|
---|
| 70 | return `webpack/sharing/provide/${this._shareScope}/${this._name}`;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * @param {NeedBuildContext} context context info
|
---|
| 75 | * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
| 76 | * @returns {void}
|
---|
| 77 | */
|
---|
| 78 | needBuild(context, callback) {
|
---|
| 79 | callback(null, !this.buildInfo);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * @param {WebpackOptions} options webpack options
|
---|
| 84 | * @param {Compilation} compilation the compilation
|
---|
| 85 | * @param {ResolverWithOptions} resolver the resolver
|
---|
| 86 | * @param {InputFileSystem} fs the file system
|
---|
| 87 | * @param {function(WebpackError=): void} callback callback function
|
---|
| 88 | * @returns {void}
|
---|
| 89 | */
|
---|
| 90 | build(options, compilation, resolver, fs, callback) {
|
---|
| 91 | this.buildMeta = {};
|
---|
| 92 | this.buildInfo = {
|
---|
| 93 | strict: true
|
---|
| 94 | };
|
---|
| 95 |
|
---|
| 96 | this.clearDependenciesAndBlocks();
|
---|
| 97 | const dep = new ProvideForSharedDependency(this._request);
|
---|
| 98 | if (this._eager) {
|
---|
| 99 | this.addDependency(dep);
|
---|
| 100 | } else {
|
---|
| 101 | const block = new AsyncDependenciesBlock({});
|
---|
| 102 | block.addDependency(dep);
|
---|
| 103 | this.addBlock(block);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | callback();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /**
|
---|
| 110 | * @param {string=} type the source type for which the size should be estimated
|
---|
| 111 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
| 112 | */
|
---|
| 113 | size(type) {
|
---|
| 114 | return 42;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * @returns {Set<string>} types available (do not mutate)
|
---|
| 119 | */
|
---|
| 120 | getSourceTypes() {
|
---|
| 121 | return TYPES;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * @param {CodeGenerationContext} context context for code generation
|
---|
| 126 | * @returns {CodeGenerationResult} result
|
---|
| 127 | */
|
---|
| 128 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
---|
| 129 | const runtimeRequirements = new Set([RuntimeGlobals.initializeSharing]);
|
---|
| 130 | const code = `register(${JSON.stringify(this._name)}, ${JSON.stringify(
|
---|
| 131 | this._version || "0"
|
---|
| 132 | )}, ${
|
---|
| 133 | this._eager
|
---|
| 134 | ? runtimeTemplate.syncModuleFactory({
|
---|
| 135 | dependency: this.dependencies[0],
|
---|
| 136 | chunkGraph,
|
---|
| 137 | request: this._request,
|
---|
| 138 | runtimeRequirements
|
---|
| 139 | })
|
---|
| 140 | : runtimeTemplate.asyncModuleFactory({
|
---|
| 141 | block: this.blocks[0],
|
---|
| 142 | chunkGraph,
|
---|
| 143 | request: this._request,
|
---|
| 144 | runtimeRequirements
|
---|
| 145 | })
|
---|
| 146 | }${this._eager ? ", 1" : ""});`;
|
---|
| 147 | const sources = new Map();
|
---|
| 148 | const data = new Map();
|
---|
| 149 | data.set("share-init", [
|
---|
| 150 | {
|
---|
| 151 | shareScope: this._shareScope,
|
---|
| 152 | initStage: 10,
|
---|
| 153 | init: code
|
---|
| 154 | }
|
---|
| 155 | ]);
|
---|
| 156 | return { sources, data, runtimeRequirements };
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | serialize(context) {
|
---|
| 160 | const { write } = context;
|
---|
| 161 | write(this._shareScope);
|
---|
| 162 | write(this._name);
|
---|
| 163 | write(this._version);
|
---|
| 164 | write(this._request);
|
---|
| 165 | write(this._eager);
|
---|
| 166 | super.serialize(context);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static deserialize(context) {
|
---|
| 170 | const { read } = context;
|
---|
| 171 | const obj = new ProvideSharedModule(read(), read(), read(), read(), read());
|
---|
| 172 | obj.deserialize(context);
|
---|
| 173 | return obj;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | makeSerializable(
|
---|
| 178 | ProvideSharedModule,
|
---|
| 179 | "webpack/lib/sharing/ProvideSharedModule"
|
---|
| 180 | );
|
---|
| 181 |
|
---|
| 182 | module.exports = ProvideSharedModule;
|
---|