Skip to content

Commit

Permalink
feat(cli): 处理 imports
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Nov 19, 2018
1 parent dad17ef commit 4438582
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions packages/taro-cli/src/convertor.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@ class Convertor {
this.root = process.cwd()
this.convertRoot = path.join(this.root, 'taroConvert')
this.convertDir = path.join(this.convertRoot, 'src')
this.importsDir = path.join(this.convertDir, 'imports')
this.fileTypes = MINI_APP_FILES[BUILD_TYPES.WEAPP]
this.pages = new Set()
this.components = new Set()
this.hadBeenCopyedFiles = new Set()
this.hadBeenBuiltComponents = new Set()
this.hadBeenBuiltImports = new Set()
this.init()
}

Expand All @@ -106,8 +108,9 @@ class Convertor {
}
}

parseAst ({ ast, sourceFilePath, outputFilePath, importStylePath, depComponents }) {
parseAst ({ ast, sourceFilePath, outputFilePath, importStylePath, depComponents, imports = [] }) {
const scriptFiles = new Set()
const self = this
traverse(ast, {
Program: {
enter (astPath) {
Expand Down Expand Up @@ -135,6 +138,19 @@ class Convertor {
if (importStylePath) {
lastImport.insertAfter(t.importDeclaration([], t.stringLiteral(promoteRelativePath(path.relative(sourceFilePath, importStylePath)))))
}
if (imports && imports.length) {
imports.forEach(({ name, ast }) => {
const importName = pascalCase(name)
const importPath = path.join(self.importsDir, importName + '.js')
if (!self.hadBeenBuiltImports.has(importPath)) {
self.hadBeenBuiltImports.add(importPath)
self.writeFileToTaro(importPath, prettier.format(generate(ast).code, prettierJSConfig))
}
lastImport.insertAfter(template(`import ${importName} from '${promoteRelativePath(path.relative(outputFilePath, importPath))}'`, {
sourceType: 'module'
})())
})
}
if (depComponents && depComponents.size) {
depComponents.forEach(componentObj => {
const name = pascalCase(componentObj.name)
Expand Down Expand Up @@ -255,12 +271,13 @@ class Convertor {
const entryJS = String(fs.readFileSync(this.entryJSPath))
const entryJSON = JSON.stringify(this.entryJSON)
const entryDistJSPath = this.getDistFilePath(this.entryJSPath)
const taroizeAst = taroize({
const taroizeResult = taroize({
json: entryJSON,
script: entryJS
script: entryJS,
path: path.dirname(entryJS)
})
const { ast, scriptFiles } = this.parseAst({
ast: taroizeAst,
ast: taroizeResult.ast,
sourceFilePath: this.entryJSPath,
outputFilePath: entryDistJSPath,
importStylePath: this.entryStyle ? this.entryStylePath.replace(path.extname(this.entryStylePath), '.css') : null
Expand Down Expand Up @@ -328,14 +345,15 @@ class Convertor {
printLog(pocessTypeEnum.CONVERT, '页面样式', this.generateShowPath(pageStylePath))
pageStyle = String(fs.readFileSync(pageStylePath))
}

const taroizeAst = taroize(param)
param.path = path.dirname(pageJSPath)
const taroizeResult = taroize(param)
const { ast, scriptFiles } = this.parseAst({
ast: taroizeAst,
ast: taroizeResult.ast,
sourceFilePath: pageJSPath,
outputFilePath: pageDistJSPath,
importStylePath: pageStyle ? pageStylePath.replace(path.extname(pageStylePath), '.css') : null,
depComponents
depComponents,
imports: taroizeResult.imports
})
const jsCode = generate(ast).code
this.writeFileToTaro(pageDistJSPath, prettier.format(jsCode, prettierJSConfig))
Expand Down Expand Up @@ -406,14 +424,15 @@ class Convertor {
printLog(pocessTypeEnum.CONVERT, '组件样式', this.generateShowPath(componentStylePath))
componentStyle = String(fs.readFileSync(componentStylePath))
}

const taroizeAst = taroize(param)
param.path = path.dirname(componentJSPath)
const taroizeResult = taroize(param)
const { ast, scriptFiles } = this.parseAst({
ast: taroizeAst,
ast: taroizeResult.ast,
sourceFilePath: componentJSPath,
outputFilePath: componentDistJSPath,
importStylePath: componentStyle ? componentStylePath.replace(path.extname(componentStylePath), '.css') : null,
depComponents
depComponents,
imports: taroizeResult.imports
})
const jsCode = generate(ast).code
this.writeFileToTaro(componentDistJSPath, prettier.format(jsCode, prettierJSConfig))
Expand Down

0 comments on commit 4438582

Please sign in to comment.