Skip to content

Commit

Permalink
fix(cli): ui 库打包时会 copy .d.ts 结尾的 interface 文件 && 移除 H5 打包时入口的多余代码,#4672
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Oct 28, 2019
1 parent a51ee6a commit 3f16e4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
10 changes: 7 additions & 3 deletions packages/taro-cli/src/h5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -107,6 +108,7 @@ class Compiler {
if (projectConfig.hasOwnProperty(deviceRatioConfigName)) {
this.pxTransformConfig.deviceRatio = projectConfig.deviceRatio
}
this.isUi = !!isUi
}

async clean () {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
45 changes: 18 additions & 27 deletions packages/taro-cli/src/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down Expand Up @@ -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[] = []
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3f16e4d

Please sign in to comment.