Skip to content

Commit 69f0870

Browse files
committed
support src import for templates
1 parent fc13120 commit 69f0870

File tree

10 files changed

+67
-31
lines changed

10 files changed

+67
-31
lines changed

lib/loader.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,20 @@ module.exports = function (content) {
5959
)
6060
}
6161

62-
function getRequireForImport (impt) {
62+
function getRequireForImport (type, impt, scoped) {
6363
return 'require(' +
64-
loaderUtils.stringifyRequest(self,
65-
'-!' +
66-
getLoaderString('style', impt, impt.scoped) +
67-
impt.src
68-
) +
64+
getRequireForImportString(type, impt, scoped) +
6965
')\n'
7066
}
7167

68+
function getRequireForImportString (type, impt, scoped) {
69+
return loaderUtils.stringifyRequest(self,
70+
'-!' +
71+
getLoaderString(type, impt, scoped) +
72+
impt.src
73+
)
74+
}
75+
7276
function getLoaderString (type, part, scoped) {
7377
var lang = part.lang || defaultLang[type]
7478
var loader = loaders[lang]
@@ -135,7 +139,7 @@ module.exports = function (content) {
135139
// add requires for src imports
136140
parts.styleImports.forEach(function (impt) {
137141
if (impt.scoped) hasLocalStyles = true
138-
output += getRequireForImport(impt)
142+
output += getRequireForImport('style', impt, impt.scoped)
139143
})
140144

141145
// add requires for styles
@@ -152,11 +156,16 @@ module.exports = function (content) {
152156
}
153157

154158
// add require for template
159+
var template
155160
if (parts.template.length) {
161+
template = parts.template[0]
156162
output += ';(typeof module.exports === "function" ' +
157163
'? module.exports.options ' +
158-
': module.exports).template = ' +
159-
getRequire('template', parts.template[0], 0, hasLocalStyles)
164+
': module.exports).template = ' + (
165+
template.src
166+
? getRequireForImport('template', template, hasLocalStyles)
167+
: getRequire('template', template, 0, hasLocalStyles)
168+
)
160169
}
161170

162171
// hot reload
@@ -165,7 +174,11 @@ module.exports = function (content) {
165174
(parts.script.length || parts.template.length)
166175
) {
167176
var scriptString = parts.script.length ? getRequireString('script', parts.script[0], 0) : ''
168-
var templateString = parts.template.length ? getRequireString('template', parts.template[0], 0, hasLocalStyles) : ''
177+
var templateString = template
178+
? template.src
179+
? getRequireForImportString('template', template, hasLocalStyles)
180+
: getRequireString('template', template, 0, hasLocalStyles)
181+
: ''
169182
var accepted = []
170183
if (scriptString) {
171184
accepted.push(scriptString.slice(1, -1))

lib/parser.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,36 @@ module.exports = function (content) {
1919
var src = getAttribute(node, 'src')
2020
var scoped = getAttribute(node, 'scoped') != null
2121

22+
// node count check
23+
if (
24+
(type === 'script' || type === 'template') &&
25+
output[type].length > 0
26+
) {
27+
return cb(new Error(
28+
'[vue-loader] Only one <script> or <template> tag is ' +
29+
'allowed inside a Vue component.'
30+
))
31+
}
32+
33+
// handle src imports
2234
if (src) {
23-
if (type !== 'style') {
35+
if (type !== 'style' && type !== 'template') {
2436
return cb(new Error(
25-
'[vue-loader] src import is only supported for <style> tags.'
37+
'[vue-loader] src import is only supported for <template> and <style> tags.'
2638
))
2739
}
28-
output.styleImports.push({
29-
src: src,
30-
lang: lang,
31-
scoped: scoped
32-
})
40+
if (type === 'style') {
41+
output.styleImports.push({
42+
src: src,
43+
lang: lang,
44+
scoped: scoped
45+
})
46+
} else if (type === 'template') {
47+
output.template.push({
48+
src: src,
49+
lang: lang
50+
})
51+
}
3352
return
3453
}
3554

@@ -41,16 +60,6 @@ module.exports = function (content) {
4160
return
4261
}
4362

44-
if (
45-
(type === 'script' || type === 'template') &&
46-
output[type].length > 0
47-
) {
48-
return cb(new Error(
49-
'[vue-loader] Only one <script> or <template> tag is ' +
50-
'allowed inside a Vue component.'
51-
))
52-
}
53-
5463
// Work around changes in parse5 >= 1.2.0
5564
if (node.childNodes[0].nodeName === '#document-fragment') {
5665
node = node.childNodes[0]

test/fixtures/import.vue

Lines changed: 0 additions & 2 deletions
This file was deleted.
File renamed without changes.

test/fixtures/style-import.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<style src="./style-import.css"></style>
2+
<style src="./style-import-scoped.css" scoped></style>

test/fixtures/template-import.jade

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div
2+
h1 hello

test/fixtures/template-import.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
window.testModule = require('./template-import.vue')

test/fixtures/template-import.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template lang="jade" src="./template-import.jade"></template>

test/test.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,27 @@ describe('vue-loader', function () {
110110

111111
it('style import', function (done) {
112112
test({
113-
entry: './test/fixtures/import.vue'
113+
entry: './test/fixtures/style-import.vue'
114114
}, function (window) {
115115
var styles = window.document.querySelectorAll('style')
116116
expect(styles[0].textContent).to.contain('h1 { color: red; }')
117117
// import with scoped
118-
var id = '_v-' + hash(require.resolve('./fixtures/import.vue'))
118+
var id = '_v-' + hash(require.resolve('./fixtures/style-import.vue'))
119119
expect(styles[1].textContent).to.contain('h1[' + id + '] { color: green; }')
120120
done()
121121
})
122122
})
123123

124+
it('template import', function (done) {
125+
test({
126+
entry: './test/fixtures/template-import.js'
127+
}, function (window) {
128+
var module = window.testModule
129+
expect(module.template).to.contain('<div><h1>hello</h1></div>')
130+
done()
131+
})
132+
})
133+
124134
it('source map', function (done) {
125135
var config = assign({}, globalConfig, {
126136
entry: './test/fixtures/basic.js',

0 commit comments

Comments
 (0)