Skip to content

Commit 7209049

Browse files
committed
wip(vue2): add template preprocessor support
1 parent d92f2b3 commit 7209049

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/createVue2SFCModule.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function createSFCModule(source : string, filename : string, option
6868
const component = {};
6969

7070

71-
const { compiledCache, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
71+
const { moduleCache, compiledCache, addStyle, log, additionalBabelPlugins = [], customBlockHandler } = options;
7272

7373
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
7474
const descriptor = sfc_parse({
@@ -105,11 +105,16 @@ export async function createSFCModule(source : string, filename : string, option
105105
scopeId: hasScoped ? scopeId : null,
106106
comments: true
107107
} as any,
108-
preprocessLang: descriptor.template.lang,
109108
isProduction: isProd,
110109
prettify: false
111110
} : null;
112111

112+
// Vue2 doesn't support preprocessCustomRequire, so we have to preprocess manually
113+
if (descriptor.template?.lang) {
114+
const preprocess = moduleCache[descriptor.template.lang] as any
115+
compileTemplateOptions.source = preprocess.render(compileTemplateOptions.source, compileTemplateOptions.preprocessOptions)
116+
}
117+
113118
if ( descriptor.script ) {
114119

115120
// eg: https://github.com/vuejs/vue-loader/blob/v15.9.6/lib/index.js

tests/basic.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,5 +513,30 @@ const { defaultFilesVue2, defaultFiles, createPage } = require('./testsTools.js'
513513

514514
await page.close();
515515
});
516+
517+
test('custom template language', async () => {
518+
const { page, output } = await createPage({
519+
files: {
520+
...files,
521+
'/component.vue': `
522+
<template lang="custom">
523+
<span>Hello World !</span>
524+
</template>
525+
`,
526+
'/optionsOverride.js': `
527+
export default (options) => {
528+
options.moduleCache.custom = {render: s => s.replace("Hello World !", "Custom Hello World !") }
529+
};
530+
`,
531+
}
532+
});
533+
534+
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('Custom Hello World !');
535+
536+
await page.close();
537+
});
538+
516539
});
540+
541+
517542
})

0 commit comments

Comments
 (0)