Skip to content

Commit

Permalink
fix(cli): 路径替换 extname 使用更严谨的正则去 replace。fix #4148
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Aug 13, 2019
1 parent 38b84dd commit 58f9829
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 39 deletions.
9 changes: 5 additions & 4 deletions packages/taro-cli/src/mini/astProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
replaceAliasPath,
traverseObjectNode,
isQuickappPkg,
getBabelConfig
getBabelConfig,
extnameExpRegOf
} from '../util'
import {
convertObjectToAstExpression,
Expand Down Expand Up @@ -208,7 +209,7 @@ function analyzeImportUrl ({
scriptFiles.push(vpath)
}
relativePath = promoteRelativePath(relativePath)
relativePath = relativePath.replace(path.extname(relativePath), '.js')
relativePath = relativePath.replace(extnameExpRegOf(relativePath), '.js')
node.source.value = relativePath
}
}
Expand Down Expand Up @@ -891,7 +892,7 @@ export function parseAst (
scriptFiles.push(vpath)
}
relativePath = promoteRelativePath(relativePath)
relativePath = relativePath.replace(path.extname(relativePath), '.js')
relativePath = relativePath.replace(extnameExpRegOf(relativePath), '.js')
args[0].value = relativePath
}
}
Expand Down Expand Up @@ -946,7 +947,7 @@ export function parseAst (
if (buildAdapter === BUILD_TYPES.WEAPP || buildAdapter === BUILD_TYPES.QQ) {
node.body.push(template(`Component(require('${taroMiniAppFrameworkPath}').default.createComponent(${exportVariableName}, true))`, babylonConfig as any)() as any)
} else if (isQuickApp) {
const pagePath = sourceFilePath.replace(sourceDir, '').replace(/\\/g, '/').replace(path.extname(sourceFilePath), '')
const pagePath = sourceFilePath.replace(sourceDir, '').replace(/\\/g, '/').replace(extnameExpRegOf(sourceFilePath), '')
if (!taroImportDefaultName) {
node.body.unshift(
template(`import Taro from '${taroMiniAppFrameworkPath}'`, babylonConfig as any)() as any
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-cli/src/mini/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
isDifferentArray,
copyFileSync,
getBabelConfig,
uglifyJS
uglifyJS,
extnameExpRegOf
} from '../util'
import {
BUILD_TYPES,
Expand Down Expand Up @@ -54,9 +55,9 @@ export function compileDepScripts (scriptFiles: string[], needUseBabel?: boolean
if (path.isAbsolute(item)) {
let outputItem
if (NODE_MODULES_REG.test(item)) {
outputItem = item.replace(nodeModulesPath, npmOutputDir).replace(path.extname(item), '.js')
outputItem = item.replace(nodeModulesPath, npmOutputDir).replace(extnameExpRegOf(item), '.js')
} else {
outputItem = item.replace(path.join(sourceDir), path.join(outputDir)).replace(path.extname(item), '.js')
outputItem = item.replace(path.join(sourceDir), path.join(outputDir)).replace(extnameExpRegOf(item), '.js')
}
const weappConf = Object.assign({}, projectConfig.weapp)
const useCompileConf = Object.assign({}, weappConf.compile)
Expand Down
17 changes: 9 additions & 8 deletions packages/taro-cli/src/mini/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
promoteRelativePath,
isDifferentArray,
generateQuickAppUx,
uglifyJS
uglifyJS,
extnameExpRegOf
} from '../util'

import { parseComponentExportAst, parseAst } from './astProcess'
Expand Down Expand Up @@ -153,13 +154,13 @@ export async function buildSingleComponent (
buildOutputDir = npmOutputDir
}
let outputComponentShowPath = componentShowPath.replace(isComponentFromNodeModules ? NODE_MODULES : sourceDirName, buildConfig.outputDirName || outputDirName)
outputComponentShowPath = outputComponentShowPath.replace(path.extname(outputComponentShowPath), '')
outputComponentShowPath = outputComponentShowPath.replace(extnameExpRegOf(outputComponentShowPath), '')
printLog(processTypeEnum.COMPILE, '组件文件', componentShowPath)
const componentContent = fs.readFileSync(component).toString()
const outputComponentJSPath = component.replace(sourceDirPath, buildConfig.outputDir || buildOutputDir).replace(path.extname(component), outputFilesTypes.SCRIPT)
const outputComponentWXMLPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.TEMPL)
const outputComponentWXSSPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.STYLE)
const outputComponentJSONPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.CONFIG)
const outputComponentJSPath = component.replace(sourceDirPath, buildConfig.outputDir || buildOutputDir).replace(extnameExpRegOf(component), outputFilesTypes.SCRIPT)
const outputComponentWXMLPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.TEMPL)
const outputComponentWXSSPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.STYLE)
const outputComponentJSONPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.CONFIG)

try {
const isTaroComponentRes = isFileToBeTaroComponent(componentContent, component, outputComponentJSPath)
Expand Down Expand Up @@ -247,7 +248,7 @@ export async function buildSingleComponent (
const importTaroSelfComponents = getImportTaroSelfComponents(outputComponentJSPath, res.taroSelfComponents)
const importCustomComponents = new Set(realComponentsPathList.map(item => {
return {
path: promoteRelativePath(path.relative(component, item.path as string)).replace(path.extname(item.path as string), ''),
path: promoteRelativePath(path.relative(component, item.path as string)).replace(extnameExpRegOf(item.path as string), ''),
name: item.name as string
}
}))
Expand Down Expand Up @@ -302,7 +303,7 @@ export async function buildSingleComponent (
} else {
realPath = promoteRelativePath(path.relative(component, (componentPath as string)))
}
depComponent.path = realPath.replace(path.extname(realPath), '')
depComponent.path = realPath.replace(extnameExpRegOf(realPath), '')
}
})
})
Expand Down
5 changes: 3 additions & 2 deletions packages/taro-cli/src/mini/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
isEmptyObject,
getInstalledNpmPkgPath,
recursiveFindNodeModules,
getBabelConfig
getBabelConfig,
extnameExpRegOf
} from '../util'
import { resolveNpmPkgMainPath } from '../util/resolve_npm_files'
import {
Expand Down Expand Up @@ -225,7 +226,7 @@ export function buildUsingComponents (
componentPath = component.path
}
if (component.name) {
usingComponents[component.name] = (componentPath as string).replace(path.extname(componentPath as string), '')
usingComponents[component.name] = (componentPath as string).replace(extnameExpRegOf(componentPath as string), '')
}
}
return Object.assign({}, isComponent ? { component: true } : { usingComponents: {} }, components.length ? {
Expand Down
18 changes: 9 additions & 9 deletions packages/taro-cli/src/mini/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Config as IConfig } from '@tarojs/taro'
import chalk from 'chalk'

import { REG_WXML_IMPORT, processTypeEnum, taroJsFramework } from '../util/constants'
import { isEmptyObject, printLog, resolveScriptPath, copyFileSync } from '../util'
import { isEmptyObject, printLog, resolveScriptPath, copyFileSync, extnameExpRegOf } from '../util'

import { buildDepComponents } from './component'
import { compileDepScripts } from './compileScript'
Expand Down Expand Up @@ -61,31 +61,31 @@ export function transfromNativeComponents (configFile: string, componentConfig:
if (!fs.existsSync(componentJSPath)) {
componentJSPath = resolveScriptPath(path.join(sourceDir, componentPath))
}
const componentJSONPath = componentJSPath.replace(path.extname(componentJSPath), outputFilesTypes.CONFIG)
const componentWXMLPath = componentJSPath.replace(path.extname(componentJSPath), outputFilesTypes.TEMPL)
const componentWXSSPath = componentJSPath.replace(path.extname(componentJSPath), outputFilesTypes.STYLE)
const outputComponentJSPath = componentJSPath.replace(sourceDir, outputDir).replace(path.extname(componentJSPath), outputFilesTypes.SCRIPT)
const componentJSONPath = componentJSPath.replace(extnameExpRegOf(componentJSPath), outputFilesTypes.CONFIG)
const componentWXMLPath = componentJSPath.replace(extnameExpRegOf(componentJSPath), outputFilesTypes.TEMPL)
const componentWXSSPath = componentJSPath.replace(extnameExpRegOf(componentJSPath), outputFilesTypes.STYLE)
const outputComponentJSPath = componentJSPath.replace(sourceDir, outputDir).replace(extnameExpRegOf(componentJSPath), outputFilesTypes.SCRIPT)
if (fs.existsSync(componentJSPath)) {
const componentJSContent = fs.readFileSync(componentJSPath).toString()
if (componentJSContent.indexOf(taroJsFramework) >= 0 && !fs.existsSync(componentWXMLPath)) {
const buildDepComponentsRes = await buildDepComponents([{ path: componentJSPath, name: item, type: 'default'}])
const buildDepComponentsRes = await buildDepComponents([{ path: componentJSPath, name: item, type: 'default' }])
return buildDepComponentsRes
}
await compileDepScripts([componentJSPath], true)
} else {
return printLog(processTypeEnum.ERROR, '编译错误', `原生组件文件 ${componentJSPath} 不存在!`)
}
if (fs.existsSync(componentWXMLPath)) {
const outputComponentWXMLPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.TEMPL)
const outputComponentWXMLPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.TEMPL)
processNativeWxml(componentWXMLPath, null, outputComponentWXMLPath)
}
if (fs.existsSync(componentWXSSPath)) {
const outputComponentWXSSPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.STYLE)
const outputComponentWXSSPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.STYLE)
await compileDepStyles(outputComponentWXSSPath, [componentWXSSPath])
}
if (fs.existsSync(componentJSONPath)) {
const componentJSON = require(componentJSONPath)
const outputComponentJSONPath = outputComponentJSPath.replace(path.extname(outputComponentJSPath), outputFilesTypes.CONFIG)
const outputComponentJSONPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.CONFIG)
copyFileSync(componentJSONPath, outputComponentJSONPath)

// 解决组件循环依赖不断编译爆栈的问题
Expand Down
21 changes: 11 additions & 10 deletions packages/taro-cli/src/mini/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
isDifferentArray,
copyFileSync,
generateQuickAppUx,
uglifyJS
uglifyJS,
extnameExpRegOf
} from '../util'
import { IWxTransformResult, TogglableOptions } from '../util/types'

Expand Down Expand Up @@ -74,16 +75,16 @@ export async function buildSinglePage (page: string) {
return
}
const pageJsContent = fs.readFileSync(pageJs).toString()
const outputPageJSPath = pageJs.replace(sourceDir, outputDir).replace(path.extname(pageJs), outputFilesTypes.SCRIPT)
const outputPageJSPath = pageJs.replace(sourceDir, outputDir).replace(extnameExpRegOf(pageJs), outputFilesTypes.SCRIPT)
const outputPagePath = path.dirname(outputPageJSPath)
const outputPageJSONPath = outputPageJSPath.replace(path.extname(outputPageJSPath), outputFilesTypes.CONFIG)
const outputPageWXMLPath = outputPageJSPath.replace(path.extname(outputPageJSPath), outputFilesTypes.TEMPL)
const outputPageWXSSPath = outputPageJSPath.replace(path.extname(outputPageJSPath), outputFilesTypes.STYLE)
const outputPageJSONPath = outputPageJSPath.replace(extnameExpRegOf(outputPageJSPath), outputFilesTypes.CONFIG)
const outputPageWXMLPath = outputPageJSPath.replace(extnameExpRegOf(outputPageJSPath), outputFilesTypes.TEMPL)
const outputPageWXSSPath = outputPageJSPath.replace(extnameExpRegOf(outputPageJSPath), outputFilesTypes.STYLE)
// 判断是不是小程序原生代码页面
const pageWXMLPath = pageJs.replace(path.extname(pageJs), outputFilesTypes.TEMPL)
const pageWXMLPath = pageJs.replace(extnameExpRegOf(pageJs), outputFilesTypes.TEMPL)
if (fs.existsSync(pageWXMLPath) && pageJsContent.indexOf(taroJsFramework) < 0) {
const pageJSONPath = pageJs.replace(path.extname(pageJs), outputFilesTypes.CONFIG)
const pageWXSSPath = pageJs.replace(path.extname(pageJs), outputFilesTypes.STYLE)
const pageJSONPath = pageJs.replace(extnameExpRegOf(pageJs), outputFilesTypes.CONFIG)
const pageWXSSPath = pageJs.replace(extnameExpRegOf(pageJs), outputFilesTypes.STYLE)
if (fs.existsSync(pageJSONPath)) {
const pageJSON = require(pageJSONPath)
copyFileSync(pageJSONPath, outputPageJSONPath)
Expand Down Expand Up @@ -169,7 +170,7 @@ export async function buildSinglePage (page: string) {
const importTaroSelfComponents = getImportTaroSelfComponents(outputPageJSPath, res.taroSelfComponents)
const importCustomComponents = new Set(realComponentsPathList.map(item => {
return {
path: promoteRelativePath(path.relative(pageJs, item.path as string)).replace(path.extname(item.path as string), ''),
path: promoteRelativePath(path.relative(pageJs, item.path as string)).replace(extnameExpRegOf(item.path as string), ''),
name: item.name as string
}
}))
Expand Down Expand Up @@ -217,7 +218,7 @@ export async function buildSinglePage (page: string) {
} else {
realPath = promoteRelativePath(path.relative(pageJs, componentPath as string))
}
depComponent.path = realPath.replace(path.extname(realPath), '')
depComponent.path = realPath.replace(extnameExpRegOf(realPath), '')
}
})
})
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-cli/src/mini/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
} from '../util/constants'
import {
printLog,
checksum
checksum,
extnameExpRegOf
} from '../util'

import { initCompileStyles, compileDepStyles } from './compileStyle'
Expand Down Expand Up @@ -146,7 +147,7 @@ export function watchFiles () {
})
if (includeStyleJSPath.length) {
includeStyleJSPath.forEach(async item => {
let outputWXSSPath = item.filePath.replace(path.extname(item.filePath), outputFilesTypes.STYLE)
let outputWXSSPath = item.filePath.replace(extnameExpRegOf(item.filePath), outputFilesTypes.STYLE)
let modifySource = outputWXSSPath.replace(appPath + path.sep, '')
modifySource = modifySource.split(path.sep).join('/')
printLog(processTypeEnum.MODIFY, '样式文件', modifySource)
Expand All @@ -170,7 +171,7 @@ export function watchFiles () {
printLog(processTypeEnum.GENERATE, '样式文件', modifyOutput)
})
} else {
let outputWXSSPath = filePath.replace(path.extname(filePath), outputFilesTypes.STYLE)
let outputWXSSPath = filePath.replace(extnameExpRegOf(filePath), outputFilesTypes.STYLE)
let modifySource = outputWXSSPath.replace(appPath + path.sep, '')
modifySource = modifySource.split(path.sep).join('/')
printLog(processTypeEnum.MODIFY, '样式文件', modifySource)
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-cli/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,3 +746,7 @@ export function readDirWithFileTypes (floder: string): FileStat[] {
})
return res
}

export function extnameExpRegOf (filePath: string): RegExp {
return new RegExp(`${path.extname(filePath)}$`)
}

0 comments on commit 58f9829

Please sign in to comment.