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

feat: Migrate to ESM and use tsup for building #1067

Merged
merged 6 commits into from
Jun 5, 2024

Conversation

ryoppippi
Copy link
Contributor

@ryoppippi ryoppippi commented Jun 3, 2024

What's changed

This commit includes several changes:

  • The package.json file has been updated to support ESM with the "type"
    field set to "module" and This commit includes several changes:
  • The package.json file has been updated to support ESM with the "type"
    field set to "module" and the "exports" field properly configured.
  • The build script in package.json has been changed to use tsup instead
    of tsc.
  • tsup has been added as a devDependency.
  • The tsconfig.json file has been simplified and updated for the new
    build process.
  • A new tsup.config.js file has been added to configure the tsup build.

feat: Update build process and dependencies

This commit updates the build process by replacing tsc --watch with
tsup --watch in the dev script. It also removes the tsp section
from package.json and deletes tsup.config.js.

A new tsup.config.ts file is added which uses fast-glob and
node:fs/promises to handle TypeScript compilation and file renaming.
This is due to an issue with tsup's dts causing out of memory errors.

Additionally, @microsoft/api-extractor and fast-glob are added as
new devDependencies.Before submitting a Pull Request, please test your code.

Result

Here is the differnece of the bundle size

source code

import typia, {type tags} from 'typia'
interface Author {
  name: string;
  age:
    & number
    & tags.Type<"uint32">
    & tags.Minimum<20>
    & tags.ExclusiveMaximum<100>;
}
const validate = typia.createValidate<Author>();
console.log(validate({name: "John", age: 30}));

Before
https://github.com/ryoppippi/typia-bundle-issue-repo/blob/8eb3cb8ec8dd8f8e9234aa2923cf352a73852c01/dist/index.js

~/p/typia-bundle main ❱ git id
8eb3cb8ec8dd8f8e9234aa2923cf352a73852c01
~/p/typia-bundle main ❱ ls -hlA dist_minify/ dist
dist_minify/:
Permissions Size User      Date Modified Name
.rw-r--r--   70k ryoppippi  3 Jun 17:28  index.js

dist:
Permissions Size User      Date Modified Name
.rw-r--r--  135k ryoppippi  3 Jun 17:28  index.js

After
https://github.com/ryoppippi/typia-bundle-issue-repo/blob/fb7bd4073cecafec84fa2e5f18656371e6df7cea/dist/index.js

~/p/typia-bundle feature/bundle-esm ❱ git id
fb7bd4073cecafec84fa2e5f18656371e6df7cea
~/p/typia-bundle feature/bundle-esm ❱ ls -hlA dist_minify/ dist
dist_minify/:
Permissions Size User      Date Modified Name
.rw-r--r--   45k ryoppippi  3 Jun 17:32  index.js

dist:
Permissions Size User      Date Modified Name
.rw-r--r--   87k ryoppippi  3 Jun 17:32  index.js

======================
If you created a new feature, then create the unit test function, too.

# COMPILE
npm run build

# RE-WRITE TEST PROGRAMS IF REQUIRED
npm run test:template

# BUILD TEST PROGRAM
npm run build:test

# DO TEST
npm run test

Learn more about the CONTRIBUTINGthe "exports" field properly configured.

  • The build script in package.json has been changed to use tsup instead
    of tsc.
  • tsup has been added as a devDependency.
  • The tsconfig.json file has been simplified and updated for the new
    build process.
  • A new tsup.config.js file has been added to configure the tsup build.

feat: Update build process and dependencies

This commit updates the build process by replacing tsc --watch with
tsup --watch in the dev script. It also removes the tsp section
from package.json and deletes tsup.config.js.

A new tsup.config.ts file is added which uses fast-glob and
node:fs/promises to handle TypeScript compilation and file renaming.
This is due to an issue with tsup's dts causing out of memory errors.

Additionally, @microsoft/api-extractor and fast-glob are added as
new devDependencies.Before submitting a Pull Request, please test your code.

If you created a new feature, then create the unit test function, too.

# COMPILE
npm run build

# RE-WRITE TEST PROGRAMS IF REQUIRED
npm run test:template

# BUILD TEST PROGRAM
npm run build:test

# DO TEST
npm run test

Learn more about the CONTRIBUTING

This commit includes several changes:
- The package.json file has been updated to support ESM with the "type"
  field set to "module" and the "exports" field properly configured.
- The build script in package.json has been changed to use tsup instead
  of tsc.
- tsup has been added as a devDependency.
- The tsconfig.json file has been simplified and updated for the new
  build process.
- A new tsup.config.js file has been added to configure the tsup build.

feat: Update build process and dependencies

This commit updates the build process by replacing `tsc --watch` with
`tsup --watch` in the `dev` script. It also removes the `tsp` section
from `package.json` and deletes `tsup.config.js`.

A new `tsup.config.ts` file is added which uses `fast-glob` and
`node:fs/promises` to handle TypeScript compilation and file renaming.
This is due to an issue with tsup's dts causing out of memory errors.

Additionally, `@microsoft/api-extractor` and `fast-glob` are added as
new devDependencies.
This commit fixes the dynamic imports in the typia.ts file by adding
the .js extension. This ensures the correct files are imported when
the script is run.
This commit replaces the usage of ts-node with tsx in the scripts
section of both benchmark and test package.json files. The version of
tsx used is 4.11.2. This change is expected to enhance the TypeScript
execution environment.
The prepare script in package.json has been updated to include the
'typia patch' command. This change ensures that the typia patch is
applied during the preparation phase of the project setup.

feat: add typia patch to prepare script

This commit modifies the 'prepare' script in the package.json file of
the benchmark directory. The 'ts-patch install' command is now followed
by 'typia patch'. This change ensures that typia patch is run during
the preparation phase.
@ryoppippi
Copy link
Contributor Author

After that, I'm planning to create monorepo

@ryoppippi
Copy link
Contributor Author

This PR does not resolve tree-shaking fully, but the bundle size is decreasing

@samchon samchon self-requested a review June 4, 2024 13:26
@samchon samchon added enhancement New feature or request good first issue Good for newcomers invalid This doesn't seem right wontfix This will not be worked on labels Jun 4, 2024
Copy link
Owner

@samchon samchon left a comment

Choose a reason for hiding this comment

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

microsoft/TypeScript#54276

Unfortunately, the transformer library like typia must be built with CJS, when considering the standard transform function that will be provided by the Microsoft/TypeScript.

Therefore, I'll try to change your commit, that can work properly with bundling, and also accomplish the bundling size diminishing. Thanks for considerations.

@ryoppippi
Copy link
Contributor Author

ryoppippi commented Jun 4, 2024

@samchon
Thank you for reviewing!!
I think this build script can also generate CJS script for tsconfig transform.
Did you try to build it?

@samchon
Copy link
Owner

samchon commented Jun 4, 2024

Will commit to your repo soon. Wait for maybe five to ten minutes please.

Currently, I'm running test program, and it is working well even in the ESM.

@samchon samchon added this to In progress in v6 Update via automation Jun 4, 2024
@ryoppippi
Copy link
Contributor Author

Thanks always

@samchon
Copy link
Owner

samchon commented Jun 4, 2024

Changed to CJS, but ESM still be supported following your idea.

By the way, I'm sorry but I justed change something different with you:

  1. Changed tsup to rollup because I'm only familiar with it. It is okay to change again.
    • No special reason for using rollup
    • It's just the reason why many libraries' developers are using that
  2. About tsx, it can fully replace in current repo
    • However, nestia requires the ts-node
    • tsx can't replace the ts-node in the nestia
    • So I just hope to keept it for consistency.

Also, about the bundling size, I'll test it tomorrow. If you want to take it earlier, you can setup it with:

npm i typia@next

Anyway, as I'm not a frontend developer, not familiar with bundler story.

By your contributions, I understood how important to support both CJS/ESM for disk space reason.

Thanks for many ideas and contributions.

@samchon samchon removed invalid This doesn't seem right wontfix This will not be worked on labels Jun 4, 2024
@samchon samchon self-assigned this Jun 4, 2024
@samchon samchon added help wanted Extra attention is needed question Further information is requested labels Jun 4, 2024
@ryoppippi
Copy link
Contributor Author

Thank you for your quick response!!

  1. Changed tsup to rollup because I'm only familiar with it. It is okay to change again.

If you are familer with it, that is fine!
I'll check the rollup build result

  1. About tsx, it can fully replace in current repo

I replaced ts-node with tsx because it is easy to handle both cjs and esm scripts.
Sometimes ts-node fails running scripts with odd errors, so I changed to my familer tool.
If you still want to use ts-node, that is totally fine!

@ryoppippi
Copy link
Contributor Author

Also, thank you for adding test for esm.
It looks great

@samchon
Copy link
Owner

samchon commented Jun 4, 2024

As you told that you're planning to adjust monorepo, so that if rollup or ts-node interrupts it, it is okay to replace it again.

@ryoppippi
Copy link
Contributor Author

As you told that you're planning to adjust monorepo, so that if rollup or ts-node interrupts it, it is okay to replace it again.

Nice! Thanks

One thing is the result of the build with rollup, but it has been combined into one mjs file!
I would like to change this.

@ryoppippi
Copy link
Contributor Author

Oh, I looked up.
I think for now it is ok for ESM
Thanks.

@ryoppippi
Copy link
Contributor Author

ryoppippi commented Jun 4, 2024

Maybe I'll replace rollup to esbuild in the future because the bundle result looks better (for me)

Looking at the build results from your rollup, that one looks better with a smaller bundle size. Thanks.
But for now it is fine!!

Thank you for reviewing!

@ryoppippi ryoppippi requested a review from samchon June 4, 2024 15:03
Copy link
Owner

@samchon samchon left a comment

Choose a reason for hiding this comment

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

Okay, I will merge at morning.

@samchon samchon merged commit 04284ef into samchon:master Jun 5, 2024
1 check passed
v6 Update automation moved this from In progress to Done Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed question Further information is requested
Projects
Development

Successfully merging this pull request may close these issues.

None yet

2 participants