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/blended build #8568

Merged
merged 5 commits into from
Jan 22, 2021
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
11 changes: 4 additions & 7 deletions packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 20 additions & 32 deletions packages/taro-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
})
Expand Down
21 changes: 14 additions & 7 deletions packages/taro-cli/src/presets/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -65,6 +62,7 @@ export default (ctx: IPluginContext) => {
...config,
isWatch,
mode: isProduction ? 'production' : 'development',
blended,
async modifyWebpackChain (chain, webpack) {
await ctx.applyPlugins({
name: 'modifyWebpackChain',
Expand Down Expand Up @@ -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',
Expand All @@ -115,6 +121,7 @@ function registerBuildHooks (ctx) {
'modifyWebpackChain',
'modifyBuildAssets',
'modifyMiniConfigs',
'onCompilerMake',
'onBuildStart',
'onBuildFinish'
].forEach(methodName => {
Expand Down
4 changes: 4 additions & 0 deletions packages/taro-cli/src/presets/files/generateProjectConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion packages/taro-loader/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 : ''}
`
}
34 changes: 26 additions & 8 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import {
FRAMEWORK_EXT_MAP,
printLog,
processTypeEnum,
FRAMEWORK_MAP
FRAMEWORK_MAP,
isAliasPath,
replaceAliasPath
} from '@tarojs/helper'

import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency'
Expand All @@ -50,6 +52,9 @@ interface ITaroMiniPluginOptions {
template: RecursiveTemplate | UnRecursiveTemplate
modifyBuildAssets?: Function
modifyMiniConfigs?: Function
onCompilerMake?: Function
blended: boolean
alias: Record<string, string>
}

export interface IComponentObj {
Expand Down Expand Up @@ -205,6 +210,7 @@ export default class TaroMiniPlugin {
}))
})
await Promise.all(promises)
await this.options.onCompilerMake?.(compilation)
})
)

Expand All @@ -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
}
})
}
Expand Down Expand Up @@ -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) {
Expand All @@ -520,6 +531,11 @@ export default class TaroMiniPlugin {
}
})
}

this.filesConfig[this.getConfigFilePath(file.name)] = {
content: fileConfig,
path: fileConfigPath
}
}

/**
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 0 additions & 42 deletions packages/taro-mini-runner/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-mini-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]>, pagesNames?: string[]) => void)
10 changes: 8 additions & 2 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
commonChunks,
addChunkPages,

blended,

modifyMiniConfigs,
modifyBuildAssets
modifyBuildAssets,
onCompilerMake
} = config

let { copy } = config
Expand Down Expand Up @@ -150,7 +153,10 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
addChunkPages,
modifyMiniConfigs,
modifyBuildAssets,
minifyXML
onCompilerMake,
minifyXML,
blended,
alias
})

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
Expand Down