Browse Source

Merge branch 'master' into linebreaks

gh-669
Rich-Harris 9 years ago
parent
commit
cc37febe23
  1. 6
      src/Bundle.js
  2. 40
      src/Module.js
  3. 4
      src/ast/isReference.js
  4. 2
      src/finalisers/iife.js
  5. 2
      src/finalisers/umd.js
  6. 16
      src/utils/promise.js
  7. 2
      src/utils/run.js
  8. 12
      test/form/computed-properties/_expected/amd.js
  9. 14
      test/form/computed-properties/_expected/cjs.js
  10. 13
      test/form/computed-properties/_expected/es6.js
  11. 14
      test/form/computed-properties/_expected/iife.js
  12. 14
      test/form/computed-properties/_expected/umd.js
  13. 11
      test/form/computed-properties/main.js
  14. 2
      test/form/dedupes-external-imports/_expected/iife.js
  15. 2
      test/form/dedupes-external-imports/_expected/umd.js
  16. 2
      test/form/export-all-from-internal/_expected/iife.js
  17. 2
      test/form/export-all-from-internal/_expected/umd.js
  18. 2
      test/form/exports-at-end-if-possible/_expected/iife.js
  19. 2
      test/form/exports-at-end-if-possible/_expected/umd.js
  20. 2
      test/form/multiple-exports/_expected/iife.js
  21. 2
      test/form/multiple-exports/_expected/umd.js
  22. 2
      test/form/namespaced-named-exports/_expected/iife.js
  23. 2
      test/form/namespaced-named-exports/_expected/umd.js
  24. 2
      test/form/preserves-comments-after-imports/_expected/iife.js
  25. 2
      test/form/preserves-comments-after-imports/_expected/umd.js
  26. 17
      test/function/empty-exports/_config.js
  27. 1
      test/function/empty-exports/main.js
  28. 10
      test/function/member-expression-assignment-in-function/_config.js
  29. 9
      test/function/member-expression-assignment-in-function/main.js

6
src/Bundle.js

@ -1,4 +1,3 @@
import Promise from 'es6-promise/lib/es6-promise/promise.js';
import MagicString from 'magic-string';
import first from './utils/first.js';
import { blank, forOwn, keys } from './utils/object.js';
@ -10,6 +9,7 @@ import { load, makeOnwarn, resolveId } from './utils/defaults.js';
import getExportMode from './utils/getExportMode.js';
import getIndentString from './utils/getIndentString.js';
import { unixizePath } from './utils/normalizePlatform.js';
import { mapSequence } from './utils/promise.js';
import transform from './utils/transform.js';
import transformBundle from './utils/transformBundle.js';
import collapseSourcemaps from './utils/collapseSourcemaps.js';
@ -175,7 +175,7 @@ export default class Bundle {
}
fetchAllDependencies ( module ) {
const promises = module.sources.map( source => {
return mapSequence( module.sources, source => {
return this.resolveId( source, module.id )
.then( resolvedId => {
// If the `resolvedId` is supposed to be external, make it so.
@ -205,8 +205,6 @@ export default class Bundle {
}
});
});
return Promise.all( promises );
}
render ( options = {} ) {

40
src/Module.js

@ -102,37 +102,37 @@ export default class Module {
this.declarations.default = new SyntheticDefaultDeclaration( node, statement, identifier || this.basename() );
}
// export { foo, bar, baz }
// export var { foo, bar } = ...
// export var foo = 42;
// export var a = 1, b = 2, c = 3;
// export function foo () {}
else if ( node.type === 'ExportNamedDeclaration' ) {
else if ( node.declaration ) {
let declaration = node.declaration;
if ( declaration.type === 'VariableDeclaration' ) {
declaration.declarations.forEach( decl => {
extractNames( decl.id ).forEach( localName => {
this.exports[ localName ] = { localName };
});
});
} else {
// export function foo () {}
const localName = declaration.id.name;
this.exports[ localName ] = { localName };
}
}
// export { foo, bar, baz }
else {
if ( node.specifiers.length ) {
// export { foo, bar, baz }
node.specifiers.forEach( specifier => {
const localName = specifier.local.name;
const exportedName = specifier.exported.name;
this.exports[ exportedName ] = { localName };
});
}
else {
let declaration = node.declaration;
if ( declaration.type === 'VariableDeclaration' ) {
declaration.declarations.forEach( decl => {
extractNames( decl.id ).forEach( localName => {
this.exports[ localName ] = { localName };
});
});
}
else {
// export function foo () {}
const localName = declaration.id.name;
this.exports[ localName ] = { localName };
}
} else {
this.bundle.onwarn( `Module ${this.id} has an empty export declaration` );
}
}
}

4
src/ast/isReference.js

@ -10,7 +10,9 @@ export default function isReference ( node, parent ) {
if ( !parent ) return true;
// TODO is this right?
if ( parent.type === 'MemberExpression' ) return parent.computed || node === parent.object;
if ( parent.type === 'MemberExpression' || parent.type === 'MethodDefinition' ) {
return parent.computed || node === parent.object;
}
// disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
if ( parent.type === 'Property' ) return parent.computed || node === parent.value;

2
src/finalisers/iife.js

@ -31,7 +31,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString }
}
if ( exportMode === 'named' ) {
dependencies.unshift( `(this.${name} = {})` );
dependencies.unshift( `(this.${name} = this.${name} || {})` );
args.unshift( 'exports' );
}

2
src/finalisers/umd.js

@ -31,7 +31,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString },
if ( exportMode === 'named' ) {
amdDeps.unshift( `'exports'` );
cjsDeps.unshift( `exports` );
globalDeps.unshift( `(${setupNamespace(options.moduleName)} = {})` );
globalDeps.unshift( `(${setupNamespace(options.moduleName)} = global.${options.moduleName} || {})` );
args.unshift( 'exports' );
}

16
src/utils/promise.js

@ -0,0 +1,16 @@
import Promise from 'es6-promise/lib/es6-promise/promise.js';
export function mapSequence ( array, fn ) {
let results = [];
let promise = Promise.resolve();
function next ( member, i ) {
return fn( member ).then( value => results[i] = value );
}
for ( let i = 0; i < array.length; i += 1 ) {
promise = promise.then( () => next( array[i], i ) );
}
return promise.then( () => results );
}

2
src/utils/run.js

@ -95,6 +95,8 @@ export default function run ( node, scope, statement, strongDependencies, force
if ( declaration ) {
if ( declaration.isParam ) hasSideEffect = true;
} else if ( !scope.isTopLevel ) {
hasSideEffect = true;
} else {
declaration = statement.module.trace( subject.name );

12
test/form/computed-properties/_expected/amd.js

@ -1,9 +1,19 @@
define(['exports'], function (exports) { 'use strict';
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
var x = {[foo]: 'bar'};
var x = { [foo]: 'bar' };
class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}
exports.x = x;
exports.X = X;
});

14
test/form/computed-properties/_expected/cjs.js

@ -1,7 +1,17 @@
'use strict';
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
var x = {[foo]: 'bar'};
var x = { [foo]: 'bar' };
exports.x = x;
class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}
exports.x = x;
exports.X = X;

13
test/form/computed-properties/_expected/es6.js

@ -1,5 +1,14 @@
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
var x = {[foo]: 'bar'};
var x = { [foo]: 'bar' };
export { x };
class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}
export { x, X };

14
test/form/computed-properties/_expected/iife.js

@ -2,9 +2,19 @@
'use strict';
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
var x = {[foo]: 'bar'};
var x = { [foo]: 'bar' };
class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}
exports.x = x;
exports.X = X;
}((this.computedProperties = {})));
}((this.computedProperties = this.computedProperties || {})));

14
test/form/computed-properties/_expected/umd.js

@ -1,13 +1,23 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.computedProperties = {})));
(factory((global.computedProperties = global.computedProperties || {})));
}(this, function (exports) { 'use strict';
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
var x = {[foo]: 'bar'};
var x = { [foo]: 'bar' };
class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}
exports.x = x;
exports.X = X;
}));

11
test/form/computed-properties/main.js

@ -1,3 +1,12 @@
var foo = 'foo';
var bar = 'bar';
var baz = 'baz';
var bam = 'bam';
export var x = {[foo]: 'bar'};
export var x = { [foo]: 'bar' };
export class X {
[bar] () {}
get [baz] () {}
set [bam] ( value ) {}
}

2
test/form/dedupes-external-imports/_expected/iife.js

@ -30,4 +30,4 @@
exports.bar = bar;
exports.baz = baz;
}((this.myBundle = {}),external));
}((this.myBundle = this.myBundle || {}),external));

2
test/form/dedupes-external-imports/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(factory((global.myBundle = {}),global.external));
(factory((global.myBundle = global.myBundle || {}),global.external));
}(this, function (exports,external) { 'use strict';
class Foo extends external.Component {

2
test/form/export-all-from-internal/_expected/iife.js

@ -7,4 +7,4 @@
exports.a = a;
exports.b = b;
}((this.exposedInternals = {})));
}((this.exposedInternals = this.exposedInternals || {})));

2
test/form/export-all-from-internal/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.exposedInternals = {})));
(factory((global.exposedInternals = global.exposedInternals || {})));
}(this, function (exports) { 'use strict';
const a = 1;

2
test/form/exports-at-end-if-possible/_expected/iife.js

@ -9,4 +9,4 @@
exports.FOO = FOO;
}((this.myBundle = {})));
}((this.myBundle = this.myBundle || {})));

2
test/form/exports-at-end-if-possible/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.myBundle = {})));
(factory((global.myBundle = global.myBundle || {})));
}(this, function (exports) { 'use strict';
var FOO = 'foo';

2
test/form/multiple-exports/_expected/iife.js

@ -7,4 +7,4 @@
exports.foo = foo;
exports.bar = bar;
}((this.myBundle = {})));
}((this.myBundle = this.myBundle || {})));

2
test/form/multiple-exports/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.myBundle = {})));
(factory((global.myBundle = global.myBundle || {})));
}(this, function (exports) { 'use strict';
var foo = 1;

2
test/form/namespaced-named-exports/_expected/iife.js

@ -7,4 +7,4 @@ this.foo.bar = this.foo.bar || {};
exports.answer = answer;
}((this.foo.bar.baz = {})));
}((this.foo.bar.baz = this.foo.bar.baz || {})));

2
test/form/namespaced-named-exports/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.foo = global.foo || {}, global.foo.bar = global.foo.bar || {}, global.foo.bar.baz = {})));
(factory((global.foo = global.foo || {}, global.foo.bar = global.foo.bar || {}, global.foo.bar.baz = global.foo.bar.baz || {})));
}(this, function (exports) { 'use strict';
var answer = 42;

2
test/form/preserves-comments-after-imports/_expected/iife.js

@ -9,4 +9,4 @@
exports.obj = obj;
}((this.myBundle = {})));
}((this.myBundle = this.myBundle || {})));

2
test/form/preserves-comments-after-imports/_expected/umd.js

@ -1,7 +1,7 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.myBundle = {})));
(factory((global.myBundle = global.myBundle || {})));
}(this, function (exports) { 'use strict';
/** A comment for a number */

17
test/function/empty-exports/_config.js

@ -0,0 +1,17 @@
var assert = require( 'assert' );
var warned = false;
module.exports = {
description: 'warns on export {}, but does not fail',
options: {
onwarn: function ( msg ) {
warned = true;
assert.ok( /main\.js has an empty export declaration/.test( msg ) );
}
},
exports: function ( exports ) {
assert.equal( Object.keys( exports ).length, 0 );
assert.ok( warned, 'did not warn' );
}
};

1
test/function/empty-exports/main.js

@ -0,0 +1 @@
export {};

10
test/function/member-expression-assignment-in-function/_config.js

@ -0,0 +1,10 @@
var assert = require( 'assert' );
module.exports = {
description: 'detect side effect in member expression assignment when not top level',
code: function ( code ) {
assert.equal( code.indexOf( 'function set(key, value) { foo[key] = value; }' ) >= 0, true, code );
assert.equal( code.indexOf( 'set("bar", 2);' ) >= 0, true, code );
assert.equal( code.indexOf( 'set("qux", 3);' ) >= 0, true, code );
}
}

9
test/function/member-expression-assignment-in-function/main.js

@ -0,0 +1,9 @@
var foo = {};
function set(key, value) { foo[key] = value; }
set("bar", 2);
set("qux", 3);
console.log(foo);
Loading…
Cancel
Save