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: build project to emit cjs and esm using rollup #93

Closed
wants to merge 2 commits into from

Conversation

amoshydra
Copy link
Contributor

@amoshydra amoshydra commented Apr 10, 2024

Edit 1: Updated base on commit 759f5b7

Summary

⚠ BREAKING CHANGE: distribution layout has been significantly changed

Support both cjs and mjs output using rollup.

The package.json now includes the modern exports field and the legacy main, modules and typings fields to ensure compatibiilty with older Node runtime / bundler

Related to

Related to #61

Motivation

To resolve #61

How is this tested

Fixtures for the different environments

✔ retested for 759f5b7 (code identical with latest @ d4c8909)

I have created a repository hosting the 4 test environments below here: https://github.com/amoshydra/repro-jessealama-decimal128/tree/ref-759f5b791

  • browser importing mjs
  • nodejs [type="module"] importing mjs
  • nodejs [type="commonjs"] importing cjs
  • bundler (vite --> rollup / esbuild) importing mjs
browser mjs nodejs cjs nodejs mjs vite mjs
browser-mjs nodejs-cjs nodejs-mjs vite-mjs

Layout change in npm package

This layout change does not really affect the consumer using a bundler, TypeScript or NodeJS as they will continue to import the package via one of the following ways:

// esm
import { Decimal128 } from "decimal128";
// cjs
const { Decimal128 } = require("decimal128");

Before

Files are exported from src

src
├── common.d.mts
├── common.mjs
├── common.mts
├── decimal128.d.mts
├── decimal128.mjs
├── decimal128.mts
├── rational.d.mts
├── rational.mjs
└── rational.mts

After

Files are exported from dist

dist
├── cjs
│   ├── common.cjs
│   ├── common.cjs.map
│   ├── common.d.mts
│   ├── decimal128.cjs
│   ├── decimal128.cjs.map
│   ├── decimal128.d.mts
│   ├── rational.cjs
│   ├── rational.cjs.map
│   └── rational.d.mts
└── esm
    ├── common.d.mts
    ├── common.mjs
    ├── common.mjs.map
    ├── decimal128.d.mts
    ├── decimal128.mjs
    ├── decimal128.mjs.map
    ├── rational.d.mts
    ├── rational.mjs
    └── rational.mjs.map

@amoshydra amoshydra force-pushed the rollup-cjs-export branch 2 times, most recently from aa01cb5 to 7feb5cd Compare April 10, 2024 09:55
@amoshydra amoshydra changed the title chore: build project to cjs and esm will rollup [Bundler] chore: build project to cjs and esm will rollup Apr 10, 2024
typescript({
tsconfig: "tsconfig.build.json",
outDir: "dist",
sourceMap: false,
Copy link
Contributor Author

@amoshydra amoshydra Apr 10, 2024

Choose a reason for hiding this comment

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

Note to reviewer

Source map is still generated by using rollup's output.sourcemap: true: https://github.com/jessealama/decimal128/pull/93/files/7feb5cd9d46d29115d1af57986e6a00ad23daeed#diff-47407fecafdf5f5cd55403c3de457833ddf9b6fab45253c04e1dc4c7cb4495b1R8.

This is turned off to prevent conflict with rollup's source map generation
See related stackoverflow post: https://stackoverflow.com/a/63235210

@amoshydra amoshydra marked this pull request as ready for review April 10, 2024 10:41
@amoshydra amoshydra changed the title [Bundler] chore: build project to cjs and esm will rollup [Bundler] chore: build project to cjs and esm with rollup Apr 10, 2024
@amoshydra amoshydra changed the title [Bundler] chore: build project to cjs and esm with rollup [Bundler] chore: build project to emit cjs and esm using rollup Apr 10, 2024
@amoshydra
Copy link
Contributor Author

amoshydra commented Apr 10, 2024

With the new understanding that tests and examples are suppose to run on transpiled code. I have updated the approach to do thing differently.

output.preserveModules: true

Because the tests still imports round.mjs and rational.mjs, in the new commit, I have used the output.preserveModules flag to keep the module structure.

Usage of preserveModules require us to output the build files into their own folder. In this PR, I have choose to output esm and cjs to their respective folder accordingly.

Before this option

dist
├── common.d.mts      
├── decimal128.cjs    
├── decimal128.cjs.map
├── decimal128.d.mts  
├── decimal128.mjs
├── decimal128.mjs.map
└── rational.d.mts

After this option

dist
├── cjs
│   ├── common.cjs
│   ├── common.cjs.map
│   ├── common.d.mts
│   ├── decimal128.cjs
│   ├── decimal128.cjs.map
│   ├── decimal128.d.mts
│   ├── rational.cjs
│   ├── rational.cjs.map
│   └── rational.d.mts
└── esm
    ├── common.d.mts
    ├── common.mjs
    ├── common.mjs.map
    ├── decimal128.d.mts
    ├── decimal128.mjs
    ├── decimal128.mjs.map
    ├── rational.d.mts
    ├── rational.mjs
    └── rational.mjs.map

Notice rational and common are kept as its own module

Tests

Tests and coverage can now run like how it previously was
image

Workflow

Workflow file has been update to include npm run build command:
759f5b7#diff-1790f2f8b7123d50d46235053b54f1cc5996bc7388b803aad4233fd5582e0bad

-              run: npx tsc
+              run: npm run build && npx tsc

@amoshydra amoshydra force-pushed the rollup-cjs-export branch 2 times, most recently from 759f5b7 to be2baa8 Compare April 10, 2024 15:33
@amoshydra amoshydra changed the title [Bundler] chore: build project to emit cjs and esm using rollup chore: build project to emit cjs and esm using rollup Apr 10, 2024
@amoshydra amoshydra force-pushed the rollup-cjs-export branch 2 times, most recently from d4c8909 to 02ded6a Compare April 10, 2024 17:55
@jessealama
Copy link
Owner

Thanks so much for your interest in this!

Could you please rebase on the newly changed main branch?

@amoshydra
Copy link
Contributor Author

sure! rebased onto cf135fd @ #93 (comment)

@amoshydra amoshydra force-pushed the rollup-cjs-export branch 2 times, most recently from d8623a0 to 4cace43 Compare April 11, 2024 20:36
Copy link

@ryzokuken ryzokuken left a comment

Choose a reason for hiding this comment

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

Overall it looks good, a few comments inline: also, you might've forgotten to update the .gitignore

"c8": "^9.1.0",
"jest": "^29.5.0",
"jest": "^29.7.0",

Choose a reason for hiding this comment

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

Was updating jest and ts-jest necessary in this PR?

Comment on lines +9 to +10
"import": "./dist/esm/decimal128.mjs",
"require": "./dist/cjs/decimal128.cjs",

Choose a reason for hiding this comment

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

check out nodejs/node#52174, this pattern is not ideal and we should use the alternative mentioned in the thread.

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"include": ["./src"]

Choose a reason for hiding this comment

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

Apologies, but I don't quite get this: which files were included so far? And why not just add the include statement to the original tsconfig?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

which files were included so far

In the original tsconfig, I suppose, all files will be included.
I believe that is fine since the original tsconfig is mainly used by VS Code / ide.

why not just add the include statement to the original tsconfig?

Adding includes to the original tsconfig will prevent tsconfig from being applied to files outside of the src folder.


My intention here with tsconfig.buid.json include is to restrict rollup's typescript compiler include scope to files within src folder only

"ts-jest": "^29.1.0",
"rollup": "^4.14.1",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",

Choose a reason for hiding this comment

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

I don't see why we need tslib since importHelpers aren't used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

likely a mistake, PR raised by Jesse in #121 no longer has this

@jessealama
Copy link
Owner

closed via #121

@jessealama jessealama closed this Nov 15, 2024
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.

Make a CommonJS variant available
3 participants