Skip to content

Commit 9a1034f

Browse files
author
Chris Joel
committed
Fixes webpack#5843
1 parent 168f923 commit 9a1034f

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/UmdMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class UmdMainTemplatePlugin {
171171
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
172172
" }\n"
173173
) +
174-
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
174+
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
175175
});
176176
mainTemplate.plugin("global-hash-paths", (paths) => {
177177
if(this.names.root) paths = paths.concat(this.names.root);

test/UmdMainTemplatePlugin.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* global describe, beforeEach, it */
2+
"use strict";
3+
4+
const should = require("should");
5+
const TemplatePluginEnvironment = require("./helpers/TemplatePluginEnvironment");
6+
const ConcatSource = require("webpack-sources").ConcatSource;
7+
const UmdMainTemplatePlugin = require("../lib/UmdMainTemplatePlugin");
8+
9+
describe("UmdMainTemplatePlugin", () => {
10+
11+
const setupBasicTemplatePlugin = name => {
12+
const plugin = new UmdMainTemplatePlugin({ amd: name }, { auxiliaryComment: {} });
13+
const templatePlugin = new TemplatePluginEnvironment();
14+
const environment = templatePlugin.getEnvironmentStub();
15+
environment.mainTemplate.applyPluginsWaterfall = () => [];
16+
plugin.apply(environment);
17+
return templatePlugin;
18+
};
19+
20+
let templatePlugin;
21+
22+
beforeEach(() => {
23+
templatePlugin = setupBasicTemplatePlugin("foo");
24+
});
25+
26+
describe("when applied", () => {
27+
describe("event handlers", () => {
28+
let eventBindings;
29+
30+
beforeEach(() => {
31+
eventBindings = templatePlugin.getEventBindings();
32+
});
33+
34+
describe("handling render-with-entry", () => {
35+
let eventHandler;
36+
37+
beforeEach(() => {
38+
eventHandler = eventBindings
39+
.filter(eventBinding => eventBinding.name === 'render-with-entry')
40+
.map(eventBinding => eventBinding.handler)
41+
.pop();
42+
});
43+
44+
it("creates source that safely detects the global object", () => {
45+
const source = eventHandler("{ foo: true }", {
46+
getModules: () => []
47+
}, "bar");
48+
49+
source.should.be.instanceof(ConcatSource);
50+
source.source().should.be.exactly(`(function webpackUniversalModuleDefinition(root, factory) {
51+
if(typeof exports === 'object' && typeof module === 'object')
52+
module.exports = factory();
53+
else if(typeof define === 'function' && define.amd)
54+
define([], factory);
55+
else {
56+
var a = factory();
57+
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
58+
}
59+
})(typeof self !== 'undefined' ? self : this, function() {
60+
return { foo: true };
61+
})`);
62+
});
63+
});
64+
});
65+
});
66+
});

0 commit comments

Comments
 (0)