Repository: vuejs/vue-loader
Pull request #98 (2015-12-09 00:22:32)

Support "src" attribute for script tag
The intention of this PR is to have *.vue files as entry points for components, but the code separated across the js/html/css files and have all the benefits of vue-loader, such as hot-reload


Patch:
diff --git a/lib/loader.js b/lib/loader.js
index c0540e3..4cdc2de 100644
--- a/lib/loader.js
+++ b/lib/loader.js
@@ -153,10 +153,15 @@ module.exports = function (content) {
     })
 
     // add require for script
+    var script
     if (parts.script.length) {
+      script = parts.script[0]
       output +=
-        'module.exports = ' + getRequire('script', parts.script[0], 0) + '\n' +
-        'if (module.exports.__esModule) module.exports = module.exports.default\n'
+        'module.exports = ' + (
+          script.src
+            ? getRequireForImport('script', script, 0)
+            : getRequire('script', script, 0)
+        ) + '\nif (module.exports.__esModule) module.exports = module.exports.default\n'
     }
 
     // add require for template
diff --git a/lib/parser.js b/lib/parser.js
index e0e9a42..29a1856 100644
--- a/lib/parser.js
+++ b/lib/parser.js
@@ -32,11 +32,6 @@ module.exports = function (content) {
 
     // handle src imports
     if (src) {
-      if (type !== 'style' && type !== 'template') {
-        return cb(new Error(
-          '[vue-loader] src import is only supported for <template> and <style> tags.'
-        ))
-      }
       if (type === 'style') {
         output.styleImports.push({
           src: src,
@@ -48,6 +43,11 @@ module.exports = function (content) {
           src: src,
           lang: lang
         })
+      } else if (type === 'script') {
+        output.script.push({
+          src: src,
+          lang: lang
+        })
       }
       return
     }


Test patch:
diff --git a/test/fixtures/script-import-entry.js b/test/fixtures/script-import-entry.js
new file mode 100644
index 0000000..5c1491e
--- /dev/null
+++ b/test/fixtures/script-import-entry.js
@@ -0,0 +1,1 @@
+window.testModule = require('./script-import.vue')
diff --git a/test/fixtures/script-import.js b/test/fixtures/script-import.js
new file mode 100644
index 0000000..8ab2015
--- /dev/null
+++ b/test/fixtures/script-import.js
@@ -0,0 +1,7 @@
+export default {
+  data () {
+    return {
+      msg: 'Hello from Component A!'
+    }
+  }
+};
\ No newline at end of file
diff --git a/test/fixtures/script-import.vue b/test/fixtures/script-import.vue
new file mode 100644
index 0000000..b45d1ec
--- /dev/null
+++ b/test/fixtures/script-import.vue
@@ -0,0 +1,1 @@
+<script src="./script-import.js"></script>
\ No newline at end of file
diff --git a/test/test.js b/test/test.js
index 2c5b5dd..243e555 100644
--- a/test/test.js
+++ b/test/test.js
@@ -130,6 +130,16 @@ describe('vue-loader', function () {
     })
   })
 
+  it('script import', function (done) {
+    test({
+      entry: './test/fixtures/script-import-entry.js'
+    }, function (window) {
+      var module = window.testModule
+      expect(module.data().msg).to.contain('Hello from Component A!')
+      done()
+    })
+  })
+
   it('source map', function (done) {
     var config = Object.assign({}, globalConfig, {
       entry: './test/fixtures/basic.js',