@w5s/tsdown-config 1.0.0-alpha.4 → 1.0.0-alpha.6
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/dist/index.d.ts +37 -2
- package/dist/index.js +84 -2
- package/dist/index.js.map +1 -0
- package/package.json +4 -15
- package/src/defineConfig.ts +12 -27
- package/src/defineConfigWith.ts +37 -0
- package/src/index.ts +2 -0
- package/src/types.ts +14 -0
- package/src/withPackageDefine.ts +50 -0
- package/dist/defaultConfig.d.ts +0 -2
- package/dist/defaultConfig.js +0 -15
- package/dist/defineConfig.d.ts +0 -2
- package/dist/defineConfig.js +0 -28
- package/dist/tsconfig.build.tsbuildinfo +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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,EAAE,QAAQ,EAAE;GAClG,qBAAqB,kBAAkB,QAAQ,IAAI,0BAA0B,eAAe,EAAE,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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@w5s/tsdown-config",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.6",
|
|
4
4
|
"description": "Shared tsdown configuration",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "git@github.com:w5s/project-config.git",
|
|
17
|
+
"url": "git+ssh@github.com:w5s/project-config.git",
|
|
18
18
|
"directory": "packages/tsdown-config"
|
|
19
19
|
},
|
|
20
20
|
"license": "MIT",
|
|
@@ -36,17 +36,6 @@
|
|
|
36
36
|
"!**/*.spec.*",
|
|
37
37
|
"!**/__tests__/**"
|
|
38
38
|
],
|
|
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
39
|
"scripts": {
|
|
51
40
|
"postpack": "clean-package restore"
|
|
52
41
|
},
|
|
@@ -59,11 +48,11 @@
|
|
|
59
48
|
}
|
|
60
49
|
},
|
|
61
50
|
"engines": {
|
|
62
|
-
"node": ">=
|
|
51
|
+
"node": ">=22.0.0"
|
|
63
52
|
},
|
|
64
53
|
"publishConfig": {
|
|
65
54
|
"access": "public"
|
|
66
55
|
},
|
|
67
56
|
"sideEffect": false,
|
|
68
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "ab7987a57772b2e7fb3ef7eb4cadb3cedd9cc9ae"
|
|
69
58
|
}
|
package/src/defineConfig.ts
CHANGED
|
@@ -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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
package/src/types.ts
ADDED
|
@@ -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
|
+
}
|
package/dist/defaultConfig.d.ts
DELETED
package/dist/defaultConfig.js
DELETED
|
@@ -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
|
-
};
|
package/dist/defineConfig.d.ts
DELETED
package/dist/defineConfig.js
DELETED
|
@@ -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"}
|