@savvy-web/tsdown-plugins 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -40,14 +40,14 @@ export default defineConfig({
40
40
  ## Features
41
41
 
42
42
  - **Entry detection** — `packageJsonEntries` and `extractEntries` derive build entries from a package's `exports` and `bin`, matching the rules used across the Silk Suite builders.
43
- - **Manifest transforms** — `transformManifest`, `transformExports`, `transformBin` and `normalizeBinPaths` rewrite a source `package.json` into a publishable one; `emitManifest` is the rolldown plugin that writes it. A dual-format build emits both `import` and `require` export conditions.
43
+ - **Manifest transforms** — `transformManifest`, `transformExports`, `transformBin` and `normalizeBinPaths` rewrite a source `package.json` into a publishable one; `emitManifest` is the rolldown plugin that writes it. A dual-format build emits both `import` and `require` export conditions, and a `"./package.json": "./package.json"` entry is added to the exports map so consumers can `import "<pkg>/package.json"`.
44
44
  - **Catalog resolution** — `resolveManifest` resolves `catalog:` and `workspace:` specifiers against the workspace, delegating to `workspaces-effect`'s `CatalogResolver`.
45
45
  - **Multi-target resolution** — `resolveTargets` turns a `publishConfig.targets` map into the distinct byte-variant groups to build and the registry bindings for each; `writeTargetsBinding` persists that resolution as `dist/prod/targets.json` for the release step.
46
46
  - **JSX resolution** — `resolveJsxConfig` and `readTsconfigJsx` derive the effective JSX transform from a package's tsconfig, with an explicit override winning.
47
47
  - **Executable binaries** — `normalizeExeOptions` fills the SEA defaults and infers targets from the package's `os`/`cpu`; `runExeBuild` drives `@tsdown/exe` to compile the binaries.
48
48
  - **Config validation** — the `ConfigValidator` Effect service (with `ConfigValidatorLive`) fast-fails on a bad `publishConfig.targets`, `exe` or `meta` config, raising the typed `ConfigValidationError`.
49
49
  - **dts tsconfig port** — `buildResolvedTsconfig` and `writeResolvedTsconfig` write a temp tsconfig with absolute paths so type declarations emit cleanly under pnpm symlinks.
50
- - **Per-target build loop** — `deriveTargetGroupOptions` and `buildTargetGroups` map a target to its `tsdown` options and run the build once per target, exposed as a helper so the escape hatch gets multi-target builds too. A `format` of `["esm", "cjs"]` (the `BuildFormat` type) derives a dual-format build — a require-able CJS output with default-export interop and `.d.cts` declarations alongside the ESM one. The `bundleNodeModules`, `bundledPackages` and `dtsExternals` options thread the dependency-bundling posture into both the JS and declaration passes.
50
+ - **Per-target build loop** — `deriveTargetGroupOptions` and `buildTargetGroups` map a target to its `tsdown` options and run the build once per target, exposed as a helper so the escape hatch gets multi-target builds too. A `format` of `["esm", "cjs"]` (the `BuildFormat` type) derives a dual-format build — a require-able CJS output with default-export interop and `.d.cts` declarations alongside the ESM one. The `bundleNodeModules`, `bundledPackages` and `dtsExternals` options thread the dependency-bundling posture into both the JS and declaration passes. The `define` option forwards compile-time global replacements to both passes, merged with an auto-injected `process.env.__PACKAGE_VERSION__` constant.
51
51
  - **Bundled declarations** — each target runs two `tsdown` passes: a JavaScript pass that preserves per-module output, then a declaration-only pass that rolls every re-exported type into a single `.d.ts` per public entry (`deriveDtsPassOptions`). Per-module JavaScript stays intact while consumers keep reaching re-exported types through your published subpaths.
52
52
  - **API Extractor meta** — `generateMeta` runs [API Extractor](https://api-extractor.com/) over a package's emitted `.d.ts` to write an api-model bundle (`.api.json`, `tsdoc-metadata.json`, resolved `tsconfig.json`); `normalizeMetaOptions` fills the `MetaOptions` defaults that drive it.
53
53
  - **Output reporter** — `renderReport` plus the `BuildReport` schema and a set of formatters (terminal, JSON, markdown, CI annotations, silent) render a build report for humans, agents or CI.
@@ -54,7 +54,8 @@ async function buildTargetGroups(options) {
54
54
  ...partBundledPackages !== void 0 ? { bundledPackages: partBundledPackages } : {},
55
55
  ...part.format !== void 0 ? { format: part.format } : {},
56
56
  ...options.minify !== void 0 ? { minify: options.minify } : {},
57
- ...options.jsx !== void 0 ? { jsx: options.jsx } : {}
57
+ ...options.jsx !== void 0 ? { jsx: options.jsx } : {},
58
+ ...options.define !== void 0 ? { define: options.define } : {}
58
59
  };
59
60
  const js = deriveTargetGroupOptions(deriveInput);
60
61
  const dts = deriveDtsPassOptions(deriveInput);
@@ -18,7 +18,10 @@ function deriveTargetGroupOptions(options) {
18
18
  fixedExtension: false,
19
19
  entry: options.entry,
20
20
  dts: false,
21
- define: { __PACKAGE_VERSION__: JSON.stringify(options.version) },
21
+ define: {
22
+ "process.env.__PACKAGE_VERSION__": JSON.stringify(options.version),
23
+ ...options.define
24
+ },
22
25
  isProd,
23
26
  ...hasCjs ? { cjsDefault: true } : {},
24
27
  ...options.jsx !== void 0 ? { jsx: options.jsx } : {}
@@ -41,7 +44,10 @@ function deriveDtsPassOptions(options) {
41
44
  tsconfig: options.tsconfigPath,
42
45
  emitDtsOnly: true
43
46
  },
44
- define: { __PACKAGE_VERSION__: JSON.stringify(options.version) },
47
+ define: {
48
+ "process.env.__PACKAGE_VERSION__": JSON.stringify(options.version),
49
+ ...options.define
50
+ },
45
51
  isProd,
46
52
  ...options.jsx !== void 0 ? { jsx: options.jsx } : {},
47
53
  ...options.bundledPackages !== void 0 ? { bundledPackages: options.bundledPackages } : {}
package/index.d.ts CHANGED
@@ -148,6 +148,12 @@ interface DeriveOptions {
148
148
  readonly minify?: boolean | undefined;
149
149
  /** JSX transform settings to forward to rolldown's inputOptions. */
150
150
  readonly jsx?: JsxConfig | undefined;
151
+ /**
152
+ * Compile-time global replacements forwarded to the build `define`. Merged AFTER the
153
+ * auto-injected `process.env.__PACKAGE_VERSION__` so a user key of the same name wins.
154
+ * Values are inserted verbatim (string literals must already be quoted).
155
+ */
156
+ readonly define?: Record<string, string> | undefined;
151
157
  /**
152
158
  * External packages whose declarations should be INLINED into the bundled dts
153
159
  * (the rslib `dtsBundledPackages` equivalent). Maps to tsdown's `deps.onlyBundle`
@@ -309,6 +315,12 @@ interface BuildTargetGroupsOptions {
309
315
  readonly extraPlugins?: ReadonlyArray<Plugin>;
310
316
  /** JSX transform settings forwarded to rolldown's inputOptions. */
311
317
  readonly jsx?: JsxConfig | undefined;
318
+ /**
319
+ * Compile-time global replacements forwarded to BOTH the JS and dts passes' `define`.
320
+ * Build-wide (shared by every entry partition); merged after the auto-injected
321
+ * `process.env.__PACKAGE_VERSION__` so a user key of the same name wins.
322
+ */
323
+ readonly define?: Record<string, string> | undefined;
312
324
  /**
313
325
  * Entry partitions with their own format/bundling, built into the same outDir after
314
326
  * the base entries. Used for per-entry format overrides (e.g. one CJS entry in an
@@ -112,7 +112,13 @@ function transformManifest(pkg, options = {}) {
112
112
  ...rest,
113
113
  private: isPrivate
114
114
  };
115
- if (result.exports) result.exports = transformExports(result.exports, options.dual ?? false);
115
+ if (result.exports !== void 0 && result.exports !== null) {
116
+ const original = result.exports;
117
+ const transformed = transformExports(original, options.dual ?? false);
118
+ const asMap = typeof original === "string" ? { ".": transformed } : { ...transformed };
119
+ if (!("./package.json" in asMap)) asMap["./package.json"] = "./package.json";
120
+ result.exports = asMap;
121
+ }
116
122
  if (result.bin) result.bin = transformBin(result.bin);
117
123
  if (options.transform) result = options.transform(result);
118
124
  if (result.bin) result.bin = normalizeBinPaths(result.bin);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@savvy-web/tsdown-plugins",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "description": "Interface-only tsdown/rolldown plugin pack powering @savvy-web/bundler",
6
6
  "homepage": "https://github.com/savvy-web/systems/tree/main/packages/tsdown-plugins",
@@ -24,7 +24,8 @@
24
24
  ".": {
25
25
  "types": "./index.d.ts",
26
26
  "import": "./index.js"
27
- }
27
+ },
28
+ "./package.json": "./package.json"
28
29
  },
29
30
  "dependencies": {
30
31
  "@effect/platform-node": "^0.106.0",
@@ -34,7 +35,7 @@
34
35
  "deep-equal": "^2.2.3",
35
36
  "json-schema-effect": "^0.2.1",
36
37
  "picocolors": "^1.1.1",
37
- "sort-package-json": "^3.6.1",
38
+ "sort-package-json": "^4.0.0",
38
39
  "std-env": "^4.1.0",
39
40
  "typescript": "^6.0.3",
40
41
  "workspaces-effect": "^1.2.0"