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(runner): support no build #15144

Merged
merged 6 commits into from
Jan 25, 2024
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
6 changes: 5 additions & 1 deletion packages/taro-cli/src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('inspect', () => {
platform: undefined,
publicPath: undefined,
isWatch: false,
withoutBuild: false,
env: undefined,
blended: false,
assetsDest: undefined,
Expand Down Expand Up @@ -168,7 +169,9 @@ describe('inspect', () => {
name: 'convert',
opts: {
_: ['convert'],
options: {},
options: {
build: true,
},
isHelp: false
}
})
Expand All @@ -188,6 +191,7 @@ describe('inspect', () => {
opts: {
_,
options: {
build: true,
type
},
isHelp: true
Expand Down
11 changes: 8 additions & 3 deletions packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions packages/taro-cli/src/presets/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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

Expand Down Expand Up @@ -110,6 +112,7 @@ export default (ctx: IPluginContext) => {
mode: isProduction ? 'production' : 'development',
blended,
isBuildNativeComp,
withoutBuild,
newBlended,
async modifyWebpackChain (chain, webpack, data) {
await ctx.applyPlugins({
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default async function build (appPath: string, config: IBuildConfig): Pro
const webpackConfig: webpack.Configuration = webpackChain.toConfig()

return new Promise<webpack.Stats>((resolve, reject) => {
if (config.withoutBuild) return

const compiler = webpack(webpackConfig)
const onBuildFinish = newConfig.onBuildFinish
let prerender: Prerender
Expand Down
1 change: 1 addition & 0 deletions packages/taro-mini-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-service/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export default class Config {
cssMinimizer: initialConfig.cssMinimizer,
terser: initialConfig.terser,
esbuild: initialConfig.esbuild,
...initialConfig[configName]
...initialConfig[configName],
...initialConfig[platform],
}
}
}
4 changes: 4 additions & 0 deletions packages/taro-webpack-runner/src/config/dev.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -167,6 +168,9 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
publicPath: ['', 'auto'].includes(publicPath) ? publicPath : addTrailingSlash(publicPath),
chunkDirectory
}, output])

config.modifyComponentConfig?.(componentConfig, config)

if (config.isBuildNativeComp) {
// Note: 当开发者没有配置时,优先使用 module 导出组件
webpackOutput.libraryTarget ||= 'commonjs'
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-webpack-runner/src/config/prod.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -190,6 +191,9 @@ export default function (appPath: string, config: Partial<BuildConfig>, appHelpe
publicPath: ['', 'auto'].includes(publicPath) ? publicPath : addTrailingSlash(publicPath),
chunkDirectory
}, output])

config.modifyComponentConfig?.(componentConfig, config)

if (config.isBuildNativeComp) {
// Note: 当开发者没有配置时,优先使用 module 导出组件
webpackOutput.libraryTarget ||= 'commonjs'
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-webpack-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -214,6 +216,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)
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-webpack-runner/src/plugins/H5Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -131,7 +132,9 @@ export default class TaroH5Plugin {
})
})

new TaroComponentsExportsPlugin(this.options).apply(compiler)
if (!componentConfig.includeAll) {
new TaroComponentsExportsPlugin(this.options).apply(compiler)
}
}

run () {
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-webpack-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +20,9 @@ export interface BuildConfig extends IProjectBaseConfig, IH5Config {
runtimePath?: string | string[]
/** special mode */
isBuildNativeComp?: boolean
withoutBuild?: boolean
/** hooks */
onCompilerMake: (compilation) => Promise<any>
onParseCreateElement: (nodeName, componentConfig) => Promise<any>
modifyComponentConfig: Func
}
4 changes: 4 additions & 0 deletions packages/taro-webpack5-runner/src/index.h5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions packages/taro-webpack5-runner/src/index.mini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stats> {
export default async function build (appPath: string, rawConfig: MiniBuildConfig): Promise<Stats | void> {
const combination = new MiniCombination(appPath, rawConfig)
await combination.make()

Expand All @@ -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<Stats>((resolve, reject) => {
return new Promise<Stats | void>((resolve, reject) => {
if (config.withoutBuild) return

const compiler = webpack(webpackConfig)
const onBuildFinish = config.onBuildFinish
let prerender: Prerender
Expand Down
5 changes: 4 additions & 1 deletion packages/taro-webpack5-runner/src/plugins/H5Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -149,7 +150,9 @@ export default class TaroH5Plugin {
})
})

new TaroComponentsExportsPlugin(this.options).apply(compiler)
if (!componentConfig.includeAll) {
new TaroComponentsExportsPlugin(this.options).apply(compiler)
}
}

run () {
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-webpack5-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export interface CommonBuildConfig extends IProjectBaseConfig {
/** special mode */
isBuildNativeComp?: boolean
newBlended?: boolean
withoutBuild?: boolean
/** hooks */
onCompilerMake: (compilation: Webpack.Compilation, compiler: Webpack.Compiler, plugin: any) => Promise<any>
onParseCreateElement: (nodeName, componentConfig) => Promise<any>
modifyComponentConfig: (componentConfig: IComponentConfig, config: Partial<CommonBuildConfig>) => Promise<any>
}

export interface MiniBuildConfig extends CommonBuildConfig, IMiniAppConfig {
Expand All @@ -61,8 +63,6 @@ export interface MiniBuildConfig extends CommonBuildConfig, IMiniAppConfig {
taroComponentsPath?: string
blended?: boolean
hot?: boolean
/** hooks */
modifyComponentConfig: (componentConfig: IComponentConfig, config: Partial<MiniBuildConfig>) => Promise<any>
}

export interface H5BuildConfig extends CommonBuildConfig, IH5Config {
Expand Down
7 changes: 6 additions & 1 deletion packages/taro-webpack5-runner/src/webpack/H5Combination.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -33,7 +34,9 @@ export class H5Combination extends Combination<H5BuildConfig> {
alias = {},
defineConstants = {},
router,
frameworkExts
frameworkExts,
/** hooks */
modifyComponentConfig,
} = config
const externals: Configuration['externals'] = []
const routerMode = router?.mode || 'hash'
Expand All @@ -46,6 +49,8 @@ export class H5Combination extends Combination<H5BuildConfig> {
defineConstants,
})

modifyComponentConfig?.(componentConfig, config)

if (this.isBuildNativeComp) {
delete entry[entryFileName]
this.appHelper.compsConfigList.forEach((comp, index) => {
Expand Down
Loading