From 777b5283edeb66e0deed08addd637bd69e81802c Mon Sep 17 00:00:00 2001 From: yuche Date: Wed, 21 Nov 2018 18:25:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(taroize):=20=E6=94=AF=E6=8C=81=E5=86=85?= =?UTF-8?q?=E8=81=94=20wxs=20module,=20close=20#1000?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/taroize/src/wxml.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/taroize/src/wxml.ts b/packages/taroize/src/wxml.ts index 617abb5c7b26..6c75519ce67c 100644 --- a/packages/taroize/src/wxml.ts +++ b/packages/taroize/src/wxml.ts @@ -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) => @@ -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) @@ -195,7 +197,7 @@ export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean } } -function getWXS (attrs: t.JSXAttribute[], path: NodePath): WXS { +function getWXS (attrs: t.JSXAttribute[], path: NodePath, dirPath: string): WXS { let moduleName: string | null = null let src: string | null = null @@ -224,6 +226,15 @@ function getWXS (attrs: t.JSXAttribute[], path: NodePath): 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`') }