Skip to content

Commit

Permalink
fix: support options for template block preprocessor render (#641)
Browse files Browse the repository at this point in the history
fix #634

Co-authored-by: Evan You <[email protected]>
  • Loading branch information
underfin and yyx990803 authored Jul 30, 2020
1 parent dd92f4f commit 540ae24
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"rollup": "^2.20.0",
"rollup-plugin-dynamic-import-variables": "^1.0.1",
"rollup-plugin-terser": "^5.3.0",
"rollup-plugin-vue": "^6.0.0-beta.9",
"rollup-plugin-vue": "^6.0.0-beta.10",
"rollup-plugin-web-worker-loader": "^1.3.0",
"rollup-pluginutils": "^2.8.2",
"selfsigned": "^1.10.7",
Expand Down
2 changes: 1 addition & 1 deletion playground/TestPreprocessors.vue
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ async function createVuePlugin(
cssPreprocessOptions,
cssModuleOptions,
vueCompilerOptions,
vueTransformAssetUrls = {}
vueTransformAssetUrls = {},
vueTemplatePreprocessOptions = {}
}: BuildConfig
) {
const {
Expand All @@ -205,6 +206,13 @@ async function createVuePlugin(

return require('rollup-plugin-vue')({
...rollupPluginVueOptions,
templatePreprocessOptions: {
...vueTemplatePreprocessOptions,
pug: {
doctype: 'html',
...(vueTemplatePreprocessOptions && vueTemplatePreprocessOptions.pug)
}
},
transformAssetUrls: vueTransformAssetUrls,
postcssOptions,
postcssPlugins,
Expand Down
12 changes: 12 additions & 0 deletions src/node/config.ts
Original file line number Diff line number Diff line change
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 @@ -323,6 +330,7 @@ export interface Plugin
| 'configureServer'
| 'vueCompilerOptions'
| 'vueTransformAssetUrls'
| 'vueTemplatePreprocessOptions'
| 'vueCustomBlockTransforms'
| 'rollupInputOptions'
| 'rollupOutputOptions'
Expand Down Expand Up @@ -491,6 +499,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
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ function compileSFCTemplate(
scoped: boolean,
bindingMetadata: BindingMetadata | undefined,
vueSpecifier: string,
{ vueCompilerOptions, vueTransformAssetUrls = {} }: ServerConfig
{
vueCompilerOptions,
vueTransformAssetUrls = {},
vueTemplatePreprocessOptions = {}
}: ServerConfig
): ResultWithMap {
let cached = vueCache.get(filePath)
if (cached && cached.template) {
Expand All @@ -551,6 +555,16 @@ function compileSFCTemplate(
}
}

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 @@ -562,7 +576,8 @@ function compileSFCTemplate(
bindingMetadata,
runtimeModuleName: vueSpecifier
},
preprocessLang: template.lang,
preprocessLang,
preprocessOptions,
preprocessCustomRequire: (id: string) => require(resolveFrom(root, id))
})

Expand Down
4 changes: 4 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ describe('vite', () => {

test('pre-processors', async () => {
expect(await getText('.pug')).toMatch('template lang="pug"')
// pug compiler with `html` option
expect(
await (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
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5843,10 +5843,10 @@ rollup-plugin-terser@^5.3.0:
serialize-javascript "^2.1.2"
terser "^4.6.2"

rollup-plugin-vue@^6.0.0-beta.9:
version "6.0.0-beta.9"
resolved "https://registry.yarnpkg.com/rollup-plugin-vue/-/rollup-plugin-vue-6.0.0-beta.9.tgz#88d3381bb25c0d9bc534c50cc7eb4b0c3715862a"
integrity sha512-VlSbALaec9WHWiEIeLsoNdBQ24iSr/qaBvekewGzHcxphgy2o4Qz1/kbpuH1zHU1DsY8mn96qTprW9KhbuYaaA==
rollup-plugin-vue@^6.0.0-beta.10:
version "6.0.0-beta.10"
resolved "https://registry.yarnpkg.com/rollup-plugin-vue/-/rollup-plugin-vue-6.0.0-beta.10.tgz#66d9b9a8dd2d085267d1cc398ea0113360879ac1"
integrity sha512-8TZJmROiSRjWoHRR6id0/ktOBOUGuI302xDBq4YBiA/tnnXdoY3oFGtvRWzT5ldX0jTJ8QX40rrJOw2SvcWwxQ==
dependencies:
debug "^4.1.1"
hash-sum "^2.0.0"
Expand Down

0 comments on commit 540ae24

Please sign in to comment.