提交 5b70a108 编写于 作者: fxy060608's avatar fxy060608

wip(mp): add directive option

上级 e3ac1ea3
......@@ -14,6 +14,7 @@ function assert(
prefixIdentifiers: true,
inline: true,
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
console.log(source)
// expect(source).toBe(templateCode)
......
......@@ -15,6 +15,7 @@ export function assert(
prefixIdentifiers: true,
inline: true,
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
// console.log(source)
if (!options.onError) {
......
......@@ -30,6 +30,7 @@ describe('compiler: transform v-bind', () => {
onError,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -53,6 +54,7 @@ describe('compiler: transform v-bind', () => {
onError,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -123,6 +125,7 @@ describe('compiler: transform v-bind', () => {
prefixIdentifiers: false,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -151,6 +154,7 @@ describe('compiler: transform v-bind', () => {
prefixIdentifiers: true,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -197,6 +201,7 @@ describe('compiler: transform v-bind', () => {
filename: 'foo.vue',
prefixIdentifiers: false,
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -225,6 +230,7 @@ describe('compiler: transform v-bind', () => {
prefixIdentifiers: true,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view/>`)
return ''
......@@ -252,6 +258,7 @@ describe('compiler: transform v-bind', () => {
onWarn,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view fooBar="{{a}}"/>`)
return ''
......@@ -279,6 +286,7 @@ describe('compiler: transform v-bind', () => {
onWarn,
filename: 'foo.vue',
miniProgram: {
directive: 'wx:',
emitFile({ source }) {
expect(source).toBe(`<view foo-bar="{{a}}"/>`)
return ''
......
......@@ -60,6 +60,7 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
if (options.filename && options.miniProgram?.emitFile) {
genTemplate(ast, {
filename: options.filename,
directive: options.miniProgram.directive,
emitFile: options.miniProgram.emitFile,
})
}
......
......@@ -63,12 +63,14 @@ export interface CodegenOptions extends SharedTransformCodegenOptions {
runtimeModuleName?: string
runtimeGlobalName?: string
miniProgram?: {
directive: string
emitFile?: (emittedFile: EmittedFile) => string
}
}
export interface TemplateCodegenOptions {
filename: string
directive: string
emitFile: (emittedFile: EmittedFile) => string
}
......
......@@ -17,15 +17,17 @@ import { isForElementNode, VForOptions } from '../transforms/vFor'
import { IfElementNode, isIfElementNode } from '../transforms/vIf'
interface TemplateCodegenContext {
code: string
directive: string
push(code: string): void
}
export function generate(
{ children }: RootNode,
{ emitFile, filename }: TemplateCodegenOptions
{ emitFile, filename, directive }: TemplateCodegenOptions
) {
const context: TemplateCodegenContext = {
code: '',
directive,
push(code) {
context.code += code
},
......@@ -62,32 +64,32 @@ function genExpression(node: ExpressionNode, { push }: TemplateCodegenContext) {
push(`{{${genExpr(node)}}}`)
}
function genVIf(exp: string, { push }: TemplateCodegenContext) {
push(` wx:if="{{${exp}}}"`)
function genVIf(exp: string, { push, directive }: TemplateCodegenContext) {
push(` ${directive}if="{{${exp}}}"`)
}
function genVElseIf(exp: string, { push }: TemplateCodegenContext) {
push(` wx:elif="{{${exp}}}"`)
function genVElseIf(exp: string, { push, directive }: TemplateCodegenContext) {
push(` ${directive}elif="{{${exp}}}"`)
}
function genVElse({ push }: TemplateCodegenContext) {
push(` wx:else`)
function genVElse({ push, directive }: TemplateCodegenContext) {
push(` ${directive}else`)
}
function genVFor(
{ sourceAlias, valueAlias, keyAlias }: VForOptions,
node: ElementNode,
{ push }: TemplateCodegenContext
{ push, directive }: TemplateCodegenContext
) {
push(` wx:for="{{${sourceAlias}}}"`)
push(` ${directive}for="{{${sourceAlias}}}"`)
if (valueAlias) {
push(` wx:for-item="${valueAlias}"`)
push(` ${directive}for-item="${valueAlias}"`)
}
if (keyAlias) {
push(` wx:for-index="${keyAlias}"`)
push(` ${directive}for-index="${keyAlias}"`)
}
const keyProp = findProp(node, 'key', true)
if (keyProp) {
const key = ((keyProp as DirectiveNode).exp as SimpleExpressionNode).content
push(` wx:key="${key.includes('.') ? key.split('.')[1] : key}"`)
push(` ${directive}key="${key.includes('.') ? key.split('.')[1] : key}"`)
node.props.splice(node.props.indexOf(keyProp), 1)
}
}
......
......@@ -32,6 +32,7 @@ export interface UniMiniProgramPluginOptions {
}
template: {
extname: string
directive: string
}
style: {
extname: string
......@@ -75,7 +76,7 @@ export function uniMiniProgramPlugin(
name: 'vite:uni-mp',
uni: uniOptions({
copyOptions,
miniProgram: { emitFile },
miniProgram: { directive: template.directive, emitFile },
}),
config() {
return {
......
......@@ -14,6 +14,7 @@ export function uniOptions({
}: {
copyOptions: CopyOptions
miniProgram: {
directive: string
emitFile?: (emittedFile: EmittedFile) => string
}
}): UniVitePlugin['uni'] {
......
......@@ -56,6 +56,7 @@ const options: UniMiniProgramPluginOptions = {
},
template: {
extname: '.wxml',
directive: 'wx:',
},
style: {
extname: '.wxss',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册