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: change the build config to bundle deps correctly, fix #111 #112

Merged
merged 6 commits into from
Apr 16, 2024

Conversation

SukkaW
Copy link
Contributor

@SukkaW SukkaW commented Apr 16, 2024

Description

#111 is caused by the Rollup (unbuild uses Rollup under the hood) incorrectly bundling @npmcli/config (and its transitive dependency cacache, which requires its own package.json).

The PR provides a workaround by changing how dists are bundled. A test case has also been added to ensure the CLI itself should never crash.

Linked Issues

#111

Additional context

This is a workaround that works for now. I am still investigating a better solution that solves the issue once and for all.

Here are the backgrounds behind #111 and this PR:

  • @npmcli/config requires cacache, and cacache requires its own package.json. This causes @rollup/plugin-commonjs to panic. Instead of using the correct named exports generated from @rollup/plugin-json, @rollup/plugin-commonjs try to wrap the exports with its interop code, causing cacache to fail.
    • Solution: change @rollup/plugin-commonjs's requireReturnsDefault to auto, disable @rollup/plugin-json's namedExports optimization.
  • @npmcli/config also requires semver/function/valid. Since @npmcli/config is a CJS module, require('semver/function/valid') works just fine. However, when Rollup bundles the dist, it transforms require('semver/function/valid') into import 'semver/function/valid' which is an invalid ESM import (missing the extname, it should be import 'semver/function/valid.js'), causing the error of npx taze@latest throws [ERR_MODULE_NOT_FOUND]  #111.
    • Solution: bundle the entire semver as well, preventing transitive dependency from becoming external.
    • Bundling semver would cause another build error, since unbuild includes @types/semver to be parsed by the rollup.
      • Solution: enable rollup-plugin-dts's respectExternal.

@SukkaW SukkaW changed the title test(#111): add a test case fix(#111): change the build config to bundle deps correctly Apr 16, 2024
@@ -31,7 +31,7 @@
"typecheck": "tsc",
"prepublishOnly": "nr build",
"release": "bumpp && pnpm publish --no-git-checks",
"test": "vitest"
"test": "unbuild && vitest"
Copy link
Contributor Author

@SukkaW SukkaW Apr 16, 2024

Choose a reason for hiding this comment

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

The new test case is against the dist, so the dist needs to be built before the test happens.

@SukkaW SukkaW marked this pull request as draft April 16, 2024 09:34
@SukkaW SukkaW marked this pull request as ready for review April 16, 2024 10:05
@@ -67,6 +66,7 @@
"npm-package-arg": "^11.0.2",
"npm-registry-fetch": "^16.2.1",
"rimraf": "^5.0.5",
"semver": "^7.6.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

semver is a CommonJS module and a dependency of both taze and taze's dependency.

Rollup has trouble handling a CommonJS external from a transitive dependency, so let's bundle semver as well.

@@ -3,7 +3,7 @@ import type { Argv } from 'yargs'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import c from 'picocolors'
import { version } from '../package.json'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we've disabled the @rollup/plugin-json's namedExports optimization, we can't use named import here.

@antfu antfu changed the title fix(#111): change the build config to bundle deps correctly fix: change the build config to bundle deps correctly, fix #111 Apr 16, 2024
@antfu
Copy link
Member

antfu commented Apr 16, 2024

Thanks for the detailed explaination!

@antfu antfu merged commit ed12bc6 into antfu-collective:main Apr 16, 2024
5 checks passed
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.

2 participants