Skip to content

Commit

Permalink
perf: use type-checker to transpile on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb committed Dec 10, 2017
1 parent 5a18d78 commit d41d95f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
26 changes: 20 additions & 6 deletions src/frontend/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import JSAsset = require('parcel-bundler/src/assets/JSAsset')

import {ConfigurationLoader} from '../backend/config-loader'
import {Transpiler} from '../backend/transpiler'
import {TranspileResult} from '../interfaces'

import {dispatchCheckFile} from './injector/worker'
import {typeCheck} from './process/checker'

export = class TSAsset extends JSAsset {
private transpiler: ConfigurationLoader<Transpiler>
private readonly transpiler: ConfigurationLoader<Transpiler>

constructor(name: string, pkg: string, options: any) {
super(name, pkg, options)
Expand All @@ -14,13 +17,24 @@ export = class TSAsset extends JSAsset {
}

public async parse(code: string) {
// ask for a type-check in the background
dispatchCheckFile(this.name)
let result: TranspileResult

// if we are in the main thread let's transpile and check
// at the same time
if(!process.send) {
const [check] = await typeCheck(this.name)

result = check.transpile()
}
else {
// ask for a type-check in the background
dispatchCheckFile(this.name)

const transpiler = await this.transpiler.wait()
const {sources} = transpiler.transpile(code, this.name)
const transpiler = await this.transpiler.wait()
result = transpiler.transpile(code, this.name)
}

this.contents = sources.js
this.contents = result.sources.js

// Parse result as ast format through babylon
return super.parse(this.contents)
Expand Down
15 changes: 10 additions & 5 deletions src/frontend/process/checker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {ConfigurationLoader} from '../../backend/config-loader'
import {reportDiagnostics} from '../../backend/reporter'
import {LanguageService} from '../../backend/service'
import {TypeCheckResult} from '../../interfaces'

let serviceConfig: ConfigurationLoader<LanguageService>|null = null

export async function typeCheck(...files: string[]) {
export async function typeCheck(...files: string[]): Promise<TypeCheckResult[]> {
if(files.length === 0) {
return
return []
}

if(serviceConfig === null) {
Expand All @@ -15,7 +16,11 @@ export async function typeCheck(...files: string[]) {

const service = await serviceConfig.wait()

files.forEach(file =>
reportDiagnostics(service.parse(file))
)
return files.map(file => {
const result = service.parse(file)

reportDiagnostics(result)

return result
})
}

0 comments on commit d41d95f

Please sign in to comment.