Skip to content

Commit b80f4e8

Browse files
committed
Merge pull request #98 from naumovs/master
Support "src" attribute for script tag
2 parents 765d85d + a05c5cf commit b80f4e8

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

lib/loader.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,15 @@ module.exports = function (content) {
153153
})
154154

155155
// add require for script
156+
var script
156157
if (parts.script.length) {
158+
script = parts.script[0]
157159
output +=
158-
'module.exports = ' + getRequire('script', parts.script[0], 0) + '\n' +
159-
'if (module.exports.__esModule) module.exports = module.exports.default\n'
160+
'module.exports = ' + (
161+
script.src
162+
? getRequireForImport('script', script, 0)
163+
: getRequire('script', script, 0)
164+
) + '\nif (module.exports.__esModule) module.exports = module.exports.default\n'
160165
}
161166

162167
// add require for template

lib/parser.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ module.exports = function (content) {
3232

3333
// handle src imports
3434
if (src) {
35-
if (type !== 'style' && type !== 'template') {
36-
return cb(new Error(
37-
'[vue-loader] src import is only supported for <template> and <style> tags.'
38-
))
39-
}
4035
if (type === 'style') {
4136
output.styleImports.push({
4237
src: src,
@@ -48,6 +43,11 @@ module.exports = function (content) {
4843
src: src,
4944
lang: lang
5045
})
46+
} else if (type === 'script') {
47+
output.script.push({
48+
src: src,
49+
lang: lang
50+
})
5151
}
5252
return
5353
}

test/fixtures/script-import-entry.js

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

test/fixtures/script-import.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
data () {
3+
return {
4+
msg: 'Hello from Component A!'
5+
}
6+
}
7+
};

test/fixtures/script-import.vue

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

test/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ describe('vue-loader', function () {
130130
})
131131
})
132132

133+
it('script import', function (done) {
134+
test({
135+
entry: './test/fixtures/script-import-entry.js'
136+
}, function (window) {
137+
var module = window.testModule
138+
expect(module.data().msg).to.contain('Hello from Component A!')
139+
done()
140+
})
141+
})
142+
133143
it('source map', function (done) {
134144
var config = Object.assign({}, globalConfig, {
135145
entry: './test/fixtures/basic.js',

0 commit comments

Comments
 (0)