Skip to content
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

chore(clerk-js): Update types for Rspack #4517

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/afraid-lamps-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
50 changes: 37 additions & 13 deletions packages/clerk-js/rspack.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-check
const rspack = require('@rspack/core');
const packageJSON = require('./package.json');
const path = require('path');
Expand All @@ -22,9 +23,13 @@ const variantToSourceFile = {
[variants.clerkHeadlessBrowser]: './src/index.headless.browser.ts',
};

/** @returns { import('@rspack/cli').Configuration } */
/**
*
* @param {object} config
* @param {'development'|'production'} config.mode
* @returns { import('@rspack/cli').Configuration }
*/
const common = ({ mode }) => {
/** @type { import('webpack').Configuration } */
return {
mode,
resolve: {
Expand Down Expand Up @@ -130,7 +135,7 @@ const svgLoader = () => {
};
};

/** @type { () => (import('@rspack/core').RuleSetRule) } */
/** @type { () => (import('@rspack/core').RuleSetRule[]) } */
const typescriptLoaderProd = () => {
return [
{
Expand Down Expand Up @@ -163,7 +168,7 @@ const typescriptLoaderProd = () => {
];
};

/** @type { () => (import('@rspack/core').RuleSetRule) } */
/** @type { () => (import('@rspack/core').RuleSetRule[]) } */
const typescriptLoaderDev = () => {
return [
{
Expand Down Expand Up @@ -194,7 +199,7 @@ const typescriptLoaderDev = () => {

/**
* Used in outputs that utilize chunking, and returns a URL to the stylesheet.
* @type { () => (import('webpack').RuleSetRule) }
* @type { () => (import('@rspack/core').RuleSetRule) }
*/
const clerkUICSSLoader = () => {
// This emits a module exporting a URL to the styles.css file.
Expand All @@ -206,7 +211,7 @@ const clerkUICSSLoader = () => {

/**
* Used in outputs that _do not_ utilize chunking, and returns the contents of the stylesheet.
* @type { () => (import('webpack').RuleSetRule) }
* @type { () => (import('@rspack/core').RuleSetRule) }
*/
const clerkUICSSSourceLoader = () => {
// This emits a module exporting the contents of the styles.css file.
Expand All @@ -218,7 +223,7 @@ const clerkUICSSSourceLoader = () => {

/**
* Used for production builds that have dynamicly loaded chunks.
* @type { () => (import('webpack').Configuration) }
* @type { () => (import('@rspack/core').Configuration) }
* */
const commonForProdChunked = () => {
return {
Expand All @@ -230,7 +235,7 @@ const commonForProdChunked = () => {

/**
* Used for production builds that combine all files into one single file (such as for Chrome Extensions).
* @type { () => (import('webpack').Configuration) }
* @type { () => (import('@rspack/core').Configuration) }
* */
const commonForProdBundled = () => {
return {
Expand All @@ -240,7 +245,7 @@ const commonForProdBundled = () => {
};
};

/** @type { () => (import('webpack').Configuration) } */
/** @type { () => (import('@rspack/core').Configuration) } */
const commonForProd = () => {
return {
devtool: false,
Expand Down Expand Up @@ -288,11 +293,22 @@ const commonForProd = () => {
// };
// };

/**
*
* @param {string} variant
* @returns {import('@rspack/core').Configuration}
*/
const entryForVariant = variant => {
return { entry: { [variant]: variantToSourceFile[variant] } };
};

/** @type { () => (import('webpack').Configuration)[] } */
/**
*
* @param {object} config
* @param {'development'|'production'} config.mode
* @param {boolean} config.analysis
* @returns {(import('@rspack/core').Configuration)[]}
*/
const prodConfig = ({ mode, analysis }) => {
const clerkBrowser = merge(
entryForVariant(variants.clerkBrowser),
Expand Down Expand Up @@ -369,24 +385,32 @@ const prodConfig = ({ mode, analysis }) => {

// webpack-bundle-analyzer only supports a single build, use clerkBrowser as that's the default build we serve
if (analysis) {
return clerkBrowser;
return [clerkBrowser];
}

return [clerkBrowser, clerkHeadless, clerkHeadlessBrowser, clerkEsm, clerkCjs];
};

/**
*
* @param {object} config
* @param {'development'|'production'} config.mode
* @param {object} config.env
* @returns
*/
const devConfig = ({ mode, env }) => {
const variant = env.variant || variants.clerkBrowser;
// accept an optional devOrigin environment option to change the origin of the dev server.
// By default we use https://js.lclclerk.com which is what our local dev proxy looks for.
const devUrl = new URL(env.devOrigin || 'https://js.lclclerk.com');

/** @type {() => import('@rspack/core').Configuration} */
const commonForDev = () => {
return {
module: {
rules: [svgLoader(), ...typescriptLoaderDev(), clerkUICSSLoader()],
},
plugins: [new ReactRefreshPlugin({ overlay: { sockHost: devUrl.host } })],
plugins: [new ReactRefreshPlugin(/** @type {any} **/ ({ overlay: { sockHost: devUrl.host } }))],
Copy link
Member Author

Choose a reason for hiding this comment

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

This cast is to workaround a type issue that I've opened a PR for. rspack-contrib/rspack-plugin-react-refresh#9

devtool: 'eval-cheap-source-map',
output: {
publicPath: `${devUrl.origin}/npm`,
Expand Down Expand Up @@ -447,5 +471,5 @@ module.exports = env => {
const mode = env.production ? 'production' : 'development';
const analysis = !!env.analysis;

return isProduction(mode) ? prodConfig({ mode, env, analysis }) : devConfig({ mode, env });
return isProduction(mode) ? prodConfig({ mode, analysis }) : devConfig({ mode, env });
alexcarpenter marked this conversation as resolved.
Show resolved Hide resolved
};
Loading