Skip to content

Commit

Permalink
init project for esbuild-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
aelbore committed May 24, 2020
1 parent e9675d0 commit b36ce34
Show file tree
Hide file tree
Showing 12 changed files with 3,909 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# esbuild-jest
# esbuild-jest

[![npm](https://img.shields.io/npm/v/esbuild-jest.svg)](https://www.npmjs.com/package/esbuild-jest)

## Install

```bash
npm install --save-dev
```

## Setup

You have to define `swc-jest` as a transformer for your JavaScript code, map _.js_ files to the `swc-jest` module.

```json
"transform": {
"^.+\\.jsx?$": "swc-jest"
},
```
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'esbuild-jest'
},
testRegex: '(/__test__/.*|(\\.|/)(test|spec))\\.tsx?$',
testPathIgnorePatterns: ['/node_modules/', '/dist/', '/types/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
}
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "esbuild-jest",
"version": "0.1.0",
"description": "Jest plugin to use esbuild for transformation",
"main": "esbuild-jest.js",
"module": "esbuild-jest.es.js",
"typings": "esbuild-jest.d.ts",
"scripts": {
"build": "rollup -c",
"test": "jest --clearCache && jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aelbore/esbuild-jest.git"
},
"keywords": [
"esbuild",
"jest",
"test"
],
"author": "Arjay Elbore",
"license": "ISC",
"bugs": {
"url": "https://github.com/aelbore/esbuild-jest/issues"
},
"homepage": "https://github.com/aelbore/esbuild-jest#readme",
"devDependencies": {
"@types/jest": "^25.2.3",
"@types/mock-fs": "^4.10.0",
"@types/node": "^14.0.5",
"aria-fs": "^0.3.0",
"jest": "^26.0.1",
"mock-fs": "^4.12.0",
"rollup-plugin-dts": "^1.4.7",
"ts-jest": "^26.0.0",
"tslib": "^2.0.0",
"typescript": "^3.9.3"
},
"dependencies": {
"rollup": "^2.10.7",
"sync-rpc": "^1.3.6",
"rollup-plugin-esbuild": "^1.4.1"
}
}
39 changes: 39 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dts from 'rollup-plugin-dts'
import esbuild from 'rollup-plugin-esbuild'
import { plugins, external } from './tools/plugins'

export default [
{
input: './src/index.ts',
external,
plugins: [ esbuild({ target: 'es2019' }), ...plugins ],
output: [
{
file: './dist/esbuild-jest.js',
format: 'cjs'
},
{
file: './dist/esbuild-jest.es.js',
format: 'es'
}
]
},
{
input: './src/esbuild.ts',
external,
plugins: [ esbuild({ target: 'es2019' }) ],
output: {
file: './dist/esbuild.js',
format: 'cjs'
}
},
{
input: './src/index.ts',
external,
plugins: [ dts() ],
output: {
format: 'es',
file: './dist/esbuild-jest.d.ts',
}
}
]
25 changes: 25 additions & 0 deletions src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { rollup, OutputChunk, RollupWarning, WarningHandler } from 'rollup'
import esbuild, { Options } from 'rollup-plugin-esbuild'

export default () => {
return async (filename: string, options?: Options) => {
const bundle = await rollup({
input: filename,
plugins: [
esbuild({
target: 'es2019',
jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
...(options ?? {})
})
],
onwarn(warning: RollupWarning, defaultHandler: WarningHandler) {
return
}
})
const { output } = await bundle.generate({ sourcemap: true, format: 'cjs' })
const { code, map } = output.shift() as OutputChunk

return { code, map: (map ?? '').toString() }
}
}
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import forceSync from 'sync-rpc'
import { join, resolve } from 'path'

const transform = forceSync(join(__dirname, 'esbuild'))

export default {
process (content: string, filename: string) {
const filePath = filename.replace(resolve(), '.')
return transform(filePath)
}
}
1 change: 1 addition & 0 deletions test/code.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const code = `import * as fs from 'fs'`
16 changes: 16 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mockfs from 'mock-fs'
import * as fs from 'fs'
import { code } from './code'

afterEach(() => {
mockfs.restore()
})

test(`Basic example`, async () => {
mockfs({
'./src/input.ts': code
})

const content = await fs.promises.readFile('./src/input.ts', 'utf-8')
expect(content).toEqual(code)
});
30 changes: 30 additions & 0 deletions tools/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as fs from 'fs'
import { symlinkDir } from 'aria-fs'

function copy() {
return {
name: 'copy',
buildEnd: async () => {
await fs.promises.mkdir('dist', { recursive: true })
const pkg = require('./package.json')
delete pkg.scripts
delete pkg.devDependencies
await Promise.all([
fs.promises.writeFile('./dist/package.json', JSON.stringify(pkg, null, 2)),
fs.promises.copyFile('./README.md', './dist/README.md')
])
}
}
}

function link() {
return {
name: 'link',
buildEnd: async () => {
await symlinkDir('./dist', './node_modules/esbuild-jest')
}
}
}

export const plugins = [ copy(), link() ]
export const external = [ 'sync-rpc', 'path', 'rollup', 'rollup-plugin-esbuild' ]
18 changes: 18 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"outDir": "dist",
"moduleResolution": "node",
"resolveJsonModule": true,
"importHelpers": true,
"esModuleInterop": true,
"lib" :[
"dom",
"es2015",
"es2017",
"es2018",
"es2019"
]
}
}
Loading

0 comments on commit b36ce34

Please sign in to comment.