Skip to content

Commit

Permalink
fix(cli): h5 编译时支持读取 symbolic link,close #4431
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Sep 19, 2019
1 parent 4564acd commit dfbe42c
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions packages/taro-cli/src/h5/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,27 @@ class Compiler {
const appPath = this.appPath

fs.ensureDirSync(tempPath)
return new Promise((resolve, reject) => {
klaw(sourcePath)
.on('data', file => {
const relativePath = path.relative(appPath, file.path)
if (!file.stats.isDirectory()) {
printLog(processTypeEnum.CREATE, '发现文件', relativePath)
this.processFiles(file.path)
}
})
.on('end', () => {
resolve()
})
})
const readPromises: any[] = []
function readFiles (sourcePath, originalFilePath) {
readPromises.push(new Promise((resolve, reject) => {
klaw(sourcePath)
.on('data', file => {
const relativePath = path.relative(appPath, file.path)
if (file.stats.isSymbolicLink()) {
const linkFile = fs.readlinkSync(file.path)
readFiles.call(this, linkFile, file.path)
} else if (!file.stats.isDirectory()) {
printLog(processTypeEnum.CREATE, '发现文件', relativePath)
this.processFiles(file.path, originalFilePath)
}
})
.on('end', () => {
resolve()
})
}))
}
readFiles.call(this, sourcePath, sourcePath)
return Promise.all(readPromises)
}

async buildDist ({ watch, port }: IBuildConfig) {
Expand Down Expand Up @@ -235,17 +243,17 @@ class Compiler {
.on('add', filePath => {
const relativePath = path.relative(appPath, filePath)
printLog(processTypeEnum.CREATE, '添加文件', relativePath)
this.processFiles(filePath)
this.processFiles(filePath, filePath)
})
.on('change', filePath => {
const relativePath = path.relative(appPath, filePath)
printLog(processTypeEnum.MODIFY, '文件变动', relativePath)
this.processFiles(filePath)
this.processFiles(filePath, filePath)
})
.on('unlink', filePath => {
const relativePath = path.relative(appPath, filePath)
const extname = path.extname(relativePath)
const distDirname = this.getTempDir(filePath)
const distDirname = this.getTempDir(filePath, filePath)
const isScriptFile = REG_SCRIPTS.test(extname)
const dist = this.getDist(distDirname, filePath, isScriptFile)
printLog(processTypeEnum.UNLINK, '删除文件', relativePath)
Expand Down Expand Up @@ -329,7 +337,7 @@ class Compiler {
${funcBody}
</${tabBarPanelComponentName}>`

const comp = `
const comp = `
<${tabBarComponentName}
conf={this.state.${tabBarConfigName}}
homePage="${homePage}"
Expand All @@ -346,7 +354,7 @@ class Compiler {
return `
<${providerImportName} store={${storeName}}>
${funcBody}
</${providerImportName}>
</${providerImportName}>
`
}

Expand Down Expand Up @@ -757,7 +765,7 @@ class Compiler {
const node = astPath.node
const key = node.key
const keyName = toVar(key)

const isRender = keyName === 'render'
const isComponentWillMount = keyName === 'componentWillMount'
const isComponentDidMount = keyName === 'componentDidMount'
Expand All @@ -773,7 +781,7 @@ class Compiler {
wrapWithFuncBody
])
)

node.body = toAst(buildFuncBody(pages), { preserveComments: true })

node.body.body = compact([
Expand Down Expand Up @@ -909,7 +917,7 @@ class Compiler {

const node = astPath.node
if (t.isFunctionDeclaration(node.declaration)) {

astPath.replaceWithMultiple([
node.declaration,
t.exportDefaultDeclaration(node.declaration.id)
Expand Down Expand Up @@ -1263,12 +1271,15 @@ class Compiler {
return generateCode
}

getTempDir (filePath) {
getTempDir (filePath, originalFilePath) {
const appPath = this.appPath
const sourcePath = this.sourcePath
const tempDir = this.tempDir
let dirname = path.dirname(filePath)

const dirname = path.dirname(filePath)
if (filePath.indexOf(sourcePath) < 0) {
dirname = path.extname(originalFilePath) ? path.dirname(originalFilePath) : originalFilePath
}
const relPath = path.relative(sourcePath, dirname)

return path.resolve(appPath, tempDir, relPath)
Expand All @@ -1285,10 +1296,10 @@ class Compiler {
: path.resolve(this.tempPath, relPath)
}

processFiles (filePath) {
processFiles (filePath, originalFilePath) {
const original = fs.readFileSync(filePath, { encoding: 'utf8' })
const extname = path.extname(filePath)
const distDirname = this.getTempDir(filePath)
const distDirname = this.getTempDir(filePath, originalFilePath)
const isScriptFile = REG_SCRIPTS.test(extname)
const distPath = this.getDist(distDirname, filePath, isScriptFile)
fs.ensureDirSync(distDirname)
Expand Down

0 comments on commit dfbe42c

Please sign in to comment.