Skip to content

Allow post loaders for template blocks #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ The following options have been deprecated and should be configured using normal
- `extractCSS`
- `template`

:::tip
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.
:::

The following options have been deprecated and should be configured using the new `compilerOptions` option:

- `preserveWhitespace` (use `compilerOptions.preserveWhitespace`)
Expand Down
7 changes: 7 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ When both options are specified, enables file-system-based template compilation
::: tip
Interaction between `vue-loader` and `cache-loader` uses [inline loader import syntax](https://webpack.js.org/concepts/loaders/#inline) under the hook, the `!` will be treated as the separator between different loaders, so please ensure `cacheDirectory` doesn't contain `!`.
:::

## templatePostLoaders

- type: `Array<string>`
- default: `[]`

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`.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports = function (source) {
const src = descriptor.template.src || resourcePath
const idQuery = `&id=${id}`
const scopedQuery = hasScoped ? `&scoped=true` : ``
const attrsQuery = attrsToQuery(descriptor.template.attrs)
const attrsQuery = attrsToQuery(descriptor.template.attrs, 'html')
const query = `?vue&type=template${idQuery}${scopedQuery}${attrsQuery}${inheritQuery}`
const request = templateRequest = stringifyRequest(src + query)
templateImport = `import { render, staticRenderFns } from ${request}`
Expand Down
1 change: 1 addition & 0 deletions lib/loaders/pitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ module.exports.pitch = function (remainingRequest) {
: []
const request = genRequest([
...cacheLoader,
...options.templatePostLoaders,
templateLoaderPath + `??vue-loader-options`,
...loaders
])
Expand Down
2 changes: 1 addition & 1 deletion lib/loaders/stylePostLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const qs = require('querystring')
const { compileStyle } = require('@vue/component-compiler-utils')

// This is a post loader that handles scoped CSS transforms.
// Injected right before css-loader by the global pitcher (../pitch.js)
// Injected right before css-loader by the global pitcher (./pitcher.js)
// for any <style scoped> selection requests initiated from within vue files.
module.exports = function (source, inMap) {
const query = qs.parse(this.resourceQuery.slice(1))
Expand Down
5 changes: 4 additions & 1 deletion lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ class VueLoaderPlugin {
},
options: {
cacheDirectory: vueLoaderUse.options.cacheDirectory,
cacheIdentifier: vueLoaderUse.options.cacheIdentifier
cacheIdentifier: vueLoaderUse.options.cacheIdentifier,
// added because we don't have access to webpack's enforce: 'post' option.
// useful to post-process the render function.
templatePostLoaders: vueLoaderUse.options.templatePostLoaders || []
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/template-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Comp1 from './template-post.vue'

window.exports = Comp1
5 changes: 5 additions & 0 deletions test/fixtures/template-post.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
<h1>Hello from Component A!</h1>
</div>
</template>
14 changes: 14 additions & 0 deletions test/template.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,17 @@ test('separate loader configuration for template lang and js imports', done => {
done()
})
})

test('render function post loaders', done => {
mockBundleAndRun({
entry: './test/fixtures/template-post.js',
vue: {
templatePostLoaders: [
path.resolve(__dirname, './mock-loaders/js')
]
}
}, ({ exports }) => {
expect(exports.staticRenderFns[0].toString()).toContain('Changed!')
Copy link
Author

@defnorep defnorep May 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is an appropriate test.

done()
})
})