From 5d1e8297bc02a6628165aadf6a22bcd9d367a8a8 Mon Sep 17 00:00:00 2001 From: Kristofer Baxter Date: Tue, 7 Jan 2020 13:32:56 -0800 Subject: [PATCH] Move CJS Extern Generation (#272) * Move CJS Extern generation to its own transformer * its 2020 --- src/transformers/cjs.ts | 48 +++++++++++++++++++++++ src/transformers/exports.ts | 10 ----- src/transforms.ts | 2 + test/export-cjs/export-cjs-extern.test.js | 12 +++--- test/export-cjs/fixtures/export.extern.js | 10 ++--- 5 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 src/transformers/cjs.ts diff --git a/src/transformers/cjs.ts b/src/transformers/cjs.ts new file mode 100644 index 00000000..f1b1e390 --- /dev/null +++ b/src/transformers/cjs.ts @@ -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 ''; + } +} diff --git a/src/transformers/exports.ts b/src/transformers/exports.ts index 128d40fc..93ba7e49 100644 --- a/src/transformers/exports.ts +++ b/src/transformers/exports.ts @@ -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. * @@ -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; diff --git a/src/transforms.ts b/src/transforms.ts index 8c901e8f..3ca2e4c6 100644 --- a/src/transforms.ts +++ b/src/transforms.ts @@ -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'; @@ -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), diff --git a/test/export-cjs/export-cjs-extern.test.js b/test/export-cjs/export-cjs-extern.test.js index 65676498..dc9df766 100644 --- a/test/export-cjs/export-cjs-extern.test.js +++ b/test/export-cjs/export-cjs-extern.test.js @@ -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'); }); \ No newline at end of file diff --git a/test/export-cjs/fixtures/export.extern.js b/test/export-cjs/fixtures/export.extern.js index 44213361..eab5bef6 100644 --- a/test/export-cjs/fixtures/export.extern.js +++ b/test/export-cjs/fixtures/export.extern.js @@ -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; \ No newline at end of file