2020-02-26 07:30:34 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2020-02-29 00:16:44 +08:00
|
|
|
const { OriginalSource } = require("webpack-sources");
|
2020-02-26 07:30:34 +08:00
|
|
|
const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
|
|
|
const Module = require("../Module");
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const Template = require("../Template");
|
2020-04-17 08:16:09 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2020-02-26 07:30:34 +08:00
|
|
|
const ContainerExposedDependency = require("./ContainerExposedDependency");
|
|
|
|
|
|
|
|
/** @typedef {import("../../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
|
|
|
/** @typedef {import("../ChunkGroup")} ChunkGroup */
|
|
|
|
/** @typedef {import("../Compilation")} Compilation */
|
|
|
|
/** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|
|
|
/** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
2020-05-05 16:43:18 +08:00
|
|
|
/** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
2020-02-26 07:30:34 +08:00
|
|
|
/** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|
|
|
/** @typedef {import("../RequestShortener")} RequestShortener */
|
|
|
|
/** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|
|
|
/** @typedef {import("../WebpackError")} WebpackError */
|
|
|
|
/** @typedef {import("../util/Hash")} Hash */
|
|
|
|
/** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|
|
|
/** @typedef {import("./ContainerEntryDependency")} ContainerEntryDependency */
|
|
|
|
|
|
|
|
const SOURCE_TYPES = new Set(["javascript"]);
|
|
|
|
|
2020-04-17 08:16:09 +08:00
|
|
|
class ContainerEntryModule extends Module {
|
2020-02-27 01:29:24 +08:00
|
|
|
/**
|
2020-05-05 16:43:18 +08:00
|
|
|
* @param {string} name container entry name
|
2020-02-27 01:29:24 +08:00
|
|
|
* @param {[string, string][]} exposes list of exposed modules
|
|
|
|
*/
|
2020-05-05 16:43:18 +08:00
|
|
|
constructor(name, exposes) {
|
2020-02-26 07:30:34 +08:00
|
|
|
super("javascript/dynamic", null);
|
2020-05-05 16:43:18 +08:00
|
|
|
this._name = name;
|
|
|
|
this._exposes = exposes;
|
2020-02-26 07:30:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-18 17:01:20 +08:00
|
|
|
* @returns {Set<string>} types available (do not mutate)
|
2020-02-26 07:30:34 +08:00
|
|
|
*/
|
|
|
|
getSourceTypes() {
|
|
|
|
return SOURCE_TYPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} a unique identifier of the module
|
|
|
|
*/
|
|
|
|
identifier() {
|
2020-05-05 16:43:18 +08:00
|
|
|
return `container entry ${JSON.stringify(this._exposes)}`;
|
2020-02-26 07:30:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {RequestShortener} requestShortener the request shortener
|
|
|
|
* @returns {string} a user readable identifier of the module
|
|
|
|
*/
|
|
|
|
readableIdentifier(requestShortener) {
|
|
|
|
return `container entry`;
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:43:18 +08:00
|
|
|
/**
|
|
|
|
* @param {LibIdentOptions} options options
|
|
|
|
* @returns {string | null} an identifier for library inclusion
|
|
|
|
*/
|
|
|
|
libIdent(options) {
|
|
|
|
return `webpack/container/entry/${this._name}`;
|
|
|
|
}
|
|
|
|
|
2020-02-26 07:30:34 +08:00
|
|
|
/**
|
|
|
|
* @param {NeedBuildContext} context context info
|
|
|
|
* @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
needBuild(context, callback) {
|
|
|
|
return callback(null, !this.buildMeta);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WebpackOptions} options webpack options
|
|
|
|
* @param {Compilation} compilation the compilation
|
|
|
|
* @param {ResolverWithOptions} resolver the resolver
|
|
|
|
* @param {InputFileSystem} fs the file system
|
|
|
|
* @param {function(WebpackError=): void} callback callback function
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
build(options, compilation, resolver, fs, callback) {
|
|
|
|
this.buildMeta = {};
|
|
|
|
this.buildInfo = {
|
|
|
|
strict: true
|
|
|
|
};
|
|
|
|
|
|
|
|
this.clearDependenciesAndBlocks();
|
|
|
|
|
2020-02-27 01:29:24 +08:00
|
|
|
const dependencies = [];
|
|
|
|
|
2020-02-26 07:30:34 +08:00
|
|
|
let idx = -1;
|
2020-05-05 16:43:18 +08:00
|
|
|
for (const [name, request] of this._exposes) {
|
2020-02-26 07:30:34 +08:00
|
|
|
++idx;
|
|
|
|
const dep = new ContainerExposedDependency(name, request);
|
|
|
|
dep.loc = {
|
|
|
|
name,
|
|
|
|
index: idx
|
|
|
|
};
|
|
|
|
|
2020-02-27 01:29:24 +08:00
|
|
|
dependencies.push(dep);
|
2020-02-26 07:30:34 +08:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:29:24 +08:00
|
|
|
for (const dep of dependencies) {
|
|
|
|
const block = new AsyncDependenciesBlock(undefined, dep.loc, dep.request);
|
2020-02-26 07:30:34 +08:00
|
|
|
block.addDependency(dep);
|
|
|
|
this.addBlock(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {CodeGenerationContext} context context for code generation
|
|
|
|
* @returns {CodeGenerationResult} result
|
|
|
|
*/
|
|
|
|
codeGeneration({ moduleGraph, chunkGraph, runtimeTemplate }) {
|
|
|
|
const sources = new Map();
|
2020-02-27 05:32:48 +08:00
|
|
|
const runtimeRequirements = new Set([
|
|
|
|
RuntimeGlobals.definePropertyGetters,
|
2020-02-29 00:12:36 +08:00
|
|
|
RuntimeGlobals.hasOwnProperty,
|
2020-02-27 05:32:48 +08:00
|
|
|
RuntimeGlobals.exports
|
|
|
|
]);
|
2020-02-26 07:30:34 +08:00
|
|
|
const getters = [];
|
|
|
|
|
|
|
|
for (const block of this.blocks) {
|
|
|
|
const {
|
2020-02-27 01:29:24 +08:00
|
|
|
dependencies: [dependency]
|
2020-02-26 07:30:34 +08:00
|
|
|
} = block;
|
2020-02-27 01:29:24 +08:00
|
|
|
const dep = /** @type {ContainerExposedDependency} */ (dependency);
|
2020-02-26 07:30:34 +08:00
|
|
|
const name = dep.exposedName;
|
|
|
|
const mod = moduleGraph.getModule(dep);
|
|
|
|
const request = dep.userRequest;
|
|
|
|
|
|
|
|
let str;
|
|
|
|
|
|
|
|
if (!mod) {
|
|
|
|
str = runtimeTemplate.throwMissingModuleErrorBlock({
|
|
|
|
request: dep.userRequest
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
str = `return ${runtimeTemplate.blockPromise({
|
|
|
|
block,
|
2020-02-29 00:12:36 +08:00
|
|
|
message: "",
|
2020-02-26 07:30:34 +08:00
|
|
|
chunkGraph,
|
|
|
|
runtimeRequirements
|
2020-02-27 01:29:24 +08:00
|
|
|
})}.then(${runtimeTemplate.returningFunction(
|
|
|
|
runtimeTemplate.returningFunction(
|
|
|
|
runtimeTemplate.moduleRaw({
|
|
|
|
module: mod,
|
|
|
|
chunkGraph,
|
|
|
|
request,
|
|
|
|
weak: false,
|
|
|
|
runtimeRequirements
|
|
|
|
})
|
|
|
|
)
|
2020-02-26 07:30:34 +08:00
|
|
|
)});`;
|
|
|
|
}
|
|
|
|
|
|
|
|
getters.push(
|
2020-02-29 00:12:36 +08:00
|
|
|
`${JSON.stringify(name)}: ${runtimeTemplate.basicFunction("", str)}`
|
2020-02-26 07:30:34 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-29 00:16:44 +08:00
|
|
|
const source = Template.asString([
|
|
|
|
`var moduleMap = {`,
|
|
|
|
Template.indent(getters.join(",\n")),
|
|
|
|
"};",
|
|
|
|
`var get = ${runtimeTemplate.basicFunction("module", [
|
|
|
|
"return (",
|
|
|
|
Template.indent([
|
|
|
|
`${RuntimeGlobals.hasOwnProperty}(moduleMap, module)`,
|
|
|
|
Template.indent([
|
|
|
|
"? moduleMap[module]()",
|
|
|
|
`: Promise.resolve().then(${runtimeTemplate.basicFunction(
|
|
|
|
"",
|
2020-05-05 20:45:18 +08:00
|
|
|
"throw new Error('Module \"' + module + '\" does not exist in container.');"
|
2020-02-29 00:16:44 +08:00
|
|
|
)})`
|
|
|
|
])
|
|
|
|
]),
|
|
|
|
");"
|
|
|
|
])};`,
|
|
|
|
`var override = ${runtimeTemplate.basicFunction(
|
|
|
|
"override",
|
|
|
|
`Object.assign(${RuntimeGlobals.overrides}, override);`
|
2020-05-04 21:00:23 +08:00
|
|
|
)};`,
|
2020-02-29 00:16:44 +08:00
|
|
|
"",
|
|
|
|
"// This exports getters to disallow modifications",
|
|
|
|
`${RuntimeGlobals.definePropertyGetters}(exports, {`,
|
|
|
|
Template.indent([
|
|
|
|
`get: ${runtimeTemplate.returningFunction("get")},`,
|
|
|
|
`override: ${runtimeTemplate.returningFunction("override")}`
|
|
|
|
]),
|
|
|
|
"});"
|
|
|
|
]);
|
|
|
|
|
2020-02-26 07:30:34 +08:00
|
|
|
sources.set(
|
|
|
|
"javascript",
|
2020-02-29 00:16:44 +08:00
|
|
|
new OriginalSource(source, "webpack/container-entry")
|
2020-02-26 07:30:34 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
sources,
|
|
|
|
runtimeRequirements
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string=} type the source type for which the size should be estimated
|
|
|
|
* @returns {number} the estimated size of the module (must be non-zero)
|
|
|
|
*/
|
|
|
|
size(type) {
|
|
|
|
return 42;
|
|
|
|
}
|
2020-04-17 08:16:09 +08:00
|
|
|
|
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
2020-05-05 16:43:18 +08:00
|
|
|
write(this._exposes);
|
2020-04-17 08:16:09 +08:00
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
2020-05-05 16:43:18 +08:00
|
|
|
this._exposes = read();
|
2020-04-17 08:16:09 +08:00
|
|
|
super.deserialize(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
makeSerializable(
|
|
|
|
ContainerEntryModule,
|
|
|
|
"webpack/lib/container/ContainerEntryModule"
|
|
|
|
);
|
|
|
|
|
|
|
|
module.exports = ContainerEntryModule;
|