Skip to content

Commit 5d1e829

Browse files
Move CJS Extern Generation (#272)
* Move CJS Extern generation to its own transformer * its 2020
1 parent 75fe1c6 commit 5d1e829

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

src/transformers/cjs.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2020 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 { Transform } from '../types';
18+
import { OutputOptions } from 'rollup';
19+
20+
const HEADER = `/**
21+
* @fileoverview Externs built via derived configuration from Rollup or input code.
22+
* This extern contains the cjs typing info for modules.
23+
* @externs
24+
*/
25+
26+
/**
27+
* @typedef {{
28+
* __esModule: boolean,
29+
* }}
30+
*/
31+
var exports;`;
32+
33+
/**
34+
* This Transform will apply only if the Rollup configuration is for a cjs output.
35+
*
36+
* In order to preserve the __esModules boolean on an Object, this typedef needs to be present.
37+
*/
38+
export default class CJSTransform extends Transform {
39+
public name = 'CJSTransform';
40+
41+
public extern(options: OutputOptions): string {
42+
if (options.format === 'cjs') {
43+
return HEADER;
44+
}
45+
46+
return '';
47+
}
48+
}

src/transformers/exports.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ const EXTERN_OVERVIEW = `/**
3939
* @externs
4040
*/`;
4141

42-
const CJS_EXTERN = `/**
43-
* @typedef {{
44-
* __esModule: boolean,
45-
* }}
46-
*/
47-
var exports;`;
48-
4942
/**
5043
* This Transform will apply only if the Rollup configuration is for 'esm' output.
5144
*
@@ -111,9 +104,6 @@ export default class ExportTransform extends Transform implements TransformInter
111104

112105
public extern(options: OutputOptions): string {
113106
let output = EXTERN_OVERVIEW;
114-
if (options.format === 'cjs') {
115-
output += CJS_EXTERN;
116-
}
117107

118108
for (const key of this.originalExports.keys()) {
119109
const value: ExportDetails = this.originalExports.get(key) as ExportDetails;

src/transforms.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { OutputOptions, PluginContext, InputOptions } from 'rollup';
1818
import { Transform } from './types';
1919
import IifeTransform from './transformers/iife';
20+
import CJSTransform from './transformers/cjs';
2021
import LiteralComputedKeys from './transformers/literal-computed-keys';
2122
import ExportTransform from './transformers/exports';
2223
import ImportTransform from './transformers/imports';
@@ -37,6 +38,7 @@ export const createTransforms = (
3738
return [
3839
new ConstTransform(context, options),
3940
new IifeTransform(context, options),
41+
new CJSTransform(context, options),
4042
new LiteralComputedKeys(context, options),
4143
new StrictTransform(context, options),
4244
new ExportTransform(context, options),

test/export-cjs/export-cjs-extern.test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ test('generate extern for cjs export pattern', async t => {
2828
const transforms = createTransforms({});
2929
const options = defaults(outputOptions, [], transforms);
3030

31-
const contentMatch = options.externs.some(async externFilePath => {
31+
for (const externFilePath of options.externs) {
3232
const fileContent = await fsPromises.readFile(externFilePath, 'utf8');
33-
return fileContent === externFixtureContent;
34-
});
35-
36-
t.is(contentMatch, true);
33+
if (fileContent === externFixtureContent) {
34+
t.pass();
35+
return;
36+
}
37+
}
38+
t.fail('None of the externs match the expected format');
3739
});
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* @fileoverview Externs built via derived configuration from Rollup or input code.
3-
* This extern contains the export global object so Closure doesn't get confused by its presence.
3+
* This extern contains the cjs typing info for modules.
44
* @externs
55
*/
66

77
/**
8-
* @typedef {{
9-
* __esModule: boolean,
10-
* }}
11-
*/
8+
* @typedef {{
9+
* __esModule: boolean,
10+
* }}
11+
*/
1212
var exports;

0 commit comments

Comments
 (0)