Skip to content

Commit

Permalink
feat: mark san components, write to js and run
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Sep 16, 2019
1 parent 43805bc commit d78c66d
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 75 deletions.
1 change: 1 addition & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ module.exports = {
'@babel/preset-env',
'@babel/preset-typescript'
],
"plugins": ["transform-class-properties"]
};
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-unused-vars": "error",
"no-unused-vars": "off",
"import/export": "off"
}
Expand Down
158 changes: 158 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@typescript-eslint/eslint-plugin": "^2.0.0",
"@typescript-eslint/parser": "^2.0.0",
"babel-jest": "^24.9.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-jest": "^24.9.0",
"eslint": "^5.12.1",
"eslint-config-standard": "^12.0.0",
Expand Down
12 changes: 0 additions & 12 deletions src/loaders/tsc.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/transpilers/ast-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function getComponentClassIdentifier (sourceFile): string | undefined {
const declaration = sourceFile.getImportDeclaration(
node => node.getModuleSpecifierValue() === 'san'
)
if (!declaration) return
const namedImports = declaration.getNamedImports()
for (const namedImport of namedImports) {
const propertyName = namedImport.getText()
if (propertyName === 'Component') {
return namedImport.getText()
}
}
}
59 changes: 59 additions & 0 deletions src/transpilers/component-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { getComponentClassIdentifier } from './ast-util'
import { Project, ts, SourceFile } from 'ts-morph'
import { getDefaultConfigPath } from './tsconfig'

export class ComponentParser {
private componentFile: string
private root: string
private tsconfigPath: string
private project: Project
private idPropertyName: string
private id: number = 0

constructor (
componentFile: string,
tsconfigPath = getDefaultConfigPath(),
idPropertyName = 'spsrId'
) {
this.idPropertyName = idPropertyName
this.componentFile = componentFile
this.tsconfigPath = tsconfigPath
this.project = new Project({
tsConfigFilePath: tsconfigPath
})
}

parseComponent () {
const files = this.getComponentFiles()
for (const file of files.values()) {
const componentClassIdentifier = getComponentClassIdentifier(file)
if (!componentClassIdentifier) continue
this.markComponents(file, componentClassIdentifier)
}
return files
}

private getComponentFiles () {
return new Map([[
this.componentFile,
this.project.getSourceFile(this.componentFile)
]])
}

private markComponents (sourceFile: SourceFile, componentClassIdentifier: string) {
for (const clazz of sourceFile.getClasses()) {
const extendClause = clazz.getHeritageClauseByKind(ts.SyntaxKind.ExtendsKeyword)
if (!extendClause) return

const typeNode = extendClause.getTypeNodes().find(x => x.getText() === componentClassIdentifier)
if (!typeNode) return

clazz.addProperty({
isStatic: true,
name: this.idPropertyName,
type: 'number',
initializer: '' + (this.id++)
})
}
}
}
10 changes: 10 additions & 0 deletions src/transpilers/metadata-transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as ts from 'typescript'
import * as path from 'path'

export function transformer (): ts.TransformerFactory<ts.SourceFile> {
return (context: ts.TransformationContext) => (file: ts.SourceFile) => addMetadata(file, context)
}

function addMetadata (node: ts.SourceFile, context: ts.TransformationContext) {
return node
}
53 changes: 40 additions & 13 deletions src/transpilers/ts2js.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import { resolve } from 'path'
import { transpileModule, convertCompilerOptionsFromJson, TranspileOptions } from 'typescript'

const tsConfigPath = resolve(__dirname, '../test/cases')
const compilerOptions = convertCompilerOptionsFromJson({}, tsConfigPath).options

export function ts2js (source) {
const { diagnostics, outputText } =
transpileModule(source, { compilerOptions })
if (diagnostics.length) {
console.log(diagnostics)
throw new Error('typescript compile error')
import { transpileModule } from 'typescript'
import { Project, SourceFile } from 'ts-morph'
import { getDefaultConfigPath } from './tsconfig'
import { sep } from 'path'

export class Compiler {
private root: string
private tsconfig: object
private project: Project

constructor (
tsconfigPath = getDefaultConfigPath(),
root = tsconfigPath.split(sep).slice(0, -1).join(sep)
) {
this.root = root
this.tsconfig = require(tsconfigPath)
this.project = new Project({
tsConfigFilePath: tsconfigPath
})
}

compileAndRun (source: SourceFile) {
const js = this.compileToJS(source)
const fn = new Function('module', 'exports', 'require', js) // eslint-disable-line
const module = {
exports: {}
}
fn(module, module.exports, require)
return module.exports
}

compileToJS (source: SourceFile) {
const compilerOptions = this.tsconfig['compilerOptions']
const { diagnostics, outputText } =
transpileModule(source.getFullText(), { compilerOptions })
if (diagnostics.length) {
console.log(diagnostics)
throw new Error('typescript compile error')
}
return outputText
}
return outputText
}
Loading

0 comments on commit d78c66d

Please sign in to comment.