Browse Source

put Object.freeze fallback behind legacy option

legacy-quote-reserved-properties
Rich-Harris 8 years ago
parent
commit
a1666103b1
  1. 3
      src/Bundle.js
  2. 5
      src/Declaration.js
  3. 4
      src/Module.js
  4. 1
      src/rollup.js
  5. 6
      test/form/legacy/_config.js
  6. 13
      test/form/legacy/_expected/amd.js
  7. 11
      test/form/legacy/_expected/cjs.js
  8. 9
      test/form/legacy/_expected/es.js
  9. 14
      test/form/legacy/_expected/iife.js
  10. 17
      test/form/legacy/_expected/umd.js
  11. 4
      test/form/legacy/main.js
  12. 1
      test/form/legacy/namespace.js
  13. 2
      test/form/prefer-const/_expected/amd.js
  14. 2
      test/form/prefer-const/_expected/cjs.js
  15. 2
      test/form/prefer-const/_expected/es.js
  16. 2
      test/form/prefer-const/_expected/iife.js
  17. 2
      test/form/prefer-const/_expected/umd.js
  18. 2
      test/test.js

3
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 );

5
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`;
}
}

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

1
src/rollup.js

@ -24,6 +24,7 @@ const ALLOWED_KEYS = [
'indent',
'interop',
'intro',
'legacy',
'moduleContext',
'moduleId',
'moduleName',

6
test/form/legacy/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'supports environments without Object.freeze',
options: {
legacy: true
}
};

13
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 );
});

11
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 );

9
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 );

14
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 );
}());

17
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 );
})));

4
test/form/legacy/main.js

@ -0,0 +1,4 @@
import * as namespace from './namespace.js';
const x = 'foo';
assert.equal( namespace[x], 42 );

1
test/form/legacy/namespace.js

@ -0,0 +1 @@
export const foo = 42;

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

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

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

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

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

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

Loading…
Cancel
Save