@w5s/tsdown-config 1.0.0-alpha.4 → 1.0.0-alpha.5

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.
@@ -1,2 +1,8 @@
1
1
  import * as TsDown from 'tsdown';
2
+ /**
3
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
4
+ * Accepts either a config object or a function `(config, context) => config`.
5
+ *
6
+ * @param optionsOrFn
7
+ */
2
8
  export declare function defineConfig(optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn): TsDown.UserConfig | TsDown.UserConfigFn;
@@ -1,25 +1,12 @@
1
1
  import * as TsDown from 'tsdown';
2
2
  import { defaultConfig } from './defaultConfig.js';
3
- function toInt(value) {
4
- if (value == null)
5
- return undefined;
6
- const parsed = Number.parseInt(value);
7
- return Number.isNaN(parsed) ? undefined : parsed;
8
- }
9
- function withPackageDefine(config) {
10
- return {
11
- ...config,
12
- define: {
13
- __PACKAGE_NAME__: JSON.stringify(process.env['npm_package_name']),
14
- __PACKAGE_VERSION__: JSON.stringify(process.env['npm_package_version']),
15
- __PACKAGE_BUILD_NUMBER__: JSON.stringify(toInt(process.env['npm_package_build_number']) ??
16
- toInt(process.env['BUILD_NUMBER']) ??
17
- toInt(process.env['CI_BUILD_NUMBER']) ??
18
- Date.now()),
19
- ...config.define,
20
- },
21
- };
22
- }
3
+ import { withPackageDefine } from './withPackageDefine.js';
4
+ /**
5
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
6
+ * Accepts either a config object or a function `(config, context) => config`.
7
+ *
8
+ * @param optionsOrFn
9
+ */
23
10
  export function defineConfig(optionsOrFn) {
24
11
  // @ts-ignore
25
12
  return TsDown.defineConfig(typeof optionsOrFn === 'function'
@@ -0,0 +1,16 @@
1
+ import * as TsDown from 'tsdown';
2
+ /**
3
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
4
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * // Helper for shared library preset
9
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
10
+ *
11
+ * // in tsdown.config.ts
12
+ * export default defineConfig({ entry: ['src/index.ts'] });
13
+ * ```
14
+ * @param baseConfig The base configuration to merge with the package default.
15
+ */
16
+ export declare function defineConfigWith(baseConfig: TsDown.UserConfig): ((optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn) => TsDown.UserConfig | TsDown.UserConfigFn);
@@ -0,0 +1,26 @@
1
+ import * as TsDown from 'tsdown';
2
+ import { defaultConfig } from './defaultConfig.js';
3
+ import { withPackageDefine } from './withPackageDefine.js';
4
+ function mergeConfig(base, extension) {
5
+ return TsDown.mergeConfig(base, extension);
6
+ }
7
+ /**
8
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
9
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * // Helper for shared library preset
14
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
15
+ *
16
+ * // in tsdown.config.ts
17
+ * export default defineConfig({ entry: ['src/index.ts'] });
18
+ * ```
19
+ * @param baseConfig The base configuration to merge with the package default.
20
+ */
21
+ export function defineConfigWith(baseConfig) {
22
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
23
+ return (optionsOrFn) => TsDown.defineConfig(typeof optionsOrFn === 'function'
24
+ ? (config, context) => withPackageDefine(optionsOrFn(resolvedBaseConfig, context))
25
+ : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)));
26
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './defaultConfig.js';
2
2
  export * from './defineConfig.js';
3
+ export * from './defineConfigWith.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './defaultConfig.js';
2
2
  export * from './defineConfig.js';
3
+ export * from './defineConfigWith.js';
@@ -0,0 +1,2 @@
1
+ import type * as TsDown from 'tsdown';
2
+ export declare function withPackageDefine<T extends TsDown.UserConfig | TsDown.InlineConfig>(config: T): T;
@@ -0,0 +1,40 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ function toInt(value) {
4
+ if (value == null)
5
+ return undefined;
6
+ const parsed = Number.parseInt(value);
7
+ return Number.isNaN(parsed) ? undefined : parsed;
8
+ }
9
+ export function withPackageDefine(config) {
10
+ let packageJSON = undefined;
11
+ const cwd = config.cwd ?? process.cwd();
12
+ function getPackageJSON() {
13
+ if (packageJSON)
14
+ return packageJSON;
15
+ try {
16
+ packageJSON = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf8'));
17
+ return packageJSON;
18
+ }
19
+ catch {
20
+ return {};
21
+ }
22
+ }
23
+ // Ensure we do not stringify undefined values
24
+ function jsonSafeStringify(value) {
25
+ return JSON.stringify(value);
26
+ }
27
+ return {
28
+ ...config,
29
+ cwd,
30
+ define: {
31
+ __PACKAGE_NAME__: jsonSafeStringify(process.env['npm_package_name'] ?? getPackageJSON().name ?? ''),
32
+ __PACKAGE_VERSION__: jsonSafeStringify(process.env['npm_package_version'] ?? getPackageJSON().version ?? ''),
33
+ __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(toInt(process.env['npm_package_build_number']) ??
34
+ toInt(process.env['BUILD_NUMBER']) ??
35
+ toInt(process.env['CI_BUILD_NUMBER']) ??
36
+ Date.now()),
37
+ ...config.define,
38
+ },
39
+ };
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w5s/tsdown-config",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-alpha.5",
4
4
  "description": "Shared tsdown configuration",
5
5
  "keywords": [
6
6
  "typescript",
@@ -59,11 +59,11 @@
59
59
  }
60
60
  },
61
61
  "engines": {
62
- "node": ">=20.0.0"
62
+ "node": ">=22.0.0"
63
63
  },
64
64
  "publishConfig": {
65
65
  "access": "public"
66
66
  },
67
67
  "sideEffect": false,
68
- "gitHead": "4e72e6d3c7b204e0f08c3f44817d48ad0d44ea33"
68
+ "gitHead": "38cf5f5877af4c8a1c292b9b7039c5a11863fc00"
69
69
  }
@@ -1,29 +1,13 @@
1
1
  import * as TsDown from 'tsdown';
2
2
  import { defaultConfig } from './defaultConfig.js';
3
+ import { withPackageDefine } from './withPackageDefine.js';
3
4
 
4
- function toInt(value: string | undefined): number | undefined {
5
- if (value == null) return undefined;
6
- const parsed = Number.parseInt(value);
7
- return Number.isNaN(parsed) ? undefined : parsed;
8
- }
9
-
10
- function withPackageDefine<T extends TsDown.UserConfig | TsDown.InlineConfig>(config: T): T {
11
- return {
12
- ...config,
13
- define: {
14
- __PACKAGE_NAME__: JSON.stringify(process.env['npm_package_name']),
15
- __PACKAGE_VERSION__: JSON.stringify(process.env['npm_package_version']),
16
- __PACKAGE_BUILD_NUMBER__: JSON.stringify(
17
- toInt(process.env['npm_package_build_number']) ??
18
- toInt(process.env['BUILD_NUMBER']) ??
19
- toInt(process.env['CI_BUILD_NUMBER']) ??
20
- Date.now(),
21
- ),
22
- ...config.define,
23
- },
24
- };
25
- }
26
-
5
+ /**
6
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
7
+ * Accepts either a config object or a function `(config, context) => config`.
8
+ *
9
+ * @param optionsOrFn
10
+ */
27
11
  export function defineConfig(
28
12
  optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn,
29
13
  ): TsDown.UserConfig | TsDown.UserConfigFn {
@@ -0,0 +1,34 @@
1
+ import * as TsDown from 'tsdown';
2
+ import { defaultConfig } from './defaultConfig.js';
3
+ import { withPackageDefine } from './withPackageDefine.js';
4
+
5
+ function mergeConfig(base: TsDown.UserConfig, extension: TsDown.UserConfig): TsDown.UserConfig {
6
+ return TsDown.mergeConfig(base, extension);
7
+ }
8
+
9
+ /**
10
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
11
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * // Helper for shared library preset
16
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
17
+ *
18
+ * // in tsdown.config.ts
19
+ * export default defineConfig({ entry: ['src/index.ts'] });
20
+ * ```
21
+ * @param baseConfig The base configuration to merge with the package default.
22
+ */
23
+ export function defineConfigWith(baseConfig: TsDown.UserConfig): ((
24
+ optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn,
25
+ ) => TsDown.UserConfig | TsDown.UserConfigFn) {
26
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
27
+ return (optionsOrFn) =>
28
+ TsDown.defineConfig(
29
+ typeof optionsOrFn === 'function'
30
+ ? (config, context) =>
31
+ withPackageDefine(optionsOrFn(resolvedBaseConfig, context) as TsDown.UserConfig)
32
+ : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)),
33
+ ) as TsDown.UserConfig | TsDown.UserConfigFn;
34
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './defaultConfig.js';
2
2
  export * from './defineConfig.js';
3
+ export * from './defineConfigWith.js';
@@ -0,0 +1,50 @@
1
+ import type * as TsDown from 'tsdown';
2
+ import { readFileSync } from 'node:fs';
3
+ import path from 'node:path';
4
+
5
+ interface PackageJSON {
6
+ name?: string;
7
+ version?: string;
8
+ }
9
+
10
+ function toInt(value: string | undefined): number | undefined {
11
+ if (value == null) return undefined;
12
+ const parsed = Number.parseInt(value);
13
+ return Number.isNaN(parsed) ? undefined : parsed;
14
+ }
15
+
16
+ export function withPackageDefine<T extends TsDown.UserConfig | TsDown.InlineConfig>(config: T): T {
17
+ let packageJSON: PackageJSON | undefined = undefined;
18
+ const cwd = config.cwd ?? process.cwd();
19
+
20
+ function getPackageJSON() {
21
+ if (packageJSON) return packageJSON;
22
+ try {
23
+ packageJSON = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf8'));
24
+ return packageJSON as unknown as PackageJSON;
25
+ } catch {
26
+ return {};
27
+ }
28
+ }
29
+
30
+ // Ensure we do not stringify undefined values
31
+ function jsonSafeStringify(value: string | number): string {
32
+ return JSON.stringify(value);
33
+ }
34
+
35
+ return {
36
+ ...config,
37
+ cwd,
38
+ define: {
39
+ __PACKAGE_NAME__: jsonSafeStringify(process.env['npm_package_name'] ?? getPackageJSON().name ?? ''),
40
+ __PACKAGE_VERSION__: jsonSafeStringify(process.env['npm_package_version'] ?? getPackageJSON().version ?? ''),
41
+ __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(
42
+ toInt(process.env['npm_package_build_number']) ??
43
+ toInt(process.env['BUILD_NUMBER']) ??
44
+ toInt(process.env['CI_BUILD_NUMBER']) ??
45
+ Date.now(),
46
+ ),
47
+ ...config.define,
48
+ },
49
+ };
50
+ }
@@ -1 +0,0 @@
1
- {"root":["../src/defaultconfig.ts","../src/defineconfig.ts","../src/index.ts"],"version":"5.9.3"}