Skip to content

Commit f1df374

Browse files
asselinyyx990803
authored andcommitted
Add the vue template compiler onto any template loader specified in the vue.loaders configuration (vuejs#595)
* Tweak to add the vue template compiler onto any loader specified in the vue.loaders configuration. This enables, for example, using a markdown loader via <template lang="md"> in a .vue file. Without this tweak, the webpack config has to specify both the loader that converts the markdown (e.g.) to HTML AND template-compiler to compile it. That, however doesn't fully work because the webpack config can't know the moduleId generated by lib/loader.js and pass it in via the `id` parameter, so scoped styles won't work. * adjustments * add tests for custom html loader
1 parent 5e110cb commit f1df374

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

lib/loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ module.exports = function (content) {
150150
if (type === 'styles') {
151151
loader = addCssModulesToLoader(loader, part, index)
152152
}
153+
// if user defines custom loaders for html, add template compiler to it
154+
if (type === 'template' && loader !== defaultLoaders.html) {
155+
loader = defaultLoaders.html + '!' + loader
156+
}
153157
// inject rewriter before css/html loader for
154158
// extractTextPlugin use cases
155159
if (rewriterInjectRE.test(loader)) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"file-loader": "^0.10.0",
6262
"inject-loader": "^2.0.0",
6363
"jsdom": "^9.2.1",
64+
"marked": "^0.3.6",
6465
"memory-fs": "^0.4.1",
6566
"mkdirp": "^0.5.1",
6667
"mocha": "^3.2.0",

test/fixtures/markdown.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template>## {{msg}}</template>

test/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,26 @@ describe('vue-loader', function () {
504504
})
505505
})
506506

507+
it('should allow adding custom html loaders', done => {
508+
test({
509+
entry: './test/fixtures/markdown.vue',
510+
vue: {
511+
loaders: {
512+
html: 'marked'
513+
}
514+
}
515+
}, (window, module) => {
516+
var vnode = mockRender(module, {
517+
msg: 'hi'
518+
})
519+
// <h2 id="-msg-">{{msg}}</h2>
520+
expect(vnode.tag).to.equal('h2')
521+
expect(vnode.data.attrs.id).to.equal('-msg-')
522+
expect(vnode.children[0]).to.equal('hi')
523+
done()
524+
})
525+
})
526+
507527
it('support chaining with other loaders', done => {
508528
const mockLoaderPath = require.resolve('./mock-loader')
509529
test({

0 commit comments

Comments
 (0)