Skip to content

Commit

Permalink
perf: improves performance by avoiding the emit-phase of TSC in the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Sep 17, 2019
1 parent 358f336 commit f0e3923
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
53 changes: 32 additions & 21 deletions src/cli/task/transform/transform-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {TransformTaskOptions} from "./transform-task-options";
import {CONSTANT} from "../../constant/constant";
import {inspect} from "util";
import {sync} from "glob";
import {CompilerOptions, createPrinter, createProgram, NewLineKind, ScriptTarget, sys} from "typescript";
import {cjsToEsm} from "../../../transformer/cjs-to-esm";
import {CompilerOptions, createEmptyStatement, createPrinter, createProgram, NewLineKind, ScriptTarget, sys, TransformationContext} from "typescript";
import {dirname, isAbsolute, join, relative} from "path";
import {cjsToEsmTransformerFactory} from "../../../transformer/cjs-to-esm-transformer-factory";

/**
* Executes the 'generate' task
Expand Down Expand Up @@ -54,25 +54,36 @@ export async function transformTask({logger, input, outDir, root, fs}: Transform
rootDir: root
}
});
program.emit(
undefined,
(_fileName, _data) => {
// Noop
},
undefined,
false,
cjsToEsm({
sourceFileHook: async sourceFile => {
if (!matchedFiles.has(sourceFile.fileName)) return;

const destinationFile = join(absoluteOutDir, relative(root, sourceFile.fileName));
// Prepare a noop TransformationContext
const context: TransformationContext = {
enableEmitNotification: () => {},
endLexicalEnvironment: () => [],
enableSubstitution: () => {},
getCompilerOptions: () => options,
startLexicalEnvironment: () => {},
hoistFunctionDeclaration: () => {},
hoistVariableDeclaration: () => {},
isEmitNotificationEnabled: () => false,
isSubstitutionEnabled: () => false,
onEmitNode: () => {},
onSubstituteNode: () => createEmptyStatement(),
readEmitHelpers: () => [],
requestEmitHelper: () => {},
resumeLexicalEnvironment: () => {},
suspendLexicalEnvironment: () => {}
};

const transformer = cjsToEsmTransformerFactory()(context);

for (const sourceFile of program.getSourceFiles()) {
if (!matchedFiles.has(sourceFile.fileName)) continue;
const transformedSourceFile = transformer(sourceFile);

if (!fs.existsSync(destinationFile)) {
fs.mkdirSync(dirname(destinationFile), {recursive: true});
fs.writeFileSync(destinationFile, printer.printFile(sourceFile));
logger.info(`${relative(root, sourceFile.fileName)} => ${relative(root, destinationFile)}`);
}
}
})
);
const destinationFile = join(absoluteOutDir, relative(root, transformedSourceFile.fileName));

fs.mkdirSync(dirname(destinationFile), {recursive: true});
fs.writeFileSync(destinationFile, printer.printFile(transformedSourceFile));
logger.info(`${relative(root, transformedSourceFile.fileName)} => ${relative(root, destinationFile)}`);
}
}
5 changes: 0 additions & 5 deletions src/transformer/before/transform-source-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export function transformSourceFile(
): BeforeTransformerSourceFileStepResult {
// Take a fast path of the text of the SourceFile doesn't contain anything that can be transformed
if (!baseVisitorContext.onlyExports && !sourceFile.text.includes("require") && !sourceFile.text.includes("exports")) {
baseVisitorContext.sourceFileHook(sourceFile);
return {sourceFile, exports: {namedExports: new Set(), hasDefaultExport: false}};
}

Expand Down Expand Up @@ -339,10 +338,6 @@ export function transformSourceFile(
console.log("EXPORTS:", visitorContext.exportedLocals);
}

if (!visitorContext.onlyExports) {
baseVisitorContext.sourceFileHook(updatedSourceFile);
}

return {
sourceFile: updatedSourceFile,
exports: moduleExports
Expand Down
2 changes: 0 additions & 2 deletions src/transformer/cjs-to-esm-options.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {ResolveOptions} from "./util/resolve-path";
import {SourceFile} from "typescript";

export interface CjsToEsmOptions {
debug?: boolean;
readFile?: ResolveOptions["readFile"];
fileExists?: ResolveOptions["fileExists"];
sourceFileHook?(sourceFile: SourceFile): void;
}
2 changes: 0 additions & 2 deletions src/transformer/cjs-to-esm-transformer-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {existsSync, readFileSync, statSync} from "fs";
export function cjsToEsmTransformerFactory({
fileExists = file => existsSync(file) && !statSync(file).isDirectory(),
readFile = (file: string, encoding?: string) => (existsSync(file) ? readFileSync(file, encoding).toString() : undefined),
sourceFileHook = () => {},
debug = false,
...rest
}: CjsToEsmOptions = {}): TransformerFactory<SourceFile> {
Expand All @@ -24,7 +23,6 @@ export function cjsToEsmTransformerFactory({
printer: debug ? createPrinter() : undefined,
fileExists,
readFile,
sourceFileHook,
onlyExports: false
};
})();
Expand Down

0 comments on commit f0e3923

Please sign in to comment.