From 5ff55bf7c718b03ab6468fee22c3af21f5a29264 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 21 Sep 2015 17:25:01 -0400 Subject: [PATCH] guard against undefined `this` in post-processed UMD bundle - fixes #109 --- src/finalisers/umd.js | 2 +- test/cli/module-name/_expected.js | 2 +- test/form/banner-and-footer/_expected/umd.js | 2 +- test/form/block-comments/_expected/umd.js | 2 +- test/form/dedupes-external-imports/_expected/umd.js | 2 +- test/form/exclude-unnecessary-modifications/_expected/umd.js | 2 +- test/form/export-all-from-internal/_expected/umd.js | 2 +- test/form/export-default/_expected/umd.js | 2 +- test/form/exports-at-end-if-possible/_expected/umd.js | 2 +- test/form/external-imports-custom-names/_expected/umd.js | 2 +- test/form/external-imports/_expected/umd.js | 2 +- test/form/indent-false/_expected/umd.js | 2 +- test/form/indent-true-spaces/_expected/umd.js | 2 +- test/form/indent-true/_expected/umd.js | 2 +- test/form/internal-conflict-resolution/_expected/umd.js | 2 +- test/form/multiple-exports/_expected/umd.js | 2 +- test/form/namespace-optimization/_expected/umd.js | 2 +- test/form/no-imports-or-exports/_expected/umd.js | 2 +- test/form/preserves-comments-after-imports/_expected/umd.js | 2 +- test/form/removes-existing-sourcemap-comments/_expected/umd.js | 2 +- test/form/self-contained-bundle/_expected/umd.js | 2 +- test/form/sourcemaps-inline/_expected/umd.js | 2 +- test/form/sourcemaps/_expected/umd.js | 2 +- .../_expected/umd.js | 2 +- test/form/unmodified-default-exports/_expected/umd.js | 2 +- test/form/unused-default-exports/_expected/umd.js | 2 +- test/form/unused-side-effect/_expected/umd.js | 2 +- 27 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index d2f01db..9b482be 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -40,7 +40,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString }, typeof exports === 'object' && typeof module !== 'undefined' ? ${cjsExport}factory(${cjsDeps.join( ', ' )}) : typeof define === 'function' && define.amd ? define(${amdParams}factory) : ${defaultExport}factory(${globalDeps}); - }(this, function (${args}) {${useStrict} + }(this || (typeof window !== 'undefined' && window), function (${args}) {${useStrict} `.replace( /^\t\t/gm, '' ).replace( /^\t/gm, magicString.getIndentString() ); diff --git a/test/cli/module-name/_expected.js b/test/cli/module-name/_expected.js index b5aa08a..6655774 100644 --- a/test/cli/module-name/_expected.js +++ b/test/cli/module-name/_expected.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.myBundle = factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var main = 42; diff --git a/test/form/banner-and-footer/_expected/umd.js b/test/form/banner-and-footer/_expected/umd.js index 74dd673..711f09f 100644 --- a/test/form/banner-and-footer/_expected/umd.js +++ b/test/form/banner-and-footer/_expected/umd.js @@ -3,7 +3,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; console.log( 'hello world' ); diff --git a/test/form/block-comments/_expected/umd.js b/test/form/block-comments/_expected/umd.js index e61b314..b7147bc 100644 --- a/test/form/block-comments/_expected/umd.js +++ b/test/form/block-comments/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { return embiggen( 6, 7 ); diff --git a/test/form/dedupes-external-imports/_expected/umd.js b/test/form/dedupes-external-imports/_expected/umd.js index 49ca55a..2da127c 100644 --- a/test/form/dedupes-external-imports/_expected/umd.js +++ b/test/form/dedupes-external-imports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('external')) : typeof define === 'function' && define.amd ? define(['external'], factory) : factory(global.external); -}(this, function (external) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (external) { 'use strict'; class Foo extends external.Component { constructor () { diff --git a/test/form/exclude-unnecessary-modifications/_expected/umd.js b/test/form/exclude-unnecessary-modifications/_expected/umd.js index 1c2bb78..44450c7 100644 --- a/test/form/exclude-unnecessary-modifications/_expected/umd.js +++ b/test/form/exclude-unnecessary-modifications/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var foo = {}; diff --git a/test/form/export-all-from-internal/_expected/umd.js b/test/form/export-all-from-internal/_expected/umd.js index 45a93fa..dcd9bf5 100644 --- a/test/form/export-all-from-internal/_expected/umd.js +++ b/test/form/export-all-from-internal/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : factory((global.exposedInternals = {})); -}(this, function (exports) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (exports) { 'use strict'; const a = 1; const b = 2; diff --git a/test/form/export-default/_expected/umd.js b/test/form/export-default/_expected/umd.js index b5aa08a..6655774 100644 --- a/test/form/export-default/_expected/umd.js +++ b/test/form/export-default/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.myBundle = factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var main = 42; diff --git a/test/form/exports-at-end-if-possible/_expected/umd.js b/test/form/exports-at-end-if-possible/_expected/umd.js index f30f365..00c5cae 100644 --- a/test/form/exports-at-end-if-possible/_expected/umd.js +++ b/test/form/exports-at-end-if-possible/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : factory((global.myBundle = {})); -}(this, function (exports) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (exports) { 'use strict'; var FOO = 'foo'; diff --git a/test/form/external-imports-custom-names/_expected/umd.js b/test/form/external-imports-custom-names/_expected/umd.js index 8912390..a3d2574 100644 --- a/test/form/external-imports-custom-names/_expected/umd.js +++ b/test/form/external-imports-custom-names/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('jquery')) : typeof define === 'function' && define.amd ? define(['jquery'], factory) : factory(global.jQuery); -}(this, function ($) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function ($) { 'use strict'; $ = 'default' in $ ? $['default'] : $; diff --git a/test/form/external-imports/_expected/umd.js b/test/form/external-imports/_expected/umd.js index 196c394..372e59b 100644 --- a/test/form/external-imports/_expected/umd.js +++ b/test/form/external-imports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('factory'), require('baz'), require('shipping-port'), require('alphabet')) : typeof define === 'function' && define.amd ? define(['factory', 'baz', 'shipping-port', 'alphabet'], factory) : factory(global.factory,global.baz,global.containers,global.alphabet); -}(this, function (factory,baz,containers,alphabet) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (factory,baz,containers,alphabet) { 'use strict'; factory = 'default' in factory ? factory['default'] : factory; var alphabet__default = 'default' in alphabet ? alphabet['default'] : alphabet; diff --git a/test/form/indent-false/_expected/umd.js b/test/form/indent-false/_expected/umd.js index ad094b9..00c751c 100644 --- a/test/form/indent-false/_expected/umd.js +++ b/test/form/indent-false/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.foo = factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { console.log( 'indented with tabs' ); diff --git a/test/form/indent-true-spaces/_expected/umd.js b/test/form/indent-true-spaces/_expected/umd.js index ecbc24d..3f7e175 100644 --- a/test/form/indent-true-spaces/_expected/umd.js +++ b/test/form/indent-true-spaces/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.foo = factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { console.log( 'indented with spaces' ); diff --git a/test/form/indent-true/_expected/umd.js b/test/form/indent-true/_expected/umd.js index e06c50d..9e18cd5 100644 --- a/test/form/indent-true/_expected/umd.js +++ b/test/form/indent-true/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : global.foo = factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { console.log( 'indented with tabs' ); diff --git a/test/form/internal-conflict-resolution/_expected/umd.js b/test/form/internal-conflict-resolution/_expected/umd.js index d9d801b..ec20f2f 100644 --- a/test/form/internal-conflict-resolution/_expected/umd.js +++ b/test/form/internal-conflict-resolution/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var bar = 42; diff --git a/test/form/multiple-exports/_expected/umd.js b/test/form/multiple-exports/_expected/umd.js index 687b75c..5219e44 100644 --- a/test/form/multiple-exports/_expected/umd.js +++ b/test/form/multiple-exports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : factory((global.myBundle = {})); -}(this, function (exports) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (exports) { 'use strict'; var foo = 1; var bar = 2; diff --git a/test/form/namespace-optimization/_expected/umd.js b/test/form/namespace-optimization/_expected/umd.js index 38f7835..da3e7ad 100644 --- a/test/form/namespace-optimization/_expected/umd.js +++ b/test/form/namespace-optimization/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function a () {} diff --git a/test/form/no-imports-or-exports/_expected/umd.js b/test/form/no-imports-or-exports/_expected/umd.js index 37794fa..68b6ed0 100644 --- a/test/form/no-imports-or-exports/_expected/umd.js +++ b/test/form/no-imports-or-exports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; console.log( 'this is it' ); diff --git a/test/form/preserves-comments-after-imports/_expected/umd.js b/test/form/preserves-comments-after-imports/_expected/umd.js index 96fec25..c581d06 100644 --- a/test/form/preserves-comments-after-imports/_expected/umd.js +++ b/test/form/preserves-comments-after-imports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : factory((global.myBundle = {})); -}(this, function (exports) { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function (exports) { 'use strict'; /** A comment for a number */ var number = 5; diff --git a/test/form/removes-existing-sourcemap-comments/_expected/umd.js b/test/form/removes-existing-sourcemap-comments/_expected/umd.js index 79f644c..5f960f0 100644 --- a/test/form/removes-existing-sourcemap-comments/_expected/umd.js +++ b/test/form/removes-existing-sourcemap-comments/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { return 42; diff --git a/test/form/self-contained-bundle/_expected/umd.js b/test/form/self-contained-bundle/_expected/umd.js index 51b72b0..dcb5071 100644 --- a/test/form/self-contained-bundle/_expected/umd.js +++ b/test/form/self-contained-bundle/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { return bar(); diff --git a/test/form/sourcemaps-inline/_expected/umd.js b/test/form/sourcemaps-inline/_expected/umd.js index a63cd7a..c082073 100644 --- a/test/form/sourcemaps-inline/_expected/umd.js +++ b/test/form/sourcemaps-inline/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { console.log( 'hello from foo.js' ); diff --git a/test/form/sourcemaps/_expected/umd.js b/test/form/sourcemaps/_expected/umd.js index 21b30a3..b1fdc01 100644 --- a/test/form/sourcemaps/_expected/umd.js +++ b/test/form/sourcemaps/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; function foo () { console.log( 'hello from foo.js' ); diff --git a/test/form/unmodified-default-exports-function-argument/_expected/umd.js b/test/form/unmodified-default-exports-function-argument/_expected/umd.js index 5ae4301..32f54aa 100644 --- a/test/form/unmodified-default-exports-function-argument/_expected/umd.js +++ b/test/form/unmodified-default-exports-function-argument/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var foo = function () { return 42; diff --git a/test/form/unmodified-default-exports/_expected/umd.js b/test/form/unmodified-default-exports/_expected/umd.js index 63cf07d..a5ae3bf 100644 --- a/test/form/unmodified-default-exports/_expected/umd.js +++ b/test/form/unmodified-default-exports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var Foo = function () { this.isFoo = true; diff --git a/test/form/unused-default-exports/_expected/umd.js b/test/form/unused-default-exports/_expected/umd.js index 140a92b..ddc2f2a 100644 --- a/test/form/unused-default-exports/_expected/umd.js +++ b/test/form/unused-default-exports/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var foo = { value: 1 }; diff --git a/test/form/unused-side-effect/_expected/umd.js b/test/form/unused-side-effect/_expected/umd.js index f96fd30..0eacecf 100644 --- a/test/form/unused-side-effect/_expected/umd.js +++ b/test/form/unused-side-effect/_expected/umd.js @@ -2,7 +2,7 @@ typeof exports === 'object' && typeof module !== 'undefined' ? factory() : typeof define === 'function' && define.amd ? define(factory) : factory(); -}(this, function () { 'use strict'; +}(this || (typeof window !== 'undefined' && window), function () { 'use strict'; var foo = 42;