webpack/lib/ExternalModule.js

200 lines
5.2 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
*/
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 WebpackMissingModule = require("./dependencies/WebpackMissingModule");
const Template = require("./Template");
2014-03-05 16:58:51 +08:00
/** @typedef {import("./util/createHash").Hash} Hash */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("webpack-sources").Source} Source */
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
2017-02-18 20:17:16 +08:00
this.request = request;
this.externalType = type;
this.userRequest = userRequest;
2017-02-18 20:17:16 +08:00
this.external = true;
}
2014-03-05 16:58:51 +08:00
libIdent() {
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
2017-02-18 20:17:16 +08:00
needRebuild() {
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
2017-02-19 08:55:07 +08:00
getSourceForGlobalVariableExternal(variableName, type) {
2018-02-25 09:00:20 +08:00
if (!Array.isArray(variableName)) {
2017-02-19 08:55:07 +08:00
// make it an array as the look up works the same basically
variableName = [variableName];
}
// needed for e.g. window["some"]["thing"]
2018-02-25 09:00:20 +08:00
const objectLookup = variableName
.map(r => `[${JSON.stringify(r)}]`)
.join("");
return `(function() { module.exports = ${type}${objectLookup}; }());`;
}
2017-02-19 08:55:07 +08:00
getSourceForCommonJsExternal(moduleAndSpecifiers) {
2018-02-25 09:00:20 +08:00
if (!Array.isArray(moduleAndSpecifiers)) {
return `module.exports = require(${JSON.stringify(
moduleAndSpecifiers
)});`;
}
2017-02-19 08:55:07 +08:00
const moduleName = moduleAndSpecifiers[0];
2018-02-25 09:00:20 +08:00
const objectLookup = moduleAndSpecifiers
.slice(1)
.map(r => `[${JSON.stringify(r)}]`)
.join("");
return `module.exports = require(${moduleName})${objectLookup};`;
}
checkExternalVariable(variableToCheck, request) {
2018-03-26 22:56:10 +08:00
return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(
request
)}}\n`;
}
getSourceForAmdOrUmdExternal(id, optional, request) {
2018-02-25 09:00:20 +08:00
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(
`${id}`
)}__`;
const missingModuleError = optional
? this.checkExternalVariable(externalVariable, request)
: "";
return `${missingModuleError}module.exports = ${externalVariable};`;
}
getSourceForDefaultCase(optional, request) {
2018-02-25 09:00:20 +08:00
const missingModuleError = optional
? this.checkExternalVariable(request, request)
: "";
return `${missingModuleError}module.exports = ${request};`;
}
getSourceString(runtime) {
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":
2018-02-25 09:00:20 +08:00
return this.getSourceForGlobalVariableExternal(
request,
this.externalType
);
case "global":
2018-02-25 09:00:20 +08:00
return this.getSourceForGlobalVariableExternal(
runtime.outputOptions.globalObject,
this.externalType
);
2017-02-18 20:17:16 +08:00
case "commonjs":
case "commonjs2":
return this.getSourceForCommonJsExternal(request);
2017-02-18 20:17:16 +08:00
case "amd":
case "umd":
case "umd2":
2018-02-25 09:00:20 +08:00
return this.getSourceForAmdOrUmdExternal(
this.id,
this.optional,
request
);
2017-02-18 20:17:16 +08:00
default:
return this.getSourceForDefaultCase(this.optional, request);
2017-02-18 20:17:16 +08:00
}
}
getSource(sourceString) {
2018-02-25 09:00:20 +08:00
if (this.useSourceMap) {
return new OriginalSource(sourceString, this.identifier());
2017-02-18 20:17:16 +08:00
}
return new RawSource(sourceString);
}
/**
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string=} type the type of source that should be returned
* @returns {Source} generated source
*/
source(dependencyTemplates, runtimeTemplate, type) {
return this.getSource(this.getSourceString(runtimeTemplate));
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
* @returns {void}
*/
updateHash(hash) {
hash.update(this.externalType);
hash.update(JSON.stringify(this.request));
hash.update(JSON.stringify(Boolean(this.optional)));
super.updateHash(hash);
}
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;