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";
|
2018-03-22 19:05:58 +08:00
|
|
|
|
|
|
|
const { OriginalSource, RawSource } = require("webpack-sources");
|
2017-02-18 20:17:16 +08:00
|
|
|
const Module = require("./Module");
|
|
|
|
const WebpackMissingModule = require("./dependencies/WebpackMissingModule");
|
2017-02-20 16:17:51 +08:00
|
|
|
const Template = require("./Template");
|
2014-03-05 16:58:51 +08:00
|
|
|
|
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);
|
2017-11-06 20:02:35 +08:00
|
|
|
|
|
|
|
// Info from Factory
|
2017-02-18 20:17:16 +08:00
|
|
|
this.request = request;
|
2017-11-10 18:51:29 +08:00
|
|
|
this.externalType = type;
|
2017-11-06 20:02:35 +08:00
|
|
|
this.userRequest = userRequest;
|
2017-02-18 20:17:16 +08:00
|
|
|
this.external = true;
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-05-18 00:49:09 +08:00
|
|
|
libIdent() {
|
2017-06-09 04:22:58 +08:00
|
|
|
return this.userRequest;
|
2017-05-18 00:49:09 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 08:55:07 +08:00
|
|
|
chunkCondition(chunk) {
|
|
|
|
return chunk.hasEntryModule();
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
identifier() {
|
|
|
|
return "external " + JSON.stringify(this.request);
|
|
|
|
}
|
2014-03-05 16:58:51 +08:00
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
readableIdentifier() {
|
|
|
|
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
|
|
|
|
2017-02-18 20:17:16 +08:00
|
|
|
build(options, compilation, resolver, fs, callback) {
|
2017-11-06 20:02:35 +08:00
|
|
|
this.built = true;
|
2017-12-06 19:09:17 +08:00
|
|
|
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];
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// needed for e.g. window["some"]["thing"]
|
2018-02-25 09:00:20 +08:00
|
|
|
const objectLookup = variableName
|
|
|
|
.map(r => `[${JSON.stringify(r)}]`)
|
|
|
|
.join("");
|
2017-02-18 21:06:23 +08:00
|
|
|
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-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
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("");
|
2017-02-18 21:06:23 +08:00
|
|
|
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`;
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 09:11:08 +08:00
|
|
|
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)
|
|
|
|
: "";
|
2017-02-19 09:11:08 +08:00
|
|
|
return `${missingModuleError}module.exports = ${externalVariable};`;
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 09:11:08 +08:00
|
|
|
getSourceForDefaultCase(optional, request) {
|
2018-02-25 09:00:20 +08:00
|
|
|
const missingModuleError = optional
|
|
|
|
? this.checkExternalVariable(request, request)
|
|
|
|
: "";
|
2017-02-19 09:11:08 +08:00
|
|
|
return `${missingModuleError}module.exports = ${request};`;
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 01:46:37 +08:00
|
|
|
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":
|
2017-12-28 01:46:37 +08:00
|
|
|
case "self":
|
2018-02-25 09:00:20 +08:00
|
|
|
return this.getSourceForGlobalVariableExternal(
|
|
|
|
request,
|
|
|
|
this.externalType
|
|
|
|
);
|
2017-12-28 01:46:37 +08:00
|
|
|
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":
|
2017-02-18 21:06:23 +08:00
|
|
|
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:
|
2017-02-19 09:11:08 +08:00
|
|
|
return this.getSourceForDefaultCase(this.optional, request);
|
2017-02-18 20:17:16 +08:00
|
|
|
}
|
2017-02-18 21:06:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getSource(sourceString) {
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.useSourceMap) {
|
2017-02-18 21:06:23 +08:00
|
|
|
return new OriginalSource(sourceString, this.identifier());
|
2017-02-18 20:17:16 +08:00
|
|
|
}
|
2017-02-18 21:06:23 +08:00
|
|
|
|
|
|
|
return new RawSource(sourceString);
|
|
|
|
}
|
|
|
|
|
2017-12-28 01:46:37 +08:00
|
|
|
source(dependencyTemplates, runtime) {
|
2018-02-25 09:00:20 +08:00
|
|
|
return this.getSource(this.getSourceString(runtime));
|
2014-03-05 16:58:51 +08:00
|
|
|
}
|
2017-02-18 20:17:16 +08:00
|
|
|
|
|
|
|
size() {
|
|
|
|
return 42;
|
2014-03-05 16:58:51 +08:00
|
|
|
}
|
2017-09-18 00:18:40 +08:00
|
|
|
|
|
|
|
updateHash(hash) {
|
2017-11-10 18:51:29 +08:00
|
|
|
hash.update(this.externalType);
|
2017-09-18 00:18:40 +08:00
|
|
|
hash.update(JSON.stringify(this.request));
|
2017-10-12 02:53:12 +08:00
|
|
|
hash.update(JSON.stringify(Boolean(this.optional)));
|
2017-09-18 00:18:40 +08:00
|
|
|
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;
|