Skip to content

Improve .babelrc lookup #93

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 4 commits into from
Oct 4, 2018
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
4 changes: 2 additions & 2 deletions lib/compilers/babel-compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const babel = require('babel-core')
const loadBabelConfig = require('../load-babel-config.js')

module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig) {
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig)
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig, filePath) {
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig, filePath)

if (!babelConfig) {
return {
Expand Down
4 changes: 2 additions & 2 deletions lib/compilers/coffee-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ var ensureRequire = require('../ensure-require.js')
const throwError = require('../throw-error')
const loadBabelConfig = require('../load-babel-config.js')

module.exports = function (raw, vueJestConfig) {
module.exports = function (raw, vueJestConfig, filePath) {
ensureRequire('coffee', ['coffeescript'])
var coffee = require('coffeescript')
var compiled
try {
compiled = coffee.compile(raw, {
bare: true,
sourceMap: true,
transpile: loadBabelConfig(vueJestConfig)
transpile: loadBabelConfig(vueJestConfig, filePath)
})
} catch (err) {
throwError(err)
Expand Down
4 changes: 2 additions & 2 deletions lib/compilers/typescript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const compileBabel = require('./babel-compiler')
const loadBabelConfig = require('../load-babel-config.js')
const { loadTypescriptConfig } = require('../load-typescript-config')

module.exports = function compileTypescript (scriptContent, vueJestConfig) {
module.exports = function compileTypescript (scriptContent, vueJestConfig, filePath) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const tsConfig = loadTypescriptConfig(vueJestConfig)
Expand All @@ -16,7 +16,7 @@ module.exports = function compileTypescript (scriptContent, vueJestConfig) {
// handle ES modules in TS source code in case user uses non commonjs module
// output and there is no .babelrc.
let inlineBabelConfig
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) {
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig, filePath)) {
inlineBabelConfig = {
plugins: [
require('babel-plugin-transform-es2015-modules-commonjs')
Expand Down
4 changes: 2 additions & 2 deletions lib/load-babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const cache = require('./cache')
const path = require('path')
const { readFileSync, existsSync } = require('fs')

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

if (!file) {
logger.info('no .babelrc found, skipping babel compilation')
Expand Down
10 changes: 5 additions & 5 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ const join = path.join
const logger = require('./logger')
const splitRE = /\r?\n/g

function processScript (scriptPart, vueJestConfig) {
function processScript (scriptPart, vueJestConfig, filePath) {
if (!scriptPart) {
return { code: '' }
}

if (/^typescript|tsx?$/.test(scriptPart.lang)) {
return compileTypescript(scriptPart.content, vueJestConfig)
return compileTypescript(scriptPart.content, vueJestConfig, filePath)
}

if (scriptPart.lang === 'coffee' || scriptPart.lang === 'coffeescript') {
return compileCoffeeScript(scriptPart.content, vueJestConfig)
return compileCoffeeScript(scriptPart.content, vueJestConfig, filePath)
}

return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig)
return compileBabel(scriptPart.content, undefined, undefined, vueJestConfig, filePath)
}

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

const result = processScript(parts.script, vueJestConfig)
const result = processScript(parts.script, vueJestConfig, filePath)
const script = result.code
const inputMap = result.sourceMap

Expand Down
10 changes: 10 additions & 0 deletions test/Babel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,31 @@ test('processes .vue files using src attributes', () => {

test('skip processing if there is no .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
const tempPath2 = resolve(__dirname, '../../.renamed')
renameSync(babelRcPath, tempPath)
renameSync(babelRcPath2, tempPath2)
const filePath = resolve(__dirname, './resources/Basic.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
try {
jestVue.process(fileString, filePath)
} catch (err) {
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
throw err
}
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
})

test('logs info when there is no .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
const tempPath2 = resolve(__dirname, '../../.renamed')
renameSync(babelRcPath, tempPath)
renameSync(babelRcPath2, tempPath2)
const info = jest.spyOn(global.console, 'info')
const filePath = resolve(__dirname, './resources/Basic.vue')
const fileString = readFileSync(filePath, { encoding: 'utf8' })
Expand All @@ -55,9 +63,11 @@ test('logs info when there is no .babelrc', () => {
expect(info).toHaveBeenCalledWith('\n[vue-jest]: no .babelrc found, skipping babel compilation\n')
} catch (err) {
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
throw err
}
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
jest.resetModules()
})

Expand Down
28 changes: 28 additions & 0 deletions test/load-babel-config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@ describe('load-babel-config.js', () => {

it('returns undefined if there is no .babelrc', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcPath2 = resolve(__dirname, '../../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
const tempPath2 = resolve(__dirname, '../../.renamed')
renameSync(babelRcPath, tempPath)
renameSync(babelRcPath2, tempPath2)
const babelConfig = loadBabelConfig({})
try {
expect(babelConfig).toBe(undefined)
} catch (err) {
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
throw err
}
renameSync(tempPath, babelRcPath)
renameSync(tempPath2, babelRcPath2)
const babelConfigCached = loadBabelConfig()
expect(babelConfigCached).toBe(undefined)
})
Expand Down Expand Up @@ -65,6 +70,29 @@ describe('load-babel-config.js', () => {
}
})

it('reads .babelrc if it is below the current working directory', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcContent = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
process.chdir('test')
const babelConfig = loadBabelConfig({})
expect(babelConfig).toEqual(babelRcContent)
process.chdir('..')
})

it('reads .babelrc from the current working directory', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const babelRcContent = JSON.parse(readFileSync(babelRcPath, { encoding: 'utf8' }))
const newBabelRcPath = resolve(__dirname, '../test/.babelrc')
const newBabelRcContent = '{"env":{}}'
process.chdir('test')
writeFileSync(newBabelRcPath, newBabelRcContent)
const babelConfig = loadBabelConfig({})
expect(babelConfig).toEqual(JSON.parse(newBabelRcContent))
expect(babelConfig).not.toEqual(babelRcContent)
unlinkSync(newBabelRcPath)
process.chdir('..')
})

it('supports babel.config.js', () => {
const babelConfigPath = resolve(__dirname, '../babel.config.js')
const config = {
Expand Down