Skip to content

Commit

Permalink
Update systemJS exports in destructuring assignments via injecting an…
Browse files Browse the repository at this point in the history
… IIFE
  • Loading branch information
lukastaegert committed Dec 8, 2018
1 parent 60e9199 commit 63cfa96
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 40 deletions.
26 changes: 20 additions & 6 deletions src/ast/nodes/AssignmentExpression.ts
@@ -1,7 +1,9 @@
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../values';
import Variable from '../variables/Variable';
import * as NodeType from './NodeType';
import { ExpressionNode, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
Expand Down Expand Up @@ -47,12 +49,24 @@ export default class AssignmentExpression extends NodeBase {
render(code: MagicString, options: RenderOptions) {
this.left.render(code, options);
this.right.render(code, options);
if (options.format === 'system' && this.left.variable && this.left.variable.exportName) {
code.prependLeft(
code.original.indexOf('=', this.left.end) + 1,
` exports('${this.left.variable.exportName}',`
);
code.appendLeft(this.right.end, `)`);
if (options.format === 'system') {
if (this.left.variable && this.left.variable.exportName) {
code.prependLeft(
code.original.indexOf('=', this.left.end) + 1,
` exports('${this.left.variable.exportName}',`
);
code.appendLeft(this.right.end, `)`);
} else if ('addExportedVariables' in this.left) {
const systemPatternExports: Variable[] = [];
this.left.addExportedVariables(systemPatternExports);
if (systemPatternExports.length > 0) {
code.prependRight(
this.start,
`function (v) {${getSystemExportStatement(systemPatternExports)} return v;} (`
);
code.appendLeft(this.end, ')');
}
}
}
}
}
24 changes: 2 additions & 22 deletions src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -5,6 +5,7 @@ import {
NodeRenderOptions,
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportStatement } from '../../utils/systemJsRendering';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { EMPTY_PATH, ObjectPath } from '../values';
import Variable from '../variables/Variable';
Expand Down Expand Up @@ -38,27 +39,6 @@ function areAllDeclarationsIncludedAndNotExported(declarations: VariableDeclarat
return true;
}

function renderSystemExports(
code: MagicString,
systemPatternExports: Variable[],
appendPosition: number
) {
if (systemPatternExports.length === 1) {
code.appendRight(
appendPosition,
` exports('${systemPatternExports[0].safeExportName ||
systemPatternExports[0].exportName}', ${systemPatternExports[0].getName()});`
);
} else {
code.appendRight(
appendPosition,
` exports({${systemPatternExports
.map(variable => `${variable.safeExportName || variable.exportName}: ${variable.getName()}`)
.join(', ')}});`
);
}
}

export default class VariableDeclaration extends NodeBase {
type: NodeType.tVariableDeclaration;
declarations: VariableDeclarator[];
Expand Down Expand Up @@ -238,7 +218,7 @@ export default class VariableDeclaration extends NodeBase {
code.appendLeft(renderedContentEnd, separatorString);
}
if (systemPatternExports.length > 0) {
renderSystemExports(code, systemPatternExports, renderedContentEnd);
code.appendLeft(renderedContentEnd, ' ' + getSystemExportStatement(systemPatternExports));
}
}
}
1 change: 0 additions & 1 deletion src/rollup/types.d.ts
Expand Up @@ -237,7 +237,6 @@ export interface InputOptions {
moduleContext?: string | ((id: string) => string) | { [id: string]: string };
watch?: WatcherOptions;
experimentalCodeSplitting?: boolean;
experimentalDynamicImport?: boolean;
experimentalTopLevelAwait?: boolean;
inlineDynamicImports?: boolean;
preserveSymlinks?: boolean;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/systemJsRendering.ts
@@ -0,0 +1,12 @@
import Variable from '../ast/variables/Variable';

export function getSystemExportStatement(exportedVariables: Variable[]): string {
if (exportedVariables.length === 1) {
return `exports('${exportedVariables[0].safeExportName ||
exportedVariables[0].exportName}', ${exportedVariables[0].getName()});`;
} else {
return `exports({${exportedVariables
.map(variable => `${variable.safeExportName || variable.exportName}: ${variable.getName()}`)
.join(', ')}});`;
}
}
@@ -1,5 +1,5 @@
module.exports = {
description: 'Supports destructuring declarations and assignments for SystemJS',
description: 'supports destructuring assignments of exports for systemJS',
options: {
output: {
format: 'system'
Expand Down
Expand Up @@ -3,12 +3,16 @@ System.register([], function (exports, module) {
return {
execute: function () {

const {a = 1, ...b} = global1, c = exports('c', global2), {d} = global3; exports({a: a, b: b, d: d});
const [e, ...f] = global4; exports({e: e, f: f});
const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5; exports({g: g, h: h, i: i, j: j, k: k, l: l});
exports({
a: void 0,
b: void 0,
c: void 0
});

var m = exports('m', 1);
var {m} = global6; exports('m', m);
let a, b, c;

console.log(function (v) {exports('a', a); return v;} ({a} = someObject));
(function (v) {exports({b: b, c: c}); return v;} ({b, c} = someObject));

}
};
Expand Down
@@ -1,6 +1,4 @@
export const {a = 1, ...b} = global1, c = global2, {d} = global3;
export const [e, ...f] = global4;
export const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5;
export let a, b, c;

export var m = 1;
var {m} = global6;
console.log({a} = someObject);
({b, c} = someObject);
@@ -0,0 +1,8 @@
module.exports = {
description: 'supports destructuring declarations for systemJS',
options: {
output: {
format: 'system'
}
}
};
@@ -0,0 +1,15 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

const {a = 1, ...b} = global1, c = exports('c', global2), {d} = global3; exports({a: a, b: b, d: d});
const [e, ...f] = global4; exports({e: e, f: f});
const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5; exports({g: g, h: h, i: i, j: j, k: k, l: l});

var m = exports('m', 1);
var {m} = global6; exports('m', m);

}
};
});
@@ -0,0 +1,6 @@
export const {a = 1, ...b} = global1, c = global2, {d} = global3;
export const [e, ...f] = global4;
export const {g, x: h = 2, y: {z: i}, a: [j ,k,, l]} = global5;

export var m = 1;
var {m} = global6;

0 comments on commit 63cfa96

Please sign in to comment.