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

fix: exclude @remix-run/server-runtime from external deps to reduce bundle size #29

Merged
merged 1 commit into from
Jan 13, 2023

Conversation

OnurGvnc
Copy link
Contributor

Hi!
zodix is a great work! thank you very much πŸ™

with the current build configuration, the size of the dist/index.js file was 258kb because the @remix-run/server-runtime and its dependencies were included in the bundle file.

https://unpkg.com/browse/zodix@0.4.0/dist/
image

I have added the @remix-run/server-runtime package as an external package.

The file size has reduced to ~8kB.

❯ du -h dist/index.js 
8.0K    dist/index.js

@xmlking
Copy link

xmlking commented Jan 11, 2023

@remix-run/server-runtime also don't have to be in peer dependencies as it can be used with SvelteKit

image

@OnurGvnc
Copy link
Contributor Author

@remix-run/server-runtime is used in two places: errors.ts and parser.ts.

It is used within errors.ts for the json helper method.

zodix/src/errors.ts

Lines 1 to 15 in f39de6c

import { json } from '@remix-run/server-runtime';
const DEFAULT_ERROR_MESSAGE = 'Bad Request';
const DEFAULT_ERROR_STATUS = 400;
export function createErrorResponse(
options: {
message?: string;
status?: number;
} = {}
): Response {
const statusText = options?.message || DEFAULT_ERROR_MESSAGE;
const status = options?.status || DEFAULT_ERROR_STATUS;
return json(statusText, { status, statusText });
}

image
this dependency can be removed by making the above change.


In parser.ts it is used for type Params = LoaderArgs['params']

import type { LoaderArgs } from '@remix-run/server-runtime';

type Params = LoaderArgs['params'];

LoaderArgs['params'] -> Params comes from the package @remix-run/router. this dependency can also be removed.

// [@remix-run/router] react-router/packages/router/utils.ts#L234
export type Params<Key extends string = string> = {
  readonly [key in Key]: string | undefined;
};

@xmlking
Copy link

xmlking commented Jan 11, 2023

Just a thought , can we make error transformation optional ?
Since zod has builtin support for flatten and other customization? https://github.com/colinhacks/zod/blob/master/ERROR_HANDLING.md
this lib has potential to be used by multiple frameworks

@rileytomasek
Copy link
Owner

@OnurGvnc: thanks for this. It won't impact runtime bundle size, but it should be external because it's required by Remix.

I'm not willing to rewrite part of @remix-run/server-runtime to drop the dependency because it's required by Remix, and this library is for Remix.

@rileytomasek rileytomasek merged commit 515bd9c into rileytomasek:master Jan 13, 2023
@xHomu
Copy link

xHomu commented Jun 2, 2023

Oh wow, thanks for fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants