Skip to content

Commit 5c8c08b

Browse files
wip(core): add buildTemplateProcessor function (closes #35)
1 parent e9fdeef commit 5c8c08b

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { posix as Path } from 'path'
33
import { createJSModule, loadModuleInternal } from './tools'
44
import { createSFCModule } from './createSFCModule'
55

6-
import { ModuleExport, ModuleHandler, PathHandlers, Options, File, Resource, PathContext } from './types'
6+
import { ModuleExport, ModuleHandler, PathHandlers, Options, File, Resource, PathContext, LangProcessor } from './types'
77

88
/**
99
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
@@ -138,3 +138,28 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
138138

139139
return await loadModuleInternal( { refPath: '/', relPath: path }, options);
140140
}
141+
142+
/**
143+
* Convert a function to template processor interface (consolidate)
144+
*/
145+
export function buildTemplateProcessor(processor: LangProcessor) {
146+
return {
147+
render: (source: string, preprocessOptions: string, cb: (_err : any, _res : any) => void) => {
148+
try {
149+
const ret = processor(source, preprocessOptions)
150+
if (typeof ret === 'string') {
151+
cb(null, ret)
152+
} else {
153+
ret.then(processed => {
154+
cb(null, processed)
155+
})
156+
ret.catch(err => {
157+
cb(err, null)
158+
})
159+
}
160+
} catch (err) {
161+
cb(err, null)
162+
}
163+
}
164+
}
165+
}

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,6 @@ export type Options = {
363363
customBlockHandler?(block : CustomBlock, filename : string, options : Options) : Promise<CustomBlockCallback | undefined>,
364364

365365
}
366+
367+
368+
export type LangProcessor = (source: string, preprocessOptions?: any) => Promise<string> | string

tests/basic.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,62 @@ const { defaultFilesFactory, createPage } = require('./testsTools.js');
520520
});
521521

522522

523+
test('custom template language with sync buildTemplateProcessor (sync)', async () => {
524+
const { page, output } = await createPage({
525+
files: {
526+
...files,
527+
'/component.vue': `
528+
<template lang="custom">
529+
<span>Hello World !</span>
530+
</template>
531+
`,
532+
'/optionsOverride.js': `
533+
const { buildTemplateProcessor } = window['vue${ vueTarget }-sfc-loader'];
534+
535+
export default (options) => {
536+
options.moduleCache.custom = buildTemplateProcessor(s => s.replace("Hello World !", "Custom Hello World !"))
537+
};
538+
`,
539+
}
540+
});
541+
542+
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('Custom Hello World !');
543+
544+
await page.close();
545+
});
546+
547+
548+
if ( vueTarget !== 3 ) {
549+
550+
// Vue3 doesn't support async template engine
551+
// https://github.com/vuejs/vue-next/blob/1fb6fbc8b8c8225441fb23918f128f7994c365ca/packages/compiler-sfc/src/compileTemplate.ts#L82-L85
552+
test('custom template language with buildTemplateProcessor (async)', async () => {
553+
const { page, output } = await createPage({
554+
files: {
555+
...files,
556+
'/component.vue': `
557+
<template lang="custom">
558+
<span>Hello World !</span>
559+
</template>
560+
`,
561+
'/optionsOverride.js': `
562+
const { buildTemplateProcessor } = window['vue${ vueTarget }-sfc-loader'];
563+
564+
export default (options) => {
565+
options.moduleCache.custom = buildTemplateProcessor(s => new Promise((resolve, reject) => {
566+
resolve(s.replace("Hello World !", "Custom Hello World !"))
567+
}))};
568+
`,
569+
}
570+
});
571+
572+
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('Custom Hello World !');
573+
574+
await page.close();
575+
});
576+
}
577+
578+
523579
test('custom style language', async () => {
524580
const { page, output } = await createPage({
525581
files: {

0 commit comments

Comments
 (0)