Skip to content

Commit c5448c7

Browse files
wataruoguchieddyerburgh
authored andcommitted
feat: transpile coffeescript written in ES6 (#35)
* Transpile coffeescript written in ES6 * Read users .babelrc in coffee-compiler * fix: copyFileSync is node > v8.5. Use createReadStream/createWriteStream * Add tests for CoffeeScript transpile option * Remove sourcemap test as the CI does not write snapshots
1 parent 2ec4144 commit c5448c7

7 files changed

+186
-32
lines changed

lib/compilers/babel-compiler.js

+2-25
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,13 @@
11
const babel = require('babel-core')
2-
const findBabelConfig = require('find-babel-config')
3-
const logger = require('../logger')
4-
const cache = require('../cache')
5-
6-
var defaultBabelOptions = {
7-
presets: [require.resolve('babel-preset-vue-app')]
8-
}
9-
10-
function getBabelConfig () {
11-
const cachedConfig = cache.get('babel-config')
12-
if (cachedConfig) {
13-
return cachedConfig
14-
} else {
15-
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
16-
if (!file) {
17-
logger.info('no .babelrc found, defaulting to default babel options')
18-
}
19-
const babelConfig = file ? config : defaultBabelOptions
20-
cache.set('babel-config', babelConfig)
21-
return babelConfig
22-
}
23-
}
2+
const loadBabelConfig = require('../load-babel-config.js')
243

254
module.exports = function compileBabel (scriptContent, inputSourceMap) {
265
const sourceMapOptions = {
276
sourceMaps: true,
287
inputSourceMap: inputSourceMap
298
}
309

31-
const babelConfig = getBabelConfig()
32-
33-
const babelOptions = Object.assign(sourceMapOptions, babelConfig)
10+
const babelOptions = Object.assign(sourceMapOptions, loadBabelConfig())
3411

3512
const res = babel.transform(scriptContent, babelOptions)
3613

lib/compilers/coffee-compiler.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var ensureRequire = require('../ensure-require.js')
22
const throwError = require('../throw-error')
3+
const loadBabelConfig = require('../load-babel-config.js')
34

45
module.exports = function (raw, cb, compiler) {
56
ensureRequire('coffee', ['coffeescript'])
@@ -8,7 +9,8 @@ module.exports = function (raw, cb, compiler) {
89
try {
910
compiled = coffee.compile(raw, {
1011
bare: true,
11-
sourceMap: true
12+
sourceMap: true,
13+
transpile: loadBabelConfig()
1214
})
1315
} catch (err) {
1416
throwError(err)

lib/load-babel-config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const findBabelConfig = require('find-babel-config')
2+
const logger = require('./logger')
3+
const cache = require('./cache')
4+
5+
var defaultBabelOptions = {
6+
presets: [require.resolve('babel-preset-vue-app')]
7+
}
8+
9+
module.exports = function getBabelConfig () {
10+
const cachedConfig = cache.get('babel-config')
11+
if (cachedConfig) {
12+
return cachedConfig
13+
} else {
14+
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
15+
if (!file) {
16+
logger.info('no .babelrc found, defaulting to default babel options')
17+
}
18+
const babelConfig = file ? config : defaultBabelOptions
19+
cache.set('babel-config', babelConfig)
20+
return babelConfig
21+
}
22+
}

test/coffee.spec.js

+89-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,94 @@
1-
import { shallow } from 'vue-test-utils'
1+
import { shallow, mount } from 'vue-test-utils'
22
import Coffee from './resources/Coffee.vue'
33
import CoffeeScript from './resources/CoffeeScript.vue'
4+
import CoffeeES6 from './resources/CoffeeES6.vue'
5+
import CoffeeScriptES6 from './resources/CoffeeScriptES6.vue'
6+
import jestVue from '../vue-jest'
7+
import { resolve } from 'path'
8+
import {
9+
readFileSync,
10+
writeFileSync,
11+
renameSync
12+
} from 'fs'
13+
import clearModule from 'clear-module'
14+
import cache from '../lib/cache'
415

5-
test('processes .vue file with lang set to coffeescript', () => {
6-
shallow(Coffee)
7-
})
16+
describe('Test CoffeeScript - coffee.spec.js', () => {
17+
beforeEach(() => {
18+
cache.flushAll()
19+
clearModule.all()
20+
})
21+
22+
test('processes .vue file with lang set to coffee', () => {
23+
shallow(Coffee)
24+
})
25+
26+
test('processes .vue file with lang set to coffeescript', () => {
27+
shallow(CoffeeScript)
28+
})
29+
30+
test('processes .vue file with lang set to coffee (ES6)', () => {
31+
shallow(CoffeeES6)
32+
})
33+
34+
test('processes .vue file with lang set to coffeescript (ES6)', () => {
35+
shallow(CoffeeScriptES6)
36+
})
37+
38+
test('processes .vue file with lang set to coffeescript (ES6)', () => {
39+
const wrapper = mount(CoffeeScriptES6)
40+
expect(typeof wrapper).toBe('object')
41+
})
42+
43+
test('processes .vue files with lang set to coffeescript using .babelrc if there is no .babelrc', () => {
44+
const babelRcPath = resolve(__dirname, '../.babelrc')
45+
const tempPath = resolve(__dirname, '../.renamed')
46+
renameSync(babelRcPath, tempPath)
47+
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
48+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
49+
try {
50+
jestVue.process(fileString, filePath)
51+
} catch (err) {
52+
renameSync(tempPath, babelRcPath)
53+
throw err
54+
}
55+
renameSync(tempPath, babelRcPath)
56+
})
57+
58+
test('processes .vue files with lang set to coffeescript, uses babelrc in package.json if none in .babelrc', () => {
59+
const babelRcPath = resolve(__dirname, '../.babelrc')
60+
const tempPath = resolve(__dirname, '../.renamed')
61+
const packagePath = resolve(__dirname, '../package.json')
62+
const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
63+
writeFileSync(packagePath, '{ "babel": {"presets": ["env"],"plugins": ["istanbul"]}}')
64+
renameSync(babelRcPath, tempPath)
65+
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
66+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
67+
68+
try {
69+
const output = jestVue.process(fileString, filePath)
70+
expect(output.code).toContain('coverageData.hash')
71+
} catch (err) {
72+
renameSync(tempPath, babelRcPath)
73+
writeFileSync(packagePath, packageOriginal)
74+
jest.resetModules()
75+
throw err
76+
}
77+
renameSync(tempPath, babelRcPath)
78+
writeFileSync(packagePath, packageOriginal)
79+
jest.resetModules()
80+
})
81+
82+
test('processes .vue files with lang set to coffeescript using .babelrc if it exists in route', () => {
83+
const babelRcPath = resolve(__dirname, '../.babelrc')
84+
const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })
85+
writeFileSync(babelRcPath, '{"presets": ["env"],"plugins": ["istanbul"]}')
86+
const filePath = resolve(__dirname, './resources/CoffeeScriptES6.vue')
87+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
888

9-
test('processes .vue file with lang set to coffeescript', () => {
10-
shallow(CoffeeScript)
89+
const output = jestVue.process(fileString, filePath)
90+
writeFileSync(babelRcPath, babelRcOriginal)
91+
// coverageData.hash is added by babel-plugin-istanbul, added to root .babelrc for this test only
92+
expect(output.code).toContain('coverageData.hash')
93+
})
1194
})

test/load-babel-config.spec.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import loadBabelConfig from '../lib/load-babel-config'
2+
import { resolve } from 'path'
3+
import {
4+
createReadStream,
5+
createWriteStream,
6+
readFileSync,
7+
renameSync
8+
} from 'fs'
9+
import clearModule from 'clear-module'
10+
import cache from '../lib/cache'
11+
12+
describe('load-babel-config.js', () => {
13+
beforeEach(() => {
14+
cache.flushAll()
15+
clearModule.all()
16+
})
17+
18+
it('reads default babel if there is no .babelrc', () => {
19+
const babelRcPath = resolve(__dirname, '../.babelrc')
20+
const babelRcOriginal = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
21+
const tempPath = resolve(__dirname, '../.renamed')
22+
renameSync(babelRcPath, tempPath)
23+
const babelConfig = loadBabelConfig()
24+
try {
25+
expect(babelConfig).not.toBe(babelRcOriginal)
26+
} catch (err) {
27+
renameSync(tempPath, babelRcPath)
28+
throw err
29+
}
30+
renameSync(tempPath, babelRcPath)
31+
const babelConfigCached = loadBabelConfig()
32+
expect(babelConfigCached).not.toBe(babelConfig)
33+
expect(babelConfigCached).toEqual(babelConfig)
34+
})
35+
36+
it('reads default babel if there is .babelrc', () => {
37+
const babelRcPath = resolve(__dirname, '../.babelrc')
38+
const babelRcCopiedPath = resolve(__dirname, '../.babelrc_cp')
39+
createReadStream(babelRcPath).pipe(createWriteStream(babelRcCopiedPath))
40+
const babelRcOriginal = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
41+
const babelConfig = loadBabelConfig()
42+
expect(babelConfig).toEqual(babelRcOriginal)
43+
const tempPath = resolve(__dirname, '../.renamed')
44+
renameSync(babelRcCopiedPath, tempPath)
45+
const babelConfigCached = loadBabelConfig()
46+
try {
47+
expect(babelConfig).not.toBe(babelConfigCached)
48+
expect(babelConfig).toEqual(babelConfigCached)
49+
} catch (err) {
50+
renameSync(tempPath, babelRcCopiedPath)
51+
throw err
52+
}
53+
})
54+
})

test/resources/CoffeeES6.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<script lang="coffee">
6+
export default
7+
data: -> {}
8+
</script>

test/resources/CoffeeScriptES6.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<script lang="coffeescript">
6+
export default
7+
data: -> {}
8+
</script>

0 commit comments

Comments
 (0)