-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support multiple component files
- Loading branch information
Showing
16 changed files
with
190 additions
and
70 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
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,31 +1,48 @@ | ||
import { readFileSync } from 'fs' | ||
import { dirname, resolve } from 'path' | ||
import { isRelativePath } from '../parsers/dependency-resolver' | ||
|
||
export class Module { | ||
private filepath: string | ||
private content: string | ||
public filepath: string | ||
public content: string | ||
public exports = {} | ||
|
||
constructor (filepath: string, content?: string) { | ||
if (content === undefined) { | ||
content = readFileSync(filepath, 'utf8') | ||
} | ||
constructor (filepath: string, content: string) { | ||
this.filepath = filepath | ||
this.content = content | ||
} | ||
} | ||
|
||
type FileLoader = ((filepath: string) => string) | ({ [key:string]: string }); | ||
|
||
const defaultFileLoader = filepath => readFileSync(filepath, 'utf8') | ||
|
||
static require (filepath: string, content?: string) { | ||
if (!Module.cache.has(filepath)) { | ||
const mod = new Module(filepath, content) | ||
Module.cache.set(filepath, Module.load(mod)) | ||
export class CMD { | ||
private readFile | ||
public cache = new Map() | ||
|
||
constructor (readFile: FileLoader = defaultFileLoader) { | ||
if (typeof readFile === 'function') { | ||
this.readFile = readFile | ||
} else { | ||
this.readFile = filepath => readFile[filepath] || '' | ||
} | ||
return Module.cache.get(filepath) | ||
} | ||
|
||
static load (mod: Module) { | ||
const fn = new Function('module', 'exports', 'require', mod.content) // eslint-disable-line | ||
fn(mod, mod.exports, require) | ||
return mod.exports | ||
require (filepath: string) { | ||
if (!this.cache.has(filepath)) { | ||
const mod = new Module(filepath, this.readFile(filepath)) | ||
// eslint-disable-next-line | ||
const fn = new Function('module', 'exports', 'require', mod.content) | ||
fn(mod, mod.exports, path => { | ||
if (isRelativePath(path)) { | ||
path = resolve(dirname(filepath), path) | ||
return this.require(path) | ||
} | ||
return require(path) | ||
}) | ||
return mod.exports | ||
} | ||
return this.cache.get(filepath) | ||
} | ||
|
||
static cache = new Map() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { SourceFile, ImportDeclaration } from 'ts-morph' | ||
|
||
function * getInlineDeclarations (sourceFile: SourceFile) { | ||
for (const decl of sourceFile.getImportDeclarations()) { | ||
if (shouldInline(decl)) yield decl | ||
} | ||
} | ||
|
||
export function * getInlineDependencies (sourceFile: SourceFile) { | ||
for (const decl of getInlineDeclarations(sourceFile)) yield decl.getModuleSpecifierSourceFile() | ||
} | ||
|
||
export function * getInlineDependencyLiterals (sourceFile: SourceFile) { | ||
for (const decl of getInlineDeclarations(sourceFile)) yield decl.getModuleSpecifierValue() | ||
} | ||
|
||
export function getDependenciesRecursively (sourceFile: SourceFile, result = new Map()) { | ||
result.set(sourceFile.getFilePath(), sourceFile) | ||
for (const dep of getInlineDependencies(sourceFile)) { | ||
if (result.has(dep.getFilePath())) continue | ||
getDependenciesRecursively(dep, result) | ||
} | ||
return result | ||
} | ||
|
||
export function shouldInline (decl: ImportDeclaration) { | ||
return isRelativePath(decl.getModuleSpecifierValue()) && !decl.getModuleSpecifierSourceFile().isDeclarationFile() | ||
} | ||
|
||
export function isRelativePath (importLiteralValue: string) { | ||
return /^\.\//.test(importLiteralValue) | ||
} |
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,8 +1,8 @@ | ||
import { movePropertyInitiatorToPrototype } from '../transformers/ast-util' | ||
import { movePropertyInitiatorToPrototype } from '../utils/ast-util' | ||
import { SanSourceFile } from '../parsers/san-sourcefile' | ||
|
||
export function transformAstToJS (sourceFile: SanSourceFile) { | ||
for (const clazz of sourceFile.getComponentClassNames()) { | ||
movePropertyInitiatorToPrototype(sourceFile.origin, sourceFile.getClass(clazz)) | ||
} | ||
// for (const clazz of sourceFile.getComponentClassNames()) { | ||
// movePropertyInitiatorToPrototype(sourceFile.origin, sourceFile.getClass(clazz)) | ||
// } | ||
} |
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
File renamed without changes.
Oops, something went wrong.