Skip to content

Commit

Permalink
Merge pull request #8043 from RubenVerborgh/externals-object-array
Browse files Browse the repository at this point in the history
Allow array as value in externals object
  • Loading branch information
sokra committed Sep 18, 2018
2 parents 7b91fa6 + 9bda629 commit ab28497
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 312 deletions.
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"
})
]
}
];

0 comments on commit ab28497

Please sign in to comment.