Skip to content

Commit 3e4bf14

Browse files
committed
test: support loader for templates
1 parent f35f233 commit 3e4bf14

File tree

5 files changed

+61
-45
lines changed

5 files changed

+61
-45
lines changed

Diff for: lib/pitch.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,37 @@ module.exports = code => code
99
// from within vue files and transform it into appropriate requests
1010
module.exports.pitch = function (remainingRequest) {
1111
const query = qs.parse(this.resourceQuery.slice(1))
12-
if (query.vue != null) {
13-
// Inject style-post-loader before css-loader for scoped CSS and trimming
14-
if (query.type === `style`) {
15-
const cssLoaderIndex = this.loaders.findIndex(l => /\/css-loader/.test(l.request))
16-
if (cssLoaderIndex) {
17-
const afterLoaders = this.loaders.slice(1, cssLoaderIndex + 1).map(l => l.request)
18-
const beforeLoaders = this.loaders.slice(cssLoaderIndex + 1).map(l => l.request)
19-
const request = '-!' + [
20-
...afterLoaders,
21-
stylePostLoaderPath,
22-
...beforeLoaders,
23-
this.resourcePath + this.resourceQuery
24-
].join('!')
25-
// use cjs to ensure exports from (vue-)style-loader/css-loader are intact
26-
return `module.exports = require(${loaderUtils.stringifyRequest(this, request)})`
27-
}
28-
}
2912

30-
// for templates: inject the template compiler
31-
if (query.type === `template`) {
32-
const beforeLoaders = this.loaders.slice(1).map(l => l.request)
13+
if (query.vue == null) {
14+
return
15+
}
16+
17+
// Inject style-post-loader before css-loader for scoped CSS and trimming
18+
if (query.type === `style`) {
19+
const cssLoaderIndex = this.loaders.findIndex(l => /\/css-loader/.test(l.request))
20+
if (cssLoaderIndex) {
21+
const afterLoaders = this.loaders.slice(1, cssLoaderIndex + 1).map(l => l.request)
22+
const beforeLoaders = this.loaders.slice(cssLoaderIndex + 1).map(l => l.request)
3323
const request = '-!' + [
34-
templateLoaderPath + `??vue-loader-options`,
24+
...afterLoaders,
25+
stylePostLoaderPath,
3526
...beforeLoaders,
3627
this.resourcePath + this.resourceQuery
3728
].join('!')
38-
// the template compiler uses esm exports
39-
return `export * from ${loaderUtils.stringifyRequest(this, request)}`
29+
// use cjs to ensure exports from (vue-)style-loader/css-loader are intact
30+
return `module.exports = require(${loaderUtils.stringifyRequest(this, request)})`
4031
}
4132
}
33+
34+
// for templates: inject the template compiler
35+
if (query.type === `template`) {
36+
const beforeLoaders = this.loaders.slice(1).map(l => l.request)
37+
const request = '-!' + [
38+
templateLoaderPath + `??vue-loader-options`,
39+
...beforeLoaders,
40+
this.resourcePath + this.resourceQuery
41+
].join('!')
42+
// the template compiler uses esm exports
43+
return `export * from ${loaderUtils.stringifyRequest(this, request)}`
44+
}
4245
}

Diff for: lib/template-loader/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function (template) {
2929
query
3030
))
3131

32-
if (query.lang) {
32+
if (query.lang && consolidate[query.lang]) {
3333
preprocess(
3434
template,
3535
options,
@@ -46,12 +46,6 @@ module.exports = function (template) {
4646
}
4747

4848
function preprocess (rawTemplate, options, loaderContext, lang, cb) {
49-
if (!consolidate[lang]) {
50-
return cb(
51-
new Error(`Template engine "${lang}" is not supported by vue-loader.`)
52-
)
53-
}
54-
5549
const engineOptions = Object.assign({
5650
filename: loaderContext.resourcePath
5751
}, options.template)

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"jest": "^22.4.2",
6060
"jsdom": "^11.6.2",
6161
"lint-staged": "^7.0.0",
62+
"markdown-loader": "^2.0.2",
6263
"mini-css-extract-plugin": "^0.2.0",
6364
"node-sass": "^4.7.2",
6465
"normalize-newline": "^3.0.0",

Diff for: test/template.spec.js

+22-15
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,28 @@ test('functional template', done => {
171171
})
172172
})
173173

174-
// TODO
175-
// test('customizing template loaders', done => {
176-
// mockBundleAndRun({
177-
// entry: 'markdown.vue'
178-
// }, ({ window, module }) => {
179-
// const vnode = mockRender(module, {
180-
// msg: 'hi'
181-
// })
182-
// // <h2 id="-msg-">{{msg}}</h2>
183-
// expect(vnode.tag).toBe('h2')
184-
// expect(vnode.data.attrs.id).toBe('-msg-')
185-
// expect(vnode.children[0].text).toBe('hi')
186-
// done()
187-
// })
188-
// })
174+
test('customizing template loaders', done => {
175+
mockBundleAndRun({
176+
entry: 'markdown.vue',
177+
module: {
178+
rules: [
179+
{
180+
test: /\.md$/,
181+
loader: 'markdown-loader'
182+
}
183+
]
184+
}
185+
}, ({ window, module }) => {
186+
const vnode = mockRender(module, {
187+
msg: 'hi'
188+
})
189+
// <h2 id="-msg-">{{msg}}</h2>
190+
expect(vnode.tag).toBe('h2')
191+
expect(vnode.data.attrs.id).toBe('-msg-')
192+
expect(vnode.children[0].text).toBe('hi')
193+
done()
194+
})
195+
})
189196

190197
test('custom compiler modules', done => {
191198
mockBundleAndRun({

Diff for: yarn.lock

+11
Original file line numberDiff line numberDiff line change
@@ -4896,6 +4896,17 @@ map-visit@^1.0.0:
48964896
dependencies:
48974897
object-visit "^1.0.0"
48984898

4899+
markdown-loader@^2.0.2:
4900+
version "2.0.2"
4901+
resolved "https://registry.yarnpkg.com/markdown-loader/-/markdown-loader-2.0.2.tgz#1cdcf11307658cd611046d7db34c2fe80542af7c"
4902+
dependencies:
4903+
loader-utils "^1.1.0"
4904+
marked "^0.3.9"
4905+
4906+
marked@^0.3.9:
4907+
version "0.3.17"
4908+
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.17.tgz#607f06668b3c6b1246b28f13da76116ac1aa2d2b"
4909+
48994910
math-expression-evaluator@^1.2.14:
49004911
version "1.2.17"
49014912
resolved "https://registry.yarnpkg.com/math-expression-evaluator/-/math-expression-evaluator-1.2.17.tgz#de819fdbcd84dccd8fae59c6aeb79615b9d266ac"

0 commit comments

Comments
 (0)