Skip to content

Commit f238f59

Browse files
committed
feat: support custom blocks
1 parent 20dbbfc commit f238f59

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Diff for: example/Button.vue

+4
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ export default {
2222
color: green;
2323
}
2424
</style>
25+
26+
<docs>
27+
This component is fire. // <-- this should be logged
28+
</docs>

Diff for: example/docs-loader.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = function(source) {
2+
this.callback(null, `export default function (Comp) {
3+
Comp.mounted = () => console.log(${JSON.stringify(source.trim())})
4+
}`)
5+
}

Diff for: example/webpack.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ module.exports = {
3939
},
4040
'css-loader'
4141
]
42+
},
43+
// target <docs> custom blocks
44+
{
45+
resourceQuery: /blockType=docs/,
46+
loader: require.resolve('./docs-loader')
4247
}
4348
]
4449
},

Diff for: src/index.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ const loader: webpack.loader.Loader = function(source) {
160160
`script.render = render`
161161
].join('\n')
162162

163-
if (descriptor.customBlocks && descriptor.customBlocks.length) {
164-
// TODO custom blocks
165-
}
166-
167163
// attach scope Id for runtime use
168164
if (hasScoped) {
169165
code += `\nscript.__scopeId = "data-v-${id}"`
@@ -187,6 +183,27 @@ const loader: webpack.loader.Loader = function(source) {
187183
code += `\nscript.__file = ${JSON.stringify(path.basename(resourcePath))}`
188184
}
189185

186+
// custom blocks
187+
if (descriptor.customBlocks && descriptor.customBlocks.length) {
188+
code += `\n/* custom blocks */\n`
189+
code +=
190+
descriptor.customBlocks
191+
.map((block, i) => {
192+
const src = block.attrs.src || resourcePath
193+
const attrsQuery = attrsToQuery(block.attrs)
194+
const blockTypeQuery = `&blockType=${qs.escape(block.type)}`
195+
const issuerQuery = block.attrs.src
196+
? `&issuerPath=${qs.escape(resourcePath)}`
197+
: ''
198+
const query = `?vue&type=custom&index=${i}${blockTypeQuery}${issuerQuery}${attrsQuery}${resourceQuery}`
199+
return (
200+
`import block${i} from ${stringifyRequest(src + query)}\n` +
201+
`if (typeof block${i} === 'function') block${i}(script)`
202+
)
203+
})
204+
.join(`\n`) + `\n`
205+
}
206+
190207
// finalize
191208
code += `\n\nexport default script`
192209
return code

0 commit comments

Comments
 (0)