Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Dec 22, 2019
1 parent 004fb25 commit a69d8f7
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 44 deletions.
31 changes: 23 additions & 8 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
RollupWarning,
TransformModuleJSON
} from './rollup/types';
import { error } from './utils/error';
import { error, Errors, errSyntheticNamedExportsNeedDefault } from './utils/error';
import getCodeFrame from './utils/getCodeFrame';
import { getOriginalLocation } from './utils/getOriginalLocation';
import { makeLegal } from './utils/identifierHelpers';
Expand Down Expand Up @@ -208,7 +208,6 @@ export default class Module {
scope!: ModuleScope;
sourcemapChain!: DecodedSourceMapOrMissing[];
sources = new Set<string>();
syntheticExports = new Map<string, SyntheticNamedExport>();
syntheticNamedExports: boolean;
transformFiles?: EmittedFile[];
userChunkNames = new Set<string>();
Expand All @@ -218,10 +217,12 @@ export default class Module {
private ast!: Program;
private astContext!: AstContext;
private context: string;
private defaultExport: ExportDefaultVariable | null | undefined = null;
private esTreeAst!: ESTree.Program;
private graph: Graph;
private magicString!: MagicString;
private namespaceVariable: NamespaceVariable | null = null;
private syntheticExports = new Map<string, SyntheticNamedExport>();
private transformDependencies: string[] = [];
private transitiveReexports: string[] | null = null;

Expand Down Expand Up @@ -310,6 +311,21 @@ export default class Module {
return allExportNames;
}

getDefaultExport() {
if (this.defaultExport === null) {
this.defaultExport = undefined;
this.defaultExport = this.astContext.traceExport('default') as ExportDefaultVariable;
if (!this.defaultExport) {
error({
code: Errors.SYNTHETIC_NAMED_EXPORTS_NEED_DEFAULT,
id: this.id,
message: `Modules with 'syntheticNamedExports' need a default export.`
});
}
}
return this.defaultExport;
}

getDynamicImportExpressions(): (string | Node)[] {
return this.dynamicImports.map(({ node }) => {
const importArgument = node.source;
Expand Down Expand Up @@ -454,12 +470,11 @@ export default class Module {
if (this.syntheticNamedExports) {
let syntheticExport = this.syntheticExports.get(name);
if (!syntheticExport && !this.exports[name]) {
syntheticExport = new SyntheticNamedExport(
this.astContext,
name,
this.astContext.traceExport('default') as ExportDefaultVariable
);
this.syntheticExports.set(name, syntheticExport);
const defaultExport = this.getDefaultExport();
if (defaultExport) {
syntheticExport = new SyntheticNamedExport(this.astContext, name, defaultExport);
this.syntheticExports.set(name, syntheticExport);
}
}
if (syntheticExport) {
return syntheticExport;
Expand Down
13 changes: 5 additions & 8 deletions src/ast/variables/NamespaceVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ export default class NamespaceVariable extends Variable {
}
}

getDefaultVariableName() {
return this.context.traceExport('default').getName();
}

include(context: InclusionContext) {
if (!this.included) {
if (this.containsExternalNamespace) {
Expand Down Expand Up @@ -106,16 +102,17 @@ export default class NamespaceVariable extends Variable {
}

const name = this.getName();
const defaultExport = this.syntheticNamedExports ? this.module.getDefaultExport() : undefined;

let output = '';
if (this.syntheticNamedExports && members.length === 0) {
output = this.getDefaultVariableName();
if (defaultExport && members.length === 0) {
output = defaultExport.getName();
} else {
members.unshift(`${t}__proto__:${_}null`);

output = `{${n}${members.join(`,${n}`)}${n}}`;
if (this.syntheticNamedExports) {
output = `Object.assign(${output}, ${this.getDefaultVariableName()})`;
if (defaultExport) {
output = `Object.assign(${output}, ${defaultExport.getName()})`;
}
if (options.freeze) {
output = `/*#__PURE__*/Object.freeze(${output})`;
Expand Down
1 change: 0 additions & 1 deletion src/ast/variables/SyntheticNamedExport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Module, { AstContext } from '../../Module';
import { RenderOptions } from '../../utils/renderHelpers';
import { InclusionContext } from '../ExecutionContext';
import ExportDefaultVariable from './ExportDefaultVariable';
import Variable from './Variable';
Expand Down
5 changes: 3 additions & 2 deletions src/utils/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export enum Errors {
UNRESOLVED_ENTRY = 'UNRESOLVED_ENTRY',
UNRESOLVED_IMPORT = 'UNRESOLVED_IMPORT',
VALIDATION_ERROR = 'VALIDATION_ERROR',
EXTERNAL_SYNTHETIC_EXPORTS = 'EXTERNAL_SYNTHETIC_EXPORTS'
EXTERNAL_SYNTHETIC_EXPORTS = 'EXTERNAL_SYNTHETIC_EXPORTS',
SYNTHETIC_NAMED_EXPORTS_NEED_DEFAULT = 'SYNTHETIC_NAMED_EXPORTS_NEED_DEFAULT'
}

export function errAssetNotFinalisedForFileName(name: string) {
Expand Down Expand Up @@ -268,7 +269,7 @@ export function errExternalSyntheticExports(source: string, importer: string) {
return {
code: Errors.EXTERNAL_SYNTHETIC_EXPORTS,
importer: relativeId(importer),
message: `External '${source}' can not have 'syntheticNamedExports' enabled`,
message: `External '${source}' can not have 'syntheticNamedExports' enabled.`,
source
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
define(['external'],function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
define(['external'],function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var x=_interopDefault(require('external'));var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
'use strict';function _interopDefault(e){return(e&&(typeof e==='object')&&'default'in e)?e['default']:e}var x=_interopDefault(require('external'));var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import x from'external';var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
import x from'external';var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var foo=(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
var foo=(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/system.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
System.register('foo',['external'],function(exports){'use strict';var x;return{setters:[function(module){x=module.default;}],execute:function(){exports('default',foo);var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
System.register('foo',['external'],function(exports){'use strict';var x;return{setters:[function(module){x=module.default;}],execute:function(){exports('default',foo);var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/compact/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({[Symbol.toStringTag]:'Module',__proto__:null,get default(){return foo}});console.log(self);
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f(require('external')):typeof define==='function'&&define.amd?define(['external'],f):(g=g||self,g.foo=f(g.x));}(this,(function(x){'use strict';x=x&&x.hasOwnProperty('default')?x['default']:x;var self=/*#__PURE__*/Object.freeze({__proto__:null,[Symbol.toStringTag]:'Module',get default(){return foo}});console.log(self);
function foo () {
console.log( x );
}
Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ define(['exports'], function (exports) { 'use strict';
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ var myBundle = (function (exports) {
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ System.register('myBundle', [], function (exports) {
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/freeze/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
const foo = 1;
const bar = 2;

var namespace = ({
var namespace = {
__proto__: null,
foo: foo,
bar: bar
});
};

console.log( Object.keys( namespace ) );

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/namespace-tostringtag/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
define(['exports'], function (exports) { 'use strict';

var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/namespace-tostringtag/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Object.defineProperty(exports, '__esModule', { value: true });

var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/namespace-tostringtag/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/namespace-tostringtag/_expected/iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ var iife = (function (exports) {
'use strict';

var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ System.register('iife', [], function (exports) {
execute: function () {

var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/namespace-tostringtag/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
}(this, (function (exports) { 'use strict';

var self = /*#__PURE__*/Object.freeze({
[Symbol.toStringTag]: 'Module',
__proto__: null,
[Symbol.toStringTag]: 'Module',
get p () { return p; }
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module.exports = {
description: 'external modules can not have syntheticNamedExports',
solo: true,
options: {
plugins: [
{
Expand Down
25 changes: 25 additions & 0 deletions test/function/samples/synthetic-exports-need-default/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const path = require('path');

module.exports = {
description: 'synthetic named exports moduleds need a default export',
options: {
plugins: [
{
resolveId(id) {
if (id === './dep.js') {
return {
id,
syntheticNamedExports: true
};
}
}
}
]
},
error: {
code: 'SYNTHETIC_NAMED_EXPORTS_NEED_DEFAULT',
id: './dep.js',
message: "Modules with 'syntheticNamedExports' need a default export.",
watchFiles: [path.resolve(__dirname, 'main.js'), './dep.js']
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 1;
3 changes: 3 additions & 0 deletions test/function/samples/synthetic-exports-need-default/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a } from './dep.js';
a;
console.log(a);

0 comments on commit a69d8f7

Please sign in to comment.