Skip to content

Commit

Permalink
fix: support options for template block preprocessor render
Browse files Browse the repository at this point in the history
fix #634
  • Loading branch information
underfin committed Jul 29, 2020
1 parent 59938bc commit b166bc3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion playground/TestPreprocessors.vue
@@ -1,6 +1,6 @@
<template lang="pug">
h2 Pre-Processors
p.pug
p(id).pug
| This is rendered from &lt;template lang="pug"&gt;
| and styled with &lt;style lang="sass"&gt;. It should be megenta.
p.pug-less
Expand Down
4 changes: 3 additions & 1 deletion src/node/build/index.ts
Expand Up @@ -185,7 +185,8 @@ async function createVuePlugin(
cssPreprocessOptions,
cssModuleOptions,
vueCompilerOptions,
vueTransformAssetUrls = {}
vueTransformAssetUrls = {},
vueTemplatePreprocessOptions = {}
}: BuildConfig
) {
const {
Expand All @@ -202,6 +203,7 @@ async function createVuePlugin(

return require('rollup-plugin-vue')({
...rollupPluginVueOptions,
templatePreprocessOptions: vueTemplatePreprocessOptions,
transformAssetUrls: vueTransformAssetUrls,
postcssOptions,
postcssPlugins,
Expand Down
12 changes: 12 additions & 0 deletions src/node/config.ts
Expand Up @@ -103,6 +103,13 @@ export interface SharedConfig {
* or disable the transform altogether with `false`.
*/
vueTransformAssetUrls?: SFCTemplateCompileOptions['transformAssetUrls']
/**
* The options for template block preprocessor render.
*/
vueTemplatePreprocessOptions?: Record<
string,
SFCTemplateCompileOptions['preprocessOptions']
>
/**
* Transform functions for Vue custom blocks.
*
Expand Down Expand Up @@ -316,6 +323,7 @@ export interface Plugin
| 'configureServer'
| 'vueCompilerOptions'
| 'vueTransformAssetUrls'
| 'vueTemplatePreprocessOptions'
| 'vueCustomBlockTransforms'
| 'rollupInputOptions'
| 'rollupOutputOptions'
Expand Down Expand Up @@ -484,6 +492,10 @@ function resolvePlugin(config: UserConfig, plugin: Plugin): UserConfig {
config.vueTransformAssetUrls,
plugin.vueTransformAssetUrls
),
vueTemplatePreprocessOptions: {
...config.vueTemplatePreprocessOptions,
...plugin.vueTemplatePreprocessOptions
},
vueCustomBlockTransforms: {
...config.vueCustomBlockTransforms,
...plugin.vueCustomBlockTransforms
Expand Down
19 changes: 17 additions & 2 deletions src/node/server/serverPluginVue.ts
Expand Up @@ -540,7 +540,11 @@ function compileSFCTemplate(
publicPath: string,
scoped: boolean,
bindingMetadata: BindingMetadata | undefined,
{ vueCompilerOptions, vueTransformAssetUrls = {} }: ServerConfig
{
vueCompilerOptions,
vueTransformAssetUrls = {},
vueTemplatePreprocessOptions = {}
}: ServerConfig
): ResultWithMap {
let cached = vueCache.get(filePath)
if (cached && cached.template) {
Expand All @@ -556,6 +560,16 @@ function compileSFCTemplate(
...vueTransformAssetUrls
}
}
const preprocessLang = template.lang
let preprocessOptions =
preprocessLang && vueTemplatePreprocessOptions[preprocessLang]
if (preprocessLang === 'pug') {
preprocessOptions = {
doctype: 'html',
...preprocessOptions
}
}

const { code, map, errors } = compileTemplate({
source: template.content,
filename: filePath,
Expand All @@ -571,7 +585,8 @@ function compileSFCTemplate(
'/@modules/vue.js'
: '/@modules/vue'
},
preprocessLang: template.lang,
preprocessLang,
preprocessOptions,
preprocessCustomRequire: (id: string) => require(resolveFrom(root, id))
})

Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Expand Up @@ -480,6 +480,10 @@ describe('vite', () => {

test('pre-processors', async () => {
expect(await getText('.pug')).toMatch('template lang="pug"')
// pug compiler with `html` option
expect(
(await getEl('.pug')).evaluate((el) => el.getAttribute('id'))
).toMatch('')
expect(await getComputedColor('.pug')).toBe('rgb(255, 0, 255)')
if (!isBuild) {
await updateFile('TestPreprocessors.vue', (c) =>
Expand Down

0 comments on commit b166bc3

Please sign in to comment.