From 240f62e9a6b612224c931195d11061ab19ca88ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Mon, 27 Nov 2017 18:12:01 +0100 Subject: [PATCH] feat(src): Add support of template and script src tags (#29) * feat(src): Add support of template and script src tags Close #28 * test(src): Add tests for src attribute support --- lib/process.js | 10 ++++++++++ test/Babel.spec.js | 6 ++++++ test/resources/BasicSrc.html | 3 +++ test/resources/BasicSrc.js | 23 +++++++++++++++++++++++ test/resources/BasicSrc.vue | 3 +++ 5 files changed, 45 insertions(+) create mode 100644 test/resources/BasicSrc.html create mode 100644 test/resources/BasicSrc.js create mode 100644 test/resources/BasicSrc.vue diff --git a/lib/process.js b/lib/process.js index 255b9513..f82f7750 100644 --- a/lib/process.js +++ b/lib/process.js @@ -7,6 +7,8 @@ const compileBabel = require('./compilers/babel-compiler') const compileTypescript = require('./compilers/typescript-compiler') const compileCoffeeScript = require('./compilers/coffee-compiler') const extractPropsFromFunctionalTemplate = require('./extract-props') +const fs = require('fs') +const join = require('path').join const splitRE = /\r?\n/g @@ -41,6 +43,10 @@ module.exports = function (src, path) { changePartsIfFunctional(parts) + if (parts.script && parts.script.src) { + parts.script.content = fs.readFileSync(join(path, '..', parts.script.src), 'utf8') + } + const result = processScript(parts.script) const script = result.code const inputMap = result.sourceMap @@ -53,6 +59,10 @@ module.exports = function (src, path) { ': module.exports)\n' if (parts.template) { + if (parts.template.src) { + parts.template.content = fs.readFileSync(join(path, '..', parts.template.src), 'utf8') + } + const renderFunctions = compileTemplate(parts.template) output += '__vue__options__.render = ' + renderFunctions.render + '\n' + diff --git a/test/Babel.spec.js b/test/Babel.spec.js index 45fae060..f5d2a148 100644 --- a/test/Babel.spec.js +++ b/test/Babel.spec.js @@ -1,5 +1,6 @@ import Vue from 'vue' import Basic from './resources/Basic.vue' +import BasicSrc from './resources/BasicSrc.vue' import jestVue from '../vue-jest' import { resolve } from 'path' import { @@ -20,6 +21,11 @@ test('processes .vue files', () => { expect(typeof vm.$el).toBe('object') }) +test('processes .vue files using src attributes', () => { + const vm = new Vue(BasicSrc).$mount() + expect(typeof vm.$el).toBe('object') +}) + test('processes .vue files with default babel if there is no .babelrc', () => { const babelRcPath = resolve(__dirname, '../.babelrc') const tempPath = resolve(__dirname, '../.renamed') diff --git a/test/resources/BasicSrc.html b/test/resources/BasicSrc.html new file mode 100644 index 00000000..67c4dfde --- /dev/null +++ b/test/resources/BasicSrc.html @@ -0,0 +1,3 @@ +
+

{{ msg }}

+
\ No newline at end of file diff --git a/test/resources/BasicSrc.js b/test/resources/BasicSrc.js new file mode 100644 index 00000000..c5da7ffc --- /dev/null +++ b/test/resources/BasicSrc.js @@ -0,0 +1,23 @@ +export default { + name: 'basic', + computed: { + headingClasses: function headingClasses () { + return { + red: this.isCrazy, + blue: !this.isCrazy, + shadow: this.isCrazy + } + } + }, + data: function data () { + return { + msg: 'Welcome to Your Vue.js App', + isCrazy: false + } + }, + methods: { + toggleClass: function toggleClass () { + this.isCrazy = !this.isCrazy + } + } +} diff --git a/test/resources/BasicSrc.vue b/test/resources/BasicSrc.vue new file mode 100644 index 00000000..2fbf9f1e --- /dev/null +++ b/test/resources/BasicSrc.vue @@ -0,0 +1,3 @@ + + +