Skip to content

Commit e979a43

Browse files
transformChunk -> renderChunk (#67)
* transformChunk -> renderChunk * Rename transformChunk method
1 parent a66022d commit e979a43

File tree

9 files changed

+92
-201
lines changed

9 files changed

+92
-201
lines changed

package-lock.json

Lines changed: 29 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ const {
2121
} = require('google-closure-compiler/lib/utils.js');
2222
import { Transform } from './types';
2323
import { postCompilation } from './transforms';
24-
import { OutputChunk } from '../node_modules/rollup';
2524

2625
enum Platform {
2726
NATIVE = 'native',
@@ -39,7 +38,6 @@ const PLATFORM_PRECEDENCE = [Platform.NATIVE, Platform.JAVA, Platform.JAVASCRIPT
3938
*/
4039
export default function(
4140
compileOptions: CompileOptions,
42-
chunk: OutputChunk,
4341
transforms: Array<Transform>,
4442
): Promise<string> {
4543
return new Promise((resolve: (stdOut: string) => void, reject: (error: any) => void) => {
@@ -64,7 +62,7 @@ export default function(
6462
} else if (exitCode !== 0) {
6563
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`));
6664
} else {
67-
resolve(await postCompilation(code, chunk, transforms));
65+
resolve(await postCompilation(code, transforms));
6866
}
6967
});
7068
});

src/debug.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ export const logSource = (preamble: string, source: string, code?: string) => {
2727
}
2828
}
2929
};
30+
31+
export const log = (preamble: string, message: string) =>
32+
DEBUG_ENABLED ? console.log(preamble) && console.log(message) : null;

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
import { CompileOptions } from 'google-closure-compiler';
1818
import * as fs from 'fs';
1919
import { promisify } from 'util';
20-
import { OutputOptions, RawSourceMap, Plugin, InputOptions, PluginContext } from 'rollup';
20+
import {
21+
OutputOptions,
22+
RawSourceMap,
23+
Plugin,
24+
InputOptions,
25+
PluginContext,
26+
RenderedChunk,
27+
} from 'rollup';
2128
import compiler from './compiler';
2229
import options from './options';
2330
import { preCompilation, createTransforms, deriveFromInputSource } from './transforms';
@@ -34,14 +41,13 @@ const readFile = promisify(fs.readFile);
3441
* @param outputOptions Rollup Output Options.
3542
* @return Closure Compiled form of the Rollup Chunk
3643
*/
37-
const transformChunk = async (
44+
const renderChunk = async (
3845
transforms: Array<Transform>,
3946
requestedCompileOptions: CompileOptions = {},
4047
sourceCode: string,
41-
chunk: any,
4248
outputOptions: OutputOptions,
4349
): Promise<{ code: string; map: RawSourceMap } | void> => {
44-
const code = await preCompilation(sourceCode, outputOptions, chunk, transforms);
50+
const code = await preCompilation(sourceCode, outputOptions, transforms);
4551
logSource('transform', sourceCode, code);
4652
const [compileOptions, mapFile] = options(
4753
requestedCompileOptions,
@@ -50,7 +56,7 @@ const transformChunk = async (
5056
transforms,
5157
);
5258

53-
return compiler(compileOptions, chunk, transforms).then(
59+
return compiler(compileOptions, transforms).then(
5460
async code => {
5561
return { code, map: JSON.parse(await readFile(mapFile, 'utf8')) };
5662
},
@@ -78,9 +84,9 @@ export default function closureCompiler(requestedCompileOptions: CompileOptions
7884
transformsDefined = true;
7985
}
8086
},
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);
87+
renderChunk: async (code: string, chunk: RenderedChunk, outputOptions: OutputOptions) => {
88+
await deriveFromInputSource(code, chunk, transforms);
89+
return await renderChunk(transforms, requestedCompileOptions, code, outputOptions);
8490
},
8591
};
8692
}

src/transformers/exports.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
Node,
2323
ClassDeclaration,
2424
} from 'estree';
25-
import { TransformSourceDescription, OutputChunk } from 'rollup';
25+
import { TransformSourceDescription, RenderedChunk } from 'rollup';
2626
import { NamedDeclaration, DefaultDeclaration } from './parsing-utilities';
2727
import { isESMFormat } from '../options';
2828
import {
@@ -49,22 +49,22 @@ export default class ExportTransform extends Transform implements TransformInter
4949
* Before Closure Compiler is given a chance to look at the code, we need to
5050
* find and store all export statements with their correct type
5151
* @param code source to parse
52-
* @param id Rollup id reference to the source
52+
* @param chunk Rollup chunk reference to the source
5353
*/
54-
public async deriveFromInputSource(code: string, id: string): Promise<void> {
54+
public async deriveFromInputSource(code: string, chunk: RenderedChunk): Promise<void> {
5555
const context = this.context;
5656
let originalExports: ExportNameToClosureMapping = {};
5757
const program = context.parse(code, { ranges: true });
5858

5959
walk.simple(program, {
6060
ExportNamedDeclaration(node: ExportNamedDeclaration) {
61-
const namedDeclarationValues = NamedDeclaration(context, id, node);
61+
const namedDeclarationValues = NamedDeclaration(context, node);
6262
if (namedDeclarationValues !== null) {
6363
originalExports = { ...originalExports, ...namedDeclarationValues };
6464
}
6565
},
6666
ExportDefaultDeclaration(node: ExportDefaultDeclaration) {
67-
const defaultDeclarationValue = DefaultDeclaration(context, id, node);
67+
const defaultDeclarationValue = DefaultDeclaration(context, node);
6868
if (defaultDeclarationValue !== null) {
6969
originalExports = { ...originalExports, ...defaultDeclarationValue };
7070
}
@@ -92,11 +92,7 @@ export default class ExportTransform extends Transform implements TransformInter
9292
* @param id Rollup id reference to the source
9393
* @return modified input source with window scoped references.
9494
*/
95-
public async preCompilation(
96-
code: string,
97-
chunk: any,
98-
id: string,
99-
): Promise<TransformSourceDescription> {
95+
public async preCompilation(code: string): Promise<TransformSourceDescription> {
10096
if (this.outputOptions === null) {
10197
this.context.warn(
10298
'Rollup Plugin Closure Compiler, OutputOptions not known before Closure Compiler invocation.',
@@ -127,11 +123,7 @@ export default class ExportTransform extends Transform implements TransformInter
127123
* @param id Rollup identifier for the source
128124
* @return Promise containing the repaired source
129125
*/
130-
public async postCompilation(
131-
code: string,
132-
chunk: OutputChunk,
133-
id: string,
134-
): Promise<TransformSourceDescription> {
126+
public async postCompilation(code: string): Promise<TransformSourceDescription> {
135127
if (this.outputOptions === null) {
136128
this.context.warn(
137129
'Rollup Plugin Closure Compiler, OutputOptions not known before Closure Compiler invocation.',

0 commit comments

Comments
 (0)