Skip to content

Commit

Permalink
fix: make plugins and cloudinaryPluginProps tree-shakeable (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Oct 31, 2024
1 parent 5e9849e commit 163cb2f
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions packages/url-loader/src/lib/cloudinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,7 @@ import type {
} from "./plugin.js";
import { entriesOf, throwError } from "./utils.js";

export const cloudinaryPluginProps = {} as Record<CloudinaryKey, true>;

const validatePlugins = <const plugins extends readonly TransformationPlugin[]>(
...plugins: plugins extends validatePlugins<plugins>
? plugins
: validatePlugins<plugins>
) => {
plugins.forEach((plugin) => {
Object.assign(cloudinaryPluginProps, plugin.props);
});
return plugins;
};

export const transformationPlugins = validatePlugins(
export const transformationPlugins = [
// Some features *must* be the first transformation applied
// thus their plugins *must* come first in the chain

Expand Down Expand Up @@ -96,10 +83,25 @@ export const transformationPlugins = validatePlugins(
UnderlaysPlugin,
VersionPlugin,
ZoompanPlugin
);
] as const;

const getCloudinaryPluginProps = <const plugins extends readonly TransformationPlugin[]>(
plugins: plugins extends validatePlugins<plugins>
? plugins
: validatePlugins<plugins>
): Record<CloudinaryKey, true> => {
const cloudinaryPluginProps = {} as Record<CloudinaryKey, true>;

plugins.forEach((plugin) => {
Object.assign(cloudinaryPluginProps, plugin.props);
});

return cloudinaryPluginProps;
};

export const cloudinaryPluginProps = /* #__PURE__ */ getCloudinaryPluginProps(transformationPlugins);

// important this comes after `validatePlugins` is called so we've collected the props
export const cloudinaryPluginKeys: readonly CloudinaryKey[] = Object.keys(
export const cloudinaryPluginKeys: readonly CloudinaryKey[] = /* #__PURE__ */ Object.keys(
cloudinaryPluginProps
) as never;

Expand Down Expand Up @@ -355,7 +357,7 @@ type validatePlugins<
]
? validatePlugins<
rest,
[
readonly [
...validated,
keyof opts & keyof next["inferOwnOptions"] extends never
? // if the intersection is never, no options duplicate existing so the plugin is valid
Expand Down

0 comments on commit 163cb2f

Please sign in to comment.