Skip to content

Commit 289c19b

Browse files
authored
Merge pull request webpack#5784 from NMinhNguyen/bugfix/only-use-umd-externals-for-umd-library-target
Only use UMD or UMD2 externals for UMD libraryTarget. Resolve webpack#5766
2 parents 1b6c738 + 2bedd6c commit 289c19b

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

lib/UmdMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UmdMainTemplatePlugin {
4242
apply(compilation) {
4343
const mainTemplate = compilation.mainTemplate;
4444
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
45-
let externals = chunk.getModules().filter(m => m.external);
45+
let externals = chunk.getModules().filter(m => m.external && (m.type === "umd" || m.type === "umd2"));
4646
const optionalExternals = [];
4747
let requiredExternals = [];
4848
if(this.optionalAmdExternalAsGlobal) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require("should");
2+
var fs = require("fs");
3+
var path = require("path");
4+
5+
it("should correctly import a UMD external", function() {
6+
var external = require("external0");
7+
external.should.be.eql("module 0");
8+
});
9+
10+
it("should contain `require()` statements for the UMD external", function() {
11+
var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
12+
source.should.containEql("require(\"external0\")");
13+
});
14+
15+
it("should correctly import a non-UMD external", function() {
16+
var external = require("external1");
17+
external.should.be.eql("abc");
18+
});
19+
20+
it("should not contain `require()` statements for the non-UMD external", function() {
21+
var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
22+
source.should.not.containEql("require(\"'abc'\")");
23+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
modules: {
3+
external0: "module 0"
4+
}
5+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
output: {
3+
libraryTarget: "umd"
4+
},
5+
externals: {
6+
external0: "external0",
7+
external1: "var 'abc'"
8+
},
9+
node: {
10+
__dirname: false,
11+
__filename: false
12+
}
13+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require("should");
2+
var fs = require("fs");
3+
var path = require("path");
4+
5+
it("should correctly import a UMD2 external", function() {
6+
var external = require("external0");
7+
external.should.be.eql("module 0");
8+
});
9+
10+
it("should contain `require()` statements for the UMD2 external", function() {
11+
var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
12+
source.should.containEql("require(\"external0\")");
13+
});
14+
15+
it("should correctly import a non-UMD2 external", function() {
16+
var external = require("external1");
17+
external.should.be.eql("abc");
18+
});
19+
20+
it("should not contain `require()` statements for the non-UMD2 external", function() {
21+
var source = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
22+
source.should.not.containEql("require(\"'abc'\")");
23+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
modules: {
3+
external0: "module 0"
4+
}
5+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
output: {
3+
libraryTarget: "umd2"
4+
},
5+
externals: {
6+
external0: "external0",
7+
external1: "var 'abc'"
8+
},
9+
node: {
10+
__dirname: false,
11+
__filename: false
12+
}
13+
};

0 commit comments

Comments
 (0)