Skip to content

Commit

Permalink
Merge pull request #5849 from cdata/fix-5843
Browse files Browse the repository at this point in the history
Fixes #5843
  • Loading branch information
sokra committed Nov 28, 2017
2 parents 801a253 + e5d03cb commit 05c5479
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/UmdMainTemplatePlugin.js
Expand Up @@ -171,7 +171,7 @@ class UmdMainTemplatePlugin {
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
" }\n"
) +
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
});
mainTemplate.plugin("global-hash-paths", (paths) => {
if(this.names.root) paths = paths.concat(this.names.root);
Expand Down
70 changes: 70 additions & 0 deletions test/UmdMainTemplatePlugin.unittest.js
@@ -0,0 +1,70 @@
/* global describe, beforeEach, it */
"use strict";

const should = require("should");
const TemplatePluginEnvironment = require("./helpers/TemplatePluginEnvironment");
const ConcatSource = require("webpack-sources").ConcatSource;
const UmdMainTemplatePlugin = require("../lib/UmdMainTemplatePlugin");

describe("UmdMainTemplatePlugin", () => {

const setupBasicTemplatePlugin = name => {
const plugin = new UmdMainTemplatePlugin({
amd: name
}, {
auxiliaryComment: {}
});
const templatePlugin = new TemplatePluginEnvironment();
const environment = templatePlugin.getEnvironmentStub();
environment.mainTemplate.applyPluginsWaterfall = () => [];
plugin.apply(environment);
return templatePlugin;
};

let templatePlugin;

beforeEach(() => {
templatePlugin = setupBasicTemplatePlugin("foo");
});

describe("when applied", () => {
describe("event handlers", () => {
let eventBindings;

beforeEach(() => {
eventBindings = templatePlugin.getEventBindings();
});

describe("handling render-with-entry", () => {
let eventHandler;

beforeEach(() => {
eventHandler = eventBindings
.filter(eventBinding => eventBinding.name === 'render-with-entry')
.map(eventBinding => eventBinding.handler)
.pop();
});

it("creates source that safely detects the global object", () => {
const source = eventHandler("{ foo: true }", {
getModules: () => []
}, "bar");

source.should.be.instanceof(ConcatSource);
source.source().should.be.exactly(`(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(typeof self !== 'undefined' ? self : this, function() {
return { foo: true };
})`);
});
});
});
});
});

0 comments on commit 05c5479

Please sign in to comment.