From e66a7cb6cc938da2c0cc54aed8f0838799c5119a Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Tue, 19 Jan 2021 22:13:19 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat(cli/mini-runner/loader):=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8E=9F=E7=94=9F=E6=B7=B7=E5=90=88=20Taro=20?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E8=AF=91=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/cli.ts | 11 ++-- packages/taro-cli/src/commands/build.ts | 52 +++++++------------ .../taro-cli/src/presets/commands/build.ts | 12 ++--- .../presets/files/generateProjectConfig.ts | 4 ++ packages/taro-loader/src/app.ts | 13 ++++- .../src/plugins/MiniPlugin.ts | 4 +- packages/taro-mini-runner/src/utils/types.ts | 1 + .../src/webpack/build.conf.ts | 5 +- 8 files changed, 53 insertions(+), 49 deletions(-) diff --git a/packages/taro-cli/src/cli.ts b/packages/taro-cli/src/cli.ts index b9ace3d4ab23..f305ceff5d6d 100644 --- a/packages/taro-cli/src/cli.ts +++ b/packages/taro-cli/src/cli.ts @@ -40,15 +40,12 @@ export default class CLI { case 'build': { build(kernel, { platform: args.type, - isWatch: !!args.watch, + isWatch: Boolean(args.watch), port: args.port, env: args.env, - release: args.release, - ui: args.ui, - uiIndex: args.uiIndex, - page: args.page, - component: args.component, - plugin: args.plugin, + blended: Boolean(args.blended), + // plugin: args.plugin, + // release: args.release, isHelp: args.h }) break diff --git a/packages/taro-cli/src/commands/build.ts b/packages/taro-cli/src/commands/build.ts index 274db5aa0ca0..a5d35e8e4f0e 100644 --- a/packages/taro-cli/src/commands/build.ts +++ b/packages/taro-cli/src/commands/build.ts @@ -3,42 +3,33 @@ import { Kernel } from '@tarojs/service' export default function build (kernel: Kernel, { platform, isWatch, - release, port, env, - ui, - uiIndex, - page, - component, + blended = false, envHasBeenSet = false, - plugin, + // plugin, + // release, isHelp }: { - platform: string, - isWatch: boolean, - release?: boolean + platform: string + isWatch: boolean port?: number env?: string - ui?: boolean - uiIndex?: string - page?: string - component?: string + blended?: boolean envHasBeenSet?: boolean - plugin?: string | boolean + // plugin?: string | boolean + // release?: boolean isHelp?: boolean }) { - if (plugin) { - if (typeof plugin === 'boolean') { - plugin = 'weapp' - } - platform = 'plugin' - } - if (platform === 'plugin') { - plugin = plugin || 'weapp' - } - if (ui) { - platform = 'ui' - } + // if (plugin) { + // if (typeof plugin === 'boolean') { + // plugin = 'weapp' + // } + // platform = 'plugin' + // } + // if (platform === 'plugin') { + // plugin = plugin || 'weapp' + // } let nodeEnv = process.env.NODE_ENV || env if (!nodeEnv) { if (isWatch) { @@ -54,14 +45,11 @@ export default function build (kernel: Kernel, { opts: { platform, isWatch, - release, port, - ui, - uiIndex, - page, - component, + blended, envHasBeenSet, - plugin, + // plugin, + // release, isHelp } }) diff --git a/packages/taro-cli/src/presets/commands/build.ts b/packages/taro-cli/src/presets/commands/build.ts index 811817148d5b..3029a918aa4a 100644 --- a/packages/taro-cli/src/presets/commands/build.ts +++ b/packages/taro-cli/src/presets/commands/build.ts @@ -9,20 +9,17 @@ export default (ctx: IPluginContext) => { optionsMap: { '--type [typeName]': 'Build type, weapp/swan/alipay/tt/h5/quickapp/rn/qq/jd', '--watch': 'Watch mode', - '--page [pagePath]': 'Build one page', - '--component [pagePath]': 'Build one component', '--env [env]': 'Env type', - '--ui': 'Build Taro UI library', - '--ui-index [uiIndexPath]': 'Index file for build Taro UI library', - '--plugin [typeName]': 'Build Taro plugin project, weapp', '--port [port]': 'Specified port', - '--release': 'Release quickapp' + '--blended': 'Blended Taro project in an original MiniApp project' + // '--plugin [typeName]': 'Build Taro plugin project, weapp', + // '--release': 'Release quickapp' }, async fn (opts) { const { platform, config } = opts const { fs, chalk, PROJECT_CONFIG } = ctx.helper const { outputPath, configPath } = ctx.paths - const { isWatch, envHasBeenSet } = ctx.runOpts + const { isWatch, envHasBeenSet, blended } = ctx.runOpts if (!configPath || !fs.existsSync(configPath)) { console.log(chalk.red(`找不到项目配置文件${PROJECT_CONFIG},请确定当前目录是 Taro 项目根目录!`)) process.exit(1) @@ -65,6 +62,7 @@ export default (ctx: IPluginContext) => { ...config, isWatch, mode: isProduction ? 'production' : 'development', + blended, async modifyWebpackChain (chain, webpack) { await ctx.applyPlugins({ name: 'modifyWebpackChain', diff --git a/packages/taro-cli/src/presets/files/generateProjectConfig.ts b/packages/taro-cli/src/presets/files/generateProjectConfig.ts index 364c85783759..7ba57870c977 100644 --- a/packages/taro-cli/src/presets/files/generateProjectConfig.ts +++ b/packages/taro-cli/src/presets/files/generateProjectConfig.ts @@ -4,6 +4,10 @@ import { IPluginContext } from '@tarojs/service' export default (ctx: IPluginContext) => { ctx.registerMethod('generateProjectConfig', ({ srcConfigName, distConfigName }) => { + // 混合模式不需要生成项目配置 + const { blended } = ctx.runOpts + if (blended) return + const { appPath, sourcePath, outputPath } = ctx.paths const { printLog, processTypeEnum, fs } = ctx.helper // 生成 project.config.json diff --git a/packages/taro-loader/src/app.ts b/packages/taro-loader/src/app.ts index 5721ca7591b5..80df2eff566e 100644 --- a/packages/taro-loader/src/app.ts +++ b/packages/taro-loader/src/app.ts @@ -8,16 +8,27 @@ export default function (this: webpack.loader.LoaderContext) { const options = getOptions(this) const { importFrameworkStatement, frameworkArgs, creator } = frameworkMeta[options.framework] const config = JSON.stringify(options.config) + const blended = options.blended + const prerender = ` if (typeof PRERENDER !== 'undefined') { global._prerender = inst }` + + const createApp = `${creator}(component, ${frameworkArgs})` + const instantiateApp = blended + ? ` +var opt = ${createApp} +exports.taroApp = opt +` + : `var inst = App(${createApp})` + return `import { ${creator}, window } from '@tarojs/runtime' import component from ${stringify(this.request.split('!').slice(1).join('!'))} ${importFrameworkStatement} var config = ${config}; window.__taroAppConfig = config -var inst = App(${creator}(component, ${frameworkArgs})) +${instantiateApp} ${options.prerender ? prerender : ''} ` } diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index e4681a6969c3..1bd103a678ef 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -50,6 +50,7 @@ interface ITaroMiniPluginOptions { template: RecursiveTemplate | UnRecursiveTemplate modifyBuildAssets?: Function modifyMiniConfigs?: Function + blended: boolean } export interface IComponentObj { @@ -227,7 +228,8 @@ export default class TaroMiniPlugin { options: { framework, prerender: this.prerenderPages.size > 0, - config: this.appConfig + config: this.appConfig, + blended: this.options.blended } }) } diff --git a/packages/taro-mini-runner/src/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index 0bb8be5d5ad7..8fe5981cfb3c 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -60,6 +60,7 @@ export interface IBuildConfig extends IProjectBaseConfig, IMiniAppConfig { baseLevel: number, prerender?: PrerenderConfig template: RecursiveTemplate | UnRecursiveTemplate + blended: boolean } export type AddPageChunks = ((pages: Map, pagesNames?: string[]) => void) diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index e013ed0edaf5..a0ac4858a224 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -71,6 +71,8 @@ export default (appPath: string, mode, config: Partial): any => { commonChunks, addChunkPages, + blended, + modifyMiniConfigs, modifyBuildAssets } = config @@ -150,7 +152,8 @@ export default (appPath: string, mode, config: Partial): any => { addChunkPages, modifyMiniConfigs, modifyBuildAssets, - minifyXML + minifyXML, + blended }) plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{ From ce56d5b981d64cc41164bd4f8c9ee601c5498977 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Thu, 21 Jan 2021 15:29:57 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat(mini-runner):=20=E5=BC=95=E7=94=A8?= =?UTF-8?q?=E5=8E=9F=E7=94=9F=E7=BB=84=E4=BB=B6=E6=94=AF=E6=8C=81=20alias?= =?UTF-8?q?=EF=BC=8Cclose=20#8472?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/plugins/MiniPlugin.ts | 24 ++++++++--- packages/taro-mini-runner/src/utils/index.ts | 42 ------------------- .../src/webpack/build.conf.ts | 3 +- 3 files changed, 20 insertions(+), 49 deletions(-) diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 1bd103a678ef..74a3d3b6a989 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -23,7 +23,9 @@ import { FRAMEWORK_EXT_MAP, printLog, processTypeEnum, - FRAMEWORK_MAP + FRAMEWORK_MAP, + isAliasPath, + replaceAliasPath } from '@tarojs/helper' import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency' @@ -51,6 +53,7 @@ interface ITaroMiniPluginOptions { modifyBuildAssets?: Function modifyMiniConfigs?: Function blended: boolean + alias: Record } export interface IComponentObj { @@ -485,19 +488,23 @@ export default class TaroMiniPlugin { const fileConfigPath = file.isNative ? this.replaceExt(filePath, '.json') : this.getConfigFilePath(filePath) const fileConfig = readConfig(fileConfigPath) const usingComponents = fileConfig.usingComponents - this.filesConfig[this.getConfigFilePath(file.name)] = { - content: fileConfig, - path: fileConfigPath - } // 递归收集依赖的第三方组件 if (usingComponents) { const componentNames = Object.keys(usingComponents) const depComponents: Array<{ name: string, path: string }> = [] + const alias = this.options.alias for (const compName of componentNames) { + let compPath = usingComponents[compName] + + if (isAliasPath(compPath, alias)) { + compPath = replaceAliasPath(filePath, compPath, alias) + fileConfig.usingComponents[compName] = compPath + } + depComponents.push({ name: compName, - path: usingComponents[compName] + path: compPath }) if (!componentConfig.thirdPartyComponents.has(compName) && !file.isNative) { @@ -522,6 +529,11 @@ export default class TaroMiniPlugin { } }) } + + this.filesConfig[this.getConfigFilePath(file.name)] = { + content: fileConfig, + path: fileConfigPath + } } /** diff --git a/packages/taro-mini-runner/src/utils/index.ts b/packages/taro-mini-runner/src/utils/index.ts index e599ee858268..e13e6ab0ea1b 100644 --- a/packages/taro-mini-runner/src/utils/index.ts +++ b/packages/taro-mini-runner/src/utils/index.ts @@ -1,51 +1,9 @@ -import * as path from 'path' -import * as fs from 'fs-extra' - import * as resolvePath from 'resolve' -import { - isAliasPath, - replaceAliasPath, - resolveMainFilePath, - NODE_MODULES_REG, - promoteRelativePath -} from '@tarojs/helper' - -import { IOption, IComponentObj } from './types' export function isQuickAppPkg (name: string): boolean { return /^@(system|service)\.[a-zA-Z]{1,}/.test(name) } -export function buildUsingComponents ( - filePath: string, - sourceDir: string, - pathAlias: IOption, - components: IComponentObj[], - isComponent?: boolean -): IOption { - const usingComponents = Object.create(null) - for (const component of components) { - let componentPath = component.path as string - if (isAliasPath(componentPath, pathAlias)) { - componentPath = replaceAliasPath(filePath, componentPath as string, pathAlias) - } - componentPath = resolveMainFilePath(path.resolve(filePath, '..', componentPath as string)) - if (fs.existsSync(componentPath)) { - if (NODE_MODULES_REG.test(componentPath) && !NODE_MODULES_REG.test(filePath)) { - componentPath = componentPath.replace(NODE_MODULES_REG, path.join(sourceDir, 'npm')) - } - componentPath = promoteRelativePath(path.relative(filePath, componentPath)) - } else { - componentPath = component.path as string - } - if (component.name) { - usingComponents[component.name] = (componentPath as string).replace(path.extname(componentPath as string), '') - } - } - return Object.assign({}, isComponent ? { component: true } : { usingComponents: {} }, components.length ? { - usingComponents - } : {}) -} const npmCached = {} export function resolveNpmSync (pkgName: string, root): string | null { try { diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index a0ac4858a224..13b88a736f03 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -153,7 +153,8 @@ export default (appPath: string, mode, config: Partial): any => { modifyMiniConfigs, modifyBuildAssets, minifyXML, - blended + blended, + alias }) plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{ From 8159e993eb9c291f968a44d1eaa904e00993d120 Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Fri, 22 Jan 2021 11:37:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?chore:=20=E6=B7=B7=E5=90=88=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=85=A5=E5=8F=A3=E7=BB=84=E4=BB=B6=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=B0=83=E7=94=A8=20launch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-loader/src/app.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/taro-loader/src/app.ts b/packages/taro-loader/src/app.ts index 80df2eff566e..fb32274f9929 100644 --- a/packages/taro-loader/src/app.ts +++ b/packages/taro-loader/src/app.ts @@ -18,8 +18,9 @@ if (typeof PRERENDER !== 'undefined') { const createApp = `${creator}(component, ${frameworkArgs})` const instantiateApp = blended ? ` -var opt = ${createApp} -exports.taroApp = opt +var app = ${createApp} +app.onLaunch() +exports.taroApp = app ` : `var inst = App(${createApp})` From 9abc52a3c31f78cd4d3b973311c10c88925d08fd Mon Sep 17 00:00:00 2001 From: chenjiajian <798095202@qq.com> Date: Fri, 22 Jan 2021 11:37:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat(mini-runner):=20=E5=A2=9E=E5=8A=A0=20o?= =?UTF-8?q?nCompilerMake=20=E9=92=A9=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taro-cli/src/presets/commands/build.ts | 9 +++++++++ packages/taro-mini-runner/src/plugins/MiniPlugin.ts | 6 +++++- packages/taro-mini-runner/src/utils/types.ts | 1 + packages/taro-mini-runner/src/webpack/build.conf.ts | 4 +++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/taro-cli/src/presets/commands/build.ts b/packages/taro-cli/src/presets/commands/build.ts index 3029a918aa4a..cc9e5ba5cc81 100644 --- a/packages/taro-cli/src/presets/commands/build.ts +++ b/packages/taro-cli/src/presets/commands/build.ts @@ -91,6 +91,14 @@ export default (ctx: IPluginContext) => { } }) }, + async onCompilerMake (compilation) { + await ctx.applyPlugins({ + name: 'onCompilerMake', + opts: { + compilation + } + }) + }, async onBuildFinish ({ error, stats, isWatch }) { await ctx.applyPlugins({ name: 'onBuildFinish', @@ -113,6 +121,7 @@ function registerBuildHooks (ctx) { 'modifyWebpackChain', 'modifyBuildAssets', 'modifyMiniConfigs', + 'onCompilerMake', 'onBuildStart', 'onBuildFinish' ].forEach(methodName => { diff --git a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts index 74a3d3b6a989..578effb84015 100644 --- a/packages/taro-mini-runner/src/plugins/MiniPlugin.ts +++ b/packages/taro-mini-runner/src/plugins/MiniPlugin.ts @@ -52,6 +52,7 @@ interface ITaroMiniPluginOptions { template: RecursiveTemplate | UnRecursiveTemplate modifyBuildAssets?: Function modifyMiniConfigs?: Function + onCompilerMake?: Function blended: boolean alias: Record } @@ -209,6 +210,7 @@ export default class TaroMiniPlugin { })) }) await Promise.all(promises) + await this.options.onCompilerMake?.(compilation) }) ) @@ -643,7 +645,9 @@ export default class TaroMiniPlugin { } const appConfigPath = this.getConfigFilePath(this.appEntry) const appConfigName = path.basename(appConfigPath).replace(path.extname(appConfigPath), '') - this.generateConfigFile(compilation, this.appEntry, this.filesConfig[appConfigName].content) + if (!this.options.blended) { + this.generateConfigFile(compilation, this.appEntry, this.filesConfig[appConfigName].content) + } if (!template.isSupportRecursive) { // 如微信、QQ 不支持递归模版的小程序,需要使用自定义组件协助递归 this.generateTemplateFile(compilation, baseCompName, template.buildBaseComponentTemplate, this.options.fileType.templ) diff --git a/packages/taro-mini-runner/src/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index 8fe5981cfb3c..e82525d12424 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -54,6 +54,7 @@ export interface IBuildConfig extends IProjectBaseConfig, IMiniAppConfig { modifyWebpackChain: Function, modifyBuildAssets: Function, modifyMiniConfigs: Function, + onCompilerMake: Function, onWebpackChainReady: Function, onBuildFinish: Function framework: string, diff --git a/packages/taro-mini-runner/src/webpack/build.conf.ts b/packages/taro-mini-runner/src/webpack/build.conf.ts index 13b88a736f03..e7f0e5c5279e 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -74,7 +74,8 @@ export default (appPath: string, mode, config: Partial): any => { blended, modifyMiniConfigs, - modifyBuildAssets + modifyBuildAssets, + onCompilerMake } = config let { copy } = config @@ -152,6 +153,7 @@ export default (appPath: string, mode, config: Partial): any => { addChunkPages, modifyMiniConfigs, modifyBuildAssets, + onCompilerMake, minifyXML, blended, alias