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..cc9e5ba5cc81 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', @@ -93,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', @@ -115,6 +121,7 @@ function registerBuildHooks (ctx) { 'modifyWebpackChain', 'modifyBuildAssets', 'modifyMiniConfigs', + 'onCompilerMake', 'onBuildStart', 'onBuildFinish' ].forEach(methodName => { 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 a32a3b16605b..368804ae3572 100644 --- a/packages/taro-loader/src/app.ts +++ b/packages/taro-loader/src/app.ts @@ -10,18 +10,31 @@ 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 loaders = this.loaders const thisLoaderIndex = loaders.findIndex(item => normalizePath(item.path).indexOf('@tarojs/taro-loader') >= 0) + const prerender = ` if (typeof PRERENDER !== 'undefined') { global._prerender = inst }` + + const createApp = `${creator}(component, ${frameworkArgs})` + + const instantiateApp = blended + ? ` +var app = ${createApp} +app.onLaunch() +exports.taroApp = app +` + : `var inst = App(${createApp})` + return `import { ${creator}, window } from '@tarojs/runtime' import component from ${stringify(this.request.split('!').slice(thisLoaderIndex + 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..578effb84015 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' @@ -50,6 +52,9 @@ interface ITaroMiniPluginOptions { template: RecursiveTemplate | UnRecursiveTemplate modifyBuildAssets?: Function modifyMiniConfigs?: Function + onCompilerMake?: Function + blended: boolean + alias: Record } export interface IComponentObj { @@ -205,6 +210,7 @@ export default class TaroMiniPlugin { })) }) await Promise.all(promises) + await this.options.onCompilerMake?.(compilation) }) ) @@ -227,7 +233,8 @@ export default class TaroMiniPlugin { options: { framework, prerender: this.prerenderPages.size > 0, - config: this.appConfig + config: this.appConfig, + blended: this.options.blended } }) } @@ -483,19 +490,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) { @@ -520,6 +531,11 @@ export default class TaroMiniPlugin { } }) } + + this.filesConfig[this.getConfigFilePath(file.name)] = { + content: fileConfig, + path: fileConfigPath + } } /** @@ -629,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/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/utils/types.ts b/packages/taro-mini-runner/src/utils/types.ts index 0bb8be5d5ad7..e82525d12424 100644 --- a/packages/taro-mini-runner/src/utils/types.ts +++ b/packages/taro-mini-runner/src/utils/types.ts @@ -54,12 +54,14 @@ export interface IBuildConfig extends IProjectBaseConfig, IMiniAppConfig { modifyWebpackChain: Function, modifyBuildAssets: Function, modifyMiniConfigs: Function, + onCompilerMake: Function, onWebpackChainReady: Function, onBuildFinish: Function framework: string, 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..e7f0e5c5279e 100644 --- a/packages/taro-mini-runner/src/webpack/build.conf.ts +++ b/packages/taro-mini-runner/src/webpack/build.conf.ts @@ -71,8 +71,11 @@ export default (appPath: string, mode, config: Partial): any => { commonChunks, addChunkPages, + blended, + modifyMiniConfigs, - modifyBuildAssets + modifyBuildAssets, + onCompilerMake } = config let { copy } = config @@ -150,7 +153,10 @@ export default (appPath: string, mode, config: Partial): any => { addChunkPages, modifyMiniConfigs, modifyBuildAssets, - minifyXML + onCompilerMake, + minifyXML, + blended, + alias }) plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{