diff --git a/packages/taro-cli/src/h5/index.ts b/packages/taro-cli/src/h5/index.ts index 0da60330d75e..41a803083903 100644 --- a/packages/taro-cli/src/h5/index.ts +++ b/packages/taro-cli/src/h5/index.ts @@ -84,8 +84,9 @@ class Compiler { [key: string]: string } pages: [PageName, FilePath][] = [] + isUi: boolean - constructor (public appPath: string, entryFile?: string) { + constructor (public appPath: string, entryFile?: string, isUi?: boolean) { const projectConfig = recursiveMerge({ h5: defaultH5Config }, require(path.join(appPath, PROJECT_CONFIG))(merge)) @@ -107,6 +108,7 @@ class Compiler { if (projectConfig.hasOwnProperty(deviceRatioConfigName)) { this.pxTransformConfig.deviceRatio = projectConfig.deviceRatio } + this.isUi = !!isUi } async clean () { @@ -286,6 +288,7 @@ class Compiler { : addLeadingSlash(stripTrailingSlash(get(this.h5Config, 'router.basename'))) const renamePagename = get(this.h5Config, 'router.renamePagename', identity) + const isUi = this.isUi let ast = wxTransformer({ code, @@ -736,8 +739,9 @@ class Compiler { isMultiRouterMode ? toAst(`mountApis(${routerConfigs});`) : toAst(`mountApis(${routerConfigs}, _taroHistory);`) ] astPath.traverse(programExitVisitor) - - lastImportNode.insertAfter(compact(extraNodes)) + if (!isUi) { + lastImportNode.insertAfter(compact(extraNodes)) + } if (renderCallCode) { const renderCallNode = toAst(renderCallCode) node.body.push(renderCallNode) diff --git a/packages/taro-cli/src/ui.ts b/packages/taro-cli/src/ui.ts index 43680a76edf4..97ec82a90744 100644 --- a/packages/taro-cli/src/ui.ts +++ b/packages/taro-cli/src/ui.ts @@ -8,6 +8,7 @@ import * as t from 'babel-types' import generate from 'babel-generator' import traverse from 'babel-traverse' import * as _ from 'lodash' +import * as glob from 'glob' import { Compiler } from './h5' import * as npmProcess from './util/npm' @@ -25,8 +26,7 @@ import { BUILD_TYPES, REG_STYLE, REG_TYPESCRIPT, - PARSE_AST_TYPE, - REG_SCRIPTS + PARSE_AST_TYPE } from './util/constants' import { IComponentObj } from './mini/interface' import { parseAst } from './mini/astProcess' @@ -166,26 +166,6 @@ function copyFileToDist (filePath: string, sourceDir: string, outputDir: string) })) } -function writeFileToDist (code: string, filePath: string, sourceDir: string, outputDir: string) { - if (!filePath && !path.isAbsolute(filePath)) { - return - } - const { appPath } = buildData - const dirname = path.dirname(filePath) - const distDirname = dirname.replace(sourceDir, outputDir) - const relativePath = path.relative(appPath, filePath) - let basename = path.basename(filePath) - if (REG_SCRIPTS.test(basename)) { - basename = path.basename(basename, path.extname(basename)) + '.js' - } - printLog(processTypeEnum.COPY, '发现文件', relativePath) - fs.ensureDirSync(distDirname) - fs.writeFileSync(path.format({ - dir: distDirname, - base: basename - }), code) -} - function parseEntryAst (ast: t.File, relativeFile: string) { const styleFiles: string[] = [] const components: IComponentObj[] = [] @@ -282,7 +262,7 @@ function analyzeFiles (files: string[], sourceDir: string, outputDir: string) { return } processedScriptFiles.add(file) - let code = fs.readFileSync(file).toString() + const code = fs.readFileSync(file).toString() const transformResult = wxTransformer({ code, sourcePath: file, @@ -296,9 +276,7 @@ function analyzeFiles (files: string[], sourceDir: string, outputDir: string) { jsonFiles, mediaFiles } = parseAst(PARSE_AST_TYPE.NORMAL, transformResult.ast, [], file, file, true) - const resFiles = styleFiles.concat(jsonFiles, mediaFiles) - code = generate(transformResult.ast).code - writeFileToDist(code, file, sourceDir, outputDir) + const resFiles = styleFiles.concat(scriptFiles, jsonFiles, mediaFiles) if (resFiles.length) { resFiles.forEach(item => { copyFileToDist(item, sourceDir, outputDir) @@ -374,16 +352,29 @@ async function buildForWeapp () { base: path.basename(outputEntryFilePath) })) if (components.length) { + components.forEach(item => { + copyFileToDist(item.path as string, sourceDir, outputDir) + }) analyzeFiles(components.map(item => item.path as string), sourceDir, outputDir) } + copyAllInterfaceFiles(sourceDir, outputDir) } catch (err) { console.log(err) } } +function copyAllInterfaceFiles (sourceDir, outputDir) { + const interfaceFiles = glob.sync(path.join(sourceDir, '**/*.d.ts')) + if (interfaceFiles && interfaceFiles.length) { + interfaceFiles.forEach(item => { + copyFileToDist(item, sourceDir, outputDir) + }) + } +} + async function buildForH5 (uiIndex = 'index') { const { appPath } = buildData - const compiler = new Compiler(appPath, uiIndex) + const compiler = new Compiler(appPath, uiIndex, true) console.log() console.log(chalk.green('开始编译 H5 端组件库!')) await compiler.buildTemp()