Skip to content

Commit

Permalink
fix(mini-runner): 修复多端文件引用的问题,close #5175
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 25, 2019
1 parent f97be38 commit 36e54a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
56 changes: 51 additions & 5 deletions packages/taro-mini-runner/src/loaders/fileParseLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as path from 'path'
import * as fs from 'fs-extra'

import { getOptions } from 'loader-utils'
import { transform, transformFromAst } from 'babel-core'
Expand All @@ -18,7 +19,11 @@ import {
} from '../utils/constants'
import {
isNpmPkg,
isQuickAppPkg
isQuickAppPkg,
isAliasPath,
replaceAliasPath,
resolveScriptPath,
promoteRelativePath
} from '../utils'
import { convertSourceStringToAstExpression } from '../utils/astConvert'
import babylonConfig from '../config/babylon'
Expand All @@ -29,15 +34,27 @@ const cannotRemoves = ['@tarojs/taro', 'react', 'nervjs']

const NON_WEBPACK_REQUIRE = '__non_webpack_require__'

function processAst (
interface IProcessAstArgs {
ast: t.File,
buildAdapter: BUILD_TYPES,
type: PARSE_AST_TYPE,
designWidth: number,
deviceRatio: number,
sourceFilePath: string,
sourceDir: string
) {
sourceDir: string,
alias: object
}

function processAst ({
ast,
buildAdapter,
type,
designWidth,
deviceRatio,
sourceFilePath,
sourceDir,
alias
}: IProcessAstArgs) {
const taroMiniAppFramework = `@tarojs/taro-${buildAdapter}`
let componentClassName: string = ''
let taroJsReduxConnect: string = ''
Expand Down Expand Up @@ -207,6 +224,9 @@ function processAst (
const source = node.source
let value = source.value
const specifiers = node.specifiers
if (isAliasPath(value, alias)) {
value = replaceAliasPath(sourceFilePath, value, alias)
}
if (isQuickApp && isQuickAppPkg(value)) {
let defaultSpecifier: string = 'LOCAL'
specifiers.forEach(item => {
Expand Down Expand Up @@ -268,6 +288,12 @@ function processAst (
}
source.value = value
}
} else {
let vpath = resolveScriptPath(path.resolve(sourceFilePath, '..', value))
if (fs.existsSync(vpath)) {
value = promoteRelativePath(path.relative(sourceFilePath, vpath))
source.value = value.replace(path.extname(value), '')
}
}
},

Expand All @@ -282,6 +308,10 @@ function processAst (
const args = node.arguments as t.StringLiteral[]
let value = args[0].value
const parentNode = astPath.parentPath.parentPath.node as t.VariableDeclaration
if (isAliasPath(value, alias)) {
value = replaceAliasPath(sourceFilePath, value, alias)
args[0].value = value
}
if (isQuickApp && isQuickAppPkg(value)) {
callee.name = NON_WEBPACK_REQUIRE
return
Expand Down Expand Up @@ -332,6 +362,12 @@ function processAst (
}
args[0].value = value
}
} else {
let vpath = resolveScriptPath(path.resolve(sourceFilePath, '..', value))
if (fs.existsSync(vpath)) {
value = promoteRelativePath(path.relative(sourceFilePath, vpath))
args[0].value = value.replace(path.extname(value), '')
}
}
}
},
Expand Down Expand Up @@ -573,6 +609,7 @@ function processAst (
export default function fileParseLoader (source, ast) {
const {
babel: babelConfig,
alias,
buildAdapter,
designWidth,
deviceRatio,
Expand All @@ -586,7 +623,16 @@ export default function fileParseLoader (source, ast) {
]
}).ast as t.File
const miniType = this._module.miniType || PARSE_AST_TYPE.NORMAL
const result = processAst(newAst, buildAdapter, miniType, designWidth, deviceRatio, filePath, sourceDir)
const result = processAst({
ast: newAst,
buildAdapter,
type: miniType,
designWidth,
deviceRatio,
sourceFilePath: filePath,
sourceDir,
alias
})
const code = generate(result).code
const res = transform(code, babelConfig)
return res.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 @@ -156,7 +156,8 @@ export default (appPath: string, mode, config: Partial<IBuildConfig>): any => {
mediaUrlLoaderOption,

postcss,
babel
babel,
alias
}),
plugin,
optimization: {
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-mini-runner/src/webpack/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ export const getModule = (appPath: string, {
mediaUrlLoaderOption,
postcss,

babel
babel,
alias
}) => {
const isQuickapp = buildAdapter === BUILD_TYPES.QUICKAPP
const postcssOption: IPostcssOption = postcss || {}
Expand Down Expand Up @@ -272,6 +273,7 @@ export const getModule = (appPath: string, {

const fileParseLoader = getFileParseLoader([{
babel,
alias,
designWidth,
deviceRatio,
buildAdapter,
Expand Down

0 comments on commit 36e54a8

Please sign in to comment.