-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
3,909 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}, | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const code = `import * as fs from 'fs'` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
Oops, something went wrong.