Skip to content

Commit

Permalink
fix(mini-runner): 修复对 alias 的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Jan 7, 2020
1 parent 0c6b31f commit 3786d6b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
34 changes: 21 additions & 13 deletions packages/taro-mini-runner/src/plugins/MiniPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as _ from 'lodash'

import { REG_TYPESCRIPT, BUILD_TYPES, PARSE_AST_TYPE, MINI_APP_FILES, NODE_MODULES_REG, CONFIG_MAP, taroJsFramework, REG_SCRIPTS, processTypeEnum } from '../utils/constants'
import { IComponentObj } from '../utils/types'
import { resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject, promoteRelativePath, printLog } from '../utils'
import { resolveScriptPath, buildUsingComponents, isNpmPkg, resolveNpmSync, isEmptyObject, promoteRelativePath, printLog, isAliasPath, replaceAliasPath } from '../utils'
import TaroSingleEntryDependency from '../dependencies/TaroSingleEntryDependency'
import { getTaroJsQuickAppComponentsPath, generateQuickAppUx, getImportTaroSelfComponents, generateQuickAppManifest } from '../utils/helper'
import parseAst from '../utils/parseAst'
Expand All @@ -37,7 +37,8 @@ interface IMiniPluginOptions {
designWidth: number,
commonChunks: string[],
pluginConfig?: object,
isBuildPlugin: boolean
isBuildPlugin: boolean,
alias: object
}

export interface ITaroFileInfo {
Expand Down Expand Up @@ -150,7 +151,8 @@ export default class MiniPlugin {
outputDir: '',
designWidth: 750,
commonChunks: ['runtime', 'vendors'],
isBuildPlugin: false
isBuildPlugin: false,
alias: {}
})
this.sourceDir = this.options.sourceDir
this.outputDir = this.options.outputDir
Expand Down Expand Up @@ -325,15 +327,21 @@ export default class MiniPlugin {
return componentRealPath
}

transfromComponentsPath (components: IComponentObj[]) {
const { buildAdapter } = this.options
transfromComponentsPath (filePath, components: IComponentObj[]) {
const { buildAdapter, alias } = this.options
components.forEach(component => {
const componentPath = component.path
let componentPath = component.path
let realComponentPath
if (componentPath && isNpmPkg(componentPath)) {
const res = resolveNpmSync(componentPath, this.context)
const code = fs.readFileSync(res).toString()
const newComponent = Object.assign({}, component, { path: res })
const realComponentPath = this.getNpmComponentRealPath(code, newComponent, buildAdapter)
if (isAliasPath(componentPath, alias)) {
componentPath = replaceAliasPath(filePath, componentPath, alias)
realComponentPath = resolveScriptPath(path.resolve(filePath, '..', componentPath as string))
} else {
const res = resolveNpmSync(componentPath, this.context)
const code = fs.readFileSync(res).toString()
const newComponent = Object.assign({}, component, { path: res })
realComponentPath = this.getNpmComponentRealPath(code, newComponent, buildAdapter)
}
component.path = realComponentPath
}
})
Expand Down Expand Up @@ -517,7 +525,7 @@ export default class MiniPlugin {
}

getComponents (fileList: Set<IComponent>, isRoot: boolean) {
const { buildAdapter } = this.options
const { buildAdapter, alias } = this.options
const isQuickApp = buildAdapter === BUILD_TYPES.QUICKAPP
fileList.forEach(file => {
const isNative = file.isNative
Expand Down Expand Up @@ -597,7 +605,7 @@ export default class MiniPlugin {
code = transformResult.code
}
depComponents = depComponents.filter(item => !/^plugin:\/\//.test(item.path))
this.transfromComponentsPath(depComponents)
this.transfromComponentsPath(file.path, depComponents)
if (isQuickApp) {
const scriptPath = file.path
const outputScriptPath = scriptPath.replace(this.sourceDir, this.outputDir).replace(path.extname(scriptPath), MINI_APP_FILES[buildAdapter].SCRIPT)
Expand Down Expand Up @@ -626,7 +634,7 @@ export default class MiniPlugin {
printLog(processTypeEnum.COMPILE, isRoot ? '发现页面' : '发现组件', file.path)
taroFileTypeMap[file.path] = {
type: isRoot ? PARSE_AST_TYPE.PAGE : PARSE_AST_TYPE.COMPONENT,
config: merge({}, isComponentConfig, buildUsingComponents(file.path, this.sourceDir, {}, depComponents), configObj),
config: merge({}, isComponentConfig, buildUsingComponents(file.path, this.sourceDir, alias, depComponents), configObj),
template,
code
}
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 @@ -93,7 +93,8 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
designWidth,
pluginConfig: entryRes!.pluginConfig,
isBuildPlugin: !!config.isBuildPlugin,
commonChunks: !!config.isBuildPlugin ? ['plugin/runtime', 'plugin/vendors'] : ['runtime', 'vendors']
commonChunks: !!config.isBuildPlugin ? ['plugin/runtime', 'plugin/vendors'] : ['runtime', 'vendors'],
alias
})

plugin.miniCssExtractPlugin = getMiniCssExtractPlugin([{
Expand Down

0 comments on commit 3786d6b

Please sign in to comment.