From 3b861ac7f09802ee1b06257caaf65e7e5bf7028e Mon Sep 17 00:00:00 2001 From: Kristofer Baxter Date: Thu, 1 Nov 2018 14:34:02 -0700 Subject: [PATCH] Remove deriveFromInputSource lifecycle (#97) --- package-lock.json | 16 +++++++--------- src/index.ts | 3 +-- src/transformers/exports.ts | 16 +++++----------- src/transforms.ts | 18 +----------------- src/types.ts | 6 ------ 5 files changed, 14 insertions(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2c86d606..d79fac69 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@ampproject/rollup-plugin-closure-compiler", - "version": "0.7.3", + "version": "0.8.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2578,16 +2578,14 @@ "minimist": "1.2.0", "vinyl": "2.2.0", "vinyl-sourcemaps-apply": "0.2.1" - }, - "dependencies": { - "google-closure-compiler-linux": { - "version": "20181008.0.0", - "resolved": "https://registry.npmjs.org/google-closure-compiler-linux/-/google-closure-compiler-linux-20181008.0.0.tgz", - "integrity": "sha512-k8njGfH2uzWJiRPPvUxM7MJB28gPrf4kI2bbuiF0gJk/1arXcWCPGjLD6pzCU0UylMy52MUXLgsIpRorqf2brw==", - "optional": true - } } }, + "google-closure-compiler-linux": { + "version": "20181008.0.0", + "resolved": "https://registry.npmjs.org/google-closure-compiler-linux/-/google-closure-compiler-linux-20181008.0.0.tgz", + "integrity": "sha512-k8njGfH2uzWJiRPPvUxM7MJB28gPrf4kI2bbuiF0gJk/1arXcWCPGjLD6pzCU0UylMy52MUXLgsIpRorqf2brw==", + "optional": true + }, "google-closure-compiler-osx": { "version": "20181008.0.0", "resolved": "https://registry.npmjs.org/google-closure-compiler-osx/-/google-closure-compiler-osx-20181008.0.0.tgz", diff --git a/src/index.ts b/src/index.ts index 33af91fd..2eaa014b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,7 +27,7 @@ import { } from 'rollup'; import compiler from './compiler'; import options from './options'; -import { preCompilation, createTransforms, deriveFromInputSource } from './transforms'; +import { preCompilation, createTransforms } from './transforms'; import { Transform } from './types'; const readFile = promisify(fs.readFile); @@ -85,7 +85,6 @@ export default function closureCompiler(requestedCompileOptions: CompileOptions }, renderChunk: async (code: string, chunk: RenderedChunk, outputOptions: OutputOptions) => { const transforms = createTransforms(context, inputOptions); - await deriveFromInputSource(code, chunk, transforms); return await renderChunk(transforms, requestedCompileOptions, code, outputOptions); }, }; diff --git a/src/transformers/exports.ts b/src/transformers/exports.ts index ffe73830..d36f2a81 100644 --- a/src/transformers/exports.ts +++ b/src/transformers/exports.ts @@ -22,7 +22,7 @@ import { Node, ClassDeclaration, } from 'estree'; -import { TransformSourceDescription, RenderedChunk } from 'rollup'; +import { TransformSourceDescription } from 'rollup'; import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities'; import { isESMFormat } from '../options'; import { @@ -45,13 +45,7 @@ import { parse, walk } from '../acorn'; export default class ExportTransform extends Transform implements TransformInterface { private originalExports: ExportNameToClosureMapping = {}; - /** - * Before Closure Compiler is given a chance to look at the code, we need to - * find and store all export statements with their correct type - * @param code source to parse - * @param chunk Rollup chunk reference to the source - */ - public async deriveFromInputSource(code: string, chunk: RenderedChunk): Promise { + private async deriveExports(code: string): Promise { const context = this.context; let originalExports: ExportNameToClosureMapping = {}; const program = parse(code); @@ -79,9 +73,7 @@ export default class ExportTransform extends Transform implements TransformInter }, }); - this.originalExports = originalExports; - - return void 0; + return originalExports; } /** @@ -99,6 +91,8 @@ export default class ExportTransform extends Transform implements TransformInter ); } else if (isESMFormat(this.outputOptions.format)) { const source = new MagicString(code); + this.originalExports = await this.deriveExports(code); + Object.keys(this.originalExports).forEach(key => { // Remove export statements before Closure Compiler sees the code // This prevents CC from transpiling `export` statements when the language_out is set to a value diff --git a/src/transforms.ts b/src/transforms.ts index e1dfe915..6f2f8f50 100644 --- a/src/transforms.ts +++ b/src/transforms.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { OutputOptions, PluginContext, InputOptions, RenderedChunk } from 'rollup'; +import { OutputOptions, PluginContext, InputOptions } from 'rollup'; import { Transform } from './types'; import IifeTransform from './transformers/iife'; import LiteralComputedKeys from './transformers/literal-computed-keys'; @@ -89,19 +89,3 @@ export async function postCompilation(code: string, transforms: Array logSource('after postCompilation handlers', code); return code; } - -/** - * Run each transform's `deriveFromInputSource` phase in parallel. - * @param code source code to derive information from, pre Closure Compiler minification. - * @param id Rollup identifier for this input source. - * @param transforms Transforms to execute. - */ -export async function deriveFromInputSource( - code: string, - chunk: RenderedChunk, - transforms: Array, -): Promise { - await Promise.all(transforms.map(transform => transform.deriveFromInputSource(code, chunk))).then( - _ => void 0, - ); -} diff --git a/src/types.ts b/src/types.ts index f8b42aba..3ac39e94 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,7 +21,6 @@ import { PluginContext, InputOptions, InputOption, - RenderedChunk, } from 'rollup'; const dynamicImport = require('acorn-dynamic-import'); @@ -66,7 +65,6 @@ export interface ExportNameToClosureMapping { export type TransformMethod = (code: string) => Promise; export interface TransformInterface { extern: (options: OutputOptions) => string; - deriveFromInputSource: (code: string, chunk: RenderedChunk) => Promise; preCompilation: TransformMethod; postCompilation: TransformMethod; } @@ -84,10 +82,6 @@ export class Transform implements TransformInterface { return ''; } - public async deriveFromInputSource(code: string, chunk: RenderedChunk): Promise { - return void 0; - } - public async preCompilation(code: string): Promise { return { code,