提交 7bc2b65f 编写于 作者: fxy060608's avatar fxy060608

wip(mp): support node_modules component

上级 fddbaa7f
export const COMPONENT_ON_LINK = 'onVI'
export const COMPONENT_BIND_LINK = '__l'
...@@ -2,4 +2,5 @@ export * from './nvue' ...@@ -2,4 +2,5 @@ export * from './nvue'
export * from './event' export * from './event'
export * from './style' export * from './style'
export * from './template' export * from './template'
export * from './constants'
export { transformVueComponentImports } from './transformImports' export { transformVueComponentImports } from './transformImports'
...@@ -7,21 +7,22 @@ export { default as hash } from 'hash-sum' ...@@ -7,21 +7,22 @@ export { default as hash } from 'hash-sum'
import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants' import { PAGE_EXTNAME, PAGE_EXTNAME_APP } from './constants'
import { SFCTemplateCompileOptions } from '@vue/compiler-sfc' import { SFCTemplateCompileOptions } from '@vue/compiler-sfc'
import { NodeTypes, ElementNode, Node } from '@vue/compiler-core' import {
NodeTypes,
ElementNode,
RootNode,
TemplateChildNode,
} from '@vue/compiler-core'
export const isWindows = os.platform() === 'win32' export const isWindows = os.platform() === 'win32'
export function normalizePath(id: string): string { export function normalizePath(id: string): string {
return isWindows ? id.replace(/\\/g, '/') : id return isWindows ? id.replace(/\\/g, '/') : id
} }
export function checkElementNodeTag( export function checkElementNodeTag(
node: Node | null | undefined, node: RootNode | TemplateChildNode | null | undefined,
tag: string tag: string
): node is ElementNode { ): node is ElementNode {
return ( return !!node && node.type === NodeTypes.ELEMENT && node.tag === tag
!!node &&
node.type === NodeTypes.ELEMENT &&
(node as ElementNode).tag === tag
)
} }
export const resolveMainPathOnce = once((inputDir: string) => { export const resolveMainPathOnce = once((inputDir: string) => {
......
...@@ -9,30 +9,35 @@ import { ...@@ -9,30 +9,35 @@ import {
TemplateChildNode, TemplateChildNode,
TransformContext, TransformContext,
} from '@vue/compiler-core' } from '@vue/compiler-core'
import { COMPONENT_BIND_LINK, COMPONENT_ON_LINK } from '../../mp/constants'
export function addComponentBindLink( export function createTransformComponentLink(
node: RootNode | TemplateChildNode, name: typeof COMPONENT_BIND_LINK | typeof COMPONENT_ON_LINK
context: TransformContext
) { ) {
if ( return function transformComponentLink(
node.type === NodeTypes.ELEMENT && node: RootNode | TemplateChildNode,
node.tagType === ElementTypes.COMPONENT context: TransformContext
) { ) {
const { tag } = node
if ( if (
isComponentTag(tag) || node.type === NodeTypes.ELEMENT &&
isCoreComponent(tag) || node.tagType === ElementTypes.COMPONENT
context.isBuiltInComponent(tag)
) { ) {
return const { tag } = node
if (
isComponentTag(tag) ||
isCoreComponent(tag) ||
context.isBuiltInComponent(tag)
) {
return
}
node.props.push({
type: NodeTypes.DIRECTIVE,
name: 'on',
modifiers: [],
loc: locStub,
arg: createSimpleExpression(name, true),
exp: createSimpleExpression('__l', true),
})
} }
node.props.push({
type: NodeTypes.DIRECTIVE,
name: 'on',
modifiers: [],
loc: locStub,
arg: createSimpleExpression('__l', true),
exp: createSimpleExpression('__l', true),
})
} }
} }
...@@ -148,7 +148,7 @@ const miniProgram = { ...@@ -148,7 +148,7 @@ const miniProgram = {
array: true, array: true,
}, },
slot: { slot: {
fallback: false, fallback: true,
}, },
directive: 's-', directive: 's-',
}; };
......
...@@ -12,7 +12,7 @@ export const miniProgram: MiniProgramCompilerOptions = { ...@@ -12,7 +12,7 @@ export const miniProgram: MiniProgramCompilerOptions = {
array: true, array: true,
}, },
slot: { slot: {
fallback: false, fallback: true,
}, },
directive: 's-', directive: 's-',
} }
......
import { addComponentBindLink } from '@dcloudio/uni-cli-shared' import {
COMPONENT_BIND_LINK,
createTransformComponentLink,
} from '@dcloudio/uni-cli-shared'
import { assert } from './testUtils' import { assert } from './testUtils'
const nodeTransforms = [createTransformComponentLink(COMPONENT_BIND_LINK)]
describe('compiler: transform component', () => { describe('compiler: transform component', () => {
// test('basic', () => {
// assert(
// `<custom/>`,
// `<custom v-i="2a9ec0b0-0" bind:__l="__l"/>`,
// `(_ctx, _cache) => {
// return {}
// }`,
// {
// nodeTransforms: [addComponentBindLink as any],
// }
// )
// })
test('component + component', () => { test('component + component', () => {
assert( assert(
`<custom><custom1/></custom>`, `<custom><custom1/></custom>`,
...@@ -22,7 +14,7 @@ describe('compiler: transform component', () => { ...@@ -22,7 +14,7 @@ describe('compiler: transform component', () => {
return {} return {}
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
...@@ -34,7 +26,7 @@ describe('compiler: transform component', () => { ...@@ -34,7 +26,7 @@ describe('compiler: transform component', () => {
return {} return {}
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
...@@ -46,7 +38,7 @@ describe('compiler: transform component', () => { ...@@ -46,7 +38,7 @@ describe('compiler: transform component', () => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-0' + '-' + i0 }; }) }
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
assert( assert(
...@@ -56,7 +48,7 @@ describe('compiler: transform component', () => { ...@@ -56,7 +48,7 @@ describe('compiler: transform component', () => {
return { a: _f(_ctx.items, (item, key, index) => { return { a: '2a9ec0b0-0' + '-' + index }; }) } return { a: _f(_ctx.items, (item, key, index) => { return { a: '2a9ec0b0-0' + '-' + index }; }) }
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
...@@ -68,7 +60,7 @@ describe('compiler: transform component', () => { ...@@ -68,7 +60,7 @@ describe('compiler: transform component', () => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }) }
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
...@@ -80,7 +72,7 @@ describe('compiler: transform component', () => { ...@@ -80,7 +72,7 @@ describe('compiler: transform component', () => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: '2a9ec0b0-1' + '-' + i0 + ',' + ('2a9ec0b0-0' + '-' + i0), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
...@@ -92,7 +84,7 @@ describe('compiler: transform component', () => { ...@@ -92,7 +84,7 @@ describe('compiler: transform component', () => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) } return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.items, (item1, k1, i1) => { return { a: '2a9ec0b0-1' + '-' + i0 + '-' + i1 + ',' + ('2a9ec0b0-0' + '-' + i0) }; }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
}`, }`,
{ {
nodeTransforms: [addComponentBindLink as any], nodeTransforms,
} }
) )
}) })
......
import { assert, miniProgram } from './testUtils'
const options = {
miniProgram: {
...miniProgram,
slot: {
fallback: true,
},
},
}
describe('compiler: transform slot', () => {
test('basic', () => {
assert(
`<button><slot/></button>`,
`<button><slot/></button>`,
`(_ctx, _cache) => {
return {}
}`,
options
)
})
test('fallback content', () => {
assert(
`<button><slot>Submit</slot></button>`,
`<button><slot>Submit</slot></button>`,
`(_ctx, _cache) => {
return {}
}`,
options
)
})
test('names slots', () => {
assert(
`<button><slot name="text"/></button>`,
`<button><slot name="text"/></button>`,
`(_ctx, _cache) => {
return {}
}`,
options
)
})
test('names slots with fallback content', () => {
assert(
`<button><slot name="text">Submit</slot></button>`,
`<button><slot name="text">Submit</slot></button>`,
`(_ctx, _cache) => {
return {}
}`,
options
)
})
})
...@@ -114,7 +114,8 @@ function genVFor( ...@@ -114,7 +114,8 @@ function genVFor(
function genSlot(node: SlotOutletNode, context: TemplateCodegenContext) { function genSlot(node: SlotOutletNode, context: TemplateCodegenContext) {
// 移除掉所有非name属性,即移除作用域插槽的绑定指令 // 移除掉所有非name属性,即移除作用域插槽的绑定指令
node.props = node.props.filter((prop) => prop.name === 'name') node.props = node.props.filter((prop) => prop.name === 'name')
if (!node.children.length) { if (!node.children.length || context.slot.fallback) {
// 无后备内容或支持后备内容
return genElement(node, context) return genElement(node, context)
} }
const children = node.children.slice() const children = node.children.slice()
......
...@@ -133,7 +133,7 @@ const options = { ...@@ -133,7 +133,7 @@ const options = {
extname: '.qml', extname: '.qml',
directive: 'qq:', directive: 'qq:',
compilerOptions: { compilerOptions: {
nodeTransforms: [uniCliShared.addComponentBindLink], nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
import path from 'path' import path from 'path'
import { addComponentBindLink } from '@dcloudio/uni-cli-shared' import {
COMPONENT_BIND_LINK,
createTransformComponentLink,
} from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
import source from './project.config.json' import source from './project.config.json'
...@@ -56,7 +59,7 @@ export const options: UniMiniProgramPluginOptions = { ...@@ -56,7 +59,7 @@ export const options: UniMiniProgramPluginOptions = {
extname: '.qml', extname: '.qml',
directive: 'qq:', directive: 'qq:',
compilerOptions: { compilerOptions: {
nodeTransforms: [addComponentBindLink], nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
...@@ -69,12 +69,12 @@ ${filter.code} ...@@ -69,12 +69,12 @@ ${filter.code}
}, },
}, },
slot: { slot: {
fallback: false, fallback: true,
}, },
extname: '.ttml', extname: '.ttml',
directive: 'tt:', directive: 'tt:',
compilerOptions: { compilerOptions: {
nodeTransforms: [uniCliShared.addComponentBindLink], nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
import path from 'path' import path from 'path'
import { addComponentBindLink } from '@dcloudio/uni-cli-shared' import {
COMPONENT_BIND_LINK,
createTransformComponentLink,
} from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
import source from './project.config.json' import source from './project.config.json'
...@@ -44,12 +47,12 @@ ${filter.code} ...@@ -44,12 +47,12 @@ ${filter.code}
}, },
}, },
slot: { slot: {
fallback: false, fallback: true,
}, },
extname: '.ttml', extname: '.ttml',
directive: 'tt:', directive: 'tt:',
compilerOptions: { compilerOptions: {
nodeTransforms: [addComponentBindLink], nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
hasJsonFile, hasJsonFile,
removeExt, removeExt,
resolveMainPathOnce, resolveMainPathOnce,
normalizeMiniProgramFilename,
} from '@dcloudio/uni-cli-shared' } from '@dcloudio/uni-cli-shared'
import { GetManualChunk, GetModuleInfo } from 'rollup' import { GetManualChunk, GetModuleInfo } from 'rollup'
import { import {
...@@ -57,10 +58,7 @@ export function buildOptions(): UserConfig['build'] { ...@@ -57,10 +58,7 @@ export function buildOptions(): UserConfig['build'] {
parseVirtualComponentPath(id) parseVirtualComponentPath(id)
) )
} }
const filepath = path.relative(inputDir, id) return removeExt(normalizeMiniProgramFilename(id, inputDir)) + '.js'
return normalizePath(
filepath.replace(path.extname(filepath), '.js')
)
} }
return '[name].js' return '[name].js'
}, },
......
...@@ -117,7 +117,10 @@ ${filter.code} ...@@ -117,7 +117,10 @@ ${filter.code}
extname: '.wxml', extname: '.wxml',
directive: 'wx:', directive: 'wx:',
compilerOptions: { compilerOptions: {
nodeTransforms: [uniCliShared.addComponentBindLink], isCustomElement: (tag) => {
return ['page-meta', 'navigation-bar', 'match-media'].includes(tag);
},
nodeTransforms: [uniCliShared.createTransformComponentLink(uniCliShared.COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
import path from 'path' import path from 'path'
import { addComponentBindLink } from '@dcloudio/uni-cli-shared' import {
COMPONENT_BIND_LINK,
createTransformComponentLink,
} from '@dcloudio/uni-cli-shared'
import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite' import { UniMiniProgramPluginOptions } from '@dcloudio/uni-mp-vite'
import source from './project.config.json' import source from './project.config.json'
...@@ -65,7 +68,10 @@ ${filter.code} ...@@ -65,7 +68,10 @@ ${filter.code}
extname: '.wxml', extname: '.wxml',
directive: 'wx:', directive: 'wx:',
compilerOptions: { compilerOptions: {
nodeTransforms: [addComponentBindLink], isCustomElement: (tag) => {
return ['page-meta', 'navigation-bar', 'match-media'].includes(tag)
},
nodeTransforms: [createTransformComponentLink(COMPONENT_BIND_LINK)],
}, },
}, },
style: { style: {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册