Skip to content

Commit

Permalink
feat(exports): Set up ESM first
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseLion committed Jun 26, 2024
1 parent febd077 commit 994fab3
Show file tree
Hide file tree
Showing 9 changed files with 1,299 additions and 55 deletions.
39 changes: 32 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,40 @@
"mocks",
"mock"
],
"main": "./dist/main.js",
"type": "module",
"source": "./src/main.ts",
"main": "./dist/main.cjs",
"module": "./dist/main.js",
"types": "./dist/main.d.ts",
"sideEffects": true,
"exports": {
".": {
"import": "./dist/main.js",
"require": "./dist/main.cjs",
"types": "./dist/main.d.ts",
"default": "./dist/main.js"
},
"./register": {
"import": "./register.js",
"require": "./register.cjs",
"types": "./dist/register.d.ts",
"default": "./register.js"
},
"./package.json": "./package.json"
},
"files": [
"dist/",
"src/",
"register.js"
"./dist",
"./src/",
"./package.json",
"./register.cjs",
"./register.js"
],
"engines": {
"node": ">=18"
},
"scripts": {
"build": "tsc -p tsconfig.prod.json",
"check": "yarn compile && yarn lint && yarn test",
"build": "vite build",
"check": "yarn build && yarn compile && yarn lint && yarn test",
"compile": "tsc",
"lint": "eslint .",
"release": "semantic-release",
Expand All @@ -37,6 +58,8 @@
"@babel/register": "^7.24.6",
"babel-plugin-module-resolver": "^5.0.2",
"dot-prop-immutable": "^2.1.1",
"pino": "^9.2.0",
"pino-pretty": "^11.2.1",
"ts-pattern": "^5.2.0"
},
"devDependencies": {
Expand Down Expand Up @@ -77,7 +100,9 @@
"ts-node": "^10.9.2",
"tslib": "^2.6.3",
"typescript": "^5.5.2",
"typescript-eslint": "^7.14.1"
"typescript-eslint": "^7.14.1",
"vite": "^5.3.1",
"vite-plugin-dts": "^3.9.1"
},
"peerDependencies": {
"@react-native/babel-preset": ">=0.73.18",
Expand Down
13 changes: 13 additions & 0 deletions register.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @ts-check
const pino = require("pino").default;
const pinoPretty = require("pino-pretty").default;

const start = Date.now();
const logger = pino(pinoPretty({ colorize: true }));

require("./dist/register.cjs");

const end = Date.now();
const diff = (end - start) / 1000;

logger.info(`React Native testing mocks registered! (${diff}s)`);
11 changes: 8 additions & 3 deletions register.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// @ts-check
import pino from "pino";
import pinoPretty from "pino-pretty";

const start = Date.now();
require("./dist/register");
const logger = pino(pinoPretty({ colorize: true }));

await import("./dist/register");

const end = Date.now();
const diff = (end - start) / 1000;

// eslint-disable-next-line no-console
console.info(`React Native testing mocks registered! (${diff}s)`);
logger.info(`React Native testing mocks registered! (${diff}s)`);
7 changes: 4 additions & 3 deletions src/lib/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import "@react-native/js-polyfills/error-guard";

global.IS_REACT_ACT_ENVIRONMENT = true;
// Suppress the `react-test-renderer` warnings until New Architecture and legacy
// mode are no longer supported by React Native.
// @ts-expect-error type not defined
global.IS_REACT_NATIVE_TEST_ENVIRONMENT = true;
Object.assign(global, {
IS_REACT_ACT_ENVIRONMENT: true,
IS_REACT_NATIVE_TEST_ENVIRONMENT: true,
});

Object.defineProperties(global, {
__DEV__: {
Expand Down
2 changes: 2 additions & 0 deletions src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ replace("react-native/Libraries/Vibration/Vibration", VibrationMock);
replace("react-native/Libraries/Components/View/View", ViewMock);
replace("react-native/Libraries/Components/View/ViewNativeComponent", ViewNativeComponentMock);
replaceEsm("react-native/Libraries/Animated/Animated", AnimatedMock);

export { };
24 changes: 12 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"esModuleInterop": true,
"importHelpers": true,
"incremental": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["DOM", "ES2023"],
"module": "CommonJS",
"moduleResolution": "Node",
"lib": ["DOM", "ES2022"],
"module": "ES2022",
"moduleResolution": "Bundler",
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
Expand All @@ -20,17 +21,16 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "ES5",
"typeRoots" : [
"./node_modules/@types/",
"./typings/"
],
"target": "ES2022",
"useDefineForClassFields": true,
"useUnknownInCatchVariables": true,
"verbatimModuleSyntax": true
},
"exclude": [
".yarn/*",
"build/*",
"dist/*",
"node_modules/*",
".yarn/**",
"build/**",
"dist/**",
"node_modules/**",
],
"ts-node": {
"transpileOnly": true,
Expand Down
11 changes: 0 additions & 11 deletions tsconfig.prod.json

This file was deleted.

31 changes: 31 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineConfig } from "vite";
import dts from "vite-plugin-dts";

export default defineConfig({
build: {
lib: {
entry: {
main: "./src/main.ts",
register: "./src/register.ts",
},
fileName: (_, entry) => entry,
formats: ["cjs", "es"],
},
rollupOptions: {
output: {
exports: "named",
preserveModules: true,
},
},
ssr: true,
},
plugins: [
dts({
compilerOptions: {
emitDeclarationOnly: true,
incremental: false,
},
include: "src/**",
}),
],
});
Loading

0 comments on commit 994fab3

Please sign in to comment.