Skip to content

Commit

Permalink
feat: get rid of gulp
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Feb 23, 2016
1 parent f9c8cb1 commit 4ce5963
Show file tree
Hide file tree
Showing 15 changed files with 2,376 additions and 95 deletions.
6 changes: 5 additions & 1 deletion .gitignore
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/
7 changes: 7 additions & 0 deletions .idea/dictionaries/develar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/libraries/ts.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/typescript-compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.idea/
ts-babel.iml
/.idea/
ts-babel.iml
/out/
/src/
83 changes: 1 addition & 82 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,85 +2,4 @@

"use strict"

const gulp = require("gulp")
const babel = require("gulp-babel")
const ts = require('gulp-typescript')
const parallel = require("run-parallel")
const fs = require("fs")
const path = require("path")
const sourcemaps = require("gulp-sourcemaps")
const vinylPaths = require("vinyl-paths")

function readFile(file, pathToData, callback) {
fs.readFile(file, "utf8", function readFileCallback(error, result) {
if (error == null) {
pathToData[path.basename(file, ".json")] = JSON.parse(result)
callback(null)
}
else {
callback(error)
}
})
}

const pathToData = Object.create(null)
parallel([readFile.bind(null, "tsconfig.json", pathToData), readFile.bind(null, "package.json", pathToData)], function (error) {
if (error != null) {
console.error(error)
process.exit(1)
}

compile()
})

function compile() {
const tsConfig = pathToData.tsconfig
const filesGlob = tsConfig.filesGlob
if (filesGlob == null) {
throw new Error("filesGlob is not specified in the tsconfig.json")
}

const compilerOptions = tsConfig.compilerOptions
const destination = path.resolve(tsConfig.compilerOptions.outDir)
if (destination == null) {
throw new Error("outDir is not specified in the tsconfig.json compilerOptions")
}

delete compilerOptions.outDir
delete compilerOptions.inlineSources
delete compilerOptions.sourceMap
compilerOptions.noExternalResolve = true

if (pathToData.package.devDependencies.typescript != null) {
compilerOptions.typescript = require(path.join(process.cwd(), "node_modules", "typescript"))
}

const tsProject = ts.createProject(compilerOptions)
const tsResult = gulp.src(filesGlob)
.pipe(sourcemaps.init())
.pipe(ts(tsProject))

const pathCollector = vinylPaths()
tsResult.js
.pipe(babel())
.pipe(sourcemaps.write("."))
.pipe(pathCollector)
.pipe(gulp.dest(destination))
.on("end", function () {
fs.readdir(destination, function (error, files) {
if (error != null) {
console.error(error)
return
}

const existing = new Set(pathCollector.paths.map(it => path.basename(it)))
for (let file of files) {
if (file[0] !== "." && !existing.has(file)) {
fs.unlink(path.join(destination, file), error => {
if (error != null) console.error(error)
})
}
}
})
})
}
require("./out/builder")
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{
"name": "ts-babel",
"description": "Transform TypeScript compiler result using Babel to granular target control",
"version": "0.2.0",
"version": "0.4.0",
"license": "MIT",
"bin": {
"ts-babel": "main.js"
},
"dependencies": {
"gulp": "^3.9.0",
"gulp-babel": "^6.1.1",
"gulp-typescript": "^2.10.0",
"run-parallel": "^1.1.4",
"vinyl-paths": "^2.1.0"
"babel-core": "^6.5.2"
},
"peerDependencies": {
"typescript": "^1.8.2"
},
"repository": {
"type": "git",
Expand All @@ -24,4 +23,4 @@
"babel",
"typescript"
]
}
}
80 changes: 80 additions & 0 deletions src/builder.ts
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
}
7 changes: 5 additions & 2 deletions ts-babel.iml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/node_modules" />
<excludeFolder url="file://$MODULE_DIR$/out" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="ts-babel node_modules" level="project" />
<orderEntry type="library" name="ts" level="project" />
</component>
</module>
23 changes: 23 additions & 0 deletions tsconfig.json
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"
]
}
5 changes: 5 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ambientDependencies": {
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#20e1eb9616922d382d918cc5a21870a9dbe255f5"
}
}
12 changes: 12 additions & 0 deletions typings/babel.d.ts
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 {

}
}
Loading

0 comments on commit 4ce5963

Please sign in to comment.