Skip to content

Commit

Permalink
feat(src): Add support of template and script src tags (#29)
Browse files Browse the repository at this point in the history
* feat(src): Add support of template and script src tags

Close #28

* test(src): Add tests for src attribute support
  • Loading branch information
Toilal authored and eddyerburgh committed Nov 27, 2017
1 parent 30506cc commit 240f62e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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' +
Expand Down
6 changes: 6 additions & 0 deletions test/Babel.spec.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')
Expand Down
3 changes: 3 additions & 0 deletions test/resources/BasicSrc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="hello">
<h1 :class="headingClasses">{{ msg }}</h1>
</div>
23 changes: 23 additions & 0 deletions test/resources/BasicSrc.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
3 changes: 3 additions & 0 deletions test/resources/BasicSrc.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template src="./BasicSrc.html"></template>

<script src="./BasicSrc.js"></script>

0 comments on commit 240f62e

Please sign in to comment.