Skip to content

Commit

Permalink
feat(mini-runner): 引用原生组件支持 alias,close #8472
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-jj committed Jan 21, 2021
1 parent e66a7cb commit ce56d5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 49 deletions.
24 changes: 18 additions & 6 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 Down Expand Up @@ -51,6 +53,7 @@ interface ITaroMiniPluginOptions {
modifyBuildAssets?: Function
modifyMiniConfigs?: Function
blended: boolean
alias: Record<string, string>
}

export interface IComponentObj {
Expand Down Expand Up @@ -485,19 +488,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 @@ -522,6 +529,11 @@ export default class TaroMiniPlugin {
}
})
}

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

/**
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
3 changes: 2 additions & 1 deletion packages/taro-mini-runner/src/webpack/build.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
modifyMiniConfigs,
modifyBuildAssets,
minifyXML,
blended
blended,
alias
})

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
Expand Down

0 comments on commit ce56d5b

Please sign in to comment.