From d89275c66da85834d3d154b3e8c435b91565109e Mon Sep 17 00:00:00 2001 From: ZakaryCode Date: Mon, 22 Jan 2024 11:26:10 +0800 Subject: [PATCH 1/4] feat(runner): support no build --- packages/taro-cli/src/cli.ts | 11 ++++++++--- packages/taro-cli/src/presets/commands/build.ts | 7 +++++-- packages/taro-mini-runner/src/index.ts | 2 ++ packages/taro-mini-runner/src/utils/types.ts | 1 + packages/taro-webpack-runner/src/index.ts | 4 ++++ packages/taro-webpack-runner/src/utils/types.ts | 1 + packages/taro-webpack5-runner/src/index.h5.ts | 4 ++++ packages/taro-webpack5-runner/src/index.mini.ts | 6 ++++-- packages/taro-webpack5-runner/src/utils/types.ts | 1 + 9 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/taro-cli/src/cli.ts b/packages/taro-cli/src/cli.ts index 09236774ad4c..b672ab3827a3 100644 --- a/packages/taro-cli/src/cli.ts +++ b/packages/taro-cli/src/cli.ts @@ -33,7 +33,10 @@ export default class CLI { assetsDest: ['assets-dest'], // specially for rn, Directory name where to store assets referenced in the bundle. envPrefix: ['env-prefix'], }, - boolean: ['version', 'help', 'disable-global-config'] + boolean: ['version', 'help', 'disable-global-config'], + default: { + build: true, + }, }) const _ = args._ const command = _[0] @@ -166,10 +169,12 @@ export default class CLI { platform, plugin, isWatch: Boolean(args.watch), - // 是否把 Taro 组件编译为原生自定义组件 + // Note: 是否把 Taro 组件编译为原生自定义组件 isBuildNativeComp: _[1] === 'native-components', - // 新的混合编译模式,支持把组件单独编译为原生组件 + // Note: 新的混合编译模式,支持把组件单独编译为原生组件 newBlended: Boolean(args['new-blended']), + // Note: 是否禁用编译 + withoutBuild: !args.build, port: args.port, env: args.env, deviceType: args.platform, diff --git a/packages/taro-cli/src/presets/commands/build.ts b/packages/taro-cli/src/presets/commands/build.ts index f8dbf6556516..37c0430a881b 100644 --- a/packages/taro-cli/src/presets/commands/build.ts +++ b/packages/taro-cli/src/presets/commands/build.ts @@ -16,6 +16,7 @@ export default (ctx: IPluginContext) => { '--env [env]': 'Value for process.env.NODE_ENV', '--mode [mode]': 'Value of dotenv extname', '-p, --port [port]': 'Specified port', + '--no-build': 'Do not build project', '--platform': '[rn] Specific React-Native build target: android / ios, android is default value', '--reset-cache': '[rn] Clear transform cache', '--public-path': '[rn] Assets public path', @@ -35,15 +36,16 @@ export default (ctx: IPluginContext) => { 'taro build --type weapp --watch', 'taro build --type weapp --env production', 'taro build --type weapp --blended', + 'taro build --type weapp --no-build', 'taro build native-components --type weapp', 'taro build --type weapp --new-blended', 'taro build --plugin weapp --watch', 'taro build --plugin weapp', - 'taro build --type weapp --mode prepare --env-prefix TARO_APP_' + 'taro build --type weapp --mode prepare --env-prefix TARO_APP_', ], async fn (opts) { const { options, config, _ } = opts - const { platform, isWatch, blended, newBlended } = options + const { platform, isWatch, blended, newBlended, withoutBuild } = options const { fs, chalk, PROJECT_CONFIG } = ctx.helper const { outputPath, configPath } = ctx.paths @@ -110,6 +112,7 @@ export default (ctx: IPluginContext) => { mode: isProduction ? 'production' : 'development', blended, isBuildNativeComp, + withoutBuild, newBlended, async modifyWebpackChain (chain, webpack, data) { await ctx.applyPlugins({ diff --git a/packages/taro-mini-runner/src/index.ts b/packages/taro-mini-runner/src/index.ts index 6835646c9a29..6d9ea72f484e 100644 --- a/packages/taro-mini-runner/src/index.ts +++ b/packages/taro-mini-runner/src/index.ts @@ -52,6 +52,8 @@ export default async function build (appPath: string, config: IBuildConfig): Pro const webpackConfig: webpack.Configuration = webpackChain.toConfig() return new Promise((resolve, reject) => { + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) const onBuildFinish = newConfig.onBuildFinish let prerender: Prerender diff --git a/packages/taro-mini-runner/src/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index d3f1b395eb61..ef8c89b2d8cb 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -48,6 +48,7 @@ export interface IBuildConfig extends IProjectBaseConfig, IMiniAppConfig { isBuildQuickapp: boolean isSupportRecursive: boolean isSupportXS: boolean + withoutBuild?: boolean mode: 'production' | 'development' modifyComponentConfig: Func nodeModulesPath: string diff --git a/packages/taro-webpack-runner/src/index.ts b/packages/taro-webpack-runner/src/index.ts index a09a99651996..0646ac72df11 100644 --- a/packages/taro-webpack-runner/src/index.ts +++ b/packages/taro-webpack-runner/src/index.ts @@ -45,6 +45,8 @@ const buildProd = async (appPath: string, config: BuildConfig, appHelper: AppHel } const errorLevel = typeof config.compiler !== 'string' && config.compiler?.errorLevel || 0 const webpackConfig = webpackChain.toConfig() + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) const onBuildFinish = config.onBuildFinish compiler.hooks.emit.tapAsync('taroBuildDone', async (compilation, callback) => { @@ -210,6 +212,8 @@ const buildDev = async (appPath: string, config: BuildConfig, appHelper: AppHelp const webpackConfig = webpackChain.toConfig() WebpackDevServer.addDevServerEntrypoints(webpackConfig, devServerOptions) + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) as webpack.Compiler bindDevLogger(compiler, devUrl) const server = new WebpackDevServer(compiler, devServerOptions) diff --git a/packages/taro-webpack-runner/src/utils/types.ts b/packages/taro-webpack-runner/src/utils/types.ts index dff2152314d7..7e4d454c3773 100644 --- a/packages/taro-webpack-runner/src/utils/types.ts +++ b/packages/taro-webpack-runner/src/utils/types.ts @@ -19,6 +19,7 @@ export interface BuildConfig extends IProjectBaseConfig, IH5Config { runtimePath?: string | string[] /** special mode */ isBuildNativeComp?: boolean + withoutBuild?: boolean /** hooks */ onCompilerMake: (compilation) => Promise onParseCreateElement: (nodeName, componentConfig) => Promise diff --git a/packages/taro-webpack5-runner/src/index.h5.ts b/packages/taro-webpack5-runner/src/index.h5.ts index 5655a124c91a..564c7f0186c1 100644 --- a/packages/taro-webpack5-runner/src/index.h5.ts +++ b/packages/taro-webpack5-runner/src/index.h5.ts @@ -51,6 +51,8 @@ export default async function build (appPath: string, rawConfig: H5BuildConfig): try { if (!config.isWatch) { + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) prebundle?.postCompilerStart(compiler) compiler.hooks.emit.tapAsync('taroBuildDone', async (compilation, callback) => { @@ -98,6 +100,8 @@ export default async function build (appPath: string, rawConfig: H5BuildConfig): webpackConfig.devServer.open = devUrl } + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) const server = new WebpackDevServer(webpackConfig.devServer, compiler) prebundle?.postCompilerStart(compiler) diff --git a/packages/taro-webpack5-runner/src/index.mini.ts b/packages/taro-webpack5-runner/src/index.mini.ts index 9f24653ae62c..ce5eded19998 100644 --- a/packages/taro-webpack5-runner/src/index.mini.ts +++ b/packages/taro-webpack5-runner/src/index.mini.ts @@ -10,7 +10,7 @@ import { MiniCombination } from './webpack/MiniCombination' import type { Stats } from 'webpack' import type { MiniBuildConfig } from './utils/types' -export default async function build (appPath: string, rawConfig: MiniBuildConfig): Promise { +export default async function build (appPath: string, rawConfig: MiniBuildConfig): Promise { const combination = new MiniCombination(appPath, rawConfig) await combination.make() @@ -37,7 +37,9 @@ export default async function build (appPath: string, rawConfig: MiniBuildConfig const webpackConfig = combination.chain.toConfig() const config = combination.config - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { + if (config.withoutBuild) return + const compiler = webpack(webpackConfig) const onBuildFinish = config.onBuildFinish let prerender: Prerender diff --git a/packages/taro-webpack5-runner/src/utils/types.ts b/packages/taro-webpack5-runner/src/utils/types.ts index 32201ffd01f5..4f1140e6f626 100644 --- a/packages/taro-webpack5-runner/src/utils/types.ts +++ b/packages/taro-webpack5-runner/src/utils/types.ts @@ -42,6 +42,7 @@ export interface CommonBuildConfig extends IProjectBaseConfig { /** special mode */ isBuildNativeComp?: boolean newBlended?: boolean + withoutBuild?: boolean /** hooks */ onCompilerMake: (compilation: Webpack.Compilation, compiler: Webpack.Compiler, plugin: any) => Promise onParseCreateElement: (nodeName, componentConfig) => Promise From a11090d9b3708003bb8fea0c58f6edfd5f3ee8c4 Mon Sep 17 00:00:00 2001 From: ZakaryCode Date: Mon, 22 Jan 2024 11:26:59 +0800 Subject: [PATCH 2/4] fix(h5): support modifyComponentConfig --- packages/taro-webpack-runner/src/config/dev.conf.ts | 4 ++++ packages/taro-webpack-runner/src/config/prod.conf.ts | 4 ++++ packages/taro-webpack-runner/src/plugins/H5Plugin.ts | 5 ++++- packages/taro-webpack-runner/src/utils/types.ts | 4 +++- packages/taro-webpack5-runner/src/plugins/H5Plugin.ts | 5 ++++- packages/taro-webpack5-runner/src/utils/types.ts | 3 +-- packages/taro-webpack5-runner/src/webpack/H5Combination.ts | 7 ++++++- 7 files changed, 26 insertions(+), 6 deletions(-) diff --git a/packages/taro-webpack-runner/src/config/dev.conf.ts b/packages/taro-webpack-runner/src/config/dev.conf.ts index 9d325afa93e2..fc4708dc49ed 100644 --- a/packages/taro-webpack-runner/src/config/dev.conf.ts +++ b/packages/taro-webpack-runner/src/config/dev.conf.ts @@ -15,6 +15,7 @@ import { parseModule, processEnvOption } from '../utils/chain' +import { componentConfig } from '../utils/component' import getBaseChain from './base.conf' import type { BuildConfig } from '../utils/types' @@ -167,6 +168,9 @@ export default function (appPath: string, config: Partial, appHelpe publicPath: ['', 'auto'].includes(publicPath) ? publicPath : addTrailingSlash(publicPath), chunkDirectory }, output]) + + config.modifyComponentConfig?.(componentConfig, config) + if (config.isBuildNativeComp) { // Note: 当开发者没有配置时,优先使用 module 导出组件 webpackOutput.libraryTarget ||= 'commonjs' diff --git a/packages/taro-webpack-runner/src/config/prod.conf.ts b/packages/taro-webpack-runner/src/config/prod.conf.ts index e50473c39f3e..ac9b524aacef 100644 --- a/packages/taro-webpack-runner/src/config/prod.conf.ts +++ b/packages/taro-webpack-runner/src/config/prod.conf.ts @@ -17,6 +17,7 @@ import { parseModule, processEnvOption } from '../utils/chain' +import { componentConfig } from '../utils/component' import getBaseChain from './base.conf' import type { BuildConfig } from '../utils/types' @@ -190,6 +191,9 @@ export default function (appPath: string, config: Partial, appHelpe publicPath: ['', 'auto'].includes(publicPath) ? publicPath : addTrailingSlash(publicPath), chunkDirectory }, output]) + + config.modifyComponentConfig?.(componentConfig, config) + if (config.isBuildNativeComp) { // Note: 当开发者没有配置时,优先使用 module 导出组件 webpackOutput.libraryTarget ||= 'commonjs' diff --git a/packages/taro-webpack-runner/src/plugins/H5Plugin.ts b/packages/taro-webpack-runner/src/plugins/H5Plugin.ts index dc61428ffaa0..a144befe4c6c 100644 --- a/packages/taro-webpack-runner/src/plugins/H5Plugin.ts +++ b/packages/taro-webpack-runner/src/plugins/H5Plugin.ts @@ -6,6 +6,7 @@ import { defaults } from 'lodash' import * as path from 'path' import { AppHelper } from '../utils' +import { componentConfig } from '../utils/component' import TaroComponentsExportsPlugin from './TaroComponentsExportsPlugin' import type { Func } from '@tarojs/taro/types/compile' @@ -131,7 +132,9 @@ export default class TaroH5Plugin { }) }) - new TaroComponentsExportsPlugin(this.options).apply(compiler) + if (!componentConfig.includeAll) { + new TaroComponentsExportsPlugin(this.options).apply(compiler) + } } run () { diff --git a/packages/taro-webpack-runner/src/utils/types.ts b/packages/taro-webpack-runner/src/utils/types.ts index 7e4d454c3773..16e926f61973 100644 --- a/packages/taro-webpack-runner/src/utils/types.ts +++ b/packages/taro-webpack-runner/src/utils/types.ts @@ -1,6 +1,7 @@ -import { IH5Config, IProjectBaseConfig } from '@tarojs/taro/types/compile' import * as webpack from 'webpack' +import type { Func, IH5Config, IProjectBaseConfig } from '@tarojs/taro/types/compile' + type FunctionLikeCustomWebpackConfig = (webpackConfig: webpack.Configuration, webpack) => webpack.Configuration export type CustomWebpackConfig = FunctionLikeCustomWebpackConfig | webpack.Configuration @@ -23,4 +24,5 @@ export interface BuildConfig extends IProjectBaseConfig, IH5Config { /** hooks */ onCompilerMake: (compilation) => Promise onParseCreateElement: (nodeName, componentConfig) => Promise + modifyComponentConfig: Func } diff --git a/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts b/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts index 6cf2a25b7129..0d1e64aa8448 100644 --- a/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts +++ b/packages/taro-webpack5-runner/src/plugins/H5Plugin.ts @@ -3,6 +3,7 @@ import { defaults } from 'lodash' import path from 'path' import AppHelper from '../utils/app' +import { componentConfig } from '../utils/component' import TaroComponentsExportsPlugin from './TaroComponentsExportsPlugin' import type { Func } from '@tarojs/taro/types/compile' @@ -149,7 +150,9 @@ export default class TaroH5Plugin { }) }) - new TaroComponentsExportsPlugin(this.options).apply(compiler) + if (!componentConfig.includeAll) { + new TaroComponentsExportsPlugin(this.options).apply(compiler) + } } run () { diff --git a/packages/taro-webpack5-runner/src/utils/types.ts b/packages/taro-webpack5-runner/src/utils/types.ts index 4f1140e6f626..00f86b79f872 100644 --- a/packages/taro-webpack5-runner/src/utils/types.ts +++ b/packages/taro-webpack5-runner/src/utils/types.ts @@ -46,6 +46,7 @@ export interface CommonBuildConfig extends IProjectBaseConfig { /** hooks */ onCompilerMake: (compilation: Webpack.Compilation, compiler: Webpack.Compiler, plugin: any) => Promise onParseCreateElement: (nodeName, componentConfig) => Promise + modifyComponentConfig: (componentConfig: IComponentConfig, config: Partial) => Promise } export interface MiniBuildConfig extends CommonBuildConfig, IMiniAppConfig { @@ -62,8 +63,6 @@ export interface MiniBuildConfig extends CommonBuildConfig, IMiniAppConfig { taroComponentsPath?: string blended?: boolean hot?: boolean - /** hooks */ - modifyComponentConfig: (componentConfig: IComponentConfig, config: Partial) => Promise } export interface H5BuildConfig extends CommonBuildConfig, IH5Config { diff --git a/packages/taro-webpack5-runner/src/webpack/H5Combination.ts b/packages/taro-webpack5-runner/src/webpack/H5Combination.ts index 10f36ac57720..49c884b38255 100644 --- a/packages/taro-webpack5-runner/src/webpack/H5Combination.ts +++ b/packages/taro-webpack5-runner/src/webpack/H5Combination.ts @@ -1,5 +1,6 @@ import { parsePublicPath } from '../utils' import AppHelper from '../utils/app' +import { componentConfig } from '../utils/component' import { Combination } from './Combination' import { H5BaseConfig } from './H5BaseConfig' import { H5WebpackModule } from './H5WebpackModule' @@ -33,7 +34,9 @@ export class H5Combination extends Combination { alias = {}, defineConstants = {}, router, - frameworkExts + frameworkExts, + /** hooks */ + modifyComponentConfig, } = config const externals: Configuration['externals'] = [] const routerMode = router?.mode || 'hash' @@ -46,6 +49,8 @@ export class H5Combination extends Combination { defineConstants, }) + modifyComponentConfig?.(componentConfig, config) + if (this.isBuildNativeComp) { delete entry[entryFileName] this.appHelper.compsConfigList.forEach((comp, index) => { From fac875830bb9d71fa95b8e91d0ca1e2c21cf00eb Mon Sep 17 00:00:00 2001 From: ZakaryCode Date: Mon, 22 Jan 2024 15:25:09 +0800 Subject: [PATCH 3/4] test: update cli have been called --- packages/taro-cli/src/__tests__/cli.spec.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/taro-cli/src/__tests__/cli.spec.ts b/packages/taro-cli/src/__tests__/cli.spec.ts index 655edef31af7..74e2404df7f0 100644 --- a/packages/taro-cli/src/__tests__/cli.spec.ts +++ b/packages/taro-cli/src/__tests__/cli.spec.ts @@ -42,6 +42,7 @@ describe('inspect', () => { platform: undefined, publicPath: undefined, isWatch: false, + withoutBuild: false, env: undefined, blended: false, assetsDest: undefined, @@ -168,7 +169,9 @@ describe('inspect', () => { name: 'convert', opts: { _: ['convert'], - options: {}, + options: { + build: true, + }, isHelp: false } }) @@ -188,6 +191,7 @@ describe('inspect', () => { opts: { _, options: { + build: true, type }, isHelp: true From a1695d49e3db4491ab63ce376210abd2bfa5d2dd Mon Sep 17 00:00:00 2001 From: ZakaryCode Date: Tue, 23 Jan 2024 18:29:08 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat(server):=20=E6=94=AF=E6=8C=81=E7=AB=AF?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=85=8D=E7=BD=AE=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-service/src/Config.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/taro-service/src/Config.ts b/packages/taro-service/src/Config.ts index b5aa7d12d5f5..8f40d373186a 100644 --- a/packages/taro-service/src/Config.ts +++ b/packages/taro-service/src/Config.ts @@ -119,7 +119,8 @@ export default class Config { cssMinimizer: initialConfig.cssMinimizer, terser: initialConfig.terser, esbuild: initialConfig.esbuild, - ...initialConfig[configName] + ...initialConfig[configName], + ...initialConfig[platform], } } }