Skip to content

Commit

Permalink
feat(taro-mini-runner): 生成多端类型文件
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent 15dede3 commit 9029d25
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 21 deletions.
9 changes: 6 additions & 3 deletions packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import * as webpack from 'webpack'

import { IBuildConfig } from './utils/types'
import { printBuildError, bindProdLogger } from './utils/logHelper'
import MiniPlugin from './plugins/miniPlugin'
import MiniPlugin, { Targets } from './plugins/MiniPlugin'
import { MINI_APP_FILES } from './utils/constants'

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

export default function build (config: IBuildConfig) {
const compilePlugins = config.plugins
Expand Down Expand Up @@ -44,14 +47,14 @@ export default function build (config: IBuildConfig) {
}]
},
{
test: /\.(scss|wxss|acss|)$/,
test: /\.(scss|wxss|acss|ttss|acss|)$/,
include: /src/,
use: [
{
loader: require.resolve('file-loader'),
options: {
useRelativePath: true,
name: `[path][name].wxss`,
name: `[path][name]${MINI_APP_FILES[config.buildAdapter].STYLE}`,
context: config.sourceDir
}
},
Expand Down
32 changes: 14 additions & 18 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as t from 'babel-types'
import traverse from 'babel-traverse'
import { Config as IConfig } from '@tarojs/taro'

import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE } from '../utils/constants'
import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES } from '../utils/constants'
import { traverseObjectNode, resolveScriptPath, buildUsingComponents } from '../utils'

import TaroTemplatePlugin from './TaroTemplatePlugin'
Expand All @@ -23,14 +23,14 @@ import TaroLoadChunksPlugin from './TaroLoadChunksPlugin'
interface IMiniPluginOptions {
appEntry?: string,
buildAdapter: BUILD_TYPES,
commonChunks?: string[]
commonChunks: string[]
}

export interface ITaroFileInfo {
[key: string]: {
type: PARSE_AST_TYPE,
config: IConfig,
wxml?: string,
template?: string,
code?: string
}
}
Expand All @@ -41,19 +41,14 @@ const PLUGIN_NAME = 'MiniPlugin'

const taroFileTypeMap: ITaroFileInfo = {}

export const createTarget = function createTarget(name) {
const target = (compiler: webpack.compiler.Compiler) => {
export const createTarget = function createTarget (name) {
return (compiler: webpack.compiler.Compiler) => {
const { options } = compiler
new TaroTemplatePlugin().apply(compiler)
new FunctionModulePlugin(options.output).apply(compiler)
new NodeSourcePlugin(options.node).apply(compiler)
new LoaderTargetPlugin('web').apply(compiler)
}

const creater = new Function(
`var t = arguments[0]; return function ${name}(c) { return t(c); }`
);
return creater(target)
}
}

export const Targets = {
Expand Down Expand Up @@ -209,7 +204,7 @@ export default class MiniPlugin {
taroFileTypeMap[this.appEntry] = {
type: PARSE_AST_TYPE.ENTRY,
config: configObj,
wxml: transformResult.template,
template: transformResult.template,
code: transformResult.code
}
this.pages = new Set([
Expand All @@ -235,7 +230,7 @@ export default class MiniPlugin {
taroFileTypeMap[file.path] = {
type: isRoot ? PARSE_AST_TYPE.PAGE : PARSE_AST_TYPE.COMPONENT,
config: merge({}, buildUsingComponents(file.path, {}, transformResult.components),configObj),
wxml: transformResult.template,
template: transformResult.template,
code: transformResult.code
}
let depComponents = transformResult.components
Expand Down Expand Up @@ -268,16 +263,17 @@ export default class MiniPlugin {
}

generateMiniFiles (compilation: webpack.compilation.Compilation) {
const { buildAdapter } = this.options
Object.keys(taroFileTypeMap).forEach(item => {
const relativePath = item.replace(this.sourceDir, '')
const extname = path.extname(item)
const wxmlPath = relativePath.replace(extname, '.wxml')
const jsonPath = relativePath.replace(extname, '.json')
const templatePath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].TEMPL)
const jsonPath = relativePath.replace(extname, MINI_APP_FILES[buildAdapter].CONFIG)
const itemInfo = taroFileTypeMap[item]
if (itemInfo.type !== PARSE_AST_TYPE.ENTRY) {
compilation.assets[wxmlPath] = {
size: () => itemInfo.wxml!.length,
source: () => itemInfo.wxml
compilation.assets[templatePath] = {
size: () => itemInfo.template!.length,
source: () => itemInfo.template
}
}
const jsonStr = JSON.stringify(itemInfo.config)
Expand Down
85 changes: 85 additions & 0 deletions packages/taro-mini-runner/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,91 @@ export const enum BUILD_TYPES {
QQ = 'qq'
}

export const enum TEMPLATE_TYPES {
WEAPP = '.wxml',
SWAN = '.swan',
ALIPAY = '.axml',
TT = '.ttml',
QUICKAPP = '.ux',
QQ = '.qml'
}

export const enum STYLE_TYPES {
WEAPP = '.wxss',
SWAN = '.css',
ALIPAY = '.acss',
TT = '.ttss',
QUICKAPP = '.css',
QQ = '.qss'
}

export const enum SCRIPT_TYPES {
WEAPP = '.js',
SWAN = '.js',
ALIPAY = '.js',
TT = '.js',
QUICKAPP = '.js',
QQ = '.js'
}

export const enum CONFIG_TYPES {
WEAPP = '.json',
SWAN = '.json',
ALIPAY = '.json',
TT = '.json',
QUICKAPP = '.json',
QQ = '.json'
}

export type IMINI_APP_FILE_TYPE = {
TEMPL: TEMPLATE_TYPES,
STYLE: STYLE_TYPES,
SCRIPT: SCRIPT_TYPES,
CONFIG: CONFIG_TYPES
}

export type IMINI_APP_FILES = {
[key: string]: IMINI_APP_FILE_TYPE
}
export const MINI_APP_FILES: IMINI_APP_FILES = {
[BUILD_TYPES.WEAPP]: {
TEMPL: TEMPLATE_TYPES.WEAPP,
STYLE: STYLE_TYPES.WEAPP,
SCRIPT: SCRIPT_TYPES.WEAPP,
CONFIG: CONFIG_TYPES.WEAPP
},
[BUILD_TYPES.SWAN]: {
TEMPL: TEMPLATE_TYPES.SWAN,
STYLE: STYLE_TYPES.SWAN,
SCRIPT: SCRIPT_TYPES.SWAN,
CONFIG: CONFIG_TYPES.SWAN
},
[BUILD_TYPES.ALIPAY]: {
TEMPL: TEMPLATE_TYPES.ALIPAY,
STYLE: STYLE_TYPES.ALIPAY,
SCRIPT: SCRIPT_TYPES.ALIPAY,
CONFIG: CONFIG_TYPES.ALIPAY
},
[BUILD_TYPES.TT]: {
TEMPL: TEMPLATE_TYPES.TT,
STYLE: STYLE_TYPES.TT,
SCRIPT: SCRIPT_TYPES.TT,
CONFIG: CONFIG_TYPES.TT
},
[BUILD_TYPES.QUICKAPP]: {
TEMPL: TEMPLATE_TYPES.QUICKAPP,
STYLE: STYLE_TYPES.QUICKAPP,
SCRIPT: SCRIPT_TYPES.QUICKAPP,
CONFIG: CONFIG_TYPES.QUICKAPP
},
[BUILD_TYPES.QQ]: {
TEMPL: TEMPLATE_TYPES.QQ,
STYLE: STYLE_TYPES.QQ,
SCRIPT: SCRIPT_TYPES.QQ,
CONFIG: CONFIG_TYPES.QQ
}
}

export const CONFIG_MAP = {
[BUILD_TYPES.WEAPP]: {
navigationBarTitleText: 'navigationBarTitleText',
Expand Down

0 comments on commit 9029d25

Please sign in to comment.