From 44385826da693b19fac2b7c0e9476ddea11095c6 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Wed, 31 Oct 2018 17:23:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(cli):=20=E5=A4=84=E7=90=86=20imports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/convertor.js | 43 +++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/packages/taro-cli/src/convertor.js b/packages/taro-cli/src/convertor.js index 08a1e139573f..12ee6669d799 100644 --- a/packages/taro-cli/src/convertor.js +++ b/packages/taro-cli/src/convertor.js @@ -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() } @@ -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) { @@ -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) @@ -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 @@ -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)) @@ -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))