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

CommonJS bundle (without Rollup) #5853

Closed
wants to merge 34 commits into from
Closed

CommonJS bundle (without Rollup) #5853

wants to merge 34 commits into from

Conversation

kraenhansen
Copy link
Member

@kraenhansen kraenhansen commented May 26, 2023

What, How & Why?

This fixed #5857 by removing rollup from the SDK package in favour of a TypeScript projects based setup which emits only CommonJS code.

This is aligned with recent discussions on the team to ensure v12.0.0 exports exactly as v11 did, to keep friction from migrating at an absolute minimum.

I ran into several problems of which some I managed to find workarounds for and others I didn't:

  • The Typescript compiler would emit .mjs files even when module was set to commonjs which would break the package upon import. I filed an issue for this, created a PR and found a temporary workaround.
  • Although the Jest issue is closed, I found problems from Jest lacking support of export conditions out of the box and had to implement a custom resolver. I later transitioned into exporting via a separate package in an attempt to work around this.
  • I was planning to rely on bundleDependencies to pull @realm/network-transport (and a new @realm/binding package) into the SDK's .tar.gz before uploading it to NPM. But this is not supported when using NPM workspaces (yet) :meow_sad: [BUG] Workspaces - bundledDependencies missing in tarball after npm pack npm/cli#3466

@kraenhansen
Copy link
Member Author

Woups - looks like I need to find a solution for re-enabling test coverage 🤔

Comment on lines 36 to 38
"react-native": "./generated/native-react-native.mjs"
},
"./binding/core": "./dist/generated/core.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the core part be moved into the other ".binding" block?

Suggested change
"react-native": "./generated/native-react-native.mjs"
},
"./binding/core": "./dist/generated/core.js",
"react-native": "./generated/native-react-native.mjs",
"core": "./dist/generated/core.js"
},

.gitignore Outdated
Comment on lines 115 to 116
/packages/realm/generated/
/packages/realm/src/generated/
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems silly to have two separate generated directories here. Can they be combined?

Copy link
Member Author

@kraenhansen kraenhansen May 31, 2023

Choose a reason for hiding this comment

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

I needed to move core.ts into src when adopting TypeScript projects, because of:

error TS6059: File '/Users/kraen.hansen/Projects/realm-js/packages/realm/generated/core.ts' is not under 'rootDir' '/Users/kraen.hansen/Projects/realm-js/packages/realm/src'. 'rootDir' is expected to contain all source files.
The file is in the program because:
Matched by include pattern '../generated/core.ts' in '/Users/kraen.hansen/Projects/realm-js/packages/realm/src/tsconfig.json'

And I don't really consider the wrapper .mjs files and realm.node file as "src" as it's not TS and won't be moved to "dist" by tsc. Adding to that, I don't know yet if we actually want to add the src files to the archive that we upload to NPM (lots of packages don't - most likely to keep their size and complexity low).

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm. I'm not sure we have a clear rule for what src/ is. Is it where we put the "source code" for the realm package, or is it only the input for tsc? I had assumed the former, but it seems like you intend for it to be the later. I guess I don't feel too strongly either way, but we should be clear what our rule is.

@@ -125,7 +125,7 @@ file(GLOB_RECURSE SDK_TS_FILES
list(FILTER SDK_TS_FILES EXCLUDE REGEX "templates/[^/]*\.ts$")

set(JS_SPEC_FILE ${SDK_DIR}/bindgen/js_spec.yml)
set(TYPESCRIPT_OUTPUT_DIR ${SDK_DIR}/generated/ts)
set(TYPESCRIPT_OUTPUT_DIR ${SDK_DIR}/generated)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be src/generated?

@@ -1433,6 +1437,7 @@ export declare namespace Realm {
CollectionChangeSet,
/** @deprecated Please use named imports */
CollectionType as Collection,
CompensatingWriteErrorType,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
CompensatingWriteErrorType,
CompensatingWriteErrorType as CompensatingWriteError,

"types": ["node"],
"noResolve": false,
"incremental": true,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to remove incremental here? It does seem to speed up incremental building.

Copy link
Member Author

@kraenhansen kraenhansen May 31, 2023

Choose a reason for hiding this comment

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

As I understand it (and as I can see tsbuildinfo files on disk), it's default for a composite config.
It's not explicit from the documentation though: https://www.typescriptlang.org/docs/handbook/project-references.html

Copy link
Contributor

Choose a reason for hiding this comment

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

What is with mixing the index.cjs.ts and cjs.ts naming conventions? Can we pick one. Also do we need both index.cjs.ts and index.ts? or could they be combined?

Copy link
Member Author

@kraenhansen kraenhansen May 31, 2023

Choose a reason for hiding this comment

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

do we need both index.cjs.ts and index.ts? or could they be combined?

I wanted the ensure the general code-base was ESM and the cjs.ts was only to export an entrypoint for the types. I'll align the naming though 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

(FYI in markdown you need a blank line after the last quoted line in order to end a quote and start your reply)

I wanted the ensure the general code-base was ESM and the cjs.ts was only to export an entrypoint for the types. I'll align the naming though

We still could get rid of index.ts and move its minimal contents to index.cjs.ts. It just seems weird to have both with the same root name. If you really want to keep as much as possible out of cjs files, it may be best to rename index.ts to reexport.ts or umbrella.ts.

Comment on lines 132 to 133
${GENERATED_OUTPUT_DIR}/native.d.mts
${TYPESCRIPT_OUTPUT_DIR}/core.ts
Copy link
Contributor

@RedBeard0531 RedBeard0531 Jun 6, 2023

Choose a reason for hiding this comment

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

I don't see how that can be right. These are build by the same command and it is only told about the path to TYPESCRIPT_OUTPUT_DIR.

Copy link
Member Author

Choose a reason for hiding this comment

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

True - I was missing a replace here 👍

Comment on lines 149 to +150
OUTPUTS ${GENERATED_JS_FILES}
OUTDIR ${TYPESCRIPT_OUTPUT_DIR}
OUTDIR ${GENERATED_OUTPUT_DIR}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you meant to change lines 144/145 instead. Right now there are out of sync.

@kraenhansen kraenhansen requested a review from takameyer June 9, 2023 17:26
@kraenhansen kraenhansen changed the title CommonJS bundle CommonJS bundle (without Rollup) Jun 12, 2023
@kraenhansen kraenhansen marked this pull request as draft June 12, 2023 10:17
@kraenhansen
Copy link
Member Author

Drafting this as it's been superseded by #5882 .. I might close when I accept my defeat and feel better about the time I've already sunk into this.

@takameyer
Copy link
Contributor

I'm guessing we are going to close this, or?

@kraenhansen
Copy link
Member Author

@takameyer see my previous comment 🙂

@kraenhansen
Copy link
Member Author

kraenhansen commented Jun 16, 2023

Closing this as I've created #5894 to eventually do this migration.
(I'll let the branch stick around for a bit longer).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Export only a CommonJS entrypoint
4 participants