Skip to content

Commit

Permalink
feat(taro): 加入快应用编译支持
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 31, 2019
1 parent ddf2199 commit 0d7e5c8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
31 changes: 29 additions & 2 deletions packages/taro-mini-runner/src/loaders/fileParseLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const template = require('babel-template')

const cannotRemoves = ['@tarojs/taro', 'react', 'nervjs']

const NON_WEBPACK_REQUIRE = '__non_webpack_require__'

function processAst (
ast: t.File,
buildAdapter: BUILD_TYPES,
Expand Down Expand Up @@ -173,10 +175,31 @@ function processAst (
const source = node.source
let value = source.value
const specifiers = node.specifiers
if (isQuickApp && isQuickAppPkg(value)) {
let defaultSpecifier: string = 'LOCAL'
specifiers.forEach(item => {
if (item.type === 'ImportDefaultSpecifier') {
defaultSpecifier = item.local.name
}
})
astPath.replaceWith(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(defaultSpecifier),
t.callExpression(
t.identifier(NON_WEBPACK_REQUIRE),[
t.stringLiteral(value)
]
)
)
])
)
return
}
if (NODE_MODULES_REG.test(sourceFilePath) && sourceFilePath.indexOf(taroMiniAppFramework) >= 0) {
return
}
if (isNpmPkg(value) && !isQuickAppPkg(value)) {
if (isNpmPkg(value)) {
if (value === taroJsComponents) {
if (isQuickApp) {
specifiers.forEach(specifier => {
Expand Down Expand Up @@ -227,10 +250,14 @@ function processAst (
const args = node.arguments as t.StringLiteral[]
let value = args[0].value
const parentNode = astPath.parentPath.parentPath.node as t.VariableDeclaration
if (isQuickApp && isQuickAppPkg(value)) {
callee.name = NON_WEBPACK_REQUIRE
return
}
if (NODE_MODULES_REG.test(sourceFilePath) && sourceFilePath.indexOf(taroMiniAppFramework) >= 0) {
return
}
if (isNpmPkg(value) && !isQuickAppPkg(value)) {
if (isNpmPkg(value)) {
if (value === taroJsComponents) {
if (isQuickApp) {
if (parentNode.declarations.length === 1 && parentNode.declarations[0].init) {
Expand Down
1 change: 1 addition & 0 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const Targets = {
[BUILD_TYPES.SWAN]: createTarget(BUILD_TYPES.SWAN),
[BUILD_TYPES.TT]: createTarget(BUILD_TYPES.TT),
[BUILD_TYPES.QQ]: createTarget(BUILD_TYPES.QQ),
[BUILD_TYPES.QUICKAPP]: createTarget(BUILD_TYPES.QUICKAPP)
}

export function isFileToBeTaroComponent (
Expand Down
18 changes: 18 additions & 0 deletions packages/taro-mini-runner/src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as path from 'path'
import * as fs from 'fs'
import { isEmptyObject } from '.'

let quickappConfig = {}

export function getQuickappConfig (appPath) {
if (!isEmptyObject(quickappConfig)) {
return quickappConfig
}
const configPath = path.join(appPath, 'project.quickapp.json')
if (!fs.existsSync(configPath)) {
quickappConfig = require('../config/manifest.default.json')
} else {
quickappConfig = JSON.parse(fs.readFileSync(configPath).toString())
}
return quickappConfig
}
2 changes: 1 addition & 1 deletion packages/taro-mini-runner/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function isNpmPkg (name: string): boolean {
}

export function isQuickAppPkg (name: string): boolean {
return /@system\./.test(name)
return /^@(system|service)\.[a-zA-Z]{1,}/.test(name)
}

export function isEmptyObject (obj: any): boolean {
Expand Down
24 changes: 20 additions & 4 deletions packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
getModule,
mergeOption,
getMiniPlugin,
getMiniCssExtractPlugin
getMiniCssExtractPlugin,
} from './chain'
import { BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES } from '../utils/constants'
import { Targets } from '../plugins/MiniPlugin'
import { getQuickappConfig } from '../utils/helper'

const emptyObj = {}

Expand Down Expand Up @@ -56,6 +57,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
const plugin: any = {}
const minimizer: any[] = []
const sourceDir = path.join(appPath, sourceRoot)
const isQuickapp = buildAdapter === BUILD_TYPES.QUICKAPP

if (copy) {
plugin.copyWebpackPlugin = getCopyWebpackPlugin({ copy, appPath })
Expand Down Expand Up @@ -90,8 +92,7 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
plugin.cssoWebpackPlugin = getCssoWebpackPlugin([csso ? csso.config : {}])
}
}

chain.merge({
const mainConfig = {
mode,
devtool: getDevtool(enableSourceMap),
entry,
Expand Down Expand Up @@ -156,6 +157,21 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
}
}
}
})
}

// if (isQuickapp) {
// const quickappConfig = getQuickappConfig(appPath)
// const features = quickappConfig['features']
// if (features && features.length) {
// const externals = {}
// features.forEach(item => {
// externals[`@${item.name}`] = {
// root: `@${item.name}`
// }
// })
// mainConfig['externals'] = externals
// }
// }
chain.merge(mainConfig)
return chain
}

0 comments on commit 0d7e5c8

Please sign in to comment.