@@ -2,6 +2,7 @@ const path = require('path')
22const { parse } = require ( '@babel/parser' )
33const traverse = require ( '@babel/traverse' )
44const generator = require ( '@babel/generator' )
5+ const { parseAttrs, stringifyAttrs } = require ( './attrs' )
56
67module . exports = async ( content , filename = 'foo.js' ) => {
78 const ext = path . extname ( filename ) . slice ( 1 )
@@ -20,6 +21,7 @@ module.exports = async (content, filename = 'foo.js') => {
2021
2122 let html = ''
2223 const styles = [ ]
24+ const customBlocks = [ ]
2325
2426 // Extract template the code
2527 traverse . default ( ast , {
@@ -55,11 +57,23 @@ module.exports = async (content, filename = 'foo.js') => {
5557
5658 // Extract styles from the template
5759 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
5861
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+ } )
6377
6478 return `
6579 <template>
@@ -71,5 +85,6 @@ module.exports = async (content, filename = 'foo.js') => {
7185 </script>
7286
7387 ${ styles . join ( '\n' ) }
88+ ${ customBlocks . join ( '\n' ) }
7489 `
7590}
0 commit comments