@w5s/tsdown-config 1.0.0-alpha.2 → 1.0.0-alpha.21

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
@@ -35,24 +35,25 @@ In the `.prettierrc.json` for your project
35
35
 
36
36
  <!-- AUTO-GENERATED-CONTENT:END -->
37
37
 
38
- Or in the `.prettierrc.js`, to be able to override rules
38
+ Or in the `tsdown.config.ts`, to be able to override rules
39
39
 
40
- <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=```js\nmodule.exports = {\n ...require('${name}'),\n // Override rules\n};\n```) -->
40
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=```js\nimport { defineConfig } from '${name}';\nexport default defineConfig({\n // Override rules\n});\n```) -->
41
41
 
42
42
  ```js
43
- module.exports = {
44
- ...require('@w5s/tsdown-config'),
43
+ import { defineConfig } from '@w5s/tsdown-config';
44
+
45
+ export default defineConfig({
45
46
  // Override rules
46
- };
47
+ });
47
48
  ```
48
49
 
49
50
  <!-- AUTO-GENERATED-CONTENT:END -->
50
51
 
51
52
  ## Requirements
52
53
 
53
- <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=Prettier: ${peerDependencies.prettier}&unknownTxt= ) -->
54
+ <!-- AUTO-GENERATED-CONTENT:START (PKG_JSON:template=TsDown: ${peerDependencies.tsdown}&unknownTxt= ) -->
54
55
 
55
- Prettier: 2.x
56
+ TsDown: 0.x
56
57
 
57
58
  <!-- AUTO-GENERATED-CONTENT:END -->
58
59
 
package/dist/index.d.ts CHANGED
@@ -1,2 +1,36 @@
1
- export * from './defaultConfig.js';
2
- export * from './defineConfig.js';
1
+ import { DtsOptions, Format, InlineConfig, OutExtensionContext, OutExtensionFactory, OutExtensionObject, Sourcemap, TsdownPlugin, TsdownPluginOption, UserConfig, UserConfig as UserConfig$1, UserConfigExport, UserConfigFn } from "tsdown";
2
+ //#region src/defaultConfig.d.ts
3
+ declare const defaultConfig: UserConfig$1;
4
+ //#endregion
5
+ //#region src/defineConfig.d.ts
6
+ /**
7
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
8
+ * Accepts either a config object or a function `(config, context) => config`.
9
+ *
10
+ * @param optionsOrFn
11
+ */
12
+ declare function defineConfig(optionsOrFn: UserConfig): UserConfig;
13
+ declare function defineConfig(optionsOrFn: UserConfigFn): UserConfigFn;
14
+ //#endregion
15
+ //#region src/defineConfigWith.d.ts
16
+ /**
17
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
18
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * // Helper for shared library preset
23
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
24
+ *
25
+ * // in tsdown.config.ts
26
+ * export default defineConfig({ entry: ['src/index.ts'] });
27
+ * ```
28
+ * @param baseConfig The base configuration to merge with the package default.
29
+ */
30
+ declare function defineConfigWith(baseConfig: UserConfig): {
31
+ (optionsOrFn: UserConfig): UserConfig;
32
+ (optionsOrFn: UserConfigFn): UserConfigFn;
33
+ };
34
+ //#endregion
35
+ export { type DtsOptions, type Format, type InlineConfig, type OutExtensionContext, type OutExtensionFactory, type OutExtensionObject, type Sourcemap, type TsdownPlugin, type TsdownPluginOption, type UserConfig, type UserConfigExport, type UserConfigFn, defaultConfig, defineConfig, defineConfigWith };
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/defaultConfig.ts","../src/defineConfig.ts","../src/defineConfigWith.ts"],"mappings":";;cAEa,eAAe;;;;;;;;;iBCWZ,aAAa,aAAa,aAAa;iBACvC,aAAa,aAAa,eAAe;;;;;;;;;;;;;;;;;iBCOzC,iBAAiB,YAAY;GAC1C,aAAa,aAAa;GAC1B,aAAa,eAAe"}
package/dist/index.js CHANGED
@@ -1,2 +1,84 @@
1
- export * from './defaultConfig.js';
2
- export * from './defineConfig.js';
1
+ import * as TsDown from "tsdown";
2
+ import { readFileSync } from "node:fs";
3
+ import path from "node:path";
4
+ //#region src/defaultConfig.ts
5
+ const defaultConfig = {
6
+ dts: true,
7
+ entry: [
8
+ "src/index.ts",
9
+ "!src/**/*.test.*",
10
+ "!src/**/*.spec.*",
11
+ "!**/__mocks__/**"
12
+ ],
13
+ format: ["esm"],
14
+ outExtensions({ format }) {
15
+ return { js: format === "es" ? ".js" : ".cjs" };
16
+ },
17
+ sourcemap: true,
18
+ tsconfig: "tsconfig.build.json"
19
+ };
20
+ //#endregion
21
+ //#region src/withPackageDefine.ts
22
+ function withPackageDefine(config) {
23
+ let packageJSON = void 0;
24
+ const cwd = config.cwd ?? process.cwd();
25
+ function getPackageJSON() {
26
+ if (packageJSON) return packageJSON;
27
+ try {
28
+ packageJSON = JSON.parse(readFileSync(path.join(cwd, "package.json"), "utf8"));
29
+ return packageJSON;
30
+ } catch {
31
+ return {};
32
+ }
33
+ }
34
+ function jsonSafeStringify(value) {
35
+ return JSON.stringify(value);
36
+ }
37
+ return {
38
+ ...config,
39
+ cwd,
40
+ define: {
41
+ __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(toInt(process.env["npm_package_build_number"]) ?? toInt(process.env["BUILD_NUMBER"]) ?? toInt(process.env["CI_BUILD_NUMBER"]) ?? Date.now()),
42
+ __PACKAGE_NAME__: jsonSafeStringify(process.env["npm_package_name"] ?? getPackageJSON().name ?? ""),
43
+ __PACKAGE_VERSION__: jsonSafeStringify(process.env["npm_package_version"] ?? getPackageJSON().version ?? ""),
44
+ ...config.define
45
+ }
46
+ };
47
+ }
48
+ function toInt(value) {
49
+ if (value == null) return void 0;
50
+ const parsed = Number.parseInt(value);
51
+ return Number.isNaN(parsed) ? void 0 : parsed;
52
+ }
53
+ //#endregion
54
+ //#region src/defineConfig.ts
55
+ function defineConfig(optionsOrFn) {
56
+ return TsDown.defineConfig(typeof optionsOrFn === "function" ? (config, context) => optionsOrFn(withPackageDefine(TsDown.mergeConfig(defaultConfig, config)), context) : withPackageDefine(TsDown.mergeConfig(defaultConfig, optionsOrFn)));
57
+ }
58
+ //#endregion
59
+ //#region src/defineConfigWith.ts
60
+ /**
61
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
62
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * // Helper for shared library preset
67
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
68
+ *
69
+ * // in tsdown.config.ts
70
+ * export default defineConfig({ entry: ['src/index.ts'] });
71
+ * ```
72
+ * @param baseConfig The base configuration to merge with the package default.
73
+ */
74
+ function defineConfigWith(baseConfig) {
75
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
76
+ return (optionsOrFn) => TsDown.defineConfig(typeof optionsOrFn === "function" ? (config, context) => withPackageDefine(optionsOrFn(resolvedBaseConfig, context)) : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)));
77
+ }
78
+ function mergeConfig(base, extension) {
79
+ return TsDown.mergeConfig(base, extension);
80
+ }
81
+ //#endregion
82
+ export { defaultConfig, defineConfig, defineConfigWith };
83
+
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/defaultConfig.ts","../src/withPackageDefine.ts","../src/defineConfig.ts","../src/defineConfigWith.ts"],"sourcesContent":["import type { UserConfig } from 'tsdown';\n\nexport const defaultConfig: UserConfig = {\n dts: true,\n entry: ['src/index.ts', '!src/**/*.test.*', '!src/**/*.spec.*', '!**/__mocks__/**'],\n format: ['esm'],\n outExtensions({ format }) {\n return {\n js: format === 'es' ? '.js' : '.cjs',\n };\n },\n sourcemap: true,\n // clean: true,\n // treeshake: 'recommended',\n // splitting: false,\n tsconfig: 'tsconfig.build.json',\n};\n","import type * as TsDown from 'tsdown';\n\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\n\ninterface PackageJSON {\n name?: string;\n version?: string;\n}\n\nexport function withPackageDefine<T extends TsDown.InlineConfig | TsDown.UserConfig>(config: T): T {\n let packageJSON: PackageJSON | undefined = undefined;\n const cwd = config.cwd ?? process.cwd();\n\n function getPackageJSON() {\n if (packageJSON) return packageJSON;\n try {\n packageJSON = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf8'));\n return packageJSON as unknown as PackageJSON;\n } catch {\n return {};\n }\n }\n\n // Ensure we do not stringify undefined values\n function jsonSafeStringify(value: number | string): string {\n return JSON.stringify(value);\n }\n\n return {\n ...config,\n cwd,\n define: {\n __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(\n toInt(process.env['npm_package_build_number']) ??\n toInt(process.env['BUILD_NUMBER']) ??\n toInt(process.env['CI_BUILD_NUMBER']) ??\n Date.now(),\n ),\n __PACKAGE_NAME__: jsonSafeStringify(process.env['npm_package_name'] ?? getPackageJSON().name ?? ''),\n __PACKAGE_VERSION__: jsonSafeStringify(process.env['npm_package_version'] ?? getPackageJSON().version ?? ''),\n ...config.define,\n },\n };\n}\n\nfunction toInt(value: string | undefined): number | undefined {\n if (value == null) return undefined;\n const parsed = Number.parseInt(value);\n return Number.isNaN(parsed) ? undefined : parsed;\n}\n","import * as TsDown from 'tsdown';\n\nimport type { UserConfig, UserConfigFn } from './types.js';\n\nimport { defaultConfig } from './defaultConfig.js';\nimport { withPackageDefine } from './withPackageDefine.js';\n\n/**\n * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).\n * Accepts either a config object or a function `(config, context) => config`.\n *\n * @param optionsOrFn\n */\nexport function defineConfig(optionsOrFn: UserConfig): UserConfig;\nexport function defineConfig(optionsOrFn: UserConfigFn): UserConfigFn;\nexport function defineConfig(optionsOrFn: UserConfig | UserConfigFn): UserConfig | UserConfigFn {\n // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.\n return TsDown.defineConfig(\n typeof optionsOrFn === 'function'\n ? (config, context) => optionsOrFn(withPackageDefine(TsDown.mergeConfig(defaultConfig, config)), context)\n : withPackageDefine(TsDown.mergeConfig(defaultConfig, optionsOrFn)),\n );\n}\n","import * as TsDown from 'tsdown';\n\nimport type { UserConfig, UserConfigFn } from './types.js';\n\nimport { defaultConfig } from './defaultConfig.js';\nimport { withPackageDefine } from './withPackageDefine.js';\n\n/**\n * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.\n * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.\n *\n * @example\n * ```ts\n * // Helper for shared library preset\n * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });\n *\n * // in tsdown.config.ts\n * export default defineConfig({ entry: ['src/index.ts'] });\n * ```\n * @param baseConfig The base configuration to merge with the package default.\n */\nexport function defineConfigWith(baseConfig: UserConfig): {\n (optionsOrFn: UserConfig): UserConfig;\n (optionsOrFn: UserConfigFn): UserConfigFn;\n} {\n const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);\n // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.\n return (optionsOrFn) =>\n TsDown.defineConfig(\n typeof optionsOrFn === 'function'\n ? (config, context) =>\n withPackageDefine(optionsOrFn(resolvedBaseConfig, context) as UserConfig)\n : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)),\n ) as UserConfig | UserConfigFn;\n}\n\nfunction mergeConfig(base: UserConfig, extension: UserConfig): UserConfig {\n return TsDown.mergeConfig(base, extension);\n}\n"],"mappings":";;;;AAEA,MAAa,gBAA4B;CACvC,KAAK;CACL,OAAO;EAAC;EAAgB;EAAoB;EAAoB;CAAkB;CAClF,QAAQ,CAAC,KAAK;CACd,cAAc,EAAE,UAAU;EACxB,OAAO,EACL,IAAI,WAAW,OAAO,QAAQ,OAChC;CACF;CACA,WAAW;CAIX,UAAU;AACZ;;;ACNA,SAAgB,kBAAqE,QAAc;CACjG,IAAI,cAAuC,KAAA;CAC3C,MAAM,MAAM,OAAO,OAAO,QAAQ,IAAI;CAEtC,SAAS,iBAAiB;EACxB,IAAI,aAAa,OAAO;EACxB,IAAI;GACF,cAAc,KAAK,MAAM,aAAa,KAAK,KAAK,KAAK,cAAc,GAAG,MAAM,CAAC;GAC7E,OAAO;EACT,QAAQ;GACN,OAAO,CAAC;EACV;CACF;CAGA,SAAS,kBAAkB,OAAgC;EACzD,OAAO,KAAK,UAAU,KAAK;CAC7B;CAEA,OAAO;EACL,GAAG;EACH;EACA,QAAQ;GACN,0BAA0B,kBACxB,MAAM,QAAQ,IAAI,2BAA2B,KAC7C,MAAM,QAAQ,IAAI,eAAe,KACjC,MAAM,QAAQ,IAAI,kBAAkB,KACpC,KAAK,IAAI,CACX;GACA,kBAAkB,kBAAkB,QAAQ,IAAI,uBAAuB,eAAe,CAAC,CAAC,QAAQ,EAAE;GAClG,qBAAqB,kBAAkB,QAAQ,IAAI,0BAA0B,eAAe,CAAC,CAAC,WAAW,EAAE;GAC3G,GAAG,OAAO;EACZ;CACF;AACF;AAEA,SAAS,MAAM,OAA+C;CAC5D,IAAI,SAAS,MAAM,OAAO,KAAA;CAC1B,MAAM,SAAS,OAAO,SAAS,KAAK;CACpC,OAAO,OAAO,MAAM,MAAM,IAAI,KAAA,IAAY;AAC5C;;;ACnCA,SAAgB,aAAa,aAAmE;CAE9F,OAAO,OAAO,aACZ,OAAO,gBAAgB,cAClB,QAAQ,YAAY,YAAY,kBAAkB,OAAO,YAAY,eAAe,MAAM,CAAC,GAAG,OAAO,IACtG,kBAAkB,OAAO,YAAY,eAAe,WAAW,CAAC,CACtE;AACF;;;;;;;;;;;;;;;;;ACDA,SAAgB,iBAAiB,YAG/B;CACA,MAAM,qBAAqB,YAAY,eAAe,UAAU;CAEhE,QAAQ,gBACN,OAAO,aACL,OAAO,gBAAgB,cAClB,QAAQ,YACP,kBAAkB,YAAY,oBAAoB,OAAO,CAAe,IAC1E,kBAAkB,YAAY,oBAAoB,WAAW,CAAC,CACpE;AACJ;AAEA,SAAS,YAAY,MAAkB,WAAmC;CACxE,OAAO,OAAO,YAAY,MAAM,SAAS;AAC3C"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@w5s/tsdown-config",
3
- "version": "1.0.0-alpha.2",
4
- "description": "tsdown shared configuration",
3
+ "version": "1.0.0-alpha.21",
4
+ "description": "Shared tsdown configuration",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "config",
@@ -14,39 +14,25 @@
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
17
- "url": "git@github.com:w5s/project-config.git",
17
+ "url": "git+https://github.com/w5s/project-config.git",
18
18
  "directory": "packages/tsdown-config"
19
19
  },
20
20
  "license": "MIT",
21
21
  "author": "Julien Polo <julien.polo@gmail.com>",
22
22
  "type": "module",
23
23
  "exports": {
24
- ".": {
25
- "types": "./dist/index.d.ts",
26
- "default": "./dist/index.js"
27
- },
28
- "./dist/*": "./dist/*"
24
+ "./package.json": "./package.json",
25
+ ".": "./dist/index.js",
26
+ "./internal/*": null
29
27
  },
30
28
  "files": [
31
29
  "dist/",
32
30
  "src/",
33
31
  "index.js",
34
32
  "index.d.ts",
35
- "!*.d.ts.map",
36
33
  "!**/*.spec.*",
37
34
  "!**/__tests__/**"
38
35
  ],
39
- "tsdown": {
40
- "entry": [
41
- "src/index.ts"
42
- ],
43
- "sourcemap": true,
44
- "format": [
45
- "esm"
46
- ],
47
- "dts": true,
48
- "clean": true
49
- },
50
36
  "scripts": {
51
37
  "postpack": "clean-package restore"
52
38
  },
@@ -59,11 +45,11 @@
59
45
  }
60
46
  },
61
47
  "engines": {
62
- "node": ">=20.0.0"
48
+ "node": ">=22.0.0"
63
49
  },
64
50
  "publishConfig": {
65
51
  "access": "public"
66
52
  },
67
53
  "sideEffect": false,
68
- "gitHead": "202a665e3ce20aabf1eff51af6b0eb013c179e0a"
54
+ "gitHead": "94f5cdc797d9115b31f8af85da85fb5d9e509562"
69
55
  }
@@ -1,17 +1,17 @@
1
1
  import type { UserConfig } from 'tsdown';
2
2
 
3
3
  export const defaultConfig: UserConfig = {
4
+ dts: true,
4
5
  entry: ['src/index.ts', '!src/**/*.test.*', '!src/**/*.spec.*', '!**/__mocks__/**'],
5
- sourcemap: true,
6
6
  format: ['esm'],
7
- dts: true,
8
- // clean: true,
9
- // treeshake: 'recommended',
10
- // splitting: false,
11
- tsconfig: 'tsconfig.build.json',
12
7
  outExtensions({ format }) {
13
8
  return {
14
9
  js: format === 'es' ? '.js' : '.cjs',
15
10
  };
16
11
  },
12
+ sourcemap: true,
13
+ // clean: true,
14
+ // treeshake: 'recommended',
15
+ // splitting: false,
16
+ tsconfig: 'tsconfig.build.json',
17
17
  };
@@ -1,33 +1,20 @@
1
1
  import * as TsDown from 'tsdown';
2
- import { defaultConfig } from './defaultConfig.js';
3
2
 
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
- }
3
+ import type { UserConfig, UserConfigFn } from './types.js';
9
4
 
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
- }
5
+ import { defaultConfig } from './defaultConfig.js';
6
+ import { withPackageDefine } from './withPackageDefine.js';
26
7
 
27
- export function defineConfig(
28
- optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn,
29
- ): TsDown.UserConfig | TsDown.UserConfigFn {
30
- // @ts-ignore
8
+ /**
9
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
10
+ * Accepts either a config object or a function `(config, context) => config`.
11
+ *
12
+ * @param optionsOrFn
13
+ */
14
+ export function defineConfig(optionsOrFn: UserConfig): UserConfig;
15
+ export function defineConfig(optionsOrFn: UserConfigFn): UserConfigFn;
16
+ export function defineConfig(optionsOrFn: UserConfig | UserConfigFn): UserConfig | UserConfigFn {
17
+ // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.
31
18
  return TsDown.defineConfig(
32
19
  typeof optionsOrFn === 'function'
33
20
  ? (config, context) => optionsOrFn(withPackageDefine(TsDown.mergeConfig(defaultConfig, config)), context)
@@ -0,0 +1,39 @@
1
+ import * as TsDown from 'tsdown';
2
+
3
+ import type { UserConfig, UserConfigFn } from './types.js';
4
+
5
+ import { defaultConfig } from './defaultConfig.js';
6
+ import { withPackageDefine } from './withPackageDefine.js';
7
+
8
+ /**
9
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
10
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // Helper for shared library preset
15
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
16
+ *
17
+ * // in tsdown.config.ts
18
+ * export default defineConfig({ entry: ['src/index.ts'] });
19
+ * ```
20
+ * @param baseConfig The base configuration to merge with the package default.
21
+ */
22
+ export function defineConfigWith(baseConfig: UserConfig): {
23
+ (optionsOrFn: UserConfig): UserConfig;
24
+ (optionsOrFn: UserConfigFn): UserConfigFn;
25
+ } {
26
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
27
+ // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.
28
+ return (optionsOrFn) =>
29
+ TsDown.defineConfig(
30
+ typeof optionsOrFn === 'function'
31
+ ? (config, context) =>
32
+ withPackageDefine(optionsOrFn(resolvedBaseConfig, context) as UserConfig)
33
+ : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)),
34
+ ) as UserConfig | UserConfigFn;
35
+ }
36
+
37
+ function mergeConfig(base: UserConfig, extension: UserConfig): UserConfig {
38
+ return TsDown.mergeConfig(base, extension);
39
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './defaultConfig.js';
2
2
  export * from './defineConfig.js';
3
+ export * from './defineConfigWith.js';
4
+ export type * from './types.js';
package/src/types.ts ADDED
@@ -0,0 +1,14 @@
1
+ export type {
2
+ DtsOptions,
3
+ Format,
4
+ InlineConfig,
5
+ OutExtensionContext,
6
+ OutExtensionFactory,
7
+ OutExtensionObject,
8
+ Sourcemap,
9
+ TsdownPlugin,
10
+ TsdownPluginOption,
11
+ UserConfig,
12
+ UserConfigExport,
13
+ UserConfigFn,
14
+ } from 'tsdown';
@@ -0,0 +1,51 @@
1
+ import type * as TsDown from 'tsdown';
2
+
3
+ import { readFileSync } from 'node:fs';
4
+ import path from 'node:path';
5
+
6
+ interface PackageJSON {
7
+ name?: string;
8
+ version?: string;
9
+ }
10
+
11
+ export function withPackageDefine<T extends TsDown.InlineConfig | TsDown.UserConfig>(config: T): T {
12
+ let packageJSON: PackageJSON | undefined = undefined;
13
+ const cwd = config.cwd ?? process.cwd();
14
+
15
+ function getPackageJSON() {
16
+ if (packageJSON) return packageJSON;
17
+ try {
18
+ packageJSON = JSON.parse(readFileSync(path.join(cwd, 'package.json'), 'utf8'));
19
+ return packageJSON as unknown as PackageJSON;
20
+ } catch {
21
+ return {};
22
+ }
23
+ }
24
+
25
+ // Ensure we do not stringify undefined values
26
+ function jsonSafeStringify(value: number | string): string {
27
+ return JSON.stringify(value);
28
+ }
29
+
30
+ return {
31
+ ...config,
32
+ cwd,
33
+ define: {
34
+ __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(
35
+ toInt(process.env['npm_package_build_number']) ??
36
+ toInt(process.env['BUILD_NUMBER']) ??
37
+ toInt(process.env['CI_BUILD_NUMBER']) ??
38
+ Date.now(),
39
+ ),
40
+ __PACKAGE_NAME__: jsonSafeStringify(process.env['npm_package_name'] ?? getPackageJSON().name ?? ''),
41
+ __PACKAGE_VERSION__: jsonSafeStringify(process.env['npm_package_version'] ?? getPackageJSON().version ?? ''),
42
+ ...config.define,
43
+ },
44
+ };
45
+ }
46
+
47
+ function toInt(value: string | undefined): number | undefined {
48
+ if (value == null) return undefined;
49
+ const parsed = Number.parseInt(value);
50
+ return Number.isNaN(parsed) ? undefined : parsed;
51
+ }
@@ -1,2 +0,0 @@
1
- import type { UserConfig } from 'tsdown';
2
- export declare const defaultConfig: UserConfig;
@@ -1,15 +0,0 @@
1
- export const defaultConfig = {
2
- entry: ['src/index.ts', '!src/**/*.test.*', '!src/**/*.spec.*', '!**/__mocks__/**'],
3
- sourcemap: true,
4
- format: ['esm'],
5
- dts: true,
6
- // clean: true,
7
- // treeshake: 'recommended',
8
- // splitting: false,
9
- tsconfig: 'tsconfig.build.json',
10
- outExtensions({ format }) {
11
- return {
12
- js: format === 'es' ? '.js' : '.cjs',
13
- };
14
- },
15
- };
@@ -1,2 +0,0 @@
1
- import * as TsDown from 'tsdown';
2
- export declare function defineConfig(optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn): TsDown.UserConfig | TsDown.UserConfigFn;
@@ -1,28 +0,0 @@
1
- import * as TsDown from 'tsdown';
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
- }
23
- export function defineConfig(optionsOrFn) {
24
- // @ts-ignore
25
- return TsDown.defineConfig(typeof optionsOrFn === 'function'
26
- ? (config, context) => optionsOrFn(withPackageDefine(TsDown.mergeConfig(defaultConfig, config)), context)
27
- : withPackageDefine(TsDown.mergeConfig(defaultConfig, optionsOrFn)));
28
- }
@@ -1 +0,0 @@
1
- {"root":["../src/defaultconfig.ts","../src/defineconfig.ts","../src/index.ts"],"version":"5.9.3"}