Skip to content

Commit 07c33b8

Browse files
TrySoundkristoferbaxter
authored andcommitted
Support export * as statements (#32)
The trick is simple. Rollup resolves `export *` statements into a list of exports. The only problem here could be export * from externals.
1 parent 0ebc9f7 commit 07c33b8

18 files changed

+67
-40
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ export default function closureCompiler(requestedCompileOptions: CompileOptions
7878
transformsDefined = true;
7979
}
8080
},
81-
transform: async (code: string, id: string) => deriveFromInputSource(code, id, transforms),
82-
transformChunk: async (code: string, outputOptions: OutputOptions, chunk: any) =>
83-
await transformChunk(transforms, requestedCompileOptions, code, chunk, outputOptions),
81+
transformChunk: async (code: string, outputOptions: OutputOptions, chunk: any) => {
82+
await deriveFromInputSource(code, chunk.id, transforms);
83+
return await transformChunk(transforms, requestedCompileOptions, code, chunk, outputOptions);
84+
},
8485
};
8586
}

src/transformers/exports.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,34 @@ export default class ExportTransform extends Transform implements TransformInter
5252
* @param id Rollup id reference to the source
5353
*/
5454
public async deriveFromInputSource(code: string, id: string): Promise<void> {
55-
if (this.isEntryPoint(id)) {
56-
const context = this.context;
57-
let originalExports: ExportNameToClosureMapping = {};
58-
const program = context.parse(code, { ranges: true });
55+
const context = this.context;
56+
let originalExports: ExportNameToClosureMapping = {};
57+
const program = context.parse(code, { ranges: true });
5958

60-
walk.simple(program, {
61-
ExportNamedDeclaration(node: ExportNamedDeclaration) {
62-
const namedDeclarationValues = NamedDeclaration(context, id, node);
63-
if (namedDeclarationValues !== null) {
64-
originalExports = { ...originalExports, ...namedDeclarationValues };
65-
}
66-
},
67-
ExportDefaultDeclaration(node: ExportDefaultDeclaration) {
68-
const defaultDeclarationValue = DefaultDeclaration(context, id, node);
69-
if (defaultDeclarationValue !== null) {
70-
originalExports = { ...originalExports, ...defaultDeclarationValue };
71-
}
72-
},
73-
ExportAllDeclaration(node: ExportAllDeclaration) {
74-
// TODO(KB): This case `export * from "./import"` is not currently supported.
75-
context.error(
76-
new Error(`Rollup Plugin Closure Compiler does not support export all syntax.`),
77-
);
78-
},
79-
});
59+
walk.simple(program, {
60+
ExportNamedDeclaration(node: ExportNamedDeclaration) {
61+
const namedDeclarationValues = NamedDeclaration(context, id, node);
62+
if (namedDeclarationValues !== null) {
63+
originalExports = { ...originalExports, ...namedDeclarationValues };
64+
}
65+
},
66+
ExportDefaultDeclaration(node: ExportDefaultDeclaration) {
67+
const defaultDeclarationValue = DefaultDeclaration(context, id, node);
68+
if (defaultDeclarationValue !== null) {
69+
originalExports = { ...originalExports, ...defaultDeclarationValue };
70+
}
71+
},
72+
ExportAllDeclaration(node: ExportAllDeclaration) {
73+
// TODO(KB): This case `export * from "./import"` is not currently supported.
74+
context.error(
75+
new Error(
76+
`Rollup Plugin Closure Compiler does not support export all syntax for externals.`,
77+
),
78+
);
79+
},
80+
});
8081

81-
this.originalExports = originalExports;
82-
}
82+
this.originalExports = originalExports;
8383

8484
return void 0;
8585
}

test/export-all/all.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {generator} from '../generator';
18+
19+
generator('export-all', 'all');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var export1=1;var export2=function(){return 2};export{export1,export2};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var export1=1;var export2=function(){return 2};export{export1,export2};

test/export-all/fixtures/all.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './dep.js';

test/export-all/fixtures/dep.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const export1 = 1;
2+
export function export2() {
3+
return 2;
4+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default class{constructor(b){this.a=b}console(){console.log(this.a)}}
1+
class a{constructor(b){this.a=b}console(){console.log(this.a)}}export default a;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default class{constructor(b){this.name_=b}console(){console.log(this.name_)}}
1+
class a{constructor(b){this.name_=b}console(){console.log(this.name_)}}export default a;

0 commit comments

Comments
 (0)