Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b40094f

Browse filesBrowse files
TheCycoONEeddyerburgh
authored andcommitted
feat: use file directory for .babelrc lookup (vuejs#93)
Remove limit for babelrc lookup Breaking change
1 parent d5ba225 commit b40094f
Copy full SHA for b40094f

File tree

Expand file treeCollapse file tree

7 files changed

+51
-13
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+51
-13
lines changed

‎lib/compilers/babel-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/babel-compiler.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const babel = require('babel-core')
22
const loadBabelConfig = require('../load-babel-config.js')
33

4-
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig) {
5-
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig)
4+
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig, filePath) {
5+
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig, filePath)
66

77
if (!babelConfig) {
88
return {

‎lib/compilers/coffee-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/coffee-compiler.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ var ensureRequire = require('../ensure-require.js')
22
const throwError = require('../throw-error')
33
const loadBabelConfig = require('../load-babel-config.js')
44

5-
module.exports = function (raw, vueJestConfig) {
5+
module.exports = function (raw, vueJestConfig, filePath) {
66
ensureRequire('coffee', ['coffeescript'])
77
var coffee = require('coffeescript')
88
var compiled
99
try {
1010
compiled = coffee.compile(raw, {
1111
bare: true,
1212
sourceMap: true,
13-
transpile: loadBabelConfig(vueJestConfig)
13+
transpile: loadBabelConfig(vueJestConfig, filePath)
1414
})
1515
} catch (err) {
1616
throwError(err)

‎lib/compilers/typescript-compiler.js

Copy file name to clipboardExpand all lines: lib/compilers/typescript-compiler.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const compileBabel = require('./babel-compiler')
33
const loadBabelConfig = require('../load-babel-config.js')
44
const { loadTypescriptConfig } = require('../load-typescript-config')
55

6-
module.exports = function compileTypescript (scriptContent, vueJestConfig) {
6+
module.exports = function compileTypescript (scriptContent, vueJestConfig, filePath) {
77
ensureRequire('typescript', ['typescript'])
88
const typescript = require('typescript')
99
const tsConfig = loadTypescriptConfig(vueJestConfig)
@@ -16,7 +16,7 @@ module.exports = function compileTypescript (scriptContent, vueJestConfig) {
1616
// handle ES modules in TS source code in case user uses non commonjs module
1717
// output and there is no .babelrc.
1818
let inlineBabelConfig
19-
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) {
19+
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig, filePath)) {
2020
inlineBabelConfig = {
2121
plugins: [
2222
require('babel-plugin-transform-es2015-modules-commonjs')

‎lib/load-babel-config.js

Copy file name to clipboardExpand all lines: lib/load-babel-config.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const cache = require('./cache')
44
const path = require('path')
55
const { readFileSync, existsSync } = require('fs')
66

7-
module.exports = function getBabelConfig (vueJestConfig) {
7+
module.exports = function getBabelConfig (vueJestConfig, filePath) {
88
const cachedConfig = cache.get('babel-config')
99
if (cachedConfig) {
1010
return cachedConfig
@@ -18,7 +18,7 @@ module.exports = function getBabelConfig (vueJestConfig) {
1818
} else if (existsSync('babel.config.js')) {
1919
babelConfig = require(path.resolve('babel.config.js'))
2020
} else {
21-
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
21+
const { file, config } = findBabelConfig.sync(filePath || process.cwd())
2222

2323
if (!file) {
2424
logger.info('no .babelrc found, skipping babel compilation')

‎lib/process.js

Copy file name to clipboardExpand all lines: lib/process.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ const join = path.join
1313
const logger = require('./logger')
1414
const splitRE = /\r?\n/g
1515

16-
function processScript (scriptPart, vueJestConfig) {
16+
function processScript (scriptPart, vueJestConfig, filePath) {
1717
if (!scriptPart) {
1818
return { code: '' }
1919
}
2020

2121
if (/^typescript|tsx?$/.test(scriptPart.lang)) {
22-
return compileTypescript(scriptPart.content, vueJestConfig)
22+
return compileTypescript(scriptPart.content, vueJestConfig, filePath)
2323
}
2424

2525
if (scriptPart.lang === 'coffee' || scriptPart.lang === 'coffeescript') {
26-
return compileCoffeeScript(scriptPart.content, vueJestConfig)
26+
return compileCoffeeScript(scriptPart.content, vueJestConfig, filePath)
2727
}
2828

29-
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig)
29+
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig, filePath)
3030
}
3131

3232
module.exports = function (src, filePath, jestConfig) {
@@ -38,7 +38,7 @@ module.exports = function (src, filePath, jestConfig) {
3838
parts.script.content = fs.readFileSync(join(filePath, '..', parts.script.src), 'utf8')
3939
}
4040

41-
const result = processScript(parts.script, vueJestConfig)
41+
const result = processScript(parts.script, vueJestConfig, filePath)
4242
const script = result.code
4343
const inputMap = result.sourceMap
4444

‎test/Babel.spec.js

Copy file name to clipboardExpand all lines: test/Babel.spec.js
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,31 @@ test('processes .vue files using src attributes', () => {
2929

3030
test('skip processing if there is no .babelrc', () => {
3131
const babelRcPath = resolve(__dirname, '../.babelrc')
32+
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
3233
const tempPath = resolve(__dirname, '../.renamed')
34+
const tempPath2 = resolve(__dirname, '../../.renamed')
3335
renameSync(babelRcPath, tempPath)
36+
renameSync(babelRcPath2, tempPath2)
3437
const filePath = resolve(__dirname, './resources/Basic.vue')
3538
const fileString = readFileSync(filePath, { encoding: 'utf8' })
3639
try {
3740
jestVue.process(fileString, filePath)
3841
} catch (err) {
3942
renameSync(tempPath, babelRcPath)
43+
renameSync(tempPath2, babelRcPath2)
4044
throw err
4145
}
4246
renameSync(tempPath, babelRcPath)
47+
renameSync(tempPath2, babelRcPath2)
4348
})
4449

4550
test('logs info when there is no .babelrc', () => {
4651
const babelRcPath = resolve(__dirname, '../.babelrc')
52+
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
4753
const tempPath = resolve(__dirname, '../.renamed')
54+
const tempPath2 = resolve(__dirname, '../../.renamed')
4855
renameSync(babelRcPath, tempPath)
56+
renameSync(babelRcPath2, tempPath2)
4957
const info = jest.spyOn(global.console, 'info')
5058
const filePath = resolve(__dirname, './resources/Basic.vue')
5159
const fileString = readFileSync(filePath, { encoding: 'utf8' })
@@ -55,9 +63,11 @@ test('logs info when there is no .babelrc', () => {
5563
expect(info).toHaveBeenCalledWith('\n[vue-jest]: no .babelrc found, skipping babel compilation\n')
5664
} catch (err) {
5765
renameSync(tempPath, babelRcPath)
66+
renameSync(tempPath2, babelRcPath2)
5867
throw err
5968
}
6069
renameSync(tempPath, babelRcPath)
70+
renameSync(tempPath2, babelRcPath2)
6171
jest.resetModules()
6272
})
6373

‎test/load-babel-config.spec.js

Copy file name to clipboardExpand all lines: test/load-babel-config.spec.js
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,21 @@ describe('load-babel-config.js', () => {
1919

2020
it('returns undefined if there is no .babelrc', () => {
2121
const babelRcPath = resolve(__dirname, '../.babelrc')
22+
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
2223
const tempPath = resolve(__dirname, '../.renamed')
24+
const tempPath2 = resolve(__dirname, '../../.renamed')
2325
renameSync(babelRcPath, tempPath)
26+
renameSync(babelRcPath2, tempPath2)
2427
const babelConfig = loadBabelConfig({})
2528
try {
2629
expect(babelConfig).toBe(undefined)
2730
} catch (err) {
2831
renameSync(tempPath, babelRcPath)
32+
renameSync(tempPath2, babelRcPath2)
2933
throw err
3034
}
3135
renameSync(tempPath, babelRcPath)
36+
renameSync(tempPath2, babelRcPath2)
3237
const babelConfigCached = loadBabelConfig()
3338
expect(babelConfigCached).toBe(undefined)
3439
})
@@ -65,6 +70,29 @@ describe('load-babel-config.js', () => {
6570
}
6671
})
6772

73+
it('reads .babelrc if it is below the current working directory', () => {
74+
const babelRcPath = resolve(__dirname, '../.babelrc')
75+
const babelRcContent = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
76+
process.chdir('test')
77+
const babelConfig = loadBabelConfig({})
78+
expect(babelConfig).toEqual(babelRcContent)
79+
process.chdir('..')
80+
})
81+
82+
it('reads .babelrc from the current working directory', () => {
83+
const babelRcPath = resolve(__dirname, '../.babelrc')
84+
const babelRcContent = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
85+
const newBabelRcPath = resolve(__dirname, '../test/.babelrc')
86+
const newBabelRcContent = '{"env":{}}'
87+
process.chdir('test')
88+
writeFileSync(newBabelRcPath, newBabelRcContent)
89+
const babelConfig = loadBabelConfig({})
90+
expect(babelConfig).toEqual(JSON.parse(newBabelRcContent))
91+
expect(babelConfig).not.toEqual(babelRcContent)
92+
unlinkSync(newBabelRcPath)
93+
process.chdir('..')
94+
})
95+
6896
it('supports babel.config.js', () => {
6997
const babelConfigPath = resolve(__dirname, '../babel.config.js')
7098
const config = {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.