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

Native ESM #57

Merged
merged 1 commit into from
Jan 12, 2024
Merged

Native ESM #57

merged 1 commit into from
Jan 12, 2024

Conversation

smorimoto
Copy link
Contributor

@smorimoto smorimoto commented Jan 9, 2024

Context

When building actions in ESM, some incompatibilities were caused by packaging using ncc, which caused runtime errors. So the solution is to switch the source to native ESM and generate code for ESM and CJS at build time.

Changes:

tsup

The build system is changed to tsup, and tsc is now used only for type checking. This makes support for both esm and cjs much simpler.

Vitest

Switch to vitest for faster and easier testing in ESM.

Biome

This project is not very complex, so formatting and linting are switched to biome. This results in faster, simpler setup and less dependencies.

.npmignore

If the files field is already included in package.json, you should not have to add the .npmignore file. As you can see by running dry-run, there are much fewer sources in the package now.

~/src/github.com/github/dependency-submission-toolkit native-esm
❯ npm pack --dry-run
npm notice 
npm notice 📦  @github/dependency-submission-toolkit@1.2.9
npm notice === Tarball Contents === 
npm notice 1.1kB  LICENSE           
npm notice 2.8kB  README.md         
npm notice 7.6kB  dist/index.cjs    
npm notice 29.0kB dist/index.cjs.map
npm notice 11.1kB dist/index.d.cts  
npm notice 11.1kB dist/index.d.ts   
npm notice 6.9kB  dist/index.js     
npm notice 29.0kB dist/index.js.map 
npm notice 1.6kB  package.json      
npm notice === Tarball Details === 
npm notice name:          @github/dependency-submission-toolkit         
npm notice version:       1.2.9                                         
npm notice filename:      github-dependency-submission-toolkit-1.2.9.tgz
npm notice package size:  17.1 kB                                       
npm notice unpacked size: 100.2 kB                                      
npm notice shasum:        aa2ad1f5115dd6020565878219052266e8c7350c      
npm notice integrity:     sha512-Iphxp7mGIctTS[...]soLs4oK6rz76Q==      
npm notice total files:   9                                             
npm notice 
github-dependency-submission-toolkit-1.2.9.tgz

Unpacked Size

1.16 MB -> 0.1 MB

@smorimoto smorimoto requested a review from a team as a code owner January 9, 2024 12:40
@smorimoto smorimoto force-pushed the native-esm branch 4 times, most recently from 8f8a2ee to 34c75ba Compare January 9, 2024 14:08
@smorimoto
Copy link
Contributor Author

Ready to go!

@febuiles
Copy link
Contributor

@smorimoto thank you very much for this contribution there's, a lot we can benefit from in here.

Can I ask you to split this into different PRs? From going over the code once I can think of a few:

  1. Upgrade Node to 20.
  2. Add ESM-required changes.
  3. Add extra tooling (e.g. Biome).

This can help us review and more quickly address each change. Thanks again.

@smorimoto
Copy link
Contributor Author

@febuiles Thanks a lot for the quick response! Actually, I considered that at first, but it's a bit hard to split things into different PRs except Node.js 20 upgrade.

The reasons for each are:

tsup

This is required to deal with the NCC outputting code which is not ESM compliant.

Vitest

This is not strictly required, but you will need to make some configuration changes to Jest to test the ESM code with Jest.

Biome

This is also not strictly required, but the difference is much smaller than changing ESLint configuration for ESM.

@smorimoto smorimoto force-pushed the native-esm branch 7 times, most recently from c5146af to e7bd516 Compare January 12, 2024 00:44
minify: true,
sourcemap: true,
splitting: true,
target: 'node16',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think Node.js 16 is the lower bound in the current "works but deprecated" sense. You should be able to easily update the lower bound to Node.js 20 by simply changing this in the future.

tsconfig.json Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just use the strict recommended 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.

Just use the strict recommended TSConfig.

@smorimoto
Copy link
Contributor Author

I tried to reduce the diff as much as possible. This makes reviewing easier (I guess).

- name: Install NPM dependencies
run: npm ci
- name: Run all NPM build/test actions
run: npm -w:example rebuild && npm run all -w:example
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using prepare instead of prepack eliminates the need for rebuild. But prepack may be less noisy during the development process.

- name: Run all NPM build/test actions
run: npm rebuild && npm run all
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same.

"ci": "biome ci .",
"format": "biome format --write .",
"lint": "biome lint --apply .",
"prepare": "npm run build",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can choose prepare or prepack depending on your needs.

with:
node-version: 16.x
registry-url: https://registry.npmjs.org/
cache: npm
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For small projects, the cache will probably just cause thrashing, which is just a waste of compute resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just removed unnecessary things out and ran sort-package-json to organise package.json order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ran sort-package-json to organise package.json order.

Comment on lines -8 to -10
"directories": {
"test": "test"
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be unnecessary.

@@ -2,67 +2,54 @@
"name": "@github/dependency-submission-toolkit",
"version": "1.2.9",
"description": "A TypeScript library for creating dependency snapshots.",
"prepare": "npm run build",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably misplaced at the top level?

.npmignore Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Conflicts with files in package.json and is not needed: https://docs.npmjs.com/cli/v10/configuring-npm/package-json#files

@smorimoto
Copy link
Contributor Author

@febuiles I left some comments. I hope these help the review process.

@febuiles
Copy link
Contributor

I think all of this makes sense, thanks for your contribution. I went over it and things look good, I'll try to carve up some time to do local testing before merging by next week at the latest.

@smorimoto
Copy link
Contributor Author

@febuiles Sorry for the lot of diff 😅
As a side note, the results of the test run for the example action can also be found here: https://github.com/smorimoto/dependency-submission-toolkit/actions/runs/7502291922

- name: Install NPM dependencies
run: npm ci
- name: Run all NPM build/test actions
run: npm -w:example rebuild && npm run all -w:example
run: npm run --workspace example all
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old way doesn't seem to work, so for clarity, use the verbose flag here. That said, the short hand should work if you do the following: -w example or -w=example, not -w:example

Signed-off-by: Sora Morimoto <sora@morimoto.io>
Comment on lines -110 to +113
['list', '--prod', '--all', '--json'],
['list', '--all', '--json', '--omit', 'dev'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

--prod is already deprecated

"*.test.ts"
]
"exclude": ["dist", "node_modules"],
"extends": "@tsconfig/strictest/tsconfig.json"
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL, this is neat!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I always recommend using this!

@febuiles febuiles merged commit 39c8d88 into github:main Jan 12, 2024
3 checks passed
@smorimoto smorimoto deleted the native-esm branch January 12, 2024 14:51
@febuiles
Copy link
Contributor

@smorimoto Thank you very much for your contribution. Once #59 is merged we'll do a new major release that will include these changes.

@smorimoto
Copy link
Contributor Author

@febuiles My pleasure! Thank you so much for this fantastic package! The OCaml community is really actively taking advantage of this 🙂

@smorimoto
Copy link
Contributor Author

Confirmed v2 works with the ESM GHA! Thanks a lot!

@smorimoto
Copy link
Contributor Author

@febuiles @hmaurer Offtopic: In the OCaml community, the OCaml core team has already released a stable version of the dependency submission action, and we want to make it well-integrated with GitHub, but is there a way to do this? I couldn't find that anywhere.

@febuiles
Copy link
Contributor

@smorimoto I think @jonjanego could help with that!

@jonjanego
Copy link
Member

@smorimoto Thanks for reaching out. For purposes of keeping this PR tidy, would you mind sharing some more details about what the OCaml community has built, and what sort of integration that you're thinking of, in a new issue in this repo so we can discuss it there?

@febuiles
Copy link
Contributor

@smorimoto the ESM changes are causing at least one issue I'm aware of: https://github.com/advanced-security/maven-dependency-submission-action/actions/runs/7505596002/job/20435167951, did we miss any steps here?

@smorimoto
Copy link
Contributor Author

@febuiles The reason for that seems to be that the index.ts does not export DependencyScope.

import { BuildTarget, Manifest } from './manifest.js'

How about exposing all public APIs in the package root like this?

export * from './manifest.js'
export * from './package-cache.js'
export * from './package.js'
export * from './snapshot.js'

@smorimoto
Copy link
Contributor Author

@febuiles See this PR! #63

@febuiles febuiles mentioned this pull request Jan 18, 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.

None yet

4 participants