-
Notifications
You must be signed in to change notification settings - Fork 15
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
1 parent
c3bb97f
commit 1cb86b1
Showing
15 changed files
with
36 additions
and
140 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
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 |
---|---|---|
@@ -1,41 +1,34 @@ | ||
import traverse, { Visitor } from '@babel/traverse'; | ||
import generator from '@babel/generator'; | ||
import { parse } from '@babel/parser'; | ||
import { Resolver } from 'nger-core' | ||
import fs from 'fs-extra'; | ||
import { Injectable } from 'nger-core'; | ||
import { NgerCompilerTypescript } from 'nger-compiler'; | ||
import { mergeVisitors } from './util' | ||
import ts from 'typescript'; | ||
const {transformSync} = require('@babel/core') | ||
@Injectable() | ||
export class NgerCompilerBabel { | ||
visitor: Visitor; | ||
constructor( | ||
public ts: NgerCompilerTypescript, | ||
public visitors: Visitor[], | ||
public resolver: Resolver | ||
) { | ||
this.visitor = mergeVisitors(this.visitors, this) | ||
} | ||
) {} | ||
getFileContent(path: string, config: ts.TranspileOptions = {}) { | ||
// 如果文件或目录存在 | ||
let code = fs.readFileSync(path).toString('utf8') | ||
if (path.endsWith('.ts') || path.endsWith('tsx')) { | ||
code = this.ts.compile(code, config) | ||
} | ||
return code; | ||
} | ||
compile(from: string, config: ts.TranspileOptions = {}) { | ||
try{ | ||
// 如果已经处理过了则忽略 | ||
// 拿到文件内容 | ||
let code = this.getFileContent(from, config); | ||
// 解析 | ||
const ast = parse(code, {}); | ||
// 替换处理 | ||
traverse(ast, this.visitor || {}); | ||
code = generator(ast).code; | ||
// 如果文件或目录存在 | ||
let code = fs.readFileSync(path).toString('utf8') | ||
if (path.endsWith('.ts') || path.endsWith('tsx')) { | ||
code = this.ts.compile(code, config) | ||
} | ||
return code; | ||
}catch(e){} | ||
} | ||
compile(from: string, config: ts.TranspileOptions = {}) { | ||
// 如果已经处理过了则忽略 | ||
// 拿到文件内容 | ||
let code = this.getFileContent(from, config); | ||
if(!code){ | ||
code = ``; | ||
} | ||
code = transformSync(code, {}).code | ||
return code; | ||
// 解析 | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
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,3 +1,3 @@ | ||
import { Visitor } from '@babel/traverse'; | ||
import { InjectionToken } from 'nger-di'; | ||
export const TraverVisitor = new InjectionToken<Visitor[]>(`TraverVisitor`) | ||
// import { Visitor } from '@babel/traverse'; | ||
// import { InjectionToken } from 'nger-di'; | ||
// export const TraverVisitor = new InjectionToken<Visitor[]>(`TraverVisitor`) |
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ export default [{ | |
useFactory: init, | ||
deps: [Injector], | ||
multi: true | ||
}] | ||
}]; |
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
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