Skip to content

Commit

Permalink
fix(mini-runner): 修复小程序混写时模板引入只有第一个生效的问题 (#7454)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzono authored Aug 27, 2020
1 parent 3453ca7 commit e981c93
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/taro-mini-runner/src/loaders/miniTemplateLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { isUrlRequest, urlToRequest } from 'loader-utils'

export default function miniTemplateLoader (source) {
this.cacheable && this.cacheable()
/**
* 两种fix方案:
* 1. 用任意xml标签包裹source,使之变成较标准的xml格式(含有一个根节点)
* 2. 修改 sax.parser 的第一个参数为 true,启用严格模式
* 2.1 该模式下小程序模板中的标签或属性不会处理(例如写入<Import SrC="..." />不会处理成<import src="..." />,而是保持原样
* 2.2 该模式将认为传入的xml为非标准的,无需标准化,且不按照以根节点模式处理,因此可以正常解析小程序模板
*
* 推荐方案1,这样在构建时会正常打入需要的包,但是若用户有 SrC 类似的写法导致引用失败,则可直接修正,不会认为是打包出现了问题
**/
const sourceWithRoot = `<root>${source}</root>`
const parser = sax.parser(false, { lowercase: true })
const requests: Set<string> = new Set()
const callback = this.async()
Expand All @@ -12,8 +22,7 @@ export default function miniTemplateLoader (source) {
this.loadModule(request, (err, src) => {
if (err) {
reject(err)
}
else {
} else {
resolve(src)
}
})
Expand All @@ -38,5 +47,5 @@ export default function miniTemplateLoader (source) {
callback(error, source)
}
}
parser.write(source).close()
parser.write(sourceWithRoot).close()
}

0 comments on commit e981c93

Please sign in to comment.