Skip to content

Commit

Permalink
feat(taroize): 支持内联 wxs module, close #1000
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Nov 21, 2018
1 parent 37fa493 commit 777b528
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { buildTemplate, DEFAULT_Component_SET, buildImportStatement, buildBlockE
import { specialEvents } from './events'
import { parseTemplate, parseModule } from './template'
import { usedComponents, errors } from './global'
import * as fs from 'fs'
import { resolve } from 'path'
// const generate = require('babel-generator').default

const allCamelCase = (str: string) =>
Expand Down Expand Up @@ -133,7 +135,7 @@ export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean
}
const tagName = jsxName.node.name
if (tagName === 'Wxs') {
wxses.push(getWXS(attrs.map(a => a.node), path))
wxses.push(getWXS(attrs.map(a => a.node), path, dirPath))
}
if (tagName === 'Template') {
const template = parseTemplate(path, dirPath)
Expand Down Expand Up @@ -195,7 +197,7 @@ export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean
}
}

function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>): WXS {
function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, dirPath: string): WXS {
let moduleName: string | null = null
let src: string | null = null

Expand Down Expand Up @@ -224,6 +226,15 @@ function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>): WXS {
}
}

if (!src) {
const { children: [ script ] } = path.node
if (!t.isJSXText(script)) {
throw new Error('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。')
}
src = './wxs__' + moduleName
fs.writeFileSync(resolve(dirPath, src + '.wxs'), script.value)
}

if (!moduleName || !src) {
throw new Error('一个 WXS 需要同时存在两个属性:`wxs`, `src`')
}
Expand Down

0 comments on commit 777b528

Please sign in to comment.