Skip to content

Commit fd58a77

Browse files
committed
feat(babel): support babelrc in package.json
1 parent 199d969 commit fd58a77

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

lib/compilers/babel-compiler.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
11
const babel = require('babel-core')
22
const path = require('path')
33
const fs = require('fs')
4+
const findBabelConfig = require('find-babel-config')
45
const throwError = require('../throw-error')
56
const logger = require('../logger')
7+
68
var defaultBabelOptions = {
79
presets: ['es2015'],
810
plugins: ['transform-runtime']
911
}
1012

11-
function getBabelRc (path) {
12-
var rc
13-
try {
14-
rc = JSON.parse(fs.readFileSync(path, 'utf-8'))
15-
} catch (e) {
16-
throwError('Your .babelrc seems to be incorrectly formatted.')
13+
module.exports = function compileBabel (scriptContent) {
14+
// directory can be an absolute or relative path
15+
// If it's a relative path, it is relative to the current working directory (process.cwd())
16+
const directory = path.resolve(__dirname, '..')
17+
const { file, config } = findBabelConfig.sync(directory);
18+
19+
if (!file) {
20+
logger.info('no .babelrc found, defaulting to default babel options')
1721
}
18-
return rc
19-
}
2022

21-
module.exports = function compileBabel (scriptContent) {
22-
const babelRcPath = path.resolve(process.cwd(), '.babelrc')
23-
const babelRcExists = fs.existsSync(babelRcPath);
24-
const baseBabelOptions = babelRcExists ? getBabelRc(babelRcPath) : defaultBabelOptions
23+
const baseBabelOptions = file ? config : defaultBabelOptions
2524
const babelOptions = Object.assign({sourceMaps: true}, baseBabelOptions)
2625

2726
const res = babel.transform(scriptContent, babelOptions)
2827

29-
if (!babelRcExists) {
30-
logger.info('no .babelrc found, defaulting to default babel options')
31-
}
32-
3328
return {
3429
code: res.code,
3530
sourceMap: res.map

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"babel-preset-es2015": "^6.24.1",
5454
"chalk": "^2.1.0",
5555
"convert-source-map": "^1.5.0",
56+
"find-babel-config": "^1.1.0",
5657
"object-assign": "^4.1.1",
5758
"source-map": "^0.5.6",
5859
"typescript": "^2.5.2"

test/Babel.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ test('logs info when there is no .babelrc', () => {
4747
jest.resetModules()
4848
})
4949

50+
test('uses babelrc in package.json if none in .babelrc', () => {
51+
const babelRcPath = resolve(__dirname, '../.babelrc')
52+
const tempPath = resolve(__dirname, '../.renamed')
53+
const packagePath = resolve(__dirname, '../package.json')
54+
const packageOriginal = readFileSync(packagePath, { encoding: 'utf8' })
55+
writeFileSync(packagePath, '{ "babel": {"presets": ["es2015"],"plugins": ["istanbul"]}}')
56+
renameSync(babelRcPath, tempPath)
57+
const filePath = resolve(__dirname, './resources/Basic.vue')
58+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
59+
60+
try {
61+
const output = jestVue.process(fileString, filePath)
62+
expect(output).toContain('coverageData.hash')
63+
} catch (err) {
64+
renameSync(tempPath, babelRcPath)
65+
writeFileSync(packagePath, packageOriginal)
66+
jest.resetModules()
67+
throw err
68+
}
69+
renameSync(tempPath, babelRcPath)
70+
writeFileSync(packagePath, packageOriginal)
71+
jest.resetModules()
72+
})
73+
5074
test('processes .vue files using .babelrc if it exists in route', () => {
5175
const babelRcPath = resolve(__dirname, '../.babelrc')
5276
const babelRcOriginal = readFileSync(babelRcPath, { encoding: 'utf8' })

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,13 @@ fill-range@^2.1.0:
15931593
repeat-element "^1.1.2"
15941594
repeat-string "^1.5.2"
15951595

1596+
find-babel-config@^1.1.0:
1597+
version "1.1.0"
1598+
resolved "https://registry.yarnpkg.com/find-babel-config/-/find-babel-config-1.1.0.tgz#acc01043a6749fec34429be6b64f542ebb5d6355"
1599+
dependencies:
1600+
json5 "^0.5.1"
1601+
path-exists "^3.0.0"
1602+
15961603
find-up@^1.0.0:
15971604
version "1.1.2"
15981605
resolved "https://registry.yarnpkg.com/find-up/-/find-up-1.1.2.tgz#6b2e9822b1a2ce0a60ab64d610eccad53cb24d0f"

0 commit comments

Comments
 (0)