@@ -2,6 +2,7 @@ const path = require('path')
2
2
const { parse } = require ( '@babel/parser' )
3
3
const traverse = require ( '@babel/traverse' )
4
4
const generator = require ( '@babel/generator' )
5
+ const { parseAttrs, stringifyAttrs } = require ( './attrs' )
5
6
6
7
module . exports = async ( content , filename = 'foo.js' ) => {
7
8
const ext = path . extname ( filename ) . slice ( 1 )
@@ -20,6 +21,7 @@ module.exports = async (content, filename = 'foo.js') => {
20
21
21
22
let html = ''
22
23
const styles = [ ]
24
+ const customBlocks = [ ]
23
25
24
26
// Extract template the code
25
27
traverse . default ( ast , {
@@ -55,11 +57,23 @@ module.exports = async (content, filename = 'foo.js') => {
55
57
56
58
// Extract styles from the template
57
59
const STYLE_RE = / < s t y l e [ \s \S ] * > [ \s \S ] * < \/ s t y l e > / g
60
+ const CUSTOM_BLOCK_RE = / < c u s t o m - b l o c k ( [ \s \S ] * ) > ( [ \s \S ] * ) < \/ c u s t o m - b l o c k > / g
58
61
59
- const template = html . replace ( STYLE_RE , match => {
60
- styles . push ( match )
61
- return ''
62
- } )
62
+ const template = html
63
+ . replace ( STYLE_RE , match => {
64
+ styles . push ( match )
65
+ return ''
66
+ } )
67
+ . replace ( CUSTOM_BLOCK_RE , ( _ , m1 , content ) => {
68
+ const attrs = parseAttrs ( m1 )
69
+ const { name } = attrs
70
+ if ( ! name ) {
71
+ throw new Error ( '[lit-vue] <custom-block> must have `name` attribute!' )
72
+ }
73
+ delete attrs . name
74
+ customBlocks . push ( `<${ name } ${ stringifyAttrs ( attrs ) } >${ content } </${ name } >` )
75
+ return ''
76
+ } )
63
77
64
78
return `
65
79
<template>
@@ -71,5 +85,6 @@ module.exports = async (content, filename = 'foo.js') => {
71
85
</script>
72
86
73
87
${ styles . join ( '\n' ) }
88
+ ${ customBlocks . join ( '\n' ) }
74
89
`
75
90
}
0 commit comments