Skip to content

Commit

Permalink
feat(cli): cli 支持事件 && 小程序编译配置向 H5 看齐
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 26, 2019
1 parent 09f3f41 commit 206b96e
Show file tree
Hide file tree
Showing 11 changed files with 1,069 additions and 146 deletions.
23 changes: 12 additions & 11 deletions packages/taro-cli/src/mini/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,30 @@ export async function build (appPath: string, { watch, adapter = BUILD_TYPES.WEA
async function buildWithWebpack ({ appPath }: { appPath: string }, builder) {
const {
entryFilePath,
outputDir,
sourceDir,
buildAdapter,
projectConfig,
isProduction,
constantsReplaceList
alias
} = getBuildData()
const miniRunner = await npmProcess.getNpmPkg('@tarojs/mini-runner', appPath)
const babelConfig = getBabelConfig(projectConfig!.plugins!.babel)
const babelConfig = getBabelConfig(projectConfig.babel)
const miniRunnerOpts = {
entry: {
app: entryFilePath
},
sourceDir,
outputDir,
alias,
copy: projectConfig.copy,
outputRoot: projectConfig.outputRoot,
buildAdapter,
plugins: {
babel: babelConfig
},
babel: babelConfig,
csso: projectConfig.csso,
sass: projectConfig.csso,
uglify: projectConfig.uglify,
isWatch: !isProduction,
constantsReplaceList,
env: projectConfig.env,
defineConstants: projectConfig.defineConstants,
designWidth: projectConfig.designWidth,
deviceRatio: projectConfig.deviceRatio
}
miniRunner(miniRunnerOpts, builder)
miniRunner(appPath, miniRunnerOpts, builder)
}
18 changes: 14 additions & 4 deletions packages/taro-mini-runner/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tarojs/mini-runner",
"version": "1.3.0-beta.2",
"version": "1.3.1",
"description": "Mini app runner for taro",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -32,8 +32,8 @@
},
"homepage": "https://github.com/NervJS/taro#readme",
"dependencies": {
"@tarojs/taro": "1.3.0-beta.2",
"@tarojs/transformer-wx": "1.3.0-beta.2",
"@tarojs/taro": "1.3.1",
"@tarojs/transformer-wx": "1.3.1",
"babel-core": "^6.26.3",
"babel-generator": "^6.26.1",
"babel-loader": "^8.0.6",
Expand All @@ -43,14 +43,24 @@
"babel-types": "^6.26.0",
"better-babel-generator": "^6.26.1",
"chalk": "^2.4.2",
"file-loader": "^3.0.1",
"copy-webpack-plugin": "^5.0.3",
"css-loader": "^3.0.0",
"csso-webpack-plugin": "^1.0.0-beta.12",
"file-loader": "^4.0.0",
"fs-extra": "^8.0.1",
"less-loader": "^5.0.0",
"loader-utils": "^1.2.3",
"lodash": "^4.17.11",
"mini-css-extract-plugin": "^0.7.0",
"node-sass": "^4.12.0",
"ora": "^3.4.0",
"postcss-loader": "^3.0.0",
"postcss-pxtransform": "^1.3.2",
"resolve": "^1.11.1",
"sass-loader": "^7.1.0",
"stylus-loader": "^3.0.2",
"uglifyjs-webpack-plugin": "^2.1.3",
"url-loader": "^2.0.0",
"virtual-module-webpack-plugin": "^0.4.1",
"webpack": "^4.31.0",
"webpack-chain": "^6.0.0",
Expand Down
113 changes: 20 additions & 93 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,32 @@
import * as path from 'path'
import * as webpack from 'webpack'

import { IBuildConfig } from './utils/types'
import { printBuildError, bindProdLogger } from './utils/logHelper'
import MiniPlugin, { Targets } from './plugins/MiniPlugin'
import { MINI_APP_FILES, BUILD_TYPES } from './utils/constants'
import buildConf from './webpack/build.conf'

const extensions = ['.ts', '.tsx', '.js', '.jsx']

const globalObjectMap = {
[BUILD_TYPES.WEAPP]: 'wx',
[BUILD_TYPES.ALIPAY]: 'my',
[BUILD_TYPES.SWAN]: 'swan',
[BUILD_TYPES.QQ]: 'qq',
[BUILD_TYPES.TT]: 'tt'
const customizeChain = (chain, customizeFunc: Function) => {
if (customizeFunc instanceof Function) {
customizeFunc(chain, webpack)
}
}

export default function build (config: IBuildConfig) {
const compilePlugins = config.plugins
const { babel } = compilePlugins
const webpackConfig = {
mode: config.isWatch ? 'development' : 'production',
devtool: false,
entry: config.entry,
output: {
filename: '[name].js',
publicPath: '/',
path: config.outputDir,
globalObject: globalObjectMap[config.buildAdapter]
},
resolve: {
extensions
},
target: Targets[config.buildAdapter],
optimization: {
runtimeChunk: {
name: 'runtime'
},
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
name: 'vendors'
}
},
module: {
rules: [
{
test: /\.(tsx?|jsx?)$/,
use: [{
loader: path.resolve(__dirname, './loaders/fileParseLoader'),
options: {
babel,
designWidth: config.designWidth,
deviceRatio: config.deviceRatio,
buildAdapter: config.buildAdapter,
constantsReplaceList: config.constantsReplaceList
}
}, {
loader: path.resolve(__dirname, './loaders/wxTransformerLoader'),
options: {
buildAdapter: config.buildAdapter
}
}]
},
{
test: /\.(scss|wxss|acss|ttss|acss|)$/,
include: /src/,
use: [
{
loader: require.resolve('file-loader'),
options: {
useRelativePath: true,
name: `[path][name]${MINI_APP_FILES[config.buildAdapter].STYLE}`,
context: config.sourceDir
}
},
{
loader: require.resolve('sass-loader'),
options: {
export default function build (appPath: string, config: IBuildConfig, mainBuilder) {
const mode = 'development'
return new Promise((resolve, reject) => {
const webpackChain = buildConf(appPath, mode, config)

},
},
],
}
]
},
plugins: [
new MiniPlugin({
buildAdapter: config.buildAdapter,
constantsReplaceList: config.constantsReplaceList
})
]
}
customizeChain(webpackChain, config.webpackChain)
const webpackConfig = webpackChain.toConfig()

const compiler = webpack(webpackConfig)
bindProdLogger(compiler)
const compiler = webpack(webpackConfig)
bindProdLogger(compiler)

compiler.run((err) => {
if (err) {
printBuildError(err)
}
compiler.run((err) => {
if (err) {
printBuildError(err);
return reject(err)
}
resolve()
})
})
}
10 changes: 4 additions & 6 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,11 @@ export default class MiniPlugin {
}
const { entry } = compiler.options
function getEntryPath (entry) {
if (Array.isArray(entry)) {
return entry.map(item => getEntryPath[item]).find(item => item)
const app = entry['app']
if (Array.isArray(app)) {
return app[0]
}
if (typeof entry === 'object') {
return entry['app']
}
return entry
return app
}
const appEntryPath = getEntryPath(entry)
this.sourceDir = path.dirname(appEntryPath)
Expand Down
10 changes: 9 additions & 1 deletion packages/taro-mini-runner/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ export const TS_EXT: string[] = ['.ts', '.tsx']

export const REG_SCRIPT: RegExp = /\.(js|jsx)(\?.*)?$/
export const REG_TYPESCRIPT: RegExp = /\.(tsx|ts)(\?.*)?$/
export const REG_SCRIPTS: RegExp = /\.[tj]sx?$/i
export const REG_SCRIPTS: RegExp = /\.[tj]sx?$/
export const REG_SASS: RegExp = /\.(s[ac]ss)$/
export const REG_LESS: RegExp = /\.less$/
export const REG_STYLUS: RegExp = /\.styl$/
export const REG_STYLE: RegExp = /\.(css|scss|sass|less|styl|wxss)(\?.*)?$/
export const REG_MEDIA: RegExp = /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/
export const REG_IMAGE: RegExp = /\.(png|jpe?g|gif|bpm|svg|webp)(\?.*)?$/
export const REG_FONT: RegExp = /\.(woff2?|eot|ttf|otf)(\?.*)?$/
export const REG_JSON: RegExp = /\.json(\?.*)?$/

export const NODE_MODULES = 'node_modules'
export const NODE_MODULES_REG = /(.*)node_modules/
Expand Down
17 changes: 17 additions & 0 deletions packages/taro-mini-runner/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import * as fs from 'fs-extra'

import * as resolvePath from 'resolve'
import * as t from 'babel-types'
import { mergeWith } from 'lodash'

import { CONFIG_MAP, JS_EXT, TS_EXT, NODE_MODULES_REG } from './constants'
import { IOption, IComponentObj } from './types'

export const isNodeModule = (filename: string) => NODE_MODULES_REG.test(filename)

export function isNpmPkg (name: string): boolean {
if (/^(\.|\/)/.test(name)) {
return false
Expand Down Expand Up @@ -173,3 +176,17 @@ export function resolveNpmSync (pkgName: string, root): string | null {
return null
}
}

export function recursiveMerge (src, ...args) {
return mergeWith(src, ...args, (value, srcValue) => {
const typeValue = typeof value
const typeSrcValue = typeof srcValue
if (typeValue !== typeSrcValue) return
if (Array.isArray(value) && Array.isArray(srcValue)) {
return value.concat(srcValue)
}
if (typeValue === 'object') {
return recursiveMerge(value, srcValue)
}
})
}
47 changes: 34 additions & 13 deletions packages/taro-mini-runner/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,35 @@ export interface IChain {
[key: string]: any
}

export interface ITaroMiniConfig {
entry: webpack.Entry
output: webpack.Output,
buildAdapter: BUILD_TYPES
export interface ICopyOptions {
patterns: {
from: string
to: string
ignore: string[]
}[]
options: {
ignore: string[]
}
}

export interface ITaroPlugins {
babel: IOption
csso?: TogglableOptions
uglify?: TogglableOptions
export interface ITaroMiniConfig {
webpackChain: (chain: any, webpack: any) => void
alias: IOption
entry: webpack.Entry
output: webpack.Output
enableSourceMap: boolean

cssLoaderOption: IOption
styleLoaderOption: IOption
sassLoaderOption: IOption
lessLoaderOption: IOption
stylusLoaderOption: IOption
mediaUrlLoaderOption: IOption
fontUrlLoaderOption: IOption
imageUrlLoaderOption: IOption
esnextModules: string[]

postcss?: IPostcssOption
}

export interface ICopyOptions {
Expand All @@ -61,9 +80,7 @@ export interface ICopyOptions {
}

export interface ITaroBaseConfig {
outputDir: string
staticDirectory: string
chunkDirectory: string
outputRoot: string
copy: ICopyOptions

designWidth: number
Expand All @@ -72,10 +89,14 @@ export interface ITaroBaseConfig {
defineConstants?: IOption
env?: IOption

plugins: ITaroPlugins
babel: IOption
csso?: TogglableOptions
uglify?: TogglableOptions,
sass?: IOption
}

export interface IBuildConfig extends ITaroBaseConfig, ITaroMiniConfig {
isWatch: boolean,
constantsReplaceList: IOption
port?: number,
buildAdapter: BUILD_TYPES
}
Loading

0 comments on commit 206b96e

Please sign in to comment.