Skip to content

Global config options for Babel and Typescript #87

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 3 commits into from
Apr 25, 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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ vue-jest compiles the script and template of SFCs into a JavaScript file that Je
- **typescript** (`lang="ts"`, `lang="typescript"`)
- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)

To define a tsconfig file that vue-jest will use when transpiling typescript, you can specify it in the jest globals

```json
{
"jest": {
"vue-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
}
}
```

To define a babelrc file that vue-jest will use when transpiling javascript, you can specify it in the jest globals

```json
{
"jest": {
"vue-jest": {
"babelRcFile": "jest.babelrc"
}
}
}
```

### Supported template languages

- **pug** (`lang="pug"`)
Expand Down
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) {
const babelConfig = inlineConfig || loadBabelConfig()
module.exports = function compileBabel (scriptContent, inputSourceMap, inlineConfig, vueJestConfig) {
const babelConfig = inlineConfig || loadBabelConfig(vueJestConfig)

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

module.exports = function compileTypescript (scriptContent) {
module.exports = function compileTypescript (scriptContent, vueJestConfig) {
ensureRequire('typescript', ['typescript'])
const typescript = require('typescript')
const tsConfig = loadTypescriptConfig()
const tsConfig = loadTypescriptConfig(vueJestConfig)

const res = typescript.transpileModule(scriptContent, tsConfig)
const inputSourceMap = (res.sourceMapText !== undefined)
Expand All @@ -16,13 +16,13 @@ module.exports = function compileTypescript (scriptContent) {
// 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()) {
if (tsConfig.compilerOptions.module !== 'commonjs' && !loadBabelConfig(vueJestConfig)) {
inlineBabelConfig = {
plugins: [
require('babel-plugin-transform-es2015-modules-commonjs')
]
}
}

return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig)
return compileBabel(res.outputText, inputSourceMap, inlineBabelConfig, vueJestConfig)
}
27 changes: 19 additions & 8 deletions lib/load-babel-config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
const findBabelConfig = require('find-babel-config')
const logger = require('./logger')
const cache = require('./cache')
const { readFileSync } = require('fs')

module.exports = function getBabelConfig () {
module.exports = function getBabelConfig (vueJestConfig) {
const cachedConfig = cache.get('babel-config')
if (cachedConfig) {
return cachedConfig
} else if (cachedConfig === false) {
return
} else {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)
if (!file) {
logger.info('no .babelrc found, skipping babel compilation')
cache.set('babel-config', false)
return
let babelConfig

if (vueJestConfig.babelRcFile) {
babelConfig = JSON.parse(readFileSync(vueJestConfig.babelRcFile))
} else {
const { file, config } = findBabelConfig.sync(process.cwd(), 0)

if (!file) {
logger.info('no .babelrc found, skipping babel compilation')
cache.set('babel-config', false)
return
}

babelConfig = config
}
cache.set('babel-config', config)
return config

cache.set('babel-config', babelConfig)
return babelConfig
}
}
17 changes: 12 additions & 5 deletions lib/load-typescript-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ const defaultTypescriptConfig = {
}
}

module.exports.loadTypescriptConfig = function loadTypescriptConfig () {
module.exports.loadTypescriptConfig = function loadTypescriptConfig (vueJestConfig) {
const cachedConfig = cache.get('typescript-config')
if (cachedConfig) {
return cachedConfig
} else {
const { path, config } = tsconfig.loadSync(process.cwd())
let typescriptConfig

if (!path) {
logger.info('no tsconfig.json found, defaulting to default typescript options')
if (vueJestConfig.tsConfigFile) {
typescriptConfig = tsconfig.readFileSync(vueJestConfig.tsConfigFile)
} else {
const { path, config } = tsconfig.loadSync(process.cwd())

if (!path) {
logger.info('no tsconfig.json found, defaulting to default typescript options')
}

typescriptConfig = path ? config : defaultTypescriptConfig
}

const typescriptConfig = path ? config : defaultTypescriptConfig
cache.set('typescript-config', typescriptConfig)
return typescriptConfig
}
Expand Down
10 changes: 5 additions & 5 deletions lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ const join = path.join
const logger = require('./logger')
const splitRE = /\r?\n/g

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

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

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

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

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

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

Expand Down
21 changes: 18 additions & 3 deletions test/load-babel-config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
createReadStream,
createWriteStream,
readFileSync,
renameSync
renameSync,
writeFileSync,
unlinkSync
} from 'fs'
import clearModule from 'clear-module'
import cache from '../lib/cache'
Expand All @@ -19,7 +21,7 @@ describe('load-babel-config.js', () => {
const babelRcPath = resolve(__dirname, '../.babelrc')
const tempPath = resolve(__dirname, '../.renamed')
renameSync(babelRcPath, tempPath)
const babelConfig = loadBabelConfig()
const babelConfig = loadBabelConfig({})
try {
expect(babelConfig).toBe(undefined)
} catch (err) {
Expand All @@ -31,12 +33,25 @@ describe('load-babel-config.js', () => {
expect(babelConfigCached).toBe(undefined)
})

it('reads babelrc from jest globals if exists', () => {
const jestGlobalBabelPath = resolve(__dirname, '../jest.babelrc')
writeFileSync(jestGlobalBabelPath, JSON.stringify({
plugins: ['foo']
}))
const jestGlobalBabelConfig = JSON.parse(readFileSync(jestGlobalBabelPath, { encoding: 'utf8' }))
const babelConfig = loadBabelConfig({
babelRcFile: 'jest.babelrc'
})
expect(babelConfig).toEqual(jestGlobalBabelConfig)
unlinkSync(jestGlobalBabelPath)
})

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()
const babelConfig = loadBabelConfig({})
expect(babelConfig).toEqual(babelRcOriginal)
const tempPath = resolve(__dirname, '../.renamed')
renameSync(babelRcCopiedPath, tempPath)
Expand Down
28 changes: 24 additions & 4 deletions test/load-typescript-config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
createReadStream,
createWriteStream,
readFileSync,
renameSync
writeFileSync,
renameSync,
unlinkSync
} from 'fs'
import clearModule from 'clear-module'
import cache from '../lib/cache'
Expand All @@ -20,7 +22,7 @@ describe('load-typescript-config.js', () => {
const tempPath = resolve(__dirname, '../.renamed')
renameSync(tsconfigPath, tempPath)

const tsConfig = loadTypescriptConfig()
const tsConfig = loadTypescriptConfig({})

try {
expect(tsConfig).toEqual(defaultConfig)
Expand All @@ -34,16 +36,34 @@ describe('load-typescript-config.js', () => {
expect(tsconfigCachedConfig).toEqual(defaultConfig)
})

it('returns the tsconfig specified in jest globals', () => {
const jestGlobalTsConfigPath = resolve(__dirname, '../tsconfig.jest.json')

writeFileSync(jestGlobalTsConfigPath, JSON.stringify({
allowJs: false
}))

const jestGlobalTsConfig = JSON.parse(readFileSync(jestGlobalTsConfigPath, { encoding: 'utf8' }))

const tsconfig = loadTypescriptConfig({
tsConfigFile: jestGlobalTsConfigPath
})

expect(tsconfig).toEqual(jestGlobalTsConfig)

unlinkSync(jestGlobalTsConfigPath)
})

it('reads default tsconfig if there is tsconfig.json', () => {
const tsconfigPath = resolve(__dirname, '../tsconfig.json')
const tsconfigCopiedPath = resolve(__dirname, '../.tsconfig.json_cp')
createReadStream(tsconfigPath).pipe(createWriteStream(tsconfigCopiedPath))
const tsconfigOriginal = JSON.parse(readFileSync(tsconfigPath, { encoding: 'utf8' }))
const tsconfig = loadTypescriptConfig()
const tsconfig = loadTypescriptConfig({})
expect(tsconfig).toEqual(tsconfigOriginal)
const tempPath = resolve(__dirname, '../.renamed')
renameSync(tsconfigCopiedPath, tempPath)
const tsconfigCached = loadTypescriptConfig()
const tsconfigCached = loadTypescriptConfig({})
try {
expect(tsconfig).not.toBe(tsconfigCached)
expect(tsconfig).toEqual(tsconfigCached)
Expand Down