@w5s/tsdown-config 1.0.0-alpha.1 → 1.0.0-alpha.12

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,37 @@
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
+
3
+ //#region src/defaultConfig.d.ts
4
+ declare const defaultConfig: UserConfig$1;
5
+ //#endregion
6
+ //#region src/defineConfig.d.ts
7
+ /**
8
+ * Define a tsdown configuration with the package default (entry, format, dts, package defines, etc.).
9
+ * Accepts either a config object or a function `(config, context) => config`.
10
+ *
11
+ * @param optionsOrFn
12
+ */
13
+ declare function defineConfig(optionsOrFn: UserConfig): UserConfig;
14
+ declare function defineConfig(optionsOrFn: UserConfigFn): UserConfigFn;
15
+ //#endregion
16
+ //#region src/defineConfigWith.d.ts
17
+ /**
18
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
19
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * // Helper for shared library preset
24
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
25
+ *
26
+ * // in tsdown.config.ts
27
+ * export default defineConfig({ entry: ['src/index.ts'] });
28
+ * ```
29
+ * @param baseConfig The base configuration to merge with the package default.
30
+ */
31
+ declare function defineConfigWith(baseConfig: UserConfig): {
32
+ (optionsOrFn: UserConfig): UserConfig;
33
+ (optionsOrFn: UserConfigFn): UserConfigFn;
34
+ };
35
+ //#endregion
36
+ 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 };
37
+ //# 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,aAAA,EAAe,YAc3B;;;;;AAdD;;;;iBCSgB,YAAA,CAAa,WAAA,EAAa,UAAA,GAAa,UAAU;AAAA,iBACjD,YAAA,CAAa,WAAA,EAAa,YAAA,GAAe,YAAY;;;;;ADVrE;;;;AAcC;;;;ACLD;;;;iBCYgB,gBAAA,CAAiB,UAAA,EAAY,UAAA;EAAA,CAC1C,WAAA,EAAa,UAAA,GAAa,UAAA;EAAA,CAC1B,WAAA,EAAa,YAAA,GAAe,YAAA;AAAA"}
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
+ entry: [
7
+ "src/index.ts",
8
+ "!src/**/*.test.*",
9
+ "!src/**/*.spec.*",
10
+ "!**/__mocks__/**"
11
+ ],
12
+ sourcemap: true,
13
+ format: ["esm"],
14
+ dts: true,
15
+ tsconfig: "tsconfig.build.json",
16
+ outExtensions({ format }) {
17
+ return { js: format === "es" ? ".js" : ".cjs" };
18
+ }
19
+ };
20
+ //#endregion
21
+ //#region src/withPackageDefine.ts
22
+ function toInt(value) {
23
+ if (value == null) return void 0;
24
+ const parsed = Number.parseInt(value);
25
+ return Number.isNaN(parsed) ? void 0 : parsed;
26
+ }
27
+ function withPackageDefine(config) {
28
+ let packageJSON = void 0;
29
+ const cwd = config.cwd ?? process.cwd();
30
+ function getPackageJSON() {
31
+ if (packageJSON) return packageJSON;
32
+ try {
33
+ packageJSON = JSON.parse(readFileSync(path.join(cwd, "package.json"), "utf8"));
34
+ return packageJSON;
35
+ } catch {
36
+ return {};
37
+ }
38
+ }
39
+ function jsonSafeStringify(value) {
40
+ return JSON.stringify(value);
41
+ }
42
+ return {
43
+ ...config,
44
+ cwd,
45
+ define: {
46
+ __PACKAGE_NAME__: jsonSafeStringify(process.env["npm_package_name"] ?? getPackageJSON().name ?? ""),
47
+ __PACKAGE_VERSION__: jsonSafeStringify(process.env["npm_package_version"] ?? getPackageJSON().version ?? ""),
48
+ __PACKAGE_BUILD_NUMBER__: jsonSafeStringify(toInt(process.env["npm_package_build_number"]) ?? toInt(process.env["BUILD_NUMBER"]) ?? toInt(process.env["CI_BUILD_NUMBER"]) ?? Date.now()),
49
+ ...config.define
50
+ }
51
+ };
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
+ function mergeConfig(base, extension) {
61
+ return TsDown.mergeConfig(base, extension);
62
+ }
63
+ /**
64
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
65
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * // Helper for shared library preset
70
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
71
+ *
72
+ * // in tsdown.config.ts
73
+ * export default defineConfig({ entry: ['src/index.ts'] });
74
+ * ```
75
+ * @param baseConfig The base configuration to merge with the package default.
76
+ */
77
+ function defineConfigWith(baseConfig) {
78
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
79
+ return (optionsOrFn) => TsDown.defineConfig(typeof optionsOrFn === "function" ? (config, context) => withPackageDefine(optionsOrFn(resolvedBaseConfig, context)) : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)));
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 entry: ['src/index.ts', '!src/**/*.test.*', '!src/**/*.spec.*', '!**/__mocks__/**'],\n sourcemap: true,\n format: ['esm'],\n dts: true,\n // clean: true,\n // treeshake: 'recommended',\n // splitting: false,\n tsconfig: 'tsconfig.build.json',\n outExtensions({ format }) {\n return {\n js: format === 'es' ? '.js' : '.cjs',\n };\n },\n};\n","import type * as TsDown from 'tsdown';\nimport { readFileSync } from 'node:fs';\nimport path from 'node:path';\n\ninterface PackageJSON {\n name?: string;\n version?: string;\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\nexport function withPackageDefine<T extends TsDown.UserConfig | TsDown.InlineConfig>(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: string | number): string {\n return JSON.stringify(value);\n }\n\n return {\n ...config,\n cwd,\n define: {\n __PACKAGE_NAME__: jsonSafeStringify(process.env['npm_package_name'] ?? getPackageJSON().name ?? ''),\n __PACKAGE_VERSION__: jsonSafeStringify(process.env['npm_package_version'] ?? getPackageJSON().version ?? ''),\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 ...config.define,\n },\n };\n}\n","import * as TsDown from 'tsdown';\nimport type { UserConfig, UserConfigFn } from './types.js';\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';\nimport type { UserConfig, UserConfigFn } from './types.js';\nimport { defaultConfig } from './defaultConfig.js';\nimport { withPackageDefine } from './withPackageDefine.js';\n\nfunction mergeConfig(base: UserConfig, extension: UserConfig): UserConfig {\n return TsDown.mergeConfig(base, extension);\n}\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"],"mappings":";;;;AAEA,MAAa,gBAA4B;CACvC,OAAO;EAAC;EAAgB;EAAoB;EAAoB;CAAkB;CAClF,WAAW;CACX,QAAQ,CAAC,KAAK;CACd,KAAK;CAIL,UAAU;CACV,cAAc,EAAE,UAAU;EACxB,OAAO,EACL,IAAI,WAAW,OAAO,QAAQ,OAChC;CACF;AACF;;;ACPA,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;AAEA,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,kBAAkB,kBAAkB,QAAQ,IAAI,uBAAuB,eAAe,CAAC,CAAC,QAAQ,EAAE;GAClG,qBAAqB,kBAAkB,QAAQ,IAAI,0BAA0B,eAAe,CAAC,CAAC,WAAW,EAAE;GAC3G,0BAA0B,kBACxB,MAAM,QAAQ,IAAI,2BAA2B,KAC7C,MAAM,QAAQ,IAAI,eAAe,KACjC,MAAM,QAAQ,IAAI,kBAAkB,KACpC,KAAK,IAAI,CACX;GACA,GAAG,OAAO;EACZ;CACF;AACF;;;ACpCA,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;;;ACfA,SAAS,YAAY,MAAkB,WAAmC;CACxE,OAAO,OAAO,YAAY,MAAM,SAAS;AAC3C;;;;;;;;;;;;;;;AAgBA,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"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@w5s/tsdown-config",
3
- "version": "1.0.0-alpha.1",
4
- "description": "tsdown shared configuration",
3
+ "version": "1.0.0-alpha.12",
4
+ "description": "Shared tsdown configuration",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "config",
@@ -14,44 +14,30 @@
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
  },
53
39
  "peerDependencies": {
54
- "tsdown": "8.x"
40
+ "tsdown": "0.x"
55
41
  },
56
42
  "peerDependenciesMeta": {
57
43
  "tsdown": {
@@ -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": "8d96574cd89109fa8268a2c77601337ad871ef1e"
54
+ "gitHead": "fcde4af12d29afdb46ed396ef832c36a399ed057"
69
55
  }
@@ -1,33 +1,18 @@
1
1
  import * as TsDown from 'tsdown';
2
+ import type { UserConfig, UserConfigFn } from './types.js';
2
3
  import { defaultConfig } from './defaultConfig.js';
4
+ import { withPackageDefine } from './withPackageDefine.js';
3
5
 
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
-
27
- export function defineConfig(
28
- optionsOrFn: TsDown.UserConfig | TsDown.UserConfigFn,
29
- ): TsDown.UserConfig | TsDown.UserConfigFn {
30
- // @ts-ignore
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
+ export function defineConfig(optionsOrFn: UserConfig): UserConfig;
13
+ export function defineConfig(optionsOrFn: UserConfigFn): UserConfigFn;
14
+ export function defineConfig(optionsOrFn: UserConfig | UserConfigFn): UserConfig | UserConfigFn {
15
+ // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.
31
16
  return TsDown.defineConfig(
32
17
  typeof optionsOrFn === 'function'
33
18
  ? (config, context) => optionsOrFn(withPackageDefine(TsDown.mergeConfig(defaultConfig, config)), context)
@@ -0,0 +1,37 @@
1
+ import * as TsDown from 'tsdown';
2
+ import type { UserConfig, UserConfigFn } from './types.js';
3
+ import { defaultConfig } from './defaultConfig.js';
4
+ import { withPackageDefine } from './withPackageDefine.js';
5
+
6
+ function mergeConfig(base: UserConfig, extension: UserConfig): UserConfig {
7
+ return TsDown.mergeConfig(base, extension);
8
+ }
9
+
10
+ /**
11
+ * Returns a `defineConfig`-like function that uses a custom base config instead of the package default.
12
+ * Use this when you have a shared base (e.g. a library preset) and want to reuse it across multiple packages.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // Helper for shared library preset
17
+ * export const defineConfig = defineConfigWith({ format: ['esm', 'cjs'] });
18
+ *
19
+ * // in tsdown.config.ts
20
+ * export default defineConfig({ entry: ['src/index.ts'] });
21
+ * ```
22
+ * @param baseConfig The base configuration to merge with the package default.
23
+ */
24
+ export function defineConfigWith(baseConfig: UserConfig): {
25
+ (optionsOrFn: UserConfig): UserConfig;
26
+ (optionsOrFn: UserConfigFn): UserConfigFn;
27
+ } {
28
+ const resolvedBaseConfig = mergeConfig(defaultConfig, baseConfig);
29
+ // @ts-ignore tsdown's overloads do not narrow cleanly after wrapping function configs.
30
+ return (optionsOrFn) =>
31
+ TsDown.defineConfig(
32
+ typeof optionsOrFn === 'function'
33
+ ? (config, context) =>
34
+ withPackageDefine(optionsOrFn(resolvedBaseConfig, context) as UserConfig)
35
+ : withPackageDefine(mergeConfig(resolvedBaseConfig, optionsOrFn)),
36
+ ) as UserConfig | UserConfigFn;
37
+ }
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,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,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"}