From a1666103b14867c3b5b45e17ccb64067406fe225 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 9 Oct 2016 15:59:19 -0400 Subject: [PATCH] put Object.freeze fallback behind legacy option --- src/Bundle.js | 3 ++- src/Declaration.js | 5 +++-- src/Module.js | 4 ++-- src/rollup.js | 1 + test/form/legacy/_config.js | 6 ++++++ test/form/legacy/_expected/amd.js | 13 +++++++++++++ test/form/legacy/_expected/cjs.js | 11 +++++++++++ test/form/legacy/_expected/es.js | 9 +++++++++ test/form/legacy/_expected/iife.js | 14 ++++++++++++++ test/form/legacy/_expected/umd.js | 17 +++++++++++++++++ test/form/legacy/main.js | 4 ++++ test/form/legacy/namespace.js | 1 + test/form/prefer-const/_expected/amd.js | 2 +- test/form/prefer-const/_expected/cjs.js | 2 +- test/form/prefer-const/_expected/es.js | 2 +- test/form/prefer-const/_expected/iife.js | 2 +- test/form/prefer-const/_expected/umd.js | 2 +- test/test.js | 2 +- 18 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 test/form/legacy/_config.js create mode 100644 test/form/legacy/_expected/amd.js create mode 100644 test/form/legacy/_expected/cjs.js create mode 100644 test/form/legacy/_expected/es.js create mode 100644 test/form/legacy/_expected/iife.js create mode 100644 test/form/legacy/_expected/umd.js create mode 100644 test/form/legacy/main.js create mode 100644 test/form/legacy/namespace.js diff --git a/src/Bundle.js b/src/Bundle.js index 7aeeec8..f1643f2 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -95,6 +95,7 @@ export default class Bundle { this.onwarn = options.onwarn || makeOnwarn(); this.varOrConst = options.preferConst ? 'const' : 'var'; + this.legacy = options.legacy; this.acornOptions = options.acorn || {}; this.dependentExpressions = []; @@ -370,7 +371,7 @@ export default class Bundle { timeStart( 'render modules' ); this.orderedModules.forEach( module => { - const source = module.render( format === 'es' ); + const source = module.render( format === 'es', this.legacy ); if ( source.toString().length ) { magicString.addSource( source ); diff --git a/src/Declaration.js b/src/Declaration.js index 2e75793..b550cec 100644 --- a/src/Declaration.js +++ b/src/Declaration.js @@ -75,7 +75,7 @@ export class SyntheticNamespaceDeclaration { return this.name; } - renderBlock ( es, indentString ) { + renderBlock ( es, legacy, indentString ) { const members = keys( this.originals ).map( name => { const original = this.originals[ name ]; @@ -86,7 +86,8 @@ export class SyntheticNamespaceDeclaration { return `${indentString}${name}: ${original.getName( es )}`; }); - return `${this.module.bundle.varOrConst} ${this.getName( es )} = (Object.freeze || Object)({\n${members.join( ',\n' )}\n});\n\n`; + const callee = legacy ? `(Object.freeze || Object)` : `Object.freeze`; + return `${this.module.bundle.varOrConst} ${this.getName( es )} = ${callee}({\n${members.join( ',\n' )}\n});\n\n`; } } diff --git a/src/Module.js b/src/Module.js index 83fbba9..81a097c 100644 --- a/src/Module.js +++ b/src/Module.js @@ -309,7 +309,7 @@ export default class Module { return this.declarations['*']; } - render ( es ) { + render ( es, legacy ) { const magicString = this.magicString.clone(); for ( const node of this.ast.body ) { @@ -317,7 +317,7 @@ export default class Module { } if ( this.namespace().needsNamespaceBlock ) { - magicString.append( '\n\n' + this.namespace().renderBlock( es, '\t' ) ); // TODO use correct indentation + magicString.append( '\n\n' + this.namespace().renderBlock( es, legacy, '\t' ) ); // TODO use correct indentation } return magicString.trim(); diff --git a/src/rollup.js b/src/rollup.js index f99e991..cf79500 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -24,6 +24,7 @@ const ALLOWED_KEYS = [ 'indent', 'interop', 'intro', + 'legacy', 'moduleContext', 'moduleId', 'moduleName', diff --git a/test/form/legacy/_config.js b/test/form/legacy/_config.js new file mode 100644 index 0000000..2bdf641 --- /dev/null +++ b/test/form/legacy/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'supports environments without Object.freeze', + options: { + legacy: true + } +}; diff --git a/test/form/legacy/_expected/amd.js b/test/form/legacy/_expected/amd.js new file mode 100644 index 0000000..3ba37d0 --- /dev/null +++ b/test/form/legacy/_expected/amd.js @@ -0,0 +1,13 @@ +define(function () { 'use strict'; + + const foo = 42; + + + var namespace = (Object.freeze || Object)({ + foo: foo + }); + + const x = 'foo'; + assert.equal( namespace[x], 42 ); + +}); diff --git a/test/form/legacy/_expected/cjs.js b/test/form/legacy/_expected/cjs.js new file mode 100644 index 0000000..33c3402 --- /dev/null +++ b/test/form/legacy/_expected/cjs.js @@ -0,0 +1,11 @@ +'use strict'; + +const foo = 42; + + +var namespace = (Object.freeze || Object)({ + foo: foo +}); + +const x = 'foo'; +assert.equal( namespace[x], 42 ); diff --git a/test/form/legacy/_expected/es.js b/test/form/legacy/_expected/es.js new file mode 100644 index 0000000..be10260 --- /dev/null +++ b/test/form/legacy/_expected/es.js @@ -0,0 +1,9 @@ +const foo = 42; + + +var namespace = (Object.freeze || Object)({ + foo: foo +}); + +const x = 'foo'; +assert.equal( namespace[x], 42 ); diff --git a/test/form/legacy/_expected/iife.js b/test/form/legacy/_expected/iife.js new file mode 100644 index 0000000..095ddd0 --- /dev/null +++ b/test/form/legacy/_expected/iife.js @@ -0,0 +1,14 @@ +(function () { + 'use strict'; + + const foo = 42; + + + var namespace = (Object.freeze || Object)({ + foo: foo + }); + + const x = 'foo'; + assert.equal( namespace[x], 42 ); + +}()); diff --git a/test/form/legacy/_expected/umd.js b/test/form/legacy/_expected/umd.js new file mode 100644 index 0000000..f3d0fa9 --- /dev/null +++ b/test/form/legacy/_expected/umd.js @@ -0,0 +1,17 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, (function () { 'use strict'; + + const foo = 42; + + + var namespace = (Object.freeze || Object)({ + foo: foo + }); + + const x = 'foo'; + assert.equal( namespace[x], 42 ); + +}))); diff --git a/test/form/legacy/main.js b/test/form/legacy/main.js new file mode 100644 index 0000000..321f9b3 --- /dev/null +++ b/test/form/legacy/main.js @@ -0,0 +1,4 @@ +import * as namespace from './namespace.js'; + +const x = 'foo'; +assert.equal( namespace[x], 42 ); diff --git a/test/form/legacy/namespace.js b/test/form/legacy/namespace.js new file mode 100644 index 0000000..9d7381d --- /dev/null +++ b/test/form/legacy/namespace.js @@ -0,0 +1 @@ +export const foo = 42; diff --git a/test/form/prefer-const/_expected/amd.js b/test/form/prefer-const/_expected/amd.js index 2e0b570..a89dacd 100644 --- a/test/form/prefer-const/_expected/amd.js +++ b/test/form/prefer-const/_expected/amd.js @@ -4,7 +4,7 @@ define(['external', 'other', 'another'], function (external, other, another) { ' const b = 2; - const namespace = (Object.freeze || Object)({ + const namespace = Object.freeze({ a: a, b: b }); diff --git a/test/form/prefer-const/_expected/cjs.js b/test/form/prefer-const/_expected/cjs.js index 692b290..5c2e4da 100644 --- a/test/form/prefer-const/_expected/cjs.js +++ b/test/form/prefer-const/_expected/cjs.js @@ -8,7 +8,7 @@ const a = 1; const b = 2; -const namespace = (Object.freeze || Object)({ +const namespace = Object.freeze({ a: a, b: b }); diff --git a/test/form/prefer-const/_expected/es.js b/test/form/prefer-const/_expected/es.js index 2545ae4..8f5a5c9 100644 --- a/test/form/prefer-const/_expected/es.js +++ b/test/form/prefer-const/_expected/es.js @@ -6,7 +6,7 @@ const a = 1; const b = 2; -const namespace = (Object.freeze || Object)({ +const namespace = Object.freeze({ a: a, b: b }); diff --git a/test/form/prefer-const/_expected/iife.js b/test/form/prefer-const/_expected/iife.js index 2aea939..9c66729 100644 --- a/test/form/prefer-const/_expected/iife.js +++ b/test/form/prefer-const/_expected/iife.js @@ -5,7 +5,7 @@ const myBundle = (function (external,other,another) { const b = 2; - const namespace = (Object.freeze || Object)({ + const namespace = Object.freeze({ a: a, b: b }); diff --git a/test/form/prefer-const/_expected/umd.js b/test/form/prefer-const/_expected/umd.js index 19a0747..0ec9750 100644 --- a/test/form/prefer-const/_expected/umd.js +++ b/test/form/prefer-const/_expected/umd.js @@ -8,7 +8,7 @@ const b = 2; - const namespace = (Object.freeze || Object)({ + const namespace = Object.freeze({ a: a, b: b }); diff --git a/test/test.js b/test/test.js index 4ea2867..8b2b66f 100644 --- a/test/test.js +++ b/test/test.js @@ -90,7 +90,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, context, dest, entry, exports, external, footer, format, globals, indent, interop, intro, moduleContext, 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, interop, intro, legacy, moduleContext, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' ); }); });