Skip to content

Commit

Permalink
Merge pull request #367 from fahasikei/release-3.6.8-app
Browse files Browse the repository at this point in the history
fix: 修复template中在引用工具类时路径转换错误
  • Loading branch information
qican777 authored Oct 30, 2023
2 parents 7ae1845 + 04a6db7 commit 221583a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/taroize/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export const usedComponents = new Set<string>()
export const errors: string[] = []

export const globals = {
rootPath: '',
hasCatchTrue: false,
logFilePath: '',
}

export const THIRD_PARTY_COMPONENTS = new Set<string>()

export const resetGlobals = (rootPath) => {
globals.rootPath = rootPath
globals.hasCatchTrue = false
globals.logFilePath = path.join(rootPath, 'taroConvert', '.convert', 'convert.log')
THIRD_PARTY_COMPONENTS.clear()
Expand Down
18 changes: 16 additions & 2 deletions packages/taroize/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname, extname, relative, resolve } from 'path'

import { errors } from './global'
import { buildBlockElement, buildRender, getLineBreak, pascalName, printToLogFile, setting } from './utils'
import { createWxmlVistor, parseWXML } from './wxml'
import { createWxmlVistor, parseWXML, WXS } from './wxml'

function isNumeric (n) {
return !isNaN(parseFloat(n)) && isFinite(n)
Expand Down Expand Up @@ -108,7 +108,7 @@ export function preParseTemplate (path: NodePath<t.JSXElement>) {
}
}

export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string, wxses: WXS[]) {
if (!path.container || !path.isJSXElement()) {
return
}
Expand Down Expand Up @@ -150,6 +150,19 @@ export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
const className = buildTemplateName(value.value)

path.traverse(createWxmlVistor(loopIds, refIds, dirPath, [], imports))

// refIds中可能包含wxs模块,应从refIds中去除并单独以模块的形式导入
const usedWxses = new Set<WXS>
const refdata = refIds
refdata.forEach((refId) => {
wxses.forEach((wxsId) => {
if (wxsId.module.includes(refId)) {
usedWxses.add(wxsId)
refIds.delete(refId)
}
})
})

const firstId = Array.from(refIds)[0]
refIds.forEach((id) => {
if (loopIds.has(id) && id !== firstId) {
Expand Down Expand Up @@ -183,6 +196,7 @@ export function parseTemplate (path: NodePath<t.JSXElement>, dirPath: string) {
name: className,
ast: classDecl,
tmplName: templateName,
usedWxses: usedWxses,
}
} else if (is && t.isJSXAttribute(is.node)) {
const value = is.node.value
Expand Down
13 changes: 13 additions & 0 deletions packages/taroize/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ export function isAliasThis (p: NodePath<t.Node>, name: string) {
return false
}

/**
* 标准化传入路径
*
* @param path 如D:\\admin格式路径
* @returns 替换\\返回标准路径
*/
export function normalizePath (path) {
if (typeof path === 'undefined') {
return ''
}
return path.replace(/\\/g, '/')
}

export function isValidVarName (str?: string) {
if (typeof str !== 'string') {
return false
Expand Down
31 changes: 26 additions & 5 deletions packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import {
codeFrameError,
DEFAULT_Component_SET,
isValidVarName,
normalizePath,
parseCode,
} from './utils'

const { prettyPrint } = require('html')
const pathTool = require('path')

const allCamelCase = (str: string) => str.charAt(0).toUpperCase() + camelCase(str.substr(1))

Expand Down Expand Up @@ -246,6 +248,26 @@ export const createPreWxmlVistor = (
} as Visitor
}

/**
* 根据template中使用的wxs组装需要导入的wxs语句
* 如: import xxx from '../xxx.wxs.js'
*
* @param templateFileName template模版文件名
* @param usedWxses template中使用的变量是wxs模块的集合
* @param dirPath 当前解析文件的路径
* @returns 需要导入的wxs语句集合
*/
export function getWxsImports (templateFileName, usedWxses, dirPath) {
const templatePath = pathTool.join(globals.rootPath, 'imports', `${templateFileName}.js`)
const wxsImports: (t.ImportDeclaration | t.VariableDeclaration)[] = []
for (const usedWxs of usedWxses) {
const wxsAbsPath = pathTool.resolve(dirPath, `${usedWxs.src}.js`)
const wxsRelPath = pathTool.relative(pathTool.dirname(templatePath), wxsAbsPath)
wxsImports.push(buildImportStatement(normalizePath(wxsRelPath), [], usedWxs.module))
}
return wxsImports
}

export const createWxmlVistor = (
loopIds: Set<string>,
refIds: Set<string>,
Expand Down Expand Up @@ -401,13 +423,11 @@ export const createWxmlVistor = (
)
}
if (tagName === 'Template') {
// path.traverse({
// JSXAttribute: jsxAttrVisitor
// })
const template = parseTemplate(path, dirPath)
const template = parseTemplate(path, dirPath, wxses)
if (template) {
let funcs = new Set<string>()
const { ast: classDecl, name, tmplName } = template
const { ast: classDecl, name, tmplName, usedWxses } = template
const wxsImports = getWxsImports(name, usedWxses, dirPath)
const taroComponentsImport = buildImportStatement('@tarojs/components', [...usedComponents])
const taroImport = buildImportStatement('@tarojs/taro', [], 'Taro')
const reactImport = buildImportStatement('react', [], 'React')
Expand All @@ -419,6 +439,7 @@ export const createWxmlVistor = (
reactImport,
taroImport,
withWeappImport,
...wxsImports,
classDecl,
t.exportDefaultDeclaration(t.identifier(name))
)
Expand Down

0 comments on commit 221583a

Please sign in to comment.