Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use default pageconfig when there are multi-terminal pages but … #14071

Merged
merged 4 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/taro-helper/src/__tests__/resolve-main-file-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as path from 'path'

import { resolveMainFilePath } from '../utils'

describe('resolveMainFilePath', () => {
it('should return the same path if it starts with "pages/" or is "app.config"', () => {
const input1 = 'pages/home/index.config'
const input2 = 'app.config'
expect(resolveMainFilePath(input1)).toBe(input1)
expect(resolveMainFilePath(input2)).toBe(input2)
})

it('should return the path with the file extension if the file exists', () => {
const configPath = path.join(__dirname, './__mocks__/app.config.ts')
const parsedPath = path.parse(configPath)
expect(resolveMainFilePath(path.join(parsedPath.dir, parsedPath.name))).toBe(configPath)
})

it('存在多端页面但是对应的多端页面配置不存在时,使用该页面默认配置', () => {
process.env.TARO_ENV = 'weapp'
const configPath = path.join(__dirname, './__mocks__/app.config.ts')
const configEnvPath = path.join(__dirname, './__mocks__/app.weapp.config.ts')
const parsedPath = path.parse(configEnvPath)
expect(resolveMainFilePath(path.join(parsedPath.dir, parsedPath.name))).toBe(configPath)
})
})
16 changes: 12 additions & 4 deletions packages/taro-helper/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ export function isEmptyObject (obj: any): boolean {
}

export function resolveMainFilePath (p: string, extArrs = SCRIPT_EXT): string {
if (p.startsWith('pages/') || p === 'app.config') {
return p
}
const realPath = p
const taroEnv = process.env.TARO_ENV
for (let i = 0; i < extArrs.length; i++) {
Expand All @@ -232,6 +235,11 @@ export function resolveMainFilePath (p: string, extArrs = SCRIPT_EXT): string {
return `${p}${path.sep}index${item}`
}
}
// 存在多端页面但是对应的多端页面配置不存在时,使用该页面默认配置
if (taroEnv && path.parse(p).base.endsWith(`.${taroEnv}.config`)) {
const idx = p.lastIndexOf(`.${taroEnv}.config`)
return resolveMainFilePath(p.slice(0, idx) + '.config')
}
return realPath
}

Expand All @@ -240,7 +248,7 @@ export function resolveScriptPath (p: string): string {
}

export function generateEnvList (env: Record<string, any>): Record<string, any> {
const res = { }
const res = {}
if (env && !isEmptyObject(env)) {
for (const key in env) {
try {
Expand All @@ -254,7 +262,7 @@ export function generateEnvList (env: Record<string, any>): Record<string, any>
}

export function generateConstantsList (constants: Record<string, any>): Record<string, any> {
const res = { }
const res = {}
if (constants && !isEmptyObject(constants)) {
for (const key in constants) {
if (isPlainObject(constants[key])) {
Expand Down Expand Up @@ -338,7 +346,7 @@ export function getInstalledNpmPkgVersion (pkgName: string, basedir: string): st
return fs.readJSONSync(pkgPath).version
}

export const recursiveMerge = <T = any>(src: Partial<T>, ...args: (Partial<T> | undefined)[]) => {
export const recursiveMerge = <T = any> (src: Partial<T>, ...args: (Partial<T> | undefined)[]) => {
return mergeWith(src, ...args, (value, srcValue) => {
const typeValue = typeof value
const typeSrcValue = typeof srcValue
Expand Down Expand Up @@ -430,7 +438,7 @@ export function unzip (zipPath) {
fileNameArr.shift()
const fileName = fileNameArr.join('/')
const writeStream = fs.createWriteStream(path.join(path.dirname(zipPath), fileName))
writeStream.on('close', () => {})
writeStream.on('close', () => { })
readStream
.pipe(filter)
.pipe(writeStream)
Expand Down