Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
feat(TypeScriptCompiler): utilize ts compiler api directly instead of
Browse files Browse the repository at this point in the history
3rd party module
  • Loading branch information
kwonoj authored and anaisbetts committed Jan 8, 2017
1 parent ae8ed51 commit ffbd700
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -41,9 +41,10 @@
"rimraf": "^2.5.4",
"sass.js": "^0.10.1",
"stylus": "^0.54.5",
"toutsuite": "^0.6.0",
"typescript": "~2.1.4",
"typescript-simple": "^7.0.2"
"toutsuite": "^0.6.0"
},
"peerDependencies": {
"typescript": ">=1.6"
},
"devDependencies": {
"babel-cli": "^6.11.4",
Expand Down
57 changes: 28 additions & 29 deletions src/js/typescript.js
Expand Up @@ -2,7 +2,8 @@ import {SimpleCompilerBase} from '../compiler-base';
import path from 'path';

const inputMimeTypes = ['text/typescript', 'text/tsx'];
let tss = null;
const d = require('debug')('electron-compile:typescript-compiler');

let ts = null;

/**
Expand All @@ -12,48 +13,46 @@ export default class TypeScriptCompiler extends SimpleCompilerBase {
constructor() {
super();

this.outMimeType = 'application/javascript';
this.compilerOptions = {
module: 'commonjs',
sourceMap: true,
doSemanticChecks: true
inlineSourceMap: true,
inlineSources: true
};
}

static getInputMimeTypes() {
return inputMimeTypes;
}

_getParsedConfigOptions(tsCompiler) {
let parsedConfig = this.parsedConfig;
if (!parsedConfig) {
const results = tsCompiler.convertCompilerOptionsFromJson(this.compilerOptions);
if (results.errors && results.errors.length) {
throw new Error(results.errors);
}
parsedConfig = this.parsedConfig = results.options;
}
return parsedConfig;
}

compileSync(sourceCode, filePath) {
tss = tss || require('typescript-simple');
ts = ts || require('typescript');
const options = this._getParsedConfigOptions(ts);

// NB: If you enable semantic checks with TSX, you're gonna have a
// Bad Time
let extraOpts = {target: ts.ScriptTarget.ES6};
let isJsx = false;
let isTs = filePath.match(/\.ts$/i);
const transpileOptions = {
compilerOptions: options,
fileName: filePath.match(/\.(ts|tsx)$/i) ? path.basename(filePath) : null
};

if (filePath.match(/\.tsx$/i)) {
extraOpts.jsx = ts.JsxEmit.React;
isJsx = true;
}
const output = ts.transpileModule(sourceCode, transpileOptions);

// NB: Work around TypeScriptSimple modifying the options object
let compiler = new tss.TypeScriptSimple(
Object.assign({}, this.compilerOptions, extraOpts),
this.compilerOptions.doSemanticChecks && !isJsx);

// NB: If we pass a filePath that is not a TypeScript file (i.e. we are
// compiling the script block inside a Vue component), TypeScript will barf
// attempting to find this file, even when it has a fully-qualified path.
let code;
if (isTs) {
code = compiler.compile(sourceCode, path.basename(filePath));
} else {
code = compiler.compile(sourceCode);
}
d(output.diagnostics);

return { code, mimeType: 'application/javascript' };
return {
code: output.outputText,
mimeType: this.outMimeType
};
}

getCompilerVersion() {
Expand Down

0 comments on commit ffbd700

Please sign in to comment.