Skip to content

Commit 0ddb799

Browse files
committed
feat(clerk-react): Add ESM/CJS build pipeline
1 parent 8ef9312 commit 0ddb799

15 files changed

+132
-61
lines changed

packages/react/package.cjs.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sideEffects": ["./index.js", "./polyfills.js"],
3+
"type": "commonjs"
4+
}

packages/react/package.esm.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"sideEffects": ["./index.js", "./polyfills.js"],
3+
"type": "module"
4+
}

packages/react/package.json

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "4.18.0",
44
"license": "MIT",
55
"description": "Clerk React library",
6-
"sideEffects": false,
76
"keywords": [
87
"clerk",
98
"react",
@@ -14,15 +13,25 @@
1413
"jwt"
1514
],
1615
"author": "Clerk",
17-
"source": "src/index.ts",
18-
"main": "dist/index.js",
19-
"typings": "dist/index.d.ts",
16+
"main": "./dist/cjs/index.js",
17+
"module": "./dist/esm/index.js",
18+
"types": "./dist/types/index.d.ts",
19+
"exports": {
20+
".": {
21+
"import": "./dist/esm/index.js",
22+
"require": "./dist/cjs/index.js",
23+
"types": "./dist/types/index.d.ts"
24+
}
25+
},
2026
"files": [
2127
"dist"
2228
],
2329
"scripts": {
24-
"build": "node ./scripts/info.js && tsc -p tsconfig.build.json",
25-
"dev": "node ./scripts/info.js && tsc -p tsconfig.build.json --watch",
30+
"build": "tsup",
31+
"dev": "tsup --watch",
32+
"dev:publish": "npm run dev -- --env.PUBLISH=true",
33+
"build:declarations": "tsc -p tsconfig.declarations.json",
34+
"publish:local": "npx yalc push --replace --sig",
2635
"clean": "rimraf ./dist",
2736
"lint": "eslint .",
2837
"prepublishOnly": "npm run build",

packages/react/scripts/info.js

-12
This file was deleted.

packages/react/src/contexts/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { ClerkProvider, ClerkProviderProps, __internal__setErrorThrowerOptions } from './ClerkProvider';
1+
export { ClerkProvider, __internal__setErrorThrowerOptions } from './ClerkProvider';
2+
export type { ClerkProviderProps } from './ClerkProvider';

packages/react/src/globals.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {};
2+
3+
declare global {
4+
const PACKAGE_NAME: string;
5+
const PACKAGE_VERSION: string;
6+
const __DEV__: boolean;
7+
}

packages/react/src/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './polyfills';
2+
13
export * from './contexts';
24
export * from './components';
35
export * from './hooks';
@@ -12,13 +14,3 @@ export type {
1214
} from './types';
1315
export { isMagicLinkError, MagicLinkErrorCode } from './errors';
1416
export { useMagicLink } from './hooks/useMagicLink';
15-
16-
/**
17-
* Vite does not define `global` by default
18-
* One workaround is to use the `define` config prop
19-
* https://vitejs.dev/config/#define
20-
* We are solving this in the SDK level to reduce setup steps.
21-
*/
22-
if (typeof global === 'undefined' && typeof window !== 'undefined' && !window.global) {
23-
(window as any).global = window;
24-
}

packages/react/src/info.ts

-3
This file was deleted.

packages/react/src/polyfills.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Vite does not define `global` by default
3+
* One workaround is to use the `define` config prop
4+
* https://vitejs.dev/config/#define
5+
* We are solving this in the SDK level to reduce setup steps.
6+
*/
7+
if (typeof global === 'undefined' && typeof window !== 'undefined' && !window.global) {
8+
console.log('nikos2?');
9+
(window as any).global = window;
10+
}
11+
12+
export {};

packages/react/src/utils/loadClerkJsScript.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { addClerkPrefix, isValidProxyUrl, loadScript, parsePublishableKey, proxyUrlToAbsoluteURL } from '@clerk/shared';
22

3-
import { LIB_VERSION } from '../info';
43
import type { IsomorphicClerkOptions } from '../types';
54
import { errorThrower } from './errorThrower';
65
import { isDevOrStagingUrl } from './isDevOrStageUrl';
@@ -45,7 +44,7 @@ const clerkJsScriptUrl = (opts: LoadClerkJsScriptOptions) => {
4544
}
4645

4746
const variant = clerkJSVariant ? `${clerkJSVariant.replace(/\.+$/, '')}.` : '';
48-
const version = clerkJSVersion || getPrereleaseTag(LIB_VERSION) || getMajorVersion(LIB_VERSION);
47+
const version = clerkJSVersion || getPrereleaseTag(PACKAGE_VERSION) || getMajorVersion(PACKAGE_VERSION);
4948
return `https://${scriptHost}/npm/@clerk/clerk-js@${version}/dist/clerk.${variant}browser.js`;
5049
};
5150

packages/react/tsconfig.build.json

-24
This file was deleted.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"skipLibCheck": true,
5+
"noEmit": false,
6+
"declaration": true,
7+
"emitDeclarationOnly": true,
8+
"declarationMap": true,
9+
"sourceMap": false,
10+
"declarationDir": "./dist/types"
11+
}
12+
}

packages/react/tsconfig.json

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
{
2-
"extends": "./tsconfig.build.json",
32
"compilerOptions": {
4-
"incremental": true
5-
}
3+
"baseUrl": ".",
4+
"declaration": true,
5+
"declarationMap": false,
6+
"esModuleInterop": true,
7+
"importHelpers": true,
8+
"isolatedModules": true,
9+
"jsx": "react",
10+
"lib": ["es6", "dom"],
11+
"moduleResolution": "node",
12+
"noImplicitReturns": true,
13+
"noUnusedLocals": false,
14+
"noUnusedParameters": true,
15+
"outDir": "dist",
16+
"resolveJsonModule": true,
17+
"skipLibCheck": true,
18+
"sourceMap": false,
19+
"strict": true,
20+
"target": "ES2020"
21+
},
22+
"include": ["src"],
23+
"exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"]
624
}

packages/react/tsup.config.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { Options } from 'tsup';
2+
import { defineConfig } from 'tsup';
3+
4+
import { runAfterLast } from '../../scripts/utils';
5+
// @ts-ignore
6+
import { name, version } from './package.json';
7+
8+
export default defineConfig(overrideOptions => {
9+
const isWatch = !!overrideOptions.watch;
10+
const shouldPublish = overrideOptions.env?.PUBLISH === 'true';
11+
12+
const common: Options = {
13+
entry: ['./src/**/*.{ts,tsx,js,jsx}'],
14+
bundle: false,
15+
clean: true,
16+
minify: false,
17+
sourcemap: true,
18+
legacyOutput: true,
19+
define: {
20+
PACKAGE_NAME: `"${name}"`,
21+
PACKAGE_VERSION: `"${version}"`,
22+
__DEV__: `${!isWatch}`,
23+
},
24+
};
25+
26+
const onSuccess = (format: 'esm' | 'cjs') => {
27+
return `cp ./package.${format}.json ./dist/${format}/package.json`;
28+
};
29+
30+
const esm: Options = {
31+
...common,
32+
format: 'esm',
33+
onSuccess: onSuccess('esm'),
34+
};
35+
36+
const cjs: Options = {
37+
...common,
38+
format: 'cjs',
39+
outDir: './dist/cjs',
40+
onSuccess: onSuccess('cjs'),
41+
};
42+
43+
return runAfterLast(['npm run build:declarations', shouldPublish && 'npm run publish:local'])(esm, cjs);
44+
});

scripts/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Options } from 'tsup';
2+
3+
export const runAfterLast =
4+
(commands: Array<string | false>) =>
5+
(...configs: Options[]) => {
6+
const [last, ...rest] = configs.reverse();
7+
return [...rest.reverse(), { ...last, onSuccess: [last.onSuccess, ...commands].filter(Boolean).join(' && ') }];
8+
};

0 commit comments

Comments
 (0)