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

fix: 增加 modifyAppConfig 钩子,修复引用抖音小程序内置页面的问题, close #15388 #15397

Merged
merged 8 commits into from
Apr 22, 2024
8 changes: 8 additions & 0 deletions packages/taro-cli/src/presets/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@
isBuildNativeComp,
withoutBuild,
newBlended,
async modifyAppConfig (appConfig) {
await ctx.applyPlugins({

Check warning on line 118 in packages/taro-cli/src/presets/commands/build.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-cli/src/presets/commands/build.ts#L117-L118

Added lines #L117 - L118 were not covered by tests
name: hooks.MODIFY_APP_CONFIG,
opts: {
appConfig
}
})
},
async modifyWebpackChain (chain, webpack, data) {
await ctx.applyPlugins({
name: hooks.MODIFY_WEBPACK_CHAIN,
Expand Down
1 change: 1 addition & 0 deletions packages/taro-cli/src/presets/constant/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const MODIFY_APP_CONFIG = 'modifyAppConfig'
export const MODIFY_WEBPACK_CHAIN = 'modifyWebpackChain'
export const MODIFY_BUILD_ASSETS = 'modifyBuildAssets'
export const MODIFY_MINI_CONFIGS = 'modifyMiniConfigs'
Expand Down
1 change: 1 addition & 0 deletions packages/taro-cli/src/presets/hooks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { IPluginContext } from '@tarojs/service'

export default (ctx: IPluginContext) => {
[
hooks.MODIFY_APP_CONFIG,
hooks.MODIFY_WEBPACK_CHAIN,
hooks.MODIFY_BUILD_ASSETS,
hooks.MODIFY_MINI_CONFIGS,
Expand Down
6 changes: 6 additions & 0 deletions packages/taro-service/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AppConfig } from '@tarojs/taro'

import type helper from '@tarojs/helper'
import type { Func, IProjectConfig } from '@tarojs/taro/types/compile'
import type { IModifyChainData } from '@tarojs/taro/types/compile/hooks'
Expand Down Expand Up @@ -133,6 +135,10 @@ export declare interface IPluginContext {
* 修改编译过程中的页面组件配置
*/
onCompilerMake: (fn: (args: { compilation: Webpack.Compilation, compiler: Webpack.Compiler, plugin: any }) => void) => void
/**
* 编译前,修改 App 配置
*/
modifyAppConfig: (fn: (args: { appConfig: AppConfig }) => void) => void
/**
* 编译中修改 webpack 配置,在这个钩子中,你可以对 webpackChain 作出想要的调整,等同于配置 [`webpackChain`](./config-detail.md#miniwebpackchain)
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-tt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"url": "https://github.com/NervJS/taro/issues"
},
"dependencies": {
"@tarojs/service": "workspace:*"
"@tarojs/service": "workspace:*",
"webpack-sources": "3.2.3"
},
"devDependencies": {
"@tarojs/components": "workspace:*",
Expand Down
25 changes: 25 additions & 0 deletions packages/taro-tt/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { RawSource } from 'webpack-sources'

import TT from './program'

import type { IPluginContext } from '@tarojs/service'
Expand All @@ -10,6 +12,29 @@ export default (ctx: IPluginContext) => {
name: 'tt',
useConfigName: 'mini',
async fn ({ config }) {
const extPages: string[] = []
await ctx.modifyAppConfig(({ appConfig }) => {
if (appConfig.pages) {
appConfig.pages = appConfig.pages.map(page => {
if (!page.startsWith('ext://')) {
return page
}
if (!extPages.includes(page)) {
extPages.push(page)
}
}).filter(Boolean) as string[]
}
})
if (extPages.length > 0) {
await ctx.modifyBuildAssets(({ assets }) => {
const appJSON = assets['app.json']
if (appJSON) {
const appJSONContent = JSON.parse(appJSON.source())
appJSONContent.pages = appJSONContent.pages.concat(extPages)
assets['app.json'] = new RawSource(JSON.stringify(appJSONContent, null, 2))
}
})
}
const program = new TT(ctx, config)
await program.start()
}
Expand Down
3 changes: 3 additions & 0 deletions packages/taro-webpack5-prebundle/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IPrebundleParam {
isBuildPlugin?: boolean
alias?: Record<string, any>
defineConstants?: Record<string, any>
modifyAppConfig?: (appConfig: any) => Promise<any>
}

export default class TaroPrebundle {
Expand Down Expand Up @@ -67,6 +68,7 @@ export default class TaroPrebundle {
isBuildPlugin,
alias,
defineConstants,
modifyAppConfig,
} = this.params
let chunkFilename = chain.output.get('chunkFilename') ?? `${chunkDirectory}/[name].js`
chunkFilename = chunkFilename.replace(/\[([a-z]*hash)[^[\]\s]*\]/ig, '_$1_')
Expand All @@ -89,6 +91,7 @@ export default class TaroPrebundle {
isBuildPlugin,
alias,
defineConstants,
modifyAppConfig,
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/taro-webpack5-prebundle/src/mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class MiniPrebundle extends BasePrebundle<IMiniPrebundleConfig> {
* TODO:
* - 目前只处理了 Page entry,例如原生小程序组件 js entry 等并没有处理
*/
const entries: string[] = this.getEntries(this.entryPath)
const entries: string[] = await this.getEntries(this.entryPath)
// plugin-platform 等插件的 runtime 文件入口
const runtimePath = typeof this.config.runtimePath === 'string' ? [this.config.runtimePath] : this.config.runtimePath || []
const { include = [], exclude = [] } = this.option
Expand Down
7 changes: 5 additions & 2 deletions packages/taro-webpack5-prebundle/src/prebundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface IPrebundleConfig {
isBuildPlugin?: boolean
alias?: Record<string, any>
defineConstants?: Record<string, any>
modifyAppConfig?: (appConfig: any) => Promise<any>
}

type TMode = 'production' | 'development' | 'none'
Expand Down Expand Up @@ -132,14 +133,16 @@ export default class BasePrebundle<T extends IPrebundleConfig = IPrebundleConfig
}

/** 找出所有 webpack entry */
getEntries (appJsPath: string) {
async getEntries (appJsPath: string) {
const { appPath, sourceRoot } = this.config
const entries: string[] = this.parseEntries(this.config.entry)

const appConfigPath = resolveMainFilePath(`${appJsPath.replace(path.extname(appJsPath), '')}.config`)
if (fs.existsSync(appConfigPath)) {
const appConfig = readConfig(appConfigPath, this.config)

if (typeof this.config.modifyAppConfig === 'function') {
await this.config.modifyAppConfig(appConfig)
}
appConfig.pages.forEach((page: string) => {
const pageJsPath = resolveMainFilePath(path.join(appPath, sourceRoot, page))
entries.push(pageJsPath)
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-webpack5-prebundle/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class WebPrebundle extends BasePrebundle<IWebPrebundleConfig> {
this.isUseCache = true

/** 扫描出所有的 node_modules 依赖 */
const entries: string[] = this.getEntries(this.entryPath)
const entries: string[] = await this.getEntries(this.entryPath)
const { include = [], exclude = [] } = this.option
const idx = exclude.findIndex(e => e === '@tarojs/runtime')
if (idx >= 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/taro-webpack5-runner/src/index.h5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async function build (appPath: string, rawConfig: H5BuildConfig):
publicPath,
alias: combination.config.alias,
defineConstants: combination.config.defineConstants,
modifyAppConfig: combination.config.modifyAppConfig
})
try {
await prebundle.run(combination.getPrebundleOptions())
Expand Down
1 change: 1 addition & 0 deletions packages/taro-webpack5-runner/src/index.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default async function build (appPath: string, rawConfig: MiniBuildConfig
isBuildPlugin: combination.isBuildPlugin,
alias: combination.config.alias,
defineConstants: combination.config.defineConstants,
modifyAppConfig: combination.config.modifyAppConfig
})
try {
await prebundle.run(combination.getPrebundleOptions())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default class BuildNativePlugin extends MiniPlugin {
this.addLoadChunksPlugin(compiler)
}

run (compiler: Compiler) {
this.appConfig = this.getAppConfig()
async run (compiler: Compiler) {
this.appConfig = await this.getAppConfig()
this.getPages()
this.getPagesConfig()
this.getConfigFiles(compiler)
Expand Down
10 changes: 7 additions & 3 deletions packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,12 @@ export default class TaroMiniPlugin {
* 分析 app 入口文件,搜集页面、组件信息,
* 往 this.dependencies 中添加资源模块
*/
run (compiler: Compiler) {
async run (compiler: Compiler) {
if (this.options.isBuildPlugin) {
this.getPluginFiles()
this.getConfigFiles(compiler)
} else {
this.appConfig = this.getAppConfig()
this.appConfig = await this.getAppConfig()
this.getPages()
this.getPagesConfig()
this.getDarkMode()
Expand Down Expand Up @@ -596,7 +596,7 @@ export default class TaroMiniPlugin {
* 获取 app config 配置内容
* @returns app config 配置内容
*/
getAppConfig (): AppConfig {
async getAppConfig (): Promise<AppConfig> {
const appName = path.basename(this.appEntry).replace(path.extname(this.appEntry), '')
this.compileFile({
name: appName,
Expand All @@ -608,6 +608,10 @@ export default class TaroMiniPlugin {
if (isEmptyObject(appConfig)) {
throw new Error('缺少 app 全局配置文件,请检查!')
}
const { modifyAppConfig } = this.options.combination.config
if (typeof modifyAppConfig === 'function') {
await modifyAppConfig(appConfig)
}
return appConfig as AppConfig
}

Expand Down
3 changes: 3 additions & 0 deletions packages/taro/types/compile/config/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ICopyOptions, IOption, ISassOptions, TogglableOptions } from './ut
import type { IH5Config } from './h5'
import type { IMiniAppConfig } from './mini'
import { IRNConfig } from './rn'
import { AppConfig } from '../..'

export type PluginItem<T = object> = string | [string, T] | [string, () => T | Promise<T>]

Expand Down Expand Up @@ -176,6 +177,8 @@ export interface IProjectBaseConfig {

onWebpackChainReady?: (webpackChain: Chain) => Promise<any>

modifyAppConfig?: (appConfig: AppConfig) => Promise<any>

/**
* 编译中修改 webpack 配置,在这个钩子中,你可以对 webpackChain 作出想要的调整,等同于配置 [`webpackChain`](./config-detail#miniwebpackchain)
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/taro/types/taro.config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ declare module './index' {
decodeQuery?: 'disable'
}

interface AppConfig {
export interface AppConfig {
/** 小程序默认启动首页,未指定 entryPagePath 时,数组的第一项代表小程序的初始页面(首页)。 */
entryPagePath?: string
/** 接受一个数组,每一项都是字符串,来指定小程序由哪些页面组成,数组的第一项代表小程序的初始页面 */
Expand Down
11 changes: 4 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading