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-27 01:29:24 +08:00
|
|
|
const { RawSource } = 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");
|
|
|
|
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 */
|
|
|
|
/** @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"]);
|
|
|
|
const RUNTIME_REQUIREMENTS = new Set([
|
|
|
|
RuntimeGlobals.definePropertyGetters,
|
2020-02-27 01:29:24 +08:00
|
|
|
RuntimeGlobals.exports
|
2020-02-26 07:30:34 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
module.exports = class ContainerEntryModule extends Module {
|
2020-02-27 01:29:24 +08:00
|
|
|
/**
|
|
|
|
* @param {[string, string][]} exposes list of exposed modules
|
|
|
|
*/
|
|
|
|
constructor(exposes) {
|
2020-02-26 07:30:34 +08:00
|
|
|
super("javascript/dynamic", null);
|
2020-02-27 01:29:24 +08:00
|
|
|
this.exposes = exposes;
|
2020-02-26 07:30:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Set<string>} types availiable (do not mutate)
|
|
|
|
*/
|
|
|
|
getSourceTypes() {
|
|
|
|
return SOURCE_TYPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} a unique identifier of the module
|
|
|
|
*/
|
|
|
|
identifier() {
|
|
|
|
return `container entry ${JSON.stringify(this.exposes)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {RequestShortener} requestShortener the request shortener
|
|
|
|
* @returns {string} a user readable identifier of the module
|
|
|
|
*/
|
|
|
|
readableIdentifier(requestShortener) {
|
|
|
|
return `container entry`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-02-27 01:29:24 +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();
|
|
|
|
const runtimeRequirements = RUNTIME_REQUIREMENTS;
|
|
|
|
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,
|
|
|
|
message: request,
|
|
|
|
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(
|
|
|
|
`${Template.toNormalComment(
|
|
|
|
`[${name}] => ${request}`
|
2020-02-27 01:29:24 +08:00
|
|
|
)} "${name}": ${runtimeTemplate.basicFunction("", str)}`
|
2020-02-26 07:30:34 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sources.set(
|
|
|
|
"javascript",
|
2020-02-27 01:29:24 +08:00
|
|
|
new RawSource(
|
|
|
|
Template.asString([
|
|
|
|
`var __MODULE_MAP__ = {`,
|
|
|
|
Template.indent(getters.join(",")),
|
|
|
|
"};",
|
|
|
|
`var __GET_MODULE__ = ${runtimeTemplate.basicFunction(
|
|
|
|
["module"],
|
|
|
|
`return typeof __MODULE_MAP__[module] ==='function' ? __MODULE_MAP__[module].apply(null) : Promise.reject(new Error('Module ' + module + ' does not exist.'))`
|
|
|
|
)};`,
|
|
|
|
"",
|
|
|
|
`${RuntimeGlobals.definePropertyGetters}(exports, {`,
|
|
|
|
Template.indent([
|
|
|
|
`get: ${runtimeTemplate.returningFunction("__GET_MODULE__")}`
|
|
|
|
]),
|
|
|
|
"});"
|
|
|
|
])
|
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;
|
|
|
|
}
|
|
|
|
};
|