Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow array as value in externals object #8043

Merged
merged 3 commits into from Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/ExternalModule.js
Expand Up @@ -74,7 +74,9 @@ class ExternalModule extends Module {
.slice(1)
.map(r => `[${JSON.stringify(r)}]`)
.join("");
return `module.exports = require(${moduleName})${objectLookup};`;
return `module.exports = require(${JSON.stringify(
moduleName
)})${objectLookup};`;
}

checkExternalVariable(variableToCheck, request) {
Expand All @@ -94,15 +96,25 @@ class ExternalModule extends Module {
}

getSourceForDefaultCase(optional, request) {
if (!Array.isArray(request)) {
// make it an array as the look up works the same basically
request = [request];
}

const variableName = request[0];
const missingModuleError = optional
? this.checkExternalVariable(request, request)
? this.checkExternalVariable(variableName, request.join("."))
: "";
return `${missingModuleError}module.exports = ${request};`;
const objectLookup = request
.slice(1)
.map(r => `[${JSON.stringify(r)}]`)
.join("");
return `${missingModuleError}module.exports = ${variableName}${objectLookup};`;
}

getSourceString(runtime) {
const request =
typeof this.request === "object"
typeof this.request === "object" && !Array.isArray(this.request)
? this.request[this.externalType]
: this.request;
switch (this.externalType) {
Expand Down
3 changes: 3 additions & 0 deletions schemas/WebpackOptions.json
Expand Up @@ -123,6 +123,9 @@
{
"type": "object"
},
{
"$ref": "#/definitions/common.arrayOfStringValues"
},
{
"type": "boolean"
}
Expand Down
308 changes: 0 additions & 308 deletions test/ExternalModule.unittest.js

This file was deleted.

4 changes: 4 additions & 0 deletions test/configCases/externals/externals-array/index.js
@@ -0,0 +1,4 @@
it("should not fail on optional externals", function() {
const external = require("external");
expect(external).toBe(EXPECTED);
});
26 changes: 26 additions & 0 deletions test/configCases/externals/externals-array/webpack.config.js
@@ -0,0 +1,26 @@
const webpack = require("../../../../");
module.exports = [
{
output: {
libraryTarget: "commonjs2"
},
externals: {
external: ["webpack", "version"]
},
plugins: [
new webpack.DefinePlugin({
EXPECTED: JSON.stringify(webpack.version)
})
]
},
{
externals: {
external: ["Array", "isArray"]
},
plugins: [
new webpack.DefinePlugin({
EXPECTED: "Array.isArray"
})
]
}
];