Skip to content

Commit

Permalink
fix(repository-tools): fix cjs package
Browse files Browse the repository at this point in the history
  • Loading branch information
altano committed Aug 28, 2024
1 parent b42cccd commit 7cf6538
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-jeans-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@altano/repository-tools": patch
---

fix cjs package
30 changes: 16 additions & 14 deletions packages/build-config/tsup.config.node-hybrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@ import { defineConfig } from "tsup";
export default defineConfig([
{
entry: ["./src/**/*.ts"],
format: "esm",
outDir: "dist/esm",
onSuccess: "pnpm build:types:esm",
clean: true,
platform: "node",
bundle: false,
minify: false,
},
{
entry: ["./src/**/*.ts"],
format: "cjs",
outDir: "dist/cjs",
legacyOutput: true,
onSuccess: "pnpm build:types:cjs",
format: ["esm", "cjs"],
outDir: "dist",
dts: true,
clean: true,
platform: "node",
bundle: false,
minify: false,
plugins: [
{
// Based on discussion in https://github.com/egoist/tsup/issues/953
// require("./path.js") → require("./path.cjs") in `.cjs` files
name: "fix-cjs-require",
renderChunk(_, { code }) {
if (this.format === "cjs") {
const regex = /require\("(?<import>.+).js"\)/g;
return { code: code.replace(regex, `require("$<import>.cjs")`) };
}
},
},
],
},
]);
15 changes: 15 additions & 0 deletions packages/repository-tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ Example:
```ts
await findRoot("/my-git-repo/some/subdirectory"); // => "/my-git-repo"
```

## Importing

This is a dual ESM/CJS package, and publishes both a top-level module with all exports, and sub-path exports. So feel free to import using any of the following:

| Your package type | Using sub-path exports | Import to use |
| ----------------- | ---------------------- | ----------------------------------------------------------------------- |
| ESM | No | `import { findRoot } from "@altano/repository-tools";` |
| ESM | Yes | `import { findRoot } from "@altano/repository-tools/findRoot.js";` |
| CJS | No | `const { findRoot } = require("@altano/repository-tools")` |
| CJS | Yes | `const { findRoot } = require("@altano/repository-tools/findSync.cjs")` |

## Contributing

This is a dual ESM/CJS package which is really hard to get right. Manually run `pnpm build && pnpm dlx @arethetypeswrong/cli --pack` before making any changes that would affect the build, packaging, or publishing.
18 changes: 9 additions & 9 deletions packages/repository-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
"name": "@altano/repository-tools",
"version": "0.1.0",
"description": "Misc tools for dealing with repositories of multiple version control systems (git, svn, etc)",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"./*": {
"import": "./dist/esm/*.js",
"require": "./dist/cjs/*.js"
"import": "./dist/*",
"require": "./dist/*"
}
},
"type": "module",
"sideEffects": false,
"files": [
"dist/**/*.js",
"dist/**/*.d.ts"
"dist/**/*.cjs",
"dist/**/*.d.ts",
"dist/**/*.d.cts"
],
"scripts": {
"build": "tsup --config build-config/tsup.config.node-hybrid.ts",
"build:types:cjs": "tsc --project tsconfig.declarations-cjs.json",
"build:types:esm": "tsc --project tsconfig.declarations-esm.json",
"clean": "rm -rf .turbo && rm -rf .tsbuildinfo && rm -rf node_modules && rm -rf dist",
"dev": "pnpm run build --watch",
"format": "prettier --check src",
Expand Down
4 changes: 2 additions & 2 deletions packages/repository-tools/tests/unit/packaging.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "node:path";
import { describe, expect, it } from "vitest";
import { findRoot } from "@altano/repository-tools/findRoot";
import { findRootSync } from "@altano/repository-tools/findRootSync";
import { findRoot } from "@altano/repository-tools/findRoot.js";
import { findRootSync } from "@altano/repository-tools/findRootSync.js";

// Test sub-path exports separately by importing using the package name. This
// has to be separate from other tests because vitest test coverage analysis
Expand Down
13 changes: 0 additions & 13 deletions packages/repository-tools/tsconfig.declarations-cjs.json

This file was deleted.

10 changes: 0 additions & 10 deletions packages/repository-tools/tsconfig.declarations-esm.json

This file was deleted.

0 comments on commit 7cf6538

Please sign in to comment.