Skip to content

Commit 1fc7fc5

Browse files
committed
wip
1 parent 32299f9 commit 1fc7fc5

30 files changed

+998
-347
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export default tseslint.config([
457457
},
458458
{
459459
name: 'repo/jsdoc-internal',
460-
files: ['packages/shared/src/**/internal/**/*.{ts,tsx}'],
460+
files: ['packages/shared/src/**/internal/**/*.{ts,tsx}', 'packages/shared/src/**/*.{ts,tsx}'],
461461
plugins: {
462462
jsdoc: pluginJsDoc,
463463
},

packages/clerk-js/package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
],
3333
"scripts": {
3434
"build": "pnpm build:bundle && pnpm build:declarations",
35-
"postbuild": "node ../../scripts/search-for-rhc.mjs file dist/clerk.no-rhc.mjs",
3635
"build:analyze": "rspack build --config rspack.config.js --env production --env variant=\"clerk.browser\" --env analysis --analyze",
3736
"build:bundle": "pnpm clean && rspack build --config rspack.config.js --env production",
3837
"build:declarations": "tsc -p tsconfig.declarations.json",
@@ -52,6 +51,7 @@
5251
"lint": "eslint src",
5352
"lint:attw": "attw --pack . --profile node16 --ignore-rules named-exports",
5453
"lint:publint": "publint || true",
54+
"postbuild:disabled": "node ../../scripts/search-for-rhc.mjs file dist/clerk.no-rhc.mjs",
5555
"test": "vitest --watch=false",
5656
"test:sandbox:integration": "playwright test",
5757
"test:sandbox:integration:ui": "playwright test --ui",
@@ -91,10 +91,6 @@
9191
"minimatch": "^10.0.3",
9292
"webpack-merge": "^5.10.0"
9393
},
94-
"peerDependencies": {
95-
"react": "catalog:peer-react",
96-
"react-dom": "catalog:peer-react"
97-
},
9894
"engines": {
9995
"node": ">=18.17.0"
10096
},

packages/clerk-js/rspack.config.js

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const { merge } = require('webpack-merge');
66
const ReactRefreshPlugin = require('@rspack/plugin-react-refresh');
77
const { RsdoctorRspackPlugin } = require('@rsdoctor/rspack-plugin');
8+
const { svgLoader, typescriptLoaderProd, typescriptLoaderDev } = require('../../scripts/rspack-common');
89

910
const isProduction = mode => mode === 'production';
1011
const isDevelopment = mode => !isProduction(mode);
@@ -117,20 +118,6 @@ const common = ({ mode, variant, disableRHC = false }) => {
117118
chunks: 'all',
118119
enforce: true,
119120
},
120-
/**
121-
* Sign up is shared between the SignUp component and the SignIn component.
122-
*/
123-
signUp: {
124-
minChunks: 1,
125-
name: 'signup',
126-
test: module => !!(module.resource && module.resource.includes('/ui/components/SignUp')),
127-
},
128-
common: {
129-
minChunks: 1,
130-
name: 'ui-common',
131-
priority: -20,
132-
test: module => !!(module.resource && !module.resource.includes('/ui/components')),
133-
},
134121
defaultVendors: {
135122
minChunks: 1,
136123
test: /[\\/]node_modules[\\/]/,
@@ -152,116 +139,6 @@ const common = ({ mode, variant, disableRHC = false }) => {
152139
};
153140
};
154141

155-
/** @type { () => (import('@rspack/core').RuleSetRule) } */
156-
const svgLoader = () => {
157-
return {
158-
test: /\.svg$/,
159-
resolve: {
160-
fullySpecified: false,
161-
},
162-
use: {
163-
loader: '@svgr/webpack',
164-
options: {
165-
svgo: true,
166-
svgoConfig: {
167-
floatPrecision: 3,
168-
transformPrecision: 1,
169-
plugins: ['preset-default', 'removeDimensions', 'removeStyleElement'],
170-
},
171-
},
172-
},
173-
};
174-
};
175-
176-
/** @type { (opts?: { targets?: string, useCoreJs?: boolean }) => (import('@rspack/core').RuleSetRule[]) } */
177-
const typescriptLoaderProd = (
178-
{ targets = packageJSON.browserslist, useCoreJs = false } = { targets: packageJSON.browserslist, useCoreJs: false },
179-
) => {
180-
return [
181-
{
182-
test: /\.(jsx?|tsx?)$/,
183-
exclude: /node_modules/,
184-
use: {
185-
loader: 'builtin:swc-loader',
186-
options: {
187-
env: {
188-
targets,
189-
...(useCoreJs
190-
? {
191-
mode: 'usage',
192-
coreJs: require('core-js/package.json').version,
193-
}
194-
: {}),
195-
},
196-
jsc: {
197-
parser: {
198-
syntax: 'typescript',
199-
tsx: true,
200-
},
201-
externalHelpers: true,
202-
transform: {
203-
react: {
204-
runtime: 'automatic',
205-
importSource: '@emotion/react',
206-
development: false,
207-
refresh: false,
208-
},
209-
},
210-
},
211-
},
212-
},
213-
},
214-
{
215-
test: /\.m?js$/,
216-
exclude: /node_modules[\\/]core-js/,
217-
use: {
218-
loader: 'builtin:swc-loader',
219-
options: {
220-
env: {
221-
targets,
222-
...(useCoreJs
223-
? {
224-
mode: 'usage',
225-
coreJs: '3.41.0',
226-
}
227-
: {}),
228-
},
229-
isModule: 'unknown',
230-
},
231-
},
232-
},
233-
];
234-
};
235-
236-
/** @type { () => (import('@rspack/core').RuleSetRule[]) } */
237-
const typescriptLoaderDev = () => {
238-
return [
239-
{
240-
test: /\.(jsx?|tsx?)$/,
241-
exclude: /node_modules/,
242-
loader: 'builtin:swc-loader',
243-
options: {
244-
jsc: {
245-
target: 'esnext',
246-
parser: {
247-
syntax: 'typescript',
248-
tsx: true,
249-
},
250-
externalHelpers: true,
251-
transform: {
252-
react: {
253-
runtime: 'automatic',
254-
importSource: '@emotion/react',
255-
development: true,
256-
refresh: true,
257-
},
258-
},
259-
},
260-
},
261-
},
262-
];
263-
};
264-
265142
/**
266143
* Used for production builds that have dynamicly loaded chunks.
267144
* @type { (opts?: { targets?: string, useCoreJs?: boolean }) => (import('@rspack/core').Configuration) }
@@ -613,7 +490,7 @@ const devConfig = ({ mode, env }) => {
613490
cache: true,
614491
experiments: {
615492
cache: {
616-
type: 'persistent',
493+
type: 'memory',
617494
},
618495
},
619496
};

packages/clerk-js/src/core/clerk.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import {
1919
noOrganizationExists,
2020
noUserExists,
2121
} from '@clerk/shared/internal/clerk-js/componentGuards';
22-
import { ERROR_CODES } from '@clerk/shared/internal/clerk-js/constants';
22+
import {
23+
CLERK_SATELLITE_URL,
24+
CLERK_SUFFIXED_COOKIES,
25+
CLERK_SYNCED,
26+
ERROR_CODES,
27+
} from '@clerk/shared/internal/clerk-js/constants';
2328
import { RedirectUrls } from '@clerk/shared/internal/clerk-js/redirectUrls';
2429
import {
2530
getTaskEndpoint,
@@ -113,7 +118,25 @@ import type {
113118
} from '@clerk/shared/types';
114119
import { addClerkPrefix, isAbsoluteUrl, stripScheme } from '@clerk/shared/url';
115120
import { allSettled, handleValueOrFn, noop } from '@clerk/shared/utils';
116-
import type { MountComponentRenderer } from '@clerk/ui/internal';
121+
122+
// TODO: Replace with proper type from @clerk/ui
123+
type MountComponentRenderer = (
124+
clerk: any,
125+
environment: any,
126+
options: any,
127+
) => {
128+
ensureMounted: (options?: { preloadHint?: string }) => Promise<{
129+
mountImpersonationFab: () => void;
130+
updateProps: (props: any) => void;
131+
openModal: (name: string, props: any) => void;
132+
closeModal: (name: string) => void;
133+
openDrawer: (name: string, props: any) => void;
134+
closeDrawer: (name: string) => void;
135+
mountComponent: (config: any) => void;
136+
unmountComponent: (config: any) => void;
137+
prefetch: (name: string) => void;
138+
}>;
139+
};
117140

118141
import { debugLogger, initDebugLogger } from '@/utils/debug';
119142

packages/clerk-js/src/global.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,14 @@ declare const __BUILD_VARIANT_CHIPS__: boolean;
1717
interface Window {
1818
__unstable__onBeforeSetActive: (intent?: 'sign-out') => Promise<void> | void;
1919
__unstable__onAfterSetActive: () => Promise<void> | void;
20+
21+
/**
22+
* TODO @nikos: implment type
23+
* UI components loaded from @clerk/ui browser bundle.
24+
* This is injected by the @clerk/ui package and consumed by clerk-js.
25+
*/
26+
__unstable_ClerkUi?: {
27+
mountComponentRenderer: any;
28+
version: string;
29+
};
2030
}

packages/clerk-js/src/index.browser.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// It's crucial this is the first import,
22
// otherwise chunk loading will not work
3-
// eslint-disable-next-line
3+
44
import './utils/setWebpackChunkPublicPath';
55

66
import { Clerk } from './core/clerk';
77

8-
import { mountComponentRenderer } from './ui/Components';
9-
10-
Clerk.mountComponentRenderer = mountComponentRenderer;
8+
// Load UI components from window (injected by @clerk/ui browser bundle)
9+
if (window.__unstable_ClerkUi) {
10+
Clerk.mountComponentRenderer = window.__unstable_ClerkUi.mountComponentRenderer;
11+
}
1112

1213
const publishableKey =
1314
document.querySelector('script[data-clerk-publishable-key]')?.getAttribute('data-clerk-publishable-key') ||
@@ -26,7 +27,7 @@ const domain =
2627
if (!window.Clerk) {
2728
window.Clerk = new Clerk(publishableKey, {
2829
proxyUrl,
29-
// @ts-expect-error
30+
// @ts-expect-error - domain is not typed
3031
domain,
3132
});
3233
}

packages/clerk-js/src/index.legacy.browser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import 'regenerator-runtime/runtime';
77

88
import { Clerk } from './core/clerk';
99

10-
import { mountComponentRenderer } from './ui/Components';
11-
12-
Clerk.mountComponentRenderer = mountComponentRenderer;
10+
// Load UI components from window (injected by @clerk/ui browser bundle)
11+
if (window.__unstable_ClerkUi) {
12+
Clerk.mountComponentRenderer = window.__unstable_ClerkUi.mountComponentRenderer;
13+
}
1314

1415
const publishableKey =
1516
document.querySelector('script[data-clerk-publishable-key]')?.getAttribute('data-clerk-publishable-key') ||

packages/clerk-js/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'regenerator-runtime/runtime';
22

33
import { Clerk } from './core/clerk';
4-
import { mountComponentRenderer } from './ui/Components';
54

65
export {
76
ClerkAPIResponseError,
@@ -19,8 +18,6 @@ export {
1918
} from '@clerk/shared/error';
2019
export { Clerk };
2120

22-
Clerk.mountComponentRenderer = mountComponentRenderer;
23-
2421
if (module.hot) {
2522
module.hot.accept();
2623
}

packages/react/src/isomorphicClerk.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inBrowser } from '@clerk/shared/browser';
22
import { clerkEvents, createClerkEventBus } from '@clerk/shared/clerkEventBus';
3-
import { loadClerkJsScript } from '@clerk/shared/loadClerkJsScript';
3+
import { loadClerkJsScript, loadClerkUiScript } from '@clerk/shared/loadClerkJsScript';
44
import type {
55
__internal_CheckoutProps,
66
__internal_OAuthConsentProps,
@@ -81,6 +81,10 @@ const SDK_METADATA = {
8181

8282
export interface Global {
8383
Clerk?: HeadlessBrowserClerk | BrowserClerk;
84+
__unstable_ClerkUi?: {
85+
mountComponentRenderer: any;
86+
version: string;
87+
};
8488
}
8589

8690
declare const global: Global;
@@ -429,6 +433,8 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
429433
}
430434

431435
async loadClerkJS(): Promise<HeadlessBrowserClerk | BrowserClerk | undefined> {
436+
console.log('local clerk react');
437+
432438
if (this.mode !== 'browser' || this.loaded) {
433439
return;
434440
}
@@ -475,20 +481,26 @@ export class IsomorphicClerk implements IsomorphicLoadedClerk {
475481
global.Clerk = c;
476482
} else if (!__BUILD_DISABLE_RHC__) {
477483
// Hot-load latest ClerkJS from Clerk CDN
478-
if (!global.Clerk) {
479-
await loadClerkJsScript({
480-
...this.options,
481-
publishableKey: this.#publishableKey,
482-
proxyUrl: this.proxyUrl,
483-
domain: this.domain,
484-
nonce: this.options.nonce,
485-
});
486-
}
484+
const scriptLoadingOptions = {
485+
...this.options,
486+
publishableKey: this.#publishableKey,
487+
proxyUrl: this.proxyUrl,
488+
domain: this.domain,
489+
nonce: this.options.nonce,
490+
};
491+
492+
await Promise.all([
493+
global.Clerk ? Promise.resolve() : loadClerkJsScript(scriptLoadingOptions),
494+
global.__unstable_ClerkUi ? Promise.resolve() : loadClerkUiScript(scriptLoadingOptions),
495+
]);
487496

488497
if (!global.Clerk) {
498+
// TODO @nikos: somehow throw if clerk ui failed to load but it was not headless
489499
throw new Error('Failed to download latest ClerkJS. Contact [email protected].');
490500
}
491501

502+
console.log('all loaded');
503+
492504
this.beforeLoad(global.Clerk);
493505
await global.Clerk.load(this.options);
494506
}

packages/shared/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ declare const PACKAGE_NAME: string;
22
declare const PACKAGE_VERSION: string;
33
declare const JS_PACKAGE_VERSION: string;
44
declare const __DEV__: boolean;
5+
declare const __BUILD_DISABLE_RHC__: boolean;
56

67
interface ImportMetaEnv {
78
readonly [key: string]: string;

0 commit comments

Comments
 (0)