Skip to content

feat(src): Add support of template and script src tags #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>