-
Notifications
You must be signed in to change notification settings - Fork 336
feat(clerk-js): Introduce legacy browser variant #5495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+178
−77
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b9261a2
feat(clerk-js): Introduce legacy browser variant
dstaley d98a8a7
fix(clerk-js): rename legacybrowserslist to browserslistLegacy
dstaley 82008dc
fix(clerk-js): Drop Safari 12 from clerk.browser.js
dstaley 1b53f83
Merge branch 'main' into ds.feat/core-js
dstaley 44a5bd6
Merge branch 'main' into ds.feat/core-js
dstaley 945c049
fix(clerk-js): Emit chunks with entry name
dstaley 59689d0
fix(clerk-js): Hardcode variant instead of using runtime value
dstaley 29f5359
fix(clerk-js): Emit chunks with the variant at the beginning of filename
dstaley 4a3d4c9
fix(clerk-js): Actually jkjk
dstaley 7f2ddc1
fix(clerk-js): use correct key
dstaley 5c0b06e
Merge branch 'main' into ds.feat/core-js
dstaley 91a3e3e
fix(clerk-js): Default targets to browserslist
dstaley da1e493
fix(clerk-js): Use correct variable
dstaley 82222b0
fix(clerk-js): Always use installed core-js version
dstaley 3d30778
Merge branch 'main' into ds.feat/core-js
dstaley a01916b
Merge branch 'main' into ds.feat/core-js
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 @@ | ||
--- | ||
'@clerk/clerk-js': minor | ||
--- | ||
|
||
Introduce `clerk.legacy.browser.js` for legacy browser support. |
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
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
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ const variants = { | |
clerkBrowser: 'clerk.browser', | ||
clerkHeadless: 'clerk.headless', | ||
clerkHeadlessBrowser: 'clerk.headless.browser', | ||
clerkLegacyBrowser: 'clerk.legacy.browser', | ||
}; | ||
|
||
const variantToSourceFile = { | ||
|
@@ -23,16 +24,18 @@ const variantToSourceFile = { | |
[variants.clerkBrowser]: './src/index.browser.ts', | ||
[variants.clerkHeadless]: './src/index.headless.ts', | ||
[variants.clerkHeadlessBrowser]: './src/index.headless.browser.ts', | ||
[variants.clerkLegacyBrowser]: './src/index.legacy.browser.ts', | ||
}; | ||
|
||
/** | ||
* | ||
* @param {object} config | ||
* @param {'development'|'production'} config.mode | ||
* @param {string} config.variant | ||
* @param {boolean} [config.disableRHC=false] | ||
* @returns { import('@rspack/cli').Configuration } | ||
*/ | ||
const common = ({ mode, disableRHC = false }) => { | ||
const common = ({ mode, variant, disableRHC = false }) => { | ||
return { | ||
mode, | ||
resolve: { | ||
|
@@ -65,7 +68,7 @@ const common = ({ mode, disableRHC = false }) => { | |
}), | ||
].filter(Boolean), | ||
output: { | ||
chunkFilename: `[name]_[fullhash:6]_${packageJSON.version}.js`, | ||
chunkFilename: `[name]_${variant}_[fullhash:6]_${packageJSON.version}.js`, | ||
}, | ||
/** | ||
* Remove the Stripe dependencies from the bundle, if RHC is disabled. | ||
|
@@ -156,8 +159,10 @@ const svgLoader = () => { | |
}; | ||
}; | ||
|
||
/** @type { () => (import('@rspack/core').RuleSetRule[]) } */ | ||
const typescriptLoaderProd = () => { | ||
/** @type { (opts?: { targets?: string, useCoreJs?: boolean }) => (import('@rspack/core').RuleSetRule[]) } */ | ||
const typescriptLoaderProd = ( | ||
{ targets = packageJSON.browserslist, useCoreJs = false } = { targets: packageJSON.browserslist, useCoreJs: false }, | ||
) => { | ||
return [ | ||
{ | ||
test: /\.(jsx?|tsx?)$/, | ||
|
@@ -166,7 +171,13 @@ const typescriptLoaderProd = () => { | |
loader: 'builtin:swc-loader', | ||
options: { | ||
env: { | ||
targets: packageJSON.browserslist, | ||
targets, | ||
...(useCoreJs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
? { | ||
mode: 'usage', | ||
coreJs: require('core-js/package.json').version, | ||
} | ||
: {}), | ||
}, | ||
jsc: { | ||
parser: { | ||
|
@@ -188,12 +199,20 @@ const typescriptLoaderProd = () => { | |
}, | ||
{ | ||
test: /\.m?js$/, | ||
exclude: /node_modules[\\/]core-js/, | ||
use: { | ||
loader: 'builtin:swc-loader', | ||
options: { | ||
env: { | ||
targets: packageJSON.browserslist, | ||
targets, | ||
...(useCoreJs | ||
? { | ||
mode: 'usage', | ||
coreJs: '3.41.0', | ||
} | ||
: {}), | ||
}, | ||
isModule: 'unknown', | ||
}, | ||
}, | ||
}, | ||
|
@@ -231,24 +250,28 @@ const typescriptLoaderDev = () => { | |
|
||
/** | ||
* Used for production builds that have dynamicly loaded chunks. | ||
* @type { () => (import('@rspack/core').Configuration) } | ||
* @type { (opts?: { targets?: string, useCoreJs?: boolean }) => (import('@rspack/core').Configuration) } | ||
* */ | ||
const commonForProdChunked = () => { | ||
const commonForProdChunked = ( | ||
{ targets = packageJSON.browserslist, useCoreJs = false } = { targets: packageJSON.browserslist, useCoreJs: false }, | ||
) => { | ||
return { | ||
module: { | ||
rules: [svgLoader(), ...typescriptLoaderProd()], | ||
rules: [svgLoader(), ...typescriptLoaderProd({ targets, useCoreJs })], | ||
}, | ||
}; | ||
}; | ||
|
||
/** | ||
* Used for production builds that combine all files into one single file (such as for Chrome Extensions). | ||
* @type { () => (import('@rspack/core').Configuration) } | ||
* @type { (opts?: { targets?: string, useCoreJs?: boolean }) => (import('@rspack/core').Configuration) } | ||
* */ | ||
const commonForProdBundled = () => { | ||
const commonForProdBundled = ( | ||
{ targets = packageJSON.browserslist, useCoreJs = false } = { targets: packageJSON.browserslist, useCoreJs: false }, | ||
) => { | ||
return { | ||
module: { | ||
rules: [svgLoader(), ...typescriptLoaderProd()], | ||
rules: [svgLoader(), ...typescriptLoaderProd({ targets, useCoreJs })], | ||
}, | ||
}; | ||
}; | ||
|
@@ -288,6 +311,14 @@ const commonForProd = () => { | |
// minChunkSize: 10000, | ||
// }) | ||
], | ||
resolve: { | ||
alias: { | ||
// SWC will inject imports to `core-js` into the source files that utilize polyfilled functions. Because we | ||
// use pnpm, imports from other packages (like `packages/shared`) will not resolve. This alias forces rspack | ||
// to resolve all `core-js` imports to the version we have installed in `clerk-js`. | ||
'core-js': path.dirname(require.resolve('core-js/package.json')), | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
|
@@ -336,14 +367,21 @@ const prodConfig = ({ mode, env, analysis }) => { | |
], | ||
} | ||
: {}, | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkBrowser }), | ||
commonForProd(), | ||
commonForProdChunked(), | ||
); | ||
|
||
const clerkLegacyBrowser = merge( | ||
entryForVariant(variants.clerkLegacyBrowser), | ||
common({ mode, variant: variants.clerkLegacyBrowser }), | ||
commonForProd(), | ||
commonForProdChunked({ targets: packageJSON.browserslistLegacy, useCoreJs: true }), | ||
); | ||
|
||
const clerkHeadless = merge( | ||
entryForVariant(variants.clerkHeadless), | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkHeadless }), | ||
commonForProd(), | ||
commonForProdChunked(), | ||
// Disable chunking for the headless variant, since it's meant to be used in a non-browser environment and | ||
|
@@ -362,50 +400,62 @@ const prodConfig = ({ mode, env, analysis }) => { | |
|
||
const clerkHeadlessBrowser = merge( | ||
entryForVariant(variants.clerkHeadlessBrowser), | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkHeadlessBrowser }), | ||
commonForProd(), | ||
commonForProdChunked(), | ||
// externalsForHeadless(), | ||
); | ||
|
||
const clerkEsm = merge(entryForVariant(variants.clerk), common({ mode }), commonForProd(), commonForProdBundled(), { | ||
experiments: { | ||
outputModule: true, | ||
}, | ||
output: { | ||
filename: '[name].mjs', | ||
libraryTarget: 'module', | ||
}, | ||
plugins: [ | ||
// Include the lazy chunks in the bundle as well | ||
// so that the final bundle can be imported and bundled again | ||
// by a different bundler, eg the webpack instance used by react-scripts | ||
new rspack.optimize.LimitChunkCountPlugin({ | ||
maxChunks: 1, | ||
}), | ||
], | ||
optimization: { | ||
splitChunks: false, | ||
const clerkEsm = merge( | ||
LekoArts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entryForVariant(variants.clerk), | ||
common({ mode, variant: variants.clerk }), | ||
commonForProd(), | ||
commonForProdBundled(), | ||
{ | ||
experiments: { | ||
outputModule: true, | ||
}, | ||
output: { | ||
filename: '[name].mjs', | ||
libraryTarget: 'module', | ||
}, | ||
plugins: [ | ||
// Include the lazy chunks in the bundle as well | ||
// so that the final bundle can be imported and bundled again | ||
// by a different bundler, eg the webpack instance used by react-scripts | ||
new rspack.optimize.LimitChunkCountPlugin({ | ||
maxChunks: 1, | ||
}), | ||
], | ||
optimization: { | ||
splitChunks: false, | ||
}, | ||
}, | ||
}); | ||
); | ||
|
||
const clerkCjs = merge(entryForVariant(variants.clerk), common({ mode }), commonForProd(), commonForProdBundled(), { | ||
output: { | ||
filename: '[name].js', | ||
libraryTarget: 'commonjs', | ||
}, | ||
plugins: [ | ||
// Include the lazy chunks in the bundle as well | ||
// so that the final bundle can be imported and bundled again | ||
// by a different bundler, eg the webpack instance used by react-scripts | ||
new rspack.optimize.LimitChunkCountPlugin({ | ||
maxChunks: 1, | ||
}), | ||
], | ||
optimization: { | ||
splitChunks: false, | ||
const clerkCjs = merge( | ||
entryForVariant(variants.clerk), | ||
common({ mode, variant: variants.clerk }), | ||
commonForProd(), | ||
commonForProdBundled(), | ||
{ | ||
output: { | ||
filename: '[name].js', | ||
libraryTarget: 'commonjs', | ||
}, | ||
plugins: [ | ||
// Include the lazy chunks in the bundle as well | ||
// so that the final bundle can be imported and bundled again | ||
// by a different bundler, eg the webpack instance used by react-scripts | ||
new rspack.optimize.LimitChunkCountPlugin({ | ||
maxChunks: 1, | ||
}), | ||
], | ||
optimization: { | ||
splitChunks: false, | ||
}, | ||
}, | ||
}); | ||
); | ||
|
||
/** @type { () => (import('@rspack/core').Configuration) } */ | ||
const commonForNoRHC = () => ({ | ||
|
@@ -424,7 +474,7 @@ const prodConfig = ({ mode, env, analysis }) => { | |
|
||
const clerkEsmNoRHC = merge( | ||
entryForVariant(variants.clerkNoRHC), | ||
common({ mode, disableRHC: true }), | ||
common({ mode, disableRHC: true, variant: variants.clerkNoRHC }), | ||
commonForProd(), | ||
commonForProdBundled(), | ||
commonForNoRHC(), | ||
|
@@ -441,7 +491,7 @@ const prodConfig = ({ mode, env, analysis }) => { | |
|
||
const clerkCjsNoRHC = merge( | ||
entryForVariant(variants.clerkNoRHC), | ||
common({ mode, disableRHC: true }), | ||
common({ mode, disableRHC: true, variant: variants.clerkNoRHC }), | ||
commonForProd(), | ||
commonForProdBundled(), | ||
commonForNoRHC(), | ||
|
@@ -458,7 +508,16 @@ const prodConfig = ({ mode, env, analysis }) => { | |
return [clerkBrowser]; | ||
} | ||
|
||
return [clerkBrowser, clerkHeadless, clerkHeadlessBrowser, clerkEsm, clerkEsmNoRHC, clerkCjs, clerkCjsNoRHC]; | ||
return [ | ||
clerkBrowser, | ||
clerkLegacyBrowser, | ||
clerkHeadless, | ||
clerkHeadlessBrowser, | ||
clerkEsm, | ||
clerkEsmNoRHC, | ||
clerkCjs, | ||
clerkCjsNoRHC, | ||
]; | ||
}; | ||
|
||
/** | ||
|
@@ -527,31 +586,31 @@ const devConfig = ({ mode, env }) => { | |
// prettier-ignore | ||
[variants.clerk]: merge( | ||
entryForVariant(variants.clerk), | ||
common({ mode }), | ||
common({ mode, variant: variants.clerk }), | ||
commonForDev(), | ||
), | ||
// prettier-ignore | ||
[variants.clerkBrowser]: merge( | ||
entryForVariant(variants.clerkBrowser), | ||
isSandbox ? { entry: { sandbox: './sandbox/app.ts' } } : {}, | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkBrowser }), | ||
commonForDev(), | ||
), | ||
// prettier-ignore | ||
[variants.clerkBrowserNoRHC]: merge( | ||
entryForVariant(variants.clerkBrowserNoRHC), | ||
common({ mode, disableRHC: true }), | ||
common({ mode, disableRHC: true, variant: variants.clerkBrowserNoRHC }), | ||
commonForDev(), | ||
), | ||
[variants.clerkHeadless]: merge( | ||
entryForVariant(variants.clerkHeadless), | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkHeadless }), | ||
commonForDev(), | ||
// externalsForHeadless(), | ||
), | ||
[variants.clerkHeadlessBrowser]: merge( | ||
entryForVariant(variants.clerkHeadlessBrowser), | ||
common({ mode }), | ||
common({ mode, variant: variants.clerkHeadlessBrowser }), | ||
commonForDev(), | ||
// externalsForHeadless(), | ||
), | ||
|
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
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,38 @@ | ||
// It's crucial this is the first import, | ||
// otherwise chunk loading will not work | ||
// eslint-disable-next-line | ||
import './utils/setWebpackChunkPublicPath'; | ||
|
||
import 'regenerator-runtime/runtime'; | ||
|
||
import { Clerk } from './core/clerk'; | ||
|
||
import { mountComponentRenderer } from './ui/Components'; | ||
|
||
Clerk.mountComponentRenderer = mountComponentRenderer; | ||
|
||
const publishableKey = | ||
document.querySelector('script[data-clerk-publishable-key]')?.getAttribute('data-clerk-publishable-key') || | ||
window.__clerk_publishable_key || | ||
''; | ||
|
||
const proxyUrl = | ||
document.querySelector('script[data-clerk-proxy-url]')?.getAttribute('data-clerk-proxy-url') || | ||
window.__clerk_proxy_url || | ||
''; | ||
|
||
const domain = | ||
document.querySelector('script[data-clerk-domain]')?.getAttribute('data-clerk-domain') || window.__clerk_domain || ''; | ||
|
||
// Ensure that if the script has already been injected we don't overwrite the existing Clerk instance. | ||
if (!window.Clerk) { | ||
window.Clerk = new Clerk(publishableKey, { | ||
proxyUrl, | ||
// @ts-expect-error | ||
domain, | ||
}); | ||
} | ||
|
||
if (module.hot) { | ||
module.hot.accept(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so that we can distinguish which variant a chunk belongs to, mainly so we could (in the future) target specific variant chunks with specific bundlewatch rules.