webpack/lib/ExternalModule.js

237 lines
6.7 KiB
JavaScript
Raw Normal View History

2014-03-05 16:58:51 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
2017-02-18 20:17:16 +08:00
"use strict";
const { OriginalSource, RawSource } = require("webpack-sources");
2017-02-18 20:17:16 +08:00
const Module = require("./Module");
const Template = require("./Template");
2014-03-05 16:58:51 +08:00
2018-07-30 23:08:51 +08:00
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
2018-07-20 22:24:35 +08:00
/** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
/** @typedef {import("./Module").SourceContext} SourceContext */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./util/createHash").Hash} Hash */
/**
* @param {string|string[]} variableName the variable name or path
* @param {string} type the module system
* @returns {string} the generated source
*/
const getSourceForGlobalVariableExternal = (variableName, type) => {
if (!Array.isArray(variableName)) {
// make it an array as the look up works the same basically
variableName = [variableName];
}
// needed for e.g. window["some"]["thing"]
const objectLookup = variableName.map(r => `[${JSON.stringify(r)}]`).join("");
return `(function() { module.exports = ${type}${objectLookup}; }());`;
};
/**
* @param {string|string[]} moduleAndSpecifiers the module request
* @returns {string} the generated source
*/
const getSourceForCommonJsExternal = moduleAndSpecifiers => {
if (!Array.isArray(moduleAndSpecifiers)) {
return `module.exports = require(${JSON.stringify(moduleAndSpecifiers)});`;
}
const moduleName = moduleAndSpecifiers[0];
const objectLookup = moduleAndSpecifiers
.slice(1)
.map(r => `[${JSON.stringify(r)}]`)
.join("");
return `module.exports = require(${moduleName})${objectLookup};`;
};
/**
* @param {string} variableName the variable name to check
* @param {string} request the request path
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @returns {string} the generated source
*/
const checkExternalVariable = (variableName, request, runtimeTemplate) => {
return `if(typeof ${variableName} === 'undefined') { ${runtimeTemplate.throwMissingModuleErrorBlock(
{ request }
)} }\n`;
};
/**
* @param {string|number} id the module id
* @param {boolean} optional true, if the module is optional
* @param {string} request the request path
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @returns {string} the generated source
*/
const getSourceForAmdOrUmdExternal = (
id,
optional,
request,
runtimeTemplate
) => {
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
`${id}`
)}__`;
const missingModuleError = optional
? checkExternalVariable(externalVariable, request, runtimeTemplate)
: "";
return `${missingModuleError}module.exports = ${externalVariable};`;
};
/**
* @param {boolean} optional true, if the module is optional
* @param {string} request the request path
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @returns {string} the generated source
*/
const getSourceForDefaultCase = (optional, request, runtimeTemplate) => {
const missingModuleError = optional
? checkExternalVariable(request, request, runtimeTemplate)
: "";
return `${missingModuleError}module.exports = ${request};`;
};
2017-02-18 20:17:16 +08:00
class ExternalModule extends Module {
2017-06-09 04:22:58 +08:00
constructor(request, type, userRequest) {
2018-01-31 04:40:44 +08:00
super("javascript/dynamic", null);
// Info from Factory
2018-07-20 22:24:35 +08:00
/** @type {string | string[] | Record<string, string | string[]>} */
2017-02-18 20:17:16 +08:00
this.request = request;
/** @type {string} */
this.externalType = type;
/** @type {string} */
this.userRequest = userRequest;
/** @type {boolean} */
2017-02-18 20:17:16 +08:00
this.external = true;
}
2014-03-05 16:58:51 +08:00
/**
2018-07-20 22:24:35 +08:00
* @param {LibIdentOptions} options options
* @returns {string | null} an identifier for library inclusion
*/
2018-07-20 22:24:35 +08:00
libIdent(options) {
2017-06-09 04:22:58 +08:00
return this.userRequest;
}
/**
* @param {Chunk} chunk the chunk which condition should be checked
* @returns {boolean} true, if the chunk is ok for the module
*/
2017-02-19 08:55:07 +08:00
chunkCondition(chunk) {
return chunk.hasEntryModule();
}
/**
* @returns {string} a unique identifier of the module
*/
2017-02-18 20:17:16 +08:00
identifier() {
return "external " + JSON.stringify(this.request);
}
2014-03-05 16:58:51 +08:00
/**
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
2017-02-18 20:17:16 +08:00
return "external " + JSON.stringify(this.request);
}
2014-03-05 16:58:51 +08:00
2018-07-25 18:38:34 +08:00
/**
* @param {TODO} fileTimestamps timestamps of files
* @param {TODO} contextTimestamps timestamps of directories
* @returns {boolean} true, if the module needs a rebuild
*/
needRebuild(fileTimestamps, contextTimestamps) {
2017-02-18 20:17:16 +08:00
return false;
}
2014-03-05 16:58:51 +08:00
/**
* @param {TODO} options TODO
* @param {Compilation} compilation the compilation
* @param {TODO} resolver TODO
* @param {TODO} fs the file system
* @param {function(Error=): void} callback callback function
* @returns {void}
*/
2017-02-18 20:17:16 +08:00
build(options, compilation, resolver, fs, callback) {
this.built = true;
this.buildMeta = {};
this.buildInfo = {};
2017-02-18 20:17:16 +08:00
callback();
}
2014-03-05 16:58:51 +08:00
getSourceString(runtimeTemplate) {
2018-02-25 09:00:20 +08:00
const request =
typeof this.request === "object"
? this.request[this.externalType]
: this.request;
switch (this.externalType) {
2017-02-18 20:17:16 +08:00
case "this":
case "window":
case "self":
return getSourceForGlobalVariableExternal(request, this.externalType);
case "global":
return getSourceForGlobalVariableExternal(
runtimeTemplate.outputOptions.globalObject,
2018-02-25 09:00:20 +08:00
this.externalType
);
2017-02-18 20:17:16 +08:00
case "commonjs":
case "commonjs2":
return getSourceForCommonJsExternal(request);
2017-02-18 20:17:16 +08:00
case "amd":
case "umd":
case "umd2":
return getSourceForAmdOrUmdExternal(
2018-02-25 09:00:20 +08:00
this.id,
this.optional,
request,
runtimeTemplate
2018-02-25 09:00:20 +08:00
);
2017-02-18 20:17:16 +08:00
default:
return getSourceForDefaultCase(this.optional, request, runtimeTemplate);
2017-02-18 20:17:16 +08:00
}
}
/**
* @param {SourceContext} sourceContext source context
* @returns {Source} generated source
*/
source({ runtimeTemplate }) {
const sourceString = this.getSourceString(runtimeTemplate);
if (this.useSourceMap) {
return new OriginalSource(sourceString, this.identifier());
} else {
return new RawSource(sourceString);
}
2014-03-05 16:58:51 +08:00
}
2017-02-18 20:17:16 +08:00
/**
* @returns {number} the estimated size of the module
*/
2017-02-18 20:17:16 +08:00
size() {
return 42;
2014-03-05 16:58:51 +08:00
}
/**
* @param {Hash} hash the hash used to track dependencies
2018-07-17 22:42:05 +08:00
* @param {Compilation} compilation the compilation
* @returns {void}
*/
2018-07-17 22:42:05 +08:00
updateHash(hash, compilation) {
hash.update(this.externalType);
hash.update(JSON.stringify(this.request));
hash.update(JSON.stringify(Boolean(this.optional)));
2018-07-17 22:42:05 +08:00
super.updateHash(hash, compilation);
}
2017-02-18 20:17:16 +08:00
}
2014-03-05 16:58:51 +08:00
2017-02-18 20:17:16 +08:00
module.exports = ExternalModule;