Skip to content

Commit

Permalink
fix(cli): 支付宝构建时需要处理安装的 UI 库中的文件路径,将 @ 替换为 _,close #4490
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Sep 23, 2019
1 parent 5237995 commit 304c4af
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
9 changes: 7 additions & 2 deletions packages/taro-cli/src/mini/astProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
traverseObjectNode,
isQuickappPkg,
getBabelConfig,
extnameExpRegOf
extnameExpRegOf,
generateAlipayPath
} from '../util'
import {
convertObjectToAstExpression,
Expand Down Expand Up @@ -103,7 +104,8 @@ function analyzeImportUrl ({
sourceDir,
outputDir,
npmConfig,
projectConfig
projectConfig,
buildAdapter
} = getBuildData()
const publicPath = (projectConfig.weapp || ({} as any)).publicPath
if (value.indexOf('.') === 0) {
Expand Down Expand Up @@ -191,6 +193,9 @@ function analyzeImportUrl ({
} else {
outputVpath = vpath.replace(sourceDir, outputDir)
}
if (buildAdapter === BUILD_TYPES.ALIPAY) {
outputVpath = generateAlipayPath(outputVpath)
}
let relativePath = path.relative(filePath, outputVpath)
if (vpath && vpath !== sourceFilePath) {
if (!fs.existsSync(vpath)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/taro-cli/src/mini/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
copyFileSync,
getBabelConfig,
uglifyJS,
extnameExpRegOf
extnameExpRegOf,
generateAlipayPath
} from '../util'
import {
BUILD_TYPES,
Expand Down Expand Up @@ -59,6 +60,9 @@ export function compileDepScripts (scriptFiles: string[], needUseBabel?: boolean
} else {
outputItem = item.replace(path.join(sourceDir), path.join(outputDir)).replace(extnameExpRegOf(item), '.js')
}
if (buildAdapter === BUILD_TYPES.ALIPAY) {
outputItem = generateAlipayPath(outputItem)
}
const weappConf = Object.assign({}, projectConfig.weapp)
const useCompileConf = Object.assign({}, weappConf.compile)
const compileExclude = (useCompileConf.exclude || []).filter(item => !/(?:\/|^)node_modules(\/|$)/.test(item))
Expand Down
11 changes: 9 additions & 2 deletions packages/taro-cli/src/mini/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
isDifferentArray,
generateQuickAppUx,
uglifyJS,
extnameExpRegOf
extnameExpRegOf,
generateAlipayPath
} from '../util'

import { parseComponentExportAst, parseAst } from './astProcess'
Expand Down Expand Up @@ -144,6 +145,9 @@ export async function buildSingleComponent (
}
let componentShowPath = component.replace(appPath + path.sep, '')
componentShowPath = componentShowPath.split(path.sep).join('/')
if (buildAdapter === BUILD_TYPES.ALIPAY) {
componentShowPath = generateAlipayPath(componentShowPath)
}
let isComponentFromNodeModules = false
let sourceDirPath = sourceDir
let buildOutputDir = outputDir
Expand All @@ -157,7 +161,10 @@ export async function buildSingleComponent (
outputComponentShowPath = outputComponentShowPath.replace(extnameExpRegOf(outputComponentShowPath), '')
printLog(processTypeEnum.COMPILE, '组件文件', componentShowPath)
const componentContent = fs.readFileSync(component).toString()
const outputComponentJSPath = component.replace(sourceDirPath, buildConfig.outputDir || buildOutputDir).replace(extnameExpRegOf(component), outputFilesTypes.SCRIPT)
let outputComponentJSPath = component.replace(sourceDirPath, buildConfig.outputDir || buildOutputDir).replace(extnameExpRegOf(component), outputFilesTypes.SCRIPT)
if (buildAdapter === BUILD_TYPES.ALIPAY) {
outputComponentJSPath = generateAlipayPath(outputComponentJSPath)
}
const outputComponentWXMLPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.TEMPL)
const outputComponentWXSSPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.STYLE)
const outputComponentJSONPath = outputComponentJSPath.replace(extnameExpRegOf(outputComponentJSPath), outputFilesTypes.CONFIG)
Expand Down
7 changes: 6 additions & 1 deletion 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,
recursiveFindNodeModules,
getBabelConfig,
extnameExpRegOf
extnameExpRegOf,
generateAlipayPath
} from '../util'
import { resolveNpmPkgMainPath } from '../util/resolve_npm_files'
import { resolveNpmSync } from '../util/npm'
Expand Down Expand Up @@ -213,6 +214,7 @@ export function buildUsingComponents (
components: IComponentObj[],
isComponent?: boolean
): IOption {
const { buildAdapter } = getBuildData()
const usingComponents = Object.create(null)
const pathAlias = BuildData.projectConfig.alias || {}
for (const component of components) {
Expand All @@ -226,6 +228,9 @@ export function buildUsingComponents (
} else {
componentPath = component.path
}
if (buildAdapter === BUILD_TYPES.ALIPAY) {
componentPath = generateAlipayPath(componentPath)
}
if (component.name) {
usingComponents[component.name] = (componentPath as string).replace(extnameExpRegOf(componentPath as string), '')
}
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 @@ -819,3 +819,7 @@ export function readDirWithFileTypes (floder: string): FileStat[] {
export function extnameExpRegOf (filePath: string): RegExp {
return new RegExp(`${path.extname(filePath)}$`)
}

export function generateAlipayPath (filePath) {
return filePath.replace(/@/g, '_')
}
4 changes: 2 additions & 2 deletions packages/taro-cli/src/util/npmExact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path'
import { resolveNpmFilesPath } from './resolve_npm_files'
import { INpmConfig, TogglableOptions, ITaroManifestConfig } from './types'
import { BUILD_TYPES, REG_STYLE, NODE_MODULES, REG_FONT, REG_MEDIA, REG_IMAGE } from './constants'
import { promoteRelativePath, recursiveFindNodeModules } from './index'
import { promoteRelativePath, recursiveFindNodeModules, generateAlipayPath } from './index'

interface IArgs {
npmName: string,
Expand Down Expand Up @@ -76,7 +76,7 @@ export function getExactedNpmFilePath ({
outputNpmPath = npmInfoMainPath.replace(nodeModulesPath, npmOutputDir)
}
if (buildAdapter === BUILD_TYPES.ALIPAY) {
outputNpmPath = outputNpmPath.replace(/@/g, '_')
outputNpmPath = generateAlipayPath(outputNpmPath)
}
const relativePath = path.relative(filePath, outputNpmPath)
return promoteRelativePath(relativePath)
Expand Down
7 changes: 4 additions & 3 deletions packages/taro-cli/src/util/resolve_npm_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
printLog,
recursiveFindNodeModules,
generateEnvList,
isQuickappPkg
isQuickappPkg,
generateAlipayPath
} from './index'

import {
Expand Down Expand Up @@ -184,7 +185,7 @@ function analyzeImportUrl ({
let relativeRequirePath = promoteRelativePath(path.relative(filePath, res.main))
relativeRequirePath = relativeRequirePath.replace(/node_modules/g, npmConfig.name)
if (buildAdapter === BUILD_TYPES.ALIPAY) {
relativeRequirePath = relativeRequirePath.replace(/@/g, '_')
relativeRequirePath = generateAlipayPath(relativeRequirePath)
}
source.value = relativeRequirePath
}
Expand Down Expand Up @@ -403,7 +404,7 @@ async function recursiveRequire ({
let fileContent = fs.readFileSync(filePath).toString()
let outputNpmPath = filePath.replace(rootNpm, npmOutputDir).replace(/node_modules/g, npmConfig.name)
if (buildAdapter === BUILD_TYPES.ALIPAY) {
outputNpmPath = outputNpmPath.replace(/@/g, '_')
outputNpmPath = generateAlipayPath(outputNpmPath)
}
if (REG_STYLE.test(path.basename(filePath))) {
return
Expand Down

0 comments on commit 304c4af

Please sign in to comment.