From 98b4da0f355c4abf6fa71f1ae835ac71b65eb58f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 16 Aug 2016 16:47:30 -0400 Subject: [PATCH] allow custom bundle context (#851) --- src/Bundle.js | 1 + src/Statement.js | 4 ++-- src/rollup.js | 1 + test/form/custom-context/_config.js | 6 ++++++ test/form/custom-context/_expected/amd.js | 5 +++++ test/form/custom-context/_expected/cjs.js | 3 +++ test/form/custom-context/_expected/es.js | 1 + test/form/custom-context/_expected/iife.js | 6 ++++++ test/form/custom-context/_expected/umd.js | 9 +++++++++ test/form/custom-context/main.js | 1 + test/test.js | 2 +- 11 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/form/custom-context/_config.js create mode 100644 test/form/custom-context/_expected/amd.js create mode 100644 test/form/custom-context/_expected/cjs.js create mode 100644 test/form/custom-context/_expected/es.js create mode 100644 test/form/custom-context/_expected/iife.js create mode 100644 test/form/custom-context/_expected/umd.js create mode 100644 test/form/custom-context/main.js diff --git a/src/Bundle.js b/src/Bundle.js index f48411f..b60d6d5 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -65,6 +65,7 @@ export default class Bundle { this.externalModules = []; this.internalNamespaces = []; + this.context = String( options.context ); this.assumedGlobals = blank(); if ( typeof options.external === 'function' ) { diff --git a/src/Statement.js b/src/Statement.js index c9b01f8..7ab8bf8 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -60,8 +60,8 @@ export default class Statement { } if ( node.type === 'ThisExpression' && contextDepth === 0 ) { - module.magicString.overwrite( node.start, node.end, 'undefined' ); - module.bundle.onwarn( 'The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten' ); + module.magicString.overwrite( node.start, node.end, module.bundle.context ); + if ( module.bundle.context === 'undefined' ) module.bundle.onwarn( 'The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten' ); } if ( node._scope ) scope = node._scope; diff --git a/src/rollup.js b/src/rollup.js index 7c1ce32..4c1a076 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -12,6 +12,7 @@ const ALLOWED_KEYS = [ 'acorn', 'banner', 'cache', + 'context', 'dest', 'entry', 'exports', diff --git a/test/form/custom-context/_config.js b/test/form/custom-context/_config.js new file mode 100644 index 0000000..84f2dbd --- /dev/null +++ b/test/form/custom-context/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'allows custom context', + options: { + context: `lolwut` + } +}; diff --git a/test/form/custom-context/_expected/amd.js b/test/form/custom-context/_expected/amd.js new file mode 100644 index 0000000..a160d17 --- /dev/null +++ b/test/form/custom-context/_expected/amd.js @@ -0,0 +1,5 @@ +define(function () { 'use strict'; + + lolwut.prop = '???'; + +}); diff --git a/test/form/custom-context/_expected/cjs.js b/test/form/custom-context/_expected/cjs.js new file mode 100644 index 0000000..f42cf3c --- /dev/null +++ b/test/form/custom-context/_expected/cjs.js @@ -0,0 +1,3 @@ +'use strict'; + +lolwut.prop = '???'; diff --git a/test/form/custom-context/_expected/es.js b/test/form/custom-context/_expected/es.js new file mode 100644 index 0000000..fa83c3d --- /dev/null +++ b/test/form/custom-context/_expected/es.js @@ -0,0 +1 @@ +lolwut.prop = '???'; diff --git a/test/form/custom-context/_expected/iife.js b/test/form/custom-context/_expected/iife.js new file mode 100644 index 0000000..a41619e --- /dev/null +++ b/test/form/custom-context/_expected/iife.js @@ -0,0 +1,6 @@ +(function () { + 'use strict'; + + lolwut.prop = '???'; + +}()); diff --git a/test/form/custom-context/_expected/umd.js b/test/form/custom-context/_expected/umd.js new file mode 100644 index 0000000..a73a22c --- /dev/null +++ b/test/form/custom-context/_expected/umd.js @@ -0,0 +1,9 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, (function () { 'use strict'; + + lolwut.prop = '???'; + +}))); diff --git a/test/form/custom-context/main.js b/test/form/custom-context/main.js new file mode 100644 index 0000000..169f08b --- /dev/null +++ b/test/form/custom-context/main.js @@ -0,0 +1 @@ +this.prop = '???'; diff --git a/test/test.js b/test/test.js index b2af38b..4eb73a6 100644 --- a/test/test.js +++ b/test/test.js @@ -89,7 +89,7 @@ describe( 'rollup', function () { return rollup.rollup({ entry: 'x', plUgins: [] }).then( () => { throw new Error( 'Missing expected error' ); }, err => { - assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' ); + assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, context, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' ); }); }); });