-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,121 @@ | ||
import * as path from 'path' | ||
import * as fs from 'fs-extra' | ||
import chalk from 'chalk' | ||
import { SyncHook, Hook } from 'tapable' | ||
import * as _ from 'lodash' | ||
import chalk from 'chalk' | ||
|
||
import * as Util from './util' | ||
import CONFIG from './config' | ||
import { BUILD_TYPES, PROJECT_CONFIG } from './util/constants' | ||
import { IBuildConfig } from './util/types' | ||
import { IBuildOptions, IProjectConfig, ICommonPlugin } from './util/types' | ||
import { emptyDirectory } from './util' | ||
import CONFIG from './config' | ||
|
||
export default async function build (appPath, buildConfig: IBuildConfig) { | ||
const { type, watch, platform, port, release, page, component, uiIndex } = buildConfig | ||
const configDir = require(path.join(appPath, PROJECT_CONFIG))(_.merge) | ||
const outputPath = path.join(appPath, configDir.outputRoot || CONFIG.OUTPUT_DIR) | ||
if (!fs.existsSync(outputPath)) { | ||
fs.ensureDirSync(outputPath) | ||
} else if (type !== BUILD_TYPES.H5 && (type !== BUILD_TYPES.QUICKAPP || !watch)) { | ||
Util.emptyDirectory(outputPath) | ||
} | ||
switch (type) { | ||
case BUILD_TYPES.H5: | ||
buildForH5(appPath, { watch, port }) | ||
break | ||
case BUILD_TYPES.WEAPP: | ||
buildForWeapp(appPath, { watch, page, component }) | ||
break | ||
case BUILD_TYPES.SWAN: | ||
buildForSwan(appPath, { watch, page, component }) | ||
break | ||
case BUILD_TYPES.ALIPAY: | ||
buildForAlipay(appPath, { watch, page, component }) | ||
break | ||
case BUILD_TYPES.TT: | ||
buildForTt(appPath, { watch, page, component }) | ||
break | ||
case BUILD_TYPES.RN: | ||
buildForRN(appPath, { watch }) | ||
break | ||
case BUILD_TYPES.QUICKAPP: | ||
buildForQuickApp(appPath, { watch, port, release }) | ||
break | ||
case BUILD_TYPES.QQ: | ||
buildForQQ(appPath, { watch, page, component }) | ||
break | ||
case BUILD_TYPES.JD: | ||
buildForJD(appPath, { watch }) | ||
break | ||
case BUILD_TYPES.UI: | ||
buildForUILibrary(appPath, { watch, uiIndex }) | ||
break | ||
case BUILD_TYPES.PLUGIN: | ||
buildForPlugin(appPath, { | ||
watch, | ||
platform | ||
}) | ||
break | ||
default: | ||
console.log(chalk.red('输入类型错误,目前只支持 weapp/swan/alipay/tt/h5/quickapp/rn 七端类型')) | ||
} | ||
interface IBuilderHooks { | ||
beforeBuild: Hook, | ||
afterBuild: Hook | ||
} | ||
|
||
function buildForWeapp (appPath: string, buildConfig: IBuildConfig) { | ||
require('./mini').build(appPath, Object.assign({ | ||
adapter: BUILD_TYPES.WEAPP | ||
}, buildConfig)) | ||
} | ||
export default class Builder { | ||
hooks: IBuilderHooks | ||
appPath: string | ||
config: IProjectConfig | ||
constructor (appPath: string) { | ||
this.hooks = { | ||
beforeBuild: new SyncHook(['config']), | ||
afterBuild: new SyncHook(['builder']) | ||
} | ||
|
||
function buildForSwan (appPath: string, buildConfig: IBuildConfig) { | ||
require('./mini').build(appPath, Object.assign({ | ||
adapter: BUILD_TYPES.SWAN | ||
}, buildConfig)) | ||
} | ||
this.appPath = appPath | ||
this.init() | ||
} | ||
|
||
function buildForAlipay (appPath: string, buildConfig: IBuildConfig) { | ||
require('./mini').build(appPath, Object.assign({ | ||
adapter: BUILD_TYPES.ALIPAY | ||
}, buildConfig)) | ||
} | ||
init () { | ||
this.resolveConfig() | ||
this.applyPlugins() | ||
} | ||
|
||
function buildForTt (appPath: string, buildConfig: IBuildConfig) { | ||
require('./mini').build(appPath, Object.assign({ | ||
adapter: BUILD_TYPES.TT | ||
}, buildConfig)) | ||
} | ||
resolveConfig () { | ||
this.config = require(path.join(this.appPath, PROJECT_CONFIG))(_.merge) | ||
} | ||
|
||
function buildForH5 (appPath: string, buildConfig: IBuildConfig) { | ||
require('./h5').build(appPath, buildConfig) | ||
} | ||
applyPlugins () { | ||
const commonConfig = Object.assign({}, { plugins: [] }, this.config.common) | ||
const plugins = commonConfig.plugins | ||
if (plugins.length) { | ||
plugins.forEach((plugin: ICommonPlugin) => { | ||
plugin.apply(this) | ||
}) | ||
} | ||
} | ||
|
||
function buildForRN (appPath: string, { watch }: IBuildConfig) { | ||
require('./rn').build(appPath, { watch }) | ||
} | ||
emptyFirst ({ watch, type }) { | ||
const outputPath = path.join(this.appPath, `${this.config.outputRoot || CONFIG.OUTPUT_DIR}}`) | ||
if (!fs.existsSync(outputPath)) { | ||
fs.ensureDirSync(outputPath) | ||
} else if (type !== BUILD_TYPES.H5 && (type !== BUILD_TYPES.QUICKAPP || !watch)) { | ||
emptyDirectory(outputPath) | ||
} | ||
} | ||
|
||
function buildForQuickApp (appPath: string, { watch, port, release }: IBuildConfig) { | ||
require('./mini').build(appPath, { | ||
watch, | ||
adapter: BUILD_TYPES.QUICKAPP, | ||
port, | ||
release | ||
}) | ||
} | ||
build (buildOptions: IBuildOptions) { | ||
this.hooks.beforeBuild.call(this.config) | ||
const { type, watch, platform, port, uiIndex } = buildOptions | ||
this.emptyFirst({ type, watch }) | ||
switch (type) { | ||
case BUILD_TYPES.H5: | ||
this.buildForH5(this.appPath, { watch, port }) | ||
break | ||
case BUILD_TYPES.WEAPP: | ||
case BUILD_TYPES.SWAN: | ||
case BUILD_TYPES.ALIPAY: | ||
case BUILD_TYPES.TT: | ||
case BUILD_TYPES.QUICKAPP: | ||
case BUILD_TYPES.QQ: | ||
case BUILD_TYPES.JD: | ||
this.buildForMini(this.appPath, buildOptions) | ||
break | ||
case BUILD_TYPES.RN: | ||
this.buildForRN(this.appPath, { watch }) | ||
break | ||
case BUILD_TYPES.UI: | ||
this.buildForUILibrary(this.appPath, { watch, uiIndex }) | ||
break | ||
case BUILD_TYPES.PLUGIN: | ||
this.buildForPlugin(this.appPath, { | ||
watch, | ||
platform | ||
}) | ||
break | ||
default: | ||
console.log(chalk.red('输入类型错误,目前只支持 weapp/swan/alipay/tt/h5/quickapp/rn 七端类型')) | ||
} | ||
} | ||
|
||
function buildForQQ (appPath: string, buildConfig: IBuildConfig) { | ||
require('./mini').build(appPath, Object.assign({ | ||
adapter: BUILD_TYPES.QQ | ||
}, buildConfig)) | ||
} | ||
buildForH5 (appPath: string, buildOptions: IBuildOptions) { | ||
require('./h5').build(appPath, buildOptions) | ||
} | ||
|
||
function buildForJD (appPath: string, { watch }: IBuildConfig) { | ||
require('./mini').build(appPath, { | ||
watch, | ||
adapter: BUILD_TYPES.JD | ||
}) | ||
} | ||
buildForMini (appPath: string, buildOptions: IBuildOptions) { | ||
require('./mini/webpack').build(appPath, buildOptions, this) | ||
} | ||
|
||
function buildForUILibrary (appPath: string, { watch, uiIndex }: IBuildConfig) { | ||
require('./ui/index').build(appPath, { watch, uiIndex }) | ||
} | ||
buildForRN (appPath: string, { watch }) { | ||
require('./rn').build(appPath, { watch }) | ||
} | ||
|
||
function buildForPlugin (appPath: string, { watch, platform }) { | ||
const typeMap = { | ||
[BUILD_TYPES.WEAPP]: '微信', | ||
[BUILD_TYPES.ALIPAY]: '支付宝' | ||
buildForUILibrary (appPath: string, { watch, uiIndex }) { | ||
require('./ui/index').build(appPath, { watch, uiIndex }) | ||
} | ||
if (platform !== BUILD_TYPES.WEAPP && platform !== BUILD_TYPES.ALIPAY) { | ||
console.log(chalk.red('目前插件编译仅支持 微信/支付宝 小程序!')) | ||
return | ||
|
||
buildForPlugin (appPath: string, { watch, platform }) { | ||
const typeMap = { | ||
[BUILD_TYPES.WEAPP]: '微信', | ||
[BUILD_TYPES.ALIPAY]: '支付宝' | ||
} | ||
if (platform !== BUILD_TYPES.WEAPP && platform !== BUILD_TYPES.ALIPAY) { | ||
console.log(chalk.red('目前插件编译仅支持 微信/支付宝 小程序!')) | ||
return | ||
} | ||
console.log(chalk.green(`开始编译${typeMap[platform]}小程序插件`)) | ||
require('./plugin').build(appPath, { watch, platform }) | ||
} | ||
console.log(chalk.green(`开始编译${typeMap[platform]}小程序插件`)) | ||
require('./plugin').build(appPath, { watch, platform }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters