Skip to content

Commit

Permalink
Move CJS Extern Generation (#272)
Browse files Browse the repository at this point in the history
* Move CJS Extern generation to its own transformer
* its 2020
  • Loading branch information
kristoferbaxter authored Jan 7, 2020
1 parent 75fe1c6 commit 5d1e829
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
48 changes: 48 additions & 0 deletions src/transformers/cjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Transform } from '../types';
import { OutputOptions } from 'rollup';

const HEADER = `/**
* @fileoverview Externs built via derived configuration from Rollup or input code.
* This extern contains the cjs typing info for modules.
* @externs
*/
/**
* @typedef {{
* __esModule: boolean,
* }}
*/
var exports;`;

/**
* This Transform will apply only if the Rollup configuration is for a cjs output.
*
* In order to preserve the __esModules boolean on an Object, this typedef needs to be present.
*/
export default class CJSTransform extends Transform {
public name = 'CJSTransform';

public extern(options: OutputOptions): string {
if (options.format === 'cjs') {
return HEADER;
}

return '';
}
}
10 changes: 0 additions & 10 deletions src/transformers/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ const EXTERN_OVERVIEW = `/**
* @externs
*/`;

const CJS_EXTERN = `/**
* @typedef {{
* __esModule: boolean,
* }}
*/
var exports;`;

/**
* This Transform will apply only if the Rollup configuration is for 'esm' output.
*
Expand Down Expand Up @@ -111,9 +104,6 @@ export default class ExportTransform extends Transform implements TransformInter

public extern(options: OutputOptions): string {
let output = EXTERN_OVERVIEW;
if (options.format === 'cjs') {
output += CJS_EXTERN;
}

for (const key of this.originalExports.keys()) {
const value: ExportDetails = this.originalExports.get(key) as ExportDetails;
Expand Down
2 changes: 2 additions & 0 deletions src/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { OutputOptions, PluginContext, InputOptions } from 'rollup';
import { Transform } from './types';
import IifeTransform from './transformers/iife';
import CJSTransform from './transformers/cjs';
import LiteralComputedKeys from './transformers/literal-computed-keys';
import ExportTransform from './transformers/exports';
import ImportTransform from './transformers/imports';
Expand All @@ -37,6 +38,7 @@ export const createTransforms = (
return [
new ConstTransform(context, options),
new IifeTransform(context, options),
new CJSTransform(context, options),
new LiteralComputedKeys(context, options),
new StrictTransform(context, options),
new ExportTransform(context, options),
Expand Down
12 changes: 7 additions & 5 deletions test/export-cjs/export-cjs-extern.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ test('generate extern for cjs export pattern', async t => {
const transforms = createTransforms({});
const options = defaults(outputOptions, [], transforms);

const contentMatch = options.externs.some(async externFilePath => {
for (const externFilePath of options.externs) {
const fileContent = await fsPromises.readFile(externFilePath, 'utf8');
return fileContent === externFixtureContent;
});

t.is(contentMatch, true);
if (fileContent === externFixtureContent) {
t.pass();
return;
}
}
t.fail('None of the externs match the expected format');
});
10 changes: 5 additions & 5 deletions test/export-cjs/fixtures/export.extern.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* @fileoverview Externs built via derived configuration from Rollup or input code.
* This extern contains the export global object so Closure doesn't get confused by its presence.
* This extern contains the cjs typing info for modules.
* @externs
*/

/**
* @typedef {{
* __esModule: boolean,
* }}
*/
* @typedef {{
* __esModule: boolean,
* }}
*/
var exports;

0 comments on commit 5d1e829

Please sign in to comment.