-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
2,376 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
node_modules/ | ||
npm-debug.log | ||
npm-debug.log | ||
/out/ | ||
/.idea/copyright/ | ||
watcherTasks.xml | ||
/.idea/inspectionProfiles/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea/ | ||
ts-babel.iml | ||
/.idea/ | ||
ts-babel.iml | ||
/out/ | ||
/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as ts from "typescript" | ||
import * as fs from "fs" | ||
import * as path from "path" | ||
import * as babel from "babel-core" | ||
|
||
const basePath = process.cwd() | ||
const tsConfigPath = path.join(basePath, "tsconfig.json") | ||
fs.readFile(tsConfigPath, "utf8", (readError, data) => { | ||
if (readError != null) { | ||
throw readError | ||
} | ||
|
||
const {config, error} = ts.parseConfigFileTextToJson(tsConfigPath, data) | ||
if (error == null || !printErrors([error])) { | ||
// we check it before in any case | ||
config.noEmitOnError = false | ||
compile(config) | ||
} | ||
}) | ||
|
||
function compile(config: any) { | ||
const compilerOptions = ts.convertCompilerOptionsFromJson(config.compilerOptions, basePath) | ||
if (printErrors(compilerOptions.errors)) { | ||
return | ||
} | ||
|
||
const program = ts.createProgram(config.files, compilerOptions.options) | ||
if (printErrors(ts.getPreEmitDiagnostics(program))) { | ||
return | ||
} | ||
|
||
const outDir = compilerOptions.options.outDir | ||
if (outDir == null) { | ||
throw new Error("outDir is not specified in the compilerOptions") | ||
} | ||
|
||
const fileToSourceMap: any = {} | ||
program.emit(undefined, (fileName, data) => { | ||
if (endsWith(fileName, ".js")) { | ||
const sourceMapFileName = fileName + ".map" | ||
processCompiled(data, fileToSourceMap[sourceMapFileName], fileName, sourceMapFileName) | ||
} | ||
else if (endsWith(fileName, ".js.map")) { | ||
fileToSourceMap[fileName] = data | ||
} | ||
}) | ||
} | ||
|
||
function processCompiled(code: string, sourceMap: string, jsFileName: string, sourceMapFileName: string) { | ||
const result = babel.transform(code, { | ||
inputSourceMap: JSON.parse(sourceMap), | ||
sourceMaps: true, | ||
filename: jsFileName, | ||
}) | ||
|
||
const handler = (e: Error) => { if (e != null) throw e } | ||
fs.writeFile(jsFileName, result.code, handler) | ||
fs.writeFile(sourceMapFileName, JSON.stringify(result.map), handler) | ||
} | ||
|
||
function printErrors(errors: Array<ts.Diagnostic>): boolean { | ||
if (errors.length === 0) { | ||
return false | ||
} | ||
|
||
for (let diagnostic of errors) { | ||
const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start) | ||
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n') | ||
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`) | ||
} | ||
|
||
process.exit(1) | ||
return true | ||
} | ||
|
||
function endsWith(subjectString: string, searchString: string) { | ||
const position = subjectString.length - searchString.length | ||
const lastIndex = subjectString.indexOf(searchString, position) | ||
return lastIndex !== -1 && lastIndex === position | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"outDir": "out", | ||
"newLine": "LF", | ||
"noResolve": true, | ||
"noEmitOnError": true, | ||
"inlineSources": true, | ||
"sourceMap": true, | ||
"noImplicitReturns": true, | ||
"noEmitHelpers": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [ | ||
"typings/main/ambient/node/node.d.ts", | ||
"typings/babel.d.ts", | ||
"node_modules/typescript/lib/typescript.d.ts", | ||
"src/builder.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"ambientDependencies": { | ||
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#20e1eb9616922d382d918cc5a21870a9dbe255f5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
declare module "babel-core" { | ||
interface TransformResult { | ||
code: string | ||
map: string | ||
} | ||
|
||
function transform(code: string, options: any): TransformResult | ||
|
||
interface OptionManager { | ||
|
||
} | ||
} |
Oops, something went wrong.