Skip to content

Commit

Permalink
fix(cli): prerender 无法正确渲染参数值,close #5994
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Apr 15, 2020
1 parent 82b9309 commit 92a4a81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/taro-mini-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IBuildConfig } from './utils/types'
import { printBuildError, bindProdLogger, bindDevLogger } from './utils/logHelper'
import buildConf from './webpack/build.conf'
import { Prerender } from './prerender/prerender'
import { isEmpty } from 'lodash'

const customizeChain = (chain, customizeFunc: Function) => {
if (customizeFunc instanceof Function) {
Expand Down Expand Up @@ -38,7 +39,7 @@ export default function build (appPath: string, config: IBuildConfig, mainBuilde
return reject(err)
}

if (config.prerender) {
if (!isEmpty(config.prerender)) {
if (prerender == null) {
prerender = new Prerender(config, webpackConfig, stats)
}
Expand Down
30 changes: 28 additions & 2 deletions packages/taro-mini-runner/src/prerender/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ import { join } from 'path'
import { IBuildConfig } from '../utils/types'
import { Adapter } from '../template/adapters'
import { printPrerenderSuccess, printPrerenderFail } from '../utils/logHelper'
import { buildAttribute, Attributes } from '../template'
import { Attributes } from '../template'

const { JSDOM } = require('jsdom')
const wx = require('miniprogram-simulate/src/api')
const micromatch = require('micromatch')

function unquote (str: string) {
const car = str.charAt(0)
const end = str.length - 1
const isQuoteStart = car === '"' || car === "'"
if (isQuoteStart && car === str.charAt(end)) {
return str.slice(1, end)
}
return str
}

function getAttrValue (value: string) {
if (value === 'true' || value === 'false') {
return `{{${value}}}`
}

return unquote(value)
}

interface MiniData {
[Shortcuts.Childnodes]?: MiniData[]
[Shortcuts.NodeName]: string
Expand Down Expand Up @@ -160,6 +178,14 @@ export class Prerender {
}
}

private buildAttributes = (attrs: Attributes) => {
return Object.keys(attrs)
.filter(Boolean)
.filter(k => !k.startsWith('bind') || !k.startsWith('on'))
.map(k => `${k}="${getAttrValue(attrs[k])}" `)
.join('')
}

private renderToXML = (data: MiniData) => {
const nodeName = data[Shortcuts.NodeName]

Expand All @@ -181,7 +207,7 @@ export class Prerender {
return internal.includes(key) || key.startsWith('data-')
})

return `<${nodeName}${style ? ` style="${style}"` : ''}${klass ? ` class="${klass}"` : ''} ${buildAttribute(attrs as Attributes, nodeName)}>${children.map(this.renderToXML).join('')}</${nodeName}>`
return `<${nodeName}${style ? ` style="${style}"` : ''}${klass ? ` class="${klass}"` : ''} ${this.buildAttributes(attrs as Attributes)}>${children.map(this.renderToXML).join('')}</${nodeName}>`
}

private async writeXML (config: PageConfig): Promise<void> {
Expand Down

0 comments on commit 92a4a81

Please sign in to comment.