Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
leegeunhyeok committed Oct 24, 2023
1 parent 18870c1 commit 94dd8b0
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ module.exports = {
'no-console': 'off',
},
},
{
files: ['packages/jest/lib/**/*.ts'],
rules: {
'import/no-default-export': 'off',
'import/no-named-as-default': 'off',
},
},
{
files: ['example/**/*'],
rules: {
Expand Down
3 changes: 3 additions & 0 deletions example/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = {
preset: 'react-native',
transform: {
'^.+\\.(tsx?|jsx?)$': '@react-native-esbuild/jest',
},
};
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@react-native-community/cli": "^11.3.6",
"@react-native-community/cli-platform-android": "^11.3.6",
"@react-native-esbuild/cli": "workspace:^",
"@react-native-esbuild/jest": "workspace:^",
"@react-native/assets-registry": "^0.73.0",
"@react-native/codegen": "^0.72.6",
"@react-native/gradle-plugin": "^0.72.11",
Expand Down
8 changes: 4 additions & 4 deletions packages/core/lib/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class ReactNativeEsbuildBundler extends BundlerEventEmitter {
}
}

public static getConfig(): Config {
return getConfigFromGlobal();
}

public static setGlobalLogLevel(logLevel: LogLevel): void {
Logger.setGlobalLogLevel(logLevel);
}
Expand Down Expand Up @@ -443,10 +447,6 @@ export class ReactNativeEsbuildBundler extends BundlerEventEmitter {
return buildTask.handler.task;
}

public getConfig(): Config {
return this.config;
}

public getRoot(): string {
return this.root;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# preset
jest-preset.js
14 changes: 14 additions & 0 deletions packages/jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `@react-native-esbuild/jest`

> react-native preset for jest powered by @react-native-esbuild
## Usage

```js
exports.default = {
preset: 'react-native',
transform: {
'^.+\\.(jsx?|tsx?)$': '@react-native-esbuild/jest',
},
};
```
9 changes: 9 additions & 0 deletions packages/jest/build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const esbuild = require('esbuild');
const { getEsbuildBaseOptions } = require('../../../shared');

const buildOptions = getEsbuildBaseOptions(__dirname);

esbuild.build(buildOptions).catch((error) => {
console.error(error);
process.exit(1);
});
10 changes: 10 additions & 0 deletions packages/jest/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { TransformerFactory } from '@jest/transform';
import { ReactNativeEsbuildJestTransformer } from './transformer';

const factory: TransformerFactory<ReactNativeEsbuildJestTransformer> = {
createTransformer() {
return new ReactNativeEsbuildJestTransformer();
},
};

export default factory;
44 changes: 44 additions & 0 deletions packages/jest/lib/transformer/ReactNativeEsbuildJestTransformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { TransformOptions, TransformedSource } from '@jest/transform';
import { ReactNativeEsbuildBundler } from '@react-native-esbuild/core';
import { SyncTransformPipeline } from '@react-native-esbuild/transformer';
import { getReactNativeInitializeCore } from '@react-native-esbuild/internal';
import type { JestTransformer } from '../types';

const DUMMY_ESBUILD_VALUE = '';

ReactNativeEsbuildBundler.bootstrap();

export class ReactNativeEsbuildJestTransformer implements JestTransformer {
private root = process.cwd();
private transformPipeline: SyncTransformPipeline;
public canInstrument = false;

constructor() {
const { transformer } = ReactNativeEsbuildBundler.getConfig();
this.transformPipeline = new SyncTransformPipeline.builder(this.root, '')
.setInjectScripts([getReactNativeInitializeCore(this.root)])
.setFullyTransformPackages(transformer?.fullyTransformPackageNames ?? [])
.setStripFlowPackages(transformer?.stripFlowPackageNames ?? [])
.setAdditionalBabelTransformRules(
transformer?.additionalTransformRules?.babel ?? [],
)
.setAdditionalSwcTransformRules(
transformer?.additionalTransformRules?.swc ?? [],
)
.build();
}

process(
code: string,
path: string,
_options: TransformOptions,
): TransformedSource {
const transformResult = this.transformPipeline.transform(code, {
path,
pluginData: DUMMY_ESBUILD_VALUE,
namespace: DUMMY_ESBUILD_VALUE,
suffix: DUMMY_ESBUILD_VALUE,
});
return { code: transformResult.code };
}
}
1 change: 1 addition & 0 deletions packages/jest/lib/transformer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ReactNativeEsbuildJestTransformer';
4 changes: 4 additions & 0 deletions packages/jest/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { SyncTransformer } from '@jest/transform';
import type { Config } from '@react-native-esbuild/core';

export type JestTransformer = SyncTransformer<Config>;
57 changes: 57 additions & 0 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@react-native-esbuild/jest",
"version": "0.1.0-beta.9",
"description": "react-native preset for jest powered by @react-native-esbuild",
"keywords": [
"react-native",
"esbuild",
"jest"
],
"author": "leegeunhyeok <[email protected]>",
"homepage": "https://github.com/leegeunhyeok/react-native-esbuild#readme",
"license": "MIT",
"type": "commonjs",
"module": "lib/index.ts",
"main": "dist/index.js",
"types": "dist/lib/index.d.ts",
"exports": {
".": "./dist/index.js",
"./preset": "./jest-preset.js"
},
"directories": {
"lib": "lib",
"test": "__tests__"
},
"files": [
"lib",
"dist",
"jest-preset.js"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/leegeunhyeok/react-native-esbuild.git"
},
"scripts": {
"prepack": "yarn cleanup && yarn build",
"cleanup": "rimraf ./dist",
"build": "node build/index.js && tsc"
},
"bugs": {
"url": "https://github.com/leegeunhyeok/react-native-esbuild/issues"
},
"peerDependencies": {
"react-native": "*"
},
"dependencies": {
"@react-native-esbuild/core": "workspace:*",
"@react-native-esbuild/internal": "workspace:^",
"@react-native-esbuild/transformer": "workspace:*"
},
"devDependencies": {
"@jest/transform": "^29.7.0",
"esbuild": "^0.19.5"
}
}
11 changes: 11 additions & 0 deletions packages/jest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "./dist",
"declaration": true,
"emitDeclarationOnly": true
},
"include": ["./lib"],
"exclude": ["./dist"]
}
17 changes: 16 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4938,7 +4938,7 @@ __metadata:
languageName: unknown
linkType: soft

"@react-native-esbuild/internal@workspace:*, @react-native-esbuild/internal@workspace:packages/internal":
"@react-native-esbuild/internal@workspace:*, @react-native-esbuild/internal@workspace:^, @react-native-esbuild/internal@workspace:packages/internal":
version: 0.0.0-use.local
resolution: "@react-native-esbuild/internal@workspace:packages/internal"
dependencies:
Expand All @@ -4948,6 +4948,20 @@ __metadata:
languageName: unknown
linkType: soft

"@react-native-esbuild/jest@workspace:^, @react-native-esbuild/jest@workspace:packages/jest":
version: 0.0.0-use.local
resolution: "@react-native-esbuild/jest@workspace:packages/jest"
dependencies:
"@jest/transform": ^29.7.0
"@react-native-esbuild/core": "workspace:*"
"@react-native-esbuild/internal": "workspace:^"
"@react-native-esbuild/transformer": "workspace:*"
esbuild: ^0.19.5
peerDependencies:
react-native: "*"
languageName: unknown
linkType: soft

"@react-native-esbuild/plugins@workspace:*, @react-native-esbuild/plugins@workspace:packages/plugins":
version: 0.0.0-use.local
resolution: "@react-native-esbuild/plugins@workspace:packages/plugins"
Expand Down Expand Up @@ -11322,6 +11336,7 @@ __metadata:
"@react-native-community/cli": ^11.3.6
"@react-native-community/cli-platform-android": ^11.3.6
"@react-native-esbuild/cli": "workspace:^"
"@react-native-esbuild/jest": "workspace:^"
"@react-native/assets-registry": ^0.73.0
"@react-native/codegen": ^0.72.6
"@react-native/gradle-plugin": ^0.72.11
Expand Down

1 comment on commit 94dd8b0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🔴 Statements 15.09% 359/2379
🔴 Branches 16.53% 137/829
🔴 Functions 10.49% 69/658
🔴 Lines 14.38% 316/2198

Test suite run success

83 tests passing in 10 suites.

Report generated by 🧪jest coverage report action from 94dd8b0

Please sign in to comment.