Skip to content

Commit

Permalink
refactor ESBuild config generation into new getEsbuildConfig helper
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinsharp committed Sep 2, 2021
1 parent daa5847 commit 320216a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
28 changes: 5 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { extname } from 'path'

import { Config } from '@jest/types'
import { TransformOptions as JestTransformOptions, Transformer } from '@jest/transform'
import { Format, Loader, TransformOptions, transformSync } from 'esbuild'
import { transformSync } from 'esbuild'

import { Options } from './options'
import { getExt, loaders } from './utils'
import { getEsbuildConfig } from './utils'

const createTransformer = (options?: Options) => ({
process(content: string,
Expand All @@ -14,16 +12,6 @@ const createTransformer = (options?: Options) => ({
opts?: JestTransformOptions
) {
const sources = { code: content }
const ext = getExt(filename), extName = extname(filename).slice(1)

const enableSourcemaps = options?.sourcemap || false
const loader = (options?.loaders && options?.loaders[ext]
? options.loaders[ext]
: loaders.includes(extName) ? extName: 'text'
) as Loader
const sourcemaps: Partial<TransformOptions> = enableSourcemaps
? { sourcemap: true, sourcesContent: false, sourcefile: filename }
: {}

/// this logic or code from
/// https://github.com/threepointone/esjest-transform/blob/main/src/index.js
Expand All @@ -40,17 +28,11 @@ const createTransformer = (options?: Options) => ({
sources.code = source
}

const result = transformSync(sources.code, {
loader,
format: options?.format as Format || 'cjs',
target: options?.target || 'es2018',
...(options?.jsxFactory ? { jsxFactory: options.jsxFactory }: {}),
...(options?.jsxFragment ? { jsxFragment: options.jsxFragment }: {}),
...sourcemaps
})
const esbuildConfig = getEsbuildConfig(options, filename)
const result = transformSync(sources.code, esbuildConfig)

let { map, code } = result;
if (enableSourcemaps) {
if (esbuildConfig.sourcemap) {
map = {
...JSON.parse(result.map),
sourcesContent: null,
Expand Down
31 changes: 28 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path'
import { Format, Loader, TransformOptions } from 'esbuild'

export const loaders = ["js", "jsx", "ts", "tsx", "json"]
const loaders = ['js', 'jsx', 'ts', 'tsx', 'json']

export const getExt = (str: string) => {
const getExt = (str: string) => {
const basename = path.basename(str);
const firstDot = basename.indexOf('.');
const lastDot = basename.lastIndexOf('.');
Expand All @@ -11,4 +12,28 @@ export const getExt = (str: string) => {
if (firstDot === lastDot) return extname

return basename.slice(firstDot, lastDot) + extname
}
}

export const getEsbuildConfig = (options, filename) => {
const ext = getExt(filename),
extName = path.extname(filename).slice(1)
const loader = (options?.loaders && options?.loaders[ext]
? options.loaders[ext]
: loaders.includes(extName)
? extName
: 'text') as Loader

const enableSourcemaps = options?.sourcemap || false
const sourcemaps: Partial<TransformOptions> = enableSourcemaps
? { sourcemap: true, sourcesContent: false, sourcefile: filename }
: {}

return {
loader,
format: (options?.format as Format) || 'cjs',
target: options?.target || 'es2018',
...(options?.jsxFactory ? { jsxFactory: options.jsxFactory } : {}),
...(options?.jsxFragment ? { jsxFragment: options.jsxFragment } : {}),
...sourcemaps,
}
}
44 changes: 44 additions & 0 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { getEsbuildConfig } from '../src/utils';

describe('getEsbuildConfig', () => {
test('with sourcemaps returns correct config', () => {
const config = getEsbuildConfig({ sourcemap: true }, 'filename.js')

expect(config).toEqual({
format: 'cjs',
loader: 'js',
target: 'es2018',
sourcefile: 'filename.js',
sourcemap: true,
sourcesContent: false,
})
})

describe('with loaders', () => {
test('.js loader returns correct config', () => {
const config = getEsbuildConfig(
{ loaders: { '.js': 'jsx' } },
'filename.js'
)

expect(config).toEqual({
format: 'cjs',
loader: 'jsx',
target: 'es2018',
});
});

xtest('.js loader returns correct config with compound extension', () => {
const config = getEsbuildConfig(
{ loaders: { '.js': 'jsx' } },
'filename.test.js'
)

expect(config).toEqual({
format: 'cjs',
loader: 'jsx',
target: 'es2018',
});
});
})
})

0 comments on commit 320216a

Please sign in to comment.