Skip to content

Commit 1ce1e9b

Browse files
feat(core): ass custom block support through customBlockHandler()
1 parent d01580e commit 1ce1e9b

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/index.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
compileScript as sfc_compileScript,
3232
compileTemplate as sfc_compileTemplate,
3333
SFCAsyncStyleCompileOptions,
34-
SFCTemplateCompileOptions
34+
SFCTemplateCompileOptions,
35+
SFCBlock
3536
} from '@vue/compiler-sfc'
3637

3738
import {
@@ -84,6 +85,12 @@ interface PathHandlers {
8485
type File = string | { content : string, extname : string };
8586

8687

88+
/**
89+
* CustomBlockCallback function type
90+
*/
91+
type CustomBlockCallback = ( component : Module ) => undefined;
92+
93+
8794
interface Options {
8895
// ts: https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types
8996

@@ -294,6 +301,28 @@ interface Options {
294301
*/
295302
pathHandlers : PathHandlers,
296303

304+
305+
/**
306+
* Called for each custom block.
307+
* @returns A Promise of the module or undefined
308+
*
309+
* ```javascript
310+
* ...
311+
* customBlockHandler(block, filename, options) {
312+
*
313+
* if ( block.type !== 'i18n' )
314+
* return;
315+
*
316+
* return (component) => {
317+
*
318+
* component.i18n = JSON.parse(block.content);
319+
* }
320+
* }
321+
* ...
322+
* ```
323+
*/
324+
customBlockHandler(block : SFCBlock, filename : string, options : Options) : Promise<CustomBlockCallback | undefined>,
325+
297326
}
298327

299328

@@ -592,16 +621,19 @@ async function createJSModule(source : string, moduleSourceType : boolean, filen
592621
/**
593622
* @internal
594623
*/
595-
async function createSFCModule(source : string, filename : string, options : Options) {
624+
async function createSFCModule(source : string, filename : string, options : Options) : Promise<Module> {
596625

597-
const { delimiters, moduleCache, compiledCache, addStyle, log, additionalBabelPlugins = [] } = options;
626+
const { delimiters, moduleCache, compiledCache, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
598627

599628
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
600629
const { descriptor, errors } = sfc_parse(source, {
601630
filename,
602631
sourceMap: genSourcemap,
603632
});
604633

634+
635+
const customBlockCallbacks : CustomBlockCallback[] = customBlockHandler !== undefined ? await Promise.all( descriptor.customBlocks.map(block => customBlockHandler(block, filename, options)) ) : [];
636+
605637
const componentHash = hash(filename, version);
606638
const hasScoped = descriptor.styles.some(e => e.scoped);
607639
const scopeId = `data-v-${componentHash}`;
@@ -758,6 +790,8 @@ async function createSFCModule(source : string, filename : string, options : Opt
758790
addStyle(style, descStyle.scoped ? scopeId : undefined);
759791
}
760792

793+
await Promise.all(customBlockCallbacks.map(cb => cb?.(component)));
794+
761795
return component;
762796
}
763797

0 commit comments

Comments
 (0)