Skip to content

Transpile coffeescript written in ES6 #35

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 5 commits into from
Dec 13, 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
27 changes: 2 additions & 25 deletions lib/compilers/babel-compiler.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
const babel = require('babel-core')
const findBabelConfig = require('find-babel-config')
const logger = require('../logger')
const cache = require('../cache')

var defaultBabelOptions = {
presets: [require.resolve('babel-preset-vue-app')]
}

function getBabelConfig () {
const cachedConfig = cache.get('babel-config')
if (cachedConfig) {
return cachedConfig
} else {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
if (!file) {
logger.info('no .babelrc found, defaulting to default babel options')
}
const babelConfig = file ? config : defaultBabelOptions
cache.set('babel-config', babelConfig)
return babelConfig
}
}
const loadBabelConfig = require('../load-babel-config.js')

module.exports = function compileBabel (scriptContent, inputSourceMap) {
const sourceMapOptions = {
sourceMaps: true,
inputSourceMap: inputSourceMap
}

const babelConfig = getBabelConfig()

const babelOptions = Object.assign(sourceMapOptions, babelConfig)
const babelOptions = Object.assign(sourceMapOptions, loadBabelConfig())

const res = babel.transform(scriptContent, babelOptions)

Expand Down
4 changes: 3 additions & 1 deletion lib/compilers/coffee-compiler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var ensureRequire = require('../ensure-require.js')
const throwError = require('../throw-error')
const loadBabelConfig = require('../load-babel-config.js')

module.exports = function (raw, cb, compiler) {
ensureRequire('coffee', ['coffeescript'])
Expand All @@ -8,7 +9,8 @@ module.exports = function (raw, cb, compiler) {
try {
compiled = coffee.compile(raw, {
bare: true,
sourceMap: true
sourceMap: true,
transpile: loadBabelConfig()
})
} catch (err) {
throwError(err)
Expand Down
22 changes: 22 additions & 0 deletions lib/load-babel-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const findBabelConfig = require('find-babel-config')
const logger = require('./logger')
const cache = require('./cache')

var defaultBabelOptions = {
presets: [require.resolve('babel-preset-vue-app')]
}

module.exports = function getBabelConfig () {
const cachedConfig = cache.get('babel-config')
if (cachedConfig) {
return cachedConfig
} else {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
if (!file) {
logger.info('no .babelrc found, defaulting to default babel options')
}
const babelConfig = file ? config : defaultBabelOptions
cache.set('babel-config', babelConfig)
return babelConfig
}
}
95 changes: 89 additions & 6 deletions test/coffee.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,94 @@
import { shallow } from 'vue-test-utils'
import { shallow, mount } from 'vue-test-utils'
import Coffee from './resources/Coffee.vue'
import CoffeeScript from './resources/CoffeeScript.vue'
import CoffeeES6 from './resources/CoffeeES6.vue'
import CoffeeScriptES6 from './resources/CoffeeScriptES6.vue'
import jestVue from '../vue-jest'
import { resolve } from 'path'
import {
readFileSync,
writeFileSync,
renameSync
} from 'fs'
import clearModule from 'clear-module'
import cache from '../lib/cache'

test('processes .vue file with lang set to coffeescript', () => {
shallow(Coffee)
})
describe('Test CoffeeScript - coffee.spec.js', () => {
beforeEach(() => {
cache.flushAll()
clearModule.all()
})

test('processes .vue file with lang set to coffee', () => {
shallow(Coffee)
})

test('processes .vue file with lang set to coffeescript', () => {
shallow(CoffeeScript)
})

test('processes .vue file with lang set to coffee (ES6)', () => {
shallow(CoffeeES6)
})

test('processes .vue file with lang set to coffeescript (ES6)', () => {
shallow(CoffeeScriptES6)
})

test('processes .vue file with lang set to coffeescript (ES6)', () => {
const wrapper = mount(CoffeeScriptES6)
expect(typeof wrapper).toBe('object')
})

test('processes .vue files with lang set to coffeescript using .babelrc if there is no .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
renameSync(babelRcPath, tempPath)
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
try {
jestVue.process(fileString, filePath)
} catch (err) {
renameSync(tempPath, babelRcPath)
throw err
}
renameSync(tempPath, babelRcPath)
})

test('processes .vue files with lang set to coffeescript, uses babelrc in package.json if none in .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
const packagePath = resolve(__dirname, '../package.json')
const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
writeFileSync(packagePath, '{ "babel": {"presets": ["env"],"plugins": ["istanbul"]}}')
renameSync(babelRcPath, tempPath)
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })

try {
const output = jestVue.process(fileString, filePath)
expect(output.code).toContain('coverageData.hash')
} catch (err) {
renameSync(tempPath, babelRcPath)
writeFileSync(packagePath, packageOriginal)
jest.resetModules()
throw err
}
renameSync(tempPath, babelRcPath)
writeFileSync(packagePath, packageOriginal)
jest.resetModules()
})

test('processes .vue files with lang set to coffeescript using .babelrc if it exists in route', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
writeFileSync(babelRcPath, '{"presets": ["env"],"plugins": ["istanbul"]}')
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })

test('processes .vue file with lang set to coffeescript', () => {
shallow(CoffeeScript)
const output = jestVue.process(fileString, filePath)
writeFileSync(babelRcPath, babelRcOriginal)
// coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
expect(output.code).toContain('coverageData.hash')
})
})
54 changes: 54 additions & 0 deletions test/load-babel-config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import loadBabelConfig from '../lib/load-babel-config'
import { resolve } from 'path'
import {
createReadStream,
createWriteStream,
readFileSync,
renameSync
} from 'fs'
import clearModule from 'clear-module'
import cache from '../lib/cache'

describe('load-babel-config.js', () => {
beforeEach(() => {
cache.flushAll()
clearModule.all()
})

it('reads default babel if there is no .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcOriginal = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
const tempPath = resolve(__dirname, '../.renamed')
renameSync(babelRcPath, tempPath)
const babelConfig = loadBabelConfig()
try {
expect(babelConfig).not.toBe(babelRcOriginal)
} catch (err) {
renameSync(tempPath, babelRcPath)
throw err
}
renameSync(tempPath, babelRcPath)
const babelConfigCached = loadBabelConfig()
expect(babelConfigCached).not.toBe(babelConfig)
expect(babelConfigCached).toEqual(babelConfig)
})

it('reads default babel if there is .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcCopiedPath = resolve(__dirname, '../.babelrc_cp')
createReadStream(babelRcPath).pipe(createWriteStream(babelRcCopiedPath))
const babelRcOriginal = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
const babelConfig = loadBabelConfig()
expect(babelConfig).toEqual(babelRcOriginal)
const tempPath = resolve(__dirname, '../.renamed')
renameSync(babelRcCopiedPath, tempPath)
const babelConfigCached = loadBabelConfig()
try {
expect(babelConfig).not.toBe(babelConfigCached)
expect(babelConfig).toEqual(babelConfigCached)
} catch (err) {
renameSync(tempPath, babelRcCopiedPath)
throw err
}
})
})
8 changes: 8 additions & 0 deletions test/resources/CoffeeES6.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<div />
</template>

<script lang="coffee">
export default
data: -> {}
</script>
8 changes: 8 additions & 0 deletions test/resources/CoffeeScriptES6.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<div />
</template>

<script lang="coffeescript">
export default
data: -> {}
</script>