diff --git a/packages/taro-helper/src/__tests__/resolve-main-file-path.spec.ts b/packages/taro-helper/src/__tests__/resolve-main-file-path.spec.ts new file mode 100644 index 000000000000..99954181a009 --- /dev/null +++ b/packages/taro-helper/src/__tests__/resolve-main-file-path.spec.ts @@ -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) + }) +}) diff --git a/packages/taro-helper/src/utils.ts b/packages/taro-helper/src/utils.ts index 5c39a2e5a22f..7a21402db618 100644 --- a/packages/taro-helper/src/utils.ts +++ b/packages/taro-helper/src/utils.ts @@ -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++) { @@ -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 } @@ -240,7 +248,7 @@ export function resolveScriptPath (p: string): string { } export function generateEnvList (env: Record): Record { - const res = { } + const res = {} if (env && !isEmptyObject(env)) { for (const key in env) { try { @@ -254,7 +262,7 @@ export function generateEnvList (env: Record): Record } export function generateConstantsList (constants: Record): Record { - const res = { } + const res = {} if (constants && !isEmptyObject(constants)) { for (const key in constants) { if (isPlainObject(constants[key])) { @@ -338,7 +346,7 @@ export function getInstalledNpmPkgVersion (pkgName: string, basedir: string): st return fs.readJSONSync(pkgPath).version } -export const recursiveMerge = (src: Partial, ...args: (Partial | undefined)[]) => { +export const recursiveMerge = (src: Partial, ...args: (Partial | undefined)[]) => { return mergeWith(src, ...args, (value, srcValue) => { const typeValue = typeof value const typeSrcValue = typeof srcValue @@ -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)