Skip to content

Commit 3878a62

Browse files
kazuponyyx990803
authored andcommitted
support pre/post loaders for custom blocks (#744)
1 parent 01bb852 commit 3878a62

File tree

7 files changed

+72
-1
lines changed

7 files changed

+72
-1
lines changed

lib/loader.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ module.exports = function (content) {
174174

175175
function getLoaderString (type, part, index, scoped) {
176176
var loader = getRawLoaderString(type, part, index, scoped)
177-
var lang = part.lang || defaultLang[type]
177+
var lang = getLangString(type, part)
178178
if (preLoaders[lang]) {
179179
loader = loader + ensureBang(preLoaders[lang])
180180
}
@@ -184,6 +184,14 @@ module.exports = function (content) {
184184
return loader
185185
}
186186

187+
function getLangString (type, part) {
188+
if (type === 'script' || type === 'template' || type === 'styles') {
189+
return part.lang || defaultLang[type]
190+
} else {
191+
return type
192+
}
193+
}
194+
187195
function getRawLoaderString (type, part, index, scoped) {
188196
var lang = part.lang || defaultLang[type]
189197
var loader = loaders[lang]

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"extract-text-webpack-plugin": "^2.0.0-rc.0",
6262
"file-loader": "^0.10.0",
6363
"inject-loader": "^2.0.0",
64+
"js-yaml": "^3.8.2",
6465
"jsdom": "^9.2.1",
6566
"marked": "^0.3.6",
6667
"memory-fs": "^0.4.1",

test/fixtures/custom-blocks.vue

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<i18n lang="yaml">
2+
en:
3+
hello: "hello world"
4+
ja:
5+
hello: "こんにちは、世界"
6+
</i18n>
7+
8+
<blog>## foo</blog>
9+
10+
<template>
11+
<div>
12+
<h1>{{ msg }}</h1>
13+
<p v-html="blog"></p>
14+
</div>
15+
</template>

test/mock-loaders/blog.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function normalize (code) {
2+
return code.split(/\r?\n/).map(function (line) { return line }).join('')
3+
}
4+
5+
module.exports = function (source) {
6+
var code = "module.exports = function (Component) { Component.options.__blog = '" + source + "' }"
7+
return normalize(code)
8+
}

test/mock-loaders/i18n.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function (source) {
2+
var value = typeof source === 'string' ? JSON.parse(source) : source
3+
var jsonString = JSON.stringify(value).replace(/\u2028/g, '\\u2028').replace(/\u2029/g, '\\u2029')
4+
var code = 'module.exports = function (Component) { Component.options.__i18n = ' + JSON.stringify(jsonString) + ' }'
5+
this.callback(null, code)
6+
}

test/mock-loaders/yaml.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var yaml = require('js-yaml')
2+
3+
module.exports = function (source) {
4+
this.cacheable && this.cacheable()
5+
var res = yaml.safeLoad(source)
6+
return JSON.stringify(res)
7+
}

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,32 @@ describe('vue-loader', function () {
716716
})
717717
})
718718

719+
it('pre/post loaders for custom blocks', done => {
720+
test({
721+
entry: './test/fixtures/custom-blocks.vue',
722+
vue: {
723+
preLoaders: {
724+
i18n: require.resolve('./mock-loaders/yaml')
725+
},
726+
loaders: {
727+
i18n: require.resolve('./mock-loaders/i18n'),
728+
blog: 'marked'
729+
},
730+
postLoaders: {
731+
blog: require.resolve('./mock-loaders/blog')
732+
}
733+
}
734+
}, (window, module) => {
735+
var vnode = mockRender(module, {
736+
msg: JSON.parse(module.__i18n).en.hello,
737+
blog: module.__blog
738+
})
739+
expect(vnode.children[0].children[0]).to.equal('hello world')
740+
expect(vnode.children[2].data.domProps.innerHTML).to.equal('<h2 id="foo">foo</h2>')
741+
done()
742+
})
743+
})
744+
719745
it('custom compiler modules', done => {
720746
test({
721747
entry: './test/fixtures/custom-module.vue',

0 commit comments

Comments
 (0)