Browse Source

allow custom bundle context (#851)

rewrite
Rich Harris 8 years ago
parent
commit
98b4da0f35
  1. 1
      src/Bundle.js
  2. 4
      src/Statement.js
  3. 1
      src/rollup.js
  4. 6
      test/form/custom-context/_config.js
  5. 5
      test/form/custom-context/_expected/amd.js
  6. 3
      test/form/custom-context/_expected/cjs.js
  7. 1
      test/form/custom-context/_expected/es.js
  8. 6
      test/form/custom-context/_expected/iife.js
  9. 9
      test/form/custom-context/_expected/umd.js
  10. 1
      test/form/custom-context/main.js
  11. 2
      test/test.js

1
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' ) {

4
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;

1
src/rollup.js

@ -12,6 +12,7 @@ const ALLOWED_KEYS = [
'acorn',
'banner',
'cache',
'context',
'dest',
'entry',
'exports',

6
test/form/custom-context/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'allows custom context',
options: {
context: `lolwut`
}
};

5
test/form/custom-context/_expected/amd.js

@ -0,0 +1,5 @@
define(function () { 'use strict';
lolwut.prop = '???';
});

3
test/form/custom-context/_expected/cjs.js

@ -0,0 +1,3 @@
'use strict';
lolwut.prop = '???';

1
test/form/custom-context/_expected/es.js

@ -0,0 +1 @@
lolwut.prop = '???';

6
test/form/custom-context/_expected/iife.js

@ -0,0 +1,6 @@
(function () {
'use strict';
lolwut.prop = '???';
}());

9
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 = '???';
})));

1
test/form/custom-context/main.js

@ -0,0 +1 @@
this.prop = '???';

2
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' );
});
});
});

Loading…
Cancel
Save