Skip to content

Commit 240f62e

Browse files
Toilaleddyerburgh
authored andcommitted
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
1 parent 30506cc commit 240f62e

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

lib/process.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const compileBabel = require('./compilers/babel-compiler')
77
const compileTypescript = require('./compilers/typescript-compiler')
88
const compileCoffeeScript = require('./compilers/coffee-compiler')
99
const extractPropsFromFunctionalTemplate = require('./extract-props')
10+
const fs = require('fs')
11+
const join = require('path').join
1012

1113
const splitRE = /\r?\n/g
1214

@@ -41,6 +43,10 @@ module.exports = function (src, path) {
4143

4244
changePartsIfFunctional(parts)
4345

46+
if (parts.script && parts.script.src) {
47+
parts.script.content = fs.readFileSync(join(path, '..', parts.script.src), 'utf8')
48+
}
49+
4450
const result = processScript(parts.script)
4551
const script = result.code
4652
const inputMap = result.sourceMap
@@ -53,6 +59,10 @@ module.exports = function (src, path) {
5359
': module.exports)\n'
5460

5561
if (parts.template) {
62+
if (parts.template.src) {
63+
parts.template.content = fs.readFileSync(join(path, '..', parts.template.src), 'utf8')
64+
}
65+
5666
const renderFunctions = compileTemplate(parts.template)
5767

5868
output += '__vue__options__.render = ' + renderFunctions.render + '\n' +

test/Babel.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Vue from 'vue'
22
import Basic from './resources/Basic.vue'
3+
import BasicSrc from './resources/BasicSrc.vue'
34
import jestVue from '../vue-jest'
45
import { resolve } from 'path'
56
import {
@@ -20,6 +21,11 @@ test('processes .vue files', () => {
2021
expect(typeof vm.$el).toBe('object')
2122
})
2223

24+
test('processes .vue files using src attributes', () => {
25+
const vm = new Vue(BasicSrc).$mount()
26+
expect(typeof vm.$el).toBe('object')
27+
})
28+
2329
test('processes .vue files with default babel if there is no .babelrc', () => {
2430
const babelRcPath = resolve(__dirname, '../.babelrc')
2531
const tempPath = resolve(__dirname, '../.renamed')

test/resources/BasicSrc.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="hello">
2+
<h1 :class="headingClasses">{{ msg }}</h1>
3+
</div>

test/resources/BasicSrc.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export default {
2+
name: 'basic',
3+
computed: {
4+
headingClasses: function headingClasses () {
5+
return {
6+
red: this.isCrazy,
7+
blue: !this.isCrazy,
8+
shadow: this.isCrazy
9+
}
10+
}
11+
},
12+
data: function data () {
13+
return {
14+
msg: 'Welcome to Your Vue.js App',
15+
isCrazy: false
16+
}
17+
},
18+
methods: {
19+
toggleClass: function toggleClass () {
20+
this.isCrazy = !this.isCrazy
21+
}
22+
}
23+
}

test/resources/BasicSrc.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template src="./BasicSrc.html"></template>
2+
3+
<script src="./BasicSrc.js"></script>

0 commit comments

Comments
 (0)