Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Remove closure compilation from the CLI #155

Merged
merged 1 commit into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ well.

### Invocation

Run `tsickle --help` for the full syntax, but basically you specify the minified
output bundle path and the input TypeScript project.
Run `tsickle --help` for the full syntax, but basically you provide any tsickle
specific options and use it as a TypeScript compiler.

## Development

Expand Down
69 changes: 5 additions & 64 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env node

import * as closure from 'google-closure-compiler';
import * as fs from 'fs';
import * as minimist from 'minimist';
import * as path from 'path';
Expand All @@ -18,26 +17,18 @@ const internalExternsFileName = 'tsickle_externs.js';

/** Tsickle settings passed on the command line. */
interface Settings {
/** If true, write temporary .js files to disk. Useful for debugging. */
saveTemporaries?: boolean;

/** If provided, path to save externs to. */
externsPath?: string;

/** Path to write the final JS bundle to. */
outputPath: string;
}

function usage() {
console.error(`usage: tsickle [tsickle args] -- [tsc args]
console.error(`usage: tsickle [tsickle options] -- [tsc options]

example:
tsickle --output bundle.js -- -p src --noImplicitAny
tsickle --externs=foo/externs.js -- -p src --noImplicitAny

tsickle flags are:
--saveTemporaries save intermediate .js files to disk
--externs=PATH save generated Closure externs.js to PATH
--output=PATH write final Closure bundle to PATH
`);
}

Expand All @@ -46,10 +37,7 @@ tsickle flags are:
* the arguments to pass on to tsc.
*/
function loadSettingsFromArgs(args: string[]): {settings: Settings, tscArgs: string[]} {
let settings: Settings = {
saveTemporaries: null,
outputPath: null,
};
let settings: Settings = {};
let parsedArgs = minimist(args);
for (let flag of Object.keys(parsedArgs)) {
switch (flag) {
Expand All @@ -58,15 +46,9 @@ function loadSettingsFromArgs(args: string[]): {settings: Settings, tscArgs: str
usage();
process.exit(0);
break;
case 'saveTemporaries':
settings.saveTemporaries = true;
break;
case 'externs':
settings.externsPath = parsedArgs[flag];
break;
case 'output':
settings.outputPath = parsedArgs[flag];
break;
case '_':
// This is part of the minimist API, and holds args after the '--'.
break;
Expand All @@ -76,11 +58,6 @@ function loadSettingsFromArgs(args: string[]): {settings: Settings, tscArgs: str
process.exit(1);
}
}
if (!settings.outputPath && !settings.externsPath) {
console.error('must specify --output or --externs path');
usage();
process.exit(1);
}
// Arguments after the '--' arg are arguments to tsc.
let tscArgs = parsedArgs['_'];
return {settings, tscArgs};
Expand Down Expand Up @@ -220,32 +197,6 @@ function toClosureJS(options: ts.CompilerOptions, fileNames: string[]):
return {jsFiles};
}

function closureCompile(
jsFiles: {[fileName: string]: string}, outFile: string,
callback: (exitCode: number, output: string) => void): void {
const closureOptions: closure.CompileOptions = {
// Read input files from stdin as JSON.
'json_streams': 'IN',
// Write output file to disk.
'js_output_file': outFile,
'warning_level': 'VERBOSE',
'language_in': 'ECMASCRIPT6_STRICT',
'compilation_level': 'ADVANCED_OPTIMIZATIONS',
};

let compiler = new closure.compiler(closureOptions);
let process = compiler.run((exitCode, stdout, stderr) => { callback(exitCode, stderr); });

let jsonInput: closure.JSONStreamFile[] = [];
for (let fileName of Object.keys(jsFiles)) {
jsonInput.push({
path: fileName,
src: jsFiles[fileName],
});
}
process.stdin.end(JSON.stringify(jsonInput));
}

function main(args: string[]) {
let {settings, tscArgs} = loadSettingsFromArgs(args);
let {options, fileNames, errors} = loadTscConfig(tscArgs);
Expand All @@ -266,23 +217,13 @@ function main(args: string[]) {
process.exit(1);
}

if (settings.saveTemporaries) {
for (let fileName of Object.keys(jsFiles)) {
fs.writeFileSync(fileName, jsFiles[fileName]);
}
for (let fileName of Object.keys(jsFiles)) {
fs.writeFileSync(fileName, jsFiles[fileName]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this always write the files next to the .ts files? Or does it obey outDir? Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, it obeys all the output rules of the TypeScript compiler. We're using outDir without any problems.

}

if (settings.externsPath) {
fs.writeFileSync(settings.externsPath, jsFiles[internalExternsFileName]);
}

if (settings.outputPath) {
// Run Closure compiiler to convert JS files to output bundle.
closureCompile(jsFiles, settings.outputPath, (exitCode, output) => {
if (output) console.error(output);
process.exit(exitCode);
});
}
}

// CLI entry point
Expand Down