Skip to content

Commit 4d70b20

Browse files
committed
Allow post loaders for template blocks
This also sets a default 'lang="html"' query for template blocks.
1 parent b1023cd commit 4d70b20

File tree

9 files changed

+40
-3
lines changed

9 files changed

+40
-3
lines changed

docs/migrating.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ The following options have been deprecated and should be configured using normal
251251
- `extractCSS`
252252
- `template`
253253

254+
:::tip
255+
Template render functions cannot be post-processed by Webpack Rules because the `enforce: 'post'` option is not preserved for Vue Loader to interact with. `templatePostLoaders` now exists to accommodate these use cases.
256+
:::
257+
254258
The following options have been deprecated and should be configured using the new `compilerOptions` option:
255259

256260
- `preserveWhitespace` (use `compilerOptions.preserveWhitespace`)

docs/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ Compiled the component for usage inside Shadow DOM. In this mode, the styles of
8181
- default: `undefined`
8282

8383
When both options are specified, enables file-system-based template compilation caching (requires `cache-loader` to be installed in the same project).
84+
85+
## templatePostLoaders
86+
87+
- type: `Array<string>`
88+
- default: `[]`
89+
90+
Attach an array of loaders to post-process a template's render function. This option only exists because Webpack's `enforce: 'post'` option is not preserved and cannot be used to determine whether loaders defined as Rules within a Webpack configuration should be attached after the `template-compiler`.

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module.exports = function (source) {
8585
const src = descriptor.template.src || resourcePath
8686
const idQuery = `&id=${id}`
8787
const scopedQuery = hasScoped ? `&scoped=true` : ``
88-
const attrsQuery = attrsToQuery(descriptor.template.attrs)
88+
const attrsQuery = attrsToQuery(descriptor.template.attrs, 'html')
8989
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}`
9090
const request = templateRequest = stringifyRequest(src + query)
9191
templateImport = `import { render, staticRenderFns } from ${request}`

lib/loaders/pitcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ module.exports.pitch = function (remainingRequest) {
7979
: []
8080
const request = genRequest([
8181
...cacheLoader,
82+
...options.templatePostLoaders,
8283
templateLoaderPath + `??vue-loader-options`,
8384
...loaders
8485
])

lib/loaders/stylePostLoader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const qs = require('querystring')
22
const { compileStyle } = require('@vue/component-compiler-utils')
33

44
// This is a post loader that handles scoped CSS transforms.
5-
// Injected right before css-loader by the global pitcher (../pitch.js)
5+
// Injected right before css-loader by the global pitcher (./pitcher.js)
66
// for any <style scoped> selection requests initiated from within vue files.
77
module.exports = function (source, inMap) {
88
const query = qs.parse(this.resourceQuery.slice(1))

lib/plugin.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ class VueLoaderPlugin {
8686
},
8787
options: {
8888
cacheDirectory: vueLoaderUse.options.cacheDirectory,
89-
cacheIdentifier: vueLoaderUse.options.cacheIdentifier
89+
cacheIdentifier: vueLoaderUse.options.cacheIdentifier,
90+
// added because we don't have access to webpack's enforce: 'post' option.
91+
// useful to post-process the render function.
92+
templatePostLoaders: vueLoaderUse.options.templatePostLoaders || []
9093
}
9194
}
9295

test/fixtures/template-post.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Comp1 from './template-post.vue'
2+
3+
window.exports = Comp1

test/fixtures/template-post.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<h1>Hello from Component A!</h1>
4+
</div>
5+
</template>

test/template.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,17 @@ test('separate loader configuration for template lang and js imports', done => {
292292
done()
293293
})
294294
})
295+
296+
test('render function post loaders', done => {
297+
mockBundleAndRun({
298+
entry: './test/fixtures/template-post.js',
299+
vue: {
300+
templatePostLoaders: [
301+
path.resolve(__dirname, './mock-loaders/js')
302+
]
303+
}
304+
}, ({ exports }) => {
305+
expect(exports.staticRenderFns[0].toString()).toContain('Changed!')
306+
done()
307+
})
308+
})

0 commit comments

Comments
 (0)