tsdown-stale-guard 0.0.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/LICENSE.md +21 -0
- package/README.md +139 -0
- package/dist/check-YFLy7mcd.mjs +117 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +31 -0
- package/dist/index.d.mts +66 -0
- package/dist/index.mjs +115 -0
- package/package.json +71 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-PRESENT Anthony Fu <https://github.com/antfu>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# tsdown-stale-guard
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![bundle][bundle-src]][bundle-href]
|
|
6
|
+
[![JSDocs][jsdocs-src]][jsdocs-href]
|
|
7
|
+
[![License][license-src]][license-href]
|
|
8
|
+
|
|
9
|
+
Build freshness validation for [tsdown](https://github.com/rolldown/tsdown). Records hashes of all files involved in a build, enabling fast up-to-date checks without re-building.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **tsdown/Rolldown plugin** — automatically tracks source files, output files, config, and package lock file
|
|
14
|
+
- **Composite hash** — single hash for quick freshness checks
|
|
15
|
+
- **Find-up search** — detects lock files and configs in monorepo setups
|
|
16
|
+
- **CLI** — `tsdown-stale-guard check` for CI pipelines
|
|
17
|
+
- **Programmatic API** — `checkBuildFreshness()` for tool integrations
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm i tsdown-stale-guard
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### As a tsdown Plugin
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// tsdown.config.ts
|
|
31
|
+
import { defineConfig } from 'tsdown'
|
|
32
|
+
import { TsdownStaleGuard } from 'tsdown-stale-guard'
|
|
33
|
+
|
|
34
|
+
export default defineConfig({
|
|
35
|
+
entry: ['src/index.ts'],
|
|
36
|
+
plugins: [
|
|
37
|
+
TsdownStaleGuard(),
|
|
38
|
+
],
|
|
39
|
+
})
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
After building, a hash file will be generated at `node_modules/.cache/tsdown-stale-guard/hash.yaml`. Since it lives inside `node_modules`, it does not need to be gitignored.
|
|
43
|
+
|
|
44
|
+
Example of the generated hash file:
|
|
45
|
+
|
|
46
|
+
```yaml
|
|
47
|
+
version: 1
|
|
48
|
+
hash: sha256:abc123...
|
|
49
|
+
|
|
50
|
+
config:
|
|
51
|
+
tsdown.config.ts: sha256:def456...
|
|
52
|
+
|
|
53
|
+
lockfile:
|
|
54
|
+
../../pnpm-lock.yaml: sha256:789abc...
|
|
55
|
+
|
|
56
|
+
sources:
|
|
57
|
+
src/index.ts: sha256:111111...
|
|
58
|
+
src/utils.ts: sha256:222222...
|
|
59
|
+
|
|
60
|
+
outputs:
|
|
61
|
+
dist/index.mjs: sha256:aaaaaa...
|
|
62
|
+
dist/index.d.mts: sha256:bbbbbb...
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Plugin Options
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
TsdownStaleGuard({
|
|
69
|
+
hashFile: 'node_modules/.cache/tsdown-stale-guard/hash.yaml', // hash file path (default)
|
|
70
|
+
root: process.cwd(), // root directory (default)
|
|
71
|
+
hashOutputs: true, // hash output files (default)
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### CLI
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# Check if the build is up to date
|
|
79
|
+
tsdown-stale-guard check
|
|
80
|
+
|
|
81
|
+
# Use a custom hash file path
|
|
82
|
+
tsdown-stale-guard check --hash-file custom.hash.yaml
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Exit code `0` if fresh, `1` if stale.
|
|
86
|
+
|
|
87
|
+
### Programmatic API
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { checkBuildFreshness } from 'tsdown-stale-guard'
|
|
91
|
+
|
|
92
|
+
const result = await checkBuildFreshness()
|
|
93
|
+
|
|
94
|
+
if (result.fresh) {
|
|
95
|
+
console.log('Build is up to date')
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
for (const change of result.changes) {
|
|
99
|
+
console.log(`${change.type}: [${change.category}] ${change.file}`)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## How It Works
|
|
105
|
+
|
|
106
|
+
The plugin hooks into Rolldown's build pipeline:
|
|
107
|
+
|
|
108
|
+
1. **`transform`** — collects all source file paths during bundling
|
|
109
|
+
2. **`generateBundle`** — collects output file names
|
|
110
|
+
3. **`writeBundle`** — hashes all collected files plus the detected tsdown config and package lock file, then writes the hash file
|
|
111
|
+
|
|
112
|
+
The hash file includes a composite `hash` computed from all individual file hashes, enabling a single-comparison freshness check.
|
|
113
|
+
|
|
114
|
+
Package lock files (`pnpm-lock.yaml`, `yarn.lock`, `package-lock.json`, `bun.lockb`, `bun.lock`) and tsdown config files are found via find-up search, supporting monorepo setups where they may live in a parent directory.
|
|
115
|
+
|
|
116
|
+
## Sponsors
|
|
117
|
+
|
|
118
|
+
<p align="center">
|
|
119
|
+
<a href="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg">
|
|
120
|
+
<img src="https://cdn.jsdelivr.net/gh/antfu/static/sponsors.svg" alt="Sponsors"/>
|
|
121
|
+
</a>
|
|
122
|
+
</p>
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
[MIT](./LICENSE) License © [Anthony Fu](https://github.com/antfu)
|
|
127
|
+
|
|
128
|
+
<!-- Badges -->
|
|
129
|
+
|
|
130
|
+
[npm-version-src]: https://img.shields.io/npm/v/tsdown-stale-guard?style=flat&colorA=080f12&colorB=1fa669
|
|
131
|
+
[npm-version-href]: https://npmjs.com/package/tsdown-stale-guard
|
|
132
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/tsdown-stale-guard?style=flat&colorA=080f12&colorB=1fa669
|
|
133
|
+
[npm-downloads-href]: https://npmjs.com/package/tsdown-stale-guard
|
|
134
|
+
[bundle-src]: https://img.shields.io/bundlephobia/minzip/tsdown-stale-guard?style=flat&colorA=080f12&colorB=1fa669&label=minzip
|
|
135
|
+
[bundle-href]: https://bundlephobia.com/result?p=tsdown-stale-guard
|
|
136
|
+
[license-src]: https://img.shields.io/github/license/antfu/tsdown-stale-guard.svg?style=flat&colorA=080f12&colorB=1fa669
|
|
137
|
+
[license-href]: https://github.com/antfu/tsdown-stale-guard/blob/main/LICENSE
|
|
138
|
+
[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669
|
|
139
|
+
[jsdocs-href]: https://www.jsdocs.io/package/tsdown-stale-guard
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { dirname, relative, resolve } from "node:path";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
6
|
+
import { parse, stringify } from "yaml";
|
|
7
|
+
//#region src/hash.ts
|
|
8
|
+
async function hashFile(path) {
|
|
9
|
+
const content = await readFile(path);
|
|
10
|
+
return `sha256:${createHash("sha256").update(content).digest("hex")}`;
|
|
11
|
+
}
|
|
12
|
+
async function hashFiles(paths, root) {
|
|
13
|
+
return (await Promise.all(paths.map(async (p) => {
|
|
14
|
+
const hash = await hashFile(p);
|
|
15
|
+
return {
|
|
16
|
+
file: toForwardSlash(relative(root, p)),
|
|
17
|
+
hash
|
|
18
|
+
};
|
|
19
|
+
}))).sort((a, b) => a.file.localeCompare(b.file));
|
|
20
|
+
}
|
|
21
|
+
function computeCompositeHash(entries) {
|
|
22
|
+
const hash = createHash("sha256");
|
|
23
|
+
for (const entry of [...entries].sort((a, b) => a.file.localeCompare(b.file))) hash.update(`${entry.file}:${entry.hash}\n`);
|
|
24
|
+
return `sha256:${hash.digest("hex")}`;
|
|
25
|
+
}
|
|
26
|
+
function toForwardSlash(p) {
|
|
27
|
+
return p.replace(/\\/g, "/");
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/lockfile.ts
|
|
31
|
+
const HEADER = "# auto-generated by tsdown-stale-guard\n# DO NOT EDIT\n";
|
|
32
|
+
function serializeHashFile(data) {
|
|
33
|
+
const obj = {
|
|
34
|
+
version: data.version,
|
|
35
|
+
hash: data.hash
|
|
36
|
+
};
|
|
37
|
+
if (data.config) obj.config = { [data.config.file]: data.config.hash };
|
|
38
|
+
if (data.lockfile) obj.lockfile = { [data.lockfile.file]: data.lockfile.hash };
|
|
39
|
+
if (data.sources.length > 0) obj.sources = Object.fromEntries(data.sources.map((e) => [e.file, e.hash]));
|
|
40
|
+
if (data.outputs.length > 0) obj.outputs = Object.fromEntries(data.outputs.map((e) => [e.file, e.hash]));
|
|
41
|
+
return HEADER + stringify(obj, { lineWidth: 0 });
|
|
42
|
+
}
|
|
43
|
+
function parseHashFile(content) {
|
|
44
|
+
const obj = parse(content);
|
|
45
|
+
return {
|
|
46
|
+
version: obj.version,
|
|
47
|
+
hash: obj.hash,
|
|
48
|
+
config: parseSection(obj.config),
|
|
49
|
+
lockfile: parseSection(obj.lockfile),
|
|
50
|
+
sources: parseSectionList(obj.sources),
|
|
51
|
+
outputs: parseSectionList(obj.outputs)
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function parseSection(section) {
|
|
55
|
+
if (!section) return void 0;
|
|
56
|
+
const entries = Object.entries(section);
|
|
57
|
+
if (entries.length === 0) return void 0;
|
|
58
|
+
const [file, hash] = entries[0];
|
|
59
|
+
return {
|
|
60
|
+
file,
|
|
61
|
+
hash
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function parseSectionList(section) {
|
|
65
|
+
if (!section) return [];
|
|
66
|
+
return Object.entries(section).map(([file, hash]) => ({
|
|
67
|
+
file,
|
|
68
|
+
hash
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
async function readHashFile(path) {
|
|
72
|
+
return parseHashFile(await readFile(path, "utf-8"));
|
|
73
|
+
}
|
|
74
|
+
async function writeHashFile(path, data) {
|
|
75
|
+
await mkdir(dirname(path), { recursive: true });
|
|
76
|
+
await writeFile(path, serializeHashFile(data), "utf-8");
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/check.ts
|
|
80
|
+
const DEFAULT_HASH_FILE = "node_modules/.cache/tsdown-stale-guard/hash.yaml";
|
|
81
|
+
async function checkBuildFreshness(options = {}) {
|
|
82
|
+
const root = options.root || process.cwd();
|
|
83
|
+
const data = await readHashFile(resolve(root, options.hashFile || DEFAULT_HASH_FILE));
|
|
84
|
+
const changes = [];
|
|
85
|
+
if (data.config) await checkEntry(data.config, "config", root, changes);
|
|
86
|
+
if (data.lockfile) await checkEntry(data.lockfile, "lockfile", root, changes);
|
|
87
|
+
for (const entry of data.sources) await checkEntry(entry, "source", root, changes);
|
|
88
|
+
for (const entry of data.outputs) await checkEntry(entry, "output", root, changes);
|
|
89
|
+
const currentHash = computeCompositeHash([
|
|
90
|
+
...data.sources,
|
|
91
|
+
...data.outputs,
|
|
92
|
+
...data.config ? [data.config] : [],
|
|
93
|
+
...data.lockfile ? [data.lockfile] : []
|
|
94
|
+
]);
|
|
95
|
+
return {
|
|
96
|
+
fresh: changes.length === 0 && currentHash === data.hash,
|
|
97
|
+
changes
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async function checkEntry(entry, category, root, changes) {
|
|
101
|
+
const filePath = resolve(root, entry.file);
|
|
102
|
+
if (!existsSync(filePath)) {
|
|
103
|
+
changes.push({
|
|
104
|
+
type: "removed",
|
|
105
|
+
category,
|
|
106
|
+
file: entry.file
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (await hashFile(filePath) !== entry.hash) changes.push({
|
|
111
|
+
type: "changed",
|
|
112
|
+
category,
|
|
113
|
+
file: entry.file
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
//#endregion
|
|
117
|
+
export { writeHashFile as a, hashFiles as c, serializeHashFile as i, parseHashFile as n, computeCompositeHash as o, readHashFile as r, hashFile as s, checkBuildFreshness as t };
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { t as checkBuildFreshness } from "./check-YFLy7mcd.mjs";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
//#region src/cli.ts
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
const command = args[0];
|
|
7
|
+
if (command === "check") {
|
|
8
|
+
const hashFileIdx = args.indexOf("--hash-file");
|
|
9
|
+
const result = await checkBuildFreshness({ hashFile: hashFileIdx !== -1 ? args[hashFileIdx + 1] : void 0 });
|
|
10
|
+
if (result.fresh) {
|
|
11
|
+
console.log("Build is up to date.");
|
|
12
|
+
process.exit(0);
|
|
13
|
+
} else {
|
|
14
|
+
console.log("Build is stale. Changes detected:");
|
|
15
|
+
for (const change of result.changes) console.log(` ${change.type}: [${change.category}] ${change.file}`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
console.log(`tsdown-stale-guard - Build freshness validation for tsdown
|
|
20
|
+
|
|
21
|
+
Usage:
|
|
22
|
+
tsdown-stale-guard check Check if the build is up to date
|
|
23
|
+
tsdown-stale-guard check --hash-file <path> Use a custom hash file path
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--hash-file <path> Path to the hash file (default: node_modules/.cache/tsdown-stale-guard/hash.yaml)
|
|
27
|
+
--help Show this help message`);
|
|
28
|
+
process.exit(command === "--help" || command === "-h" ? 0 : 1);
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export {};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Plugin } from "rolldown";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
interface TsdownStaleGuardEntry {
|
|
5
|
+
file: string;
|
|
6
|
+
hash: string;
|
|
7
|
+
}
|
|
8
|
+
interface TsdownStaleGuardData {
|
|
9
|
+
version: 1;
|
|
10
|
+
hash: string;
|
|
11
|
+
config?: TsdownStaleGuardEntry;
|
|
12
|
+
lockfile?: TsdownStaleGuardEntry;
|
|
13
|
+
sources: TsdownStaleGuardEntry[];
|
|
14
|
+
outputs: TsdownStaleGuardEntry[];
|
|
15
|
+
}
|
|
16
|
+
interface CheckResult {
|
|
17
|
+
fresh: boolean;
|
|
18
|
+
changes: CheckChange[];
|
|
19
|
+
}
|
|
20
|
+
interface CheckChange {
|
|
21
|
+
type: 'changed' | 'added' | 'removed';
|
|
22
|
+
category: 'config' | 'lockfile' | 'source' | 'output';
|
|
23
|
+
file: string;
|
|
24
|
+
}
|
|
25
|
+
interface TsdownStaleGuardPluginOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Path to the hash file.
|
|
28
|
+
* @default 'node_modules/.cache/tsdown-stale-guard/hash.yaml'
|
|
29
|
+
*/
|
|
30
|
+
hashFile?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Root directory for resolving relative paths.
|
|
33
|
+
* @default process.cwd()
|
|
34
|
+
*/
|
|
35
|
+
root?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to hash output files.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
hashOutputs?: boolean;
|
|
41
|
+
}
|
|
42
|
+
interface CheckOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Path to the hash file.
|
|
45
|
+
* @default 'node_modules/.cache/tsdown-stale-guard/hash.yaml'
|
|
46
|
+
*/
|
|
47
|
+
hashFile?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Root directory for resolving relative paths.
|
|
50
|
+
* @default process.cwd()
|
|
51
|
+
*/
|
|
52
|
+
root?: string;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/check.d.ts
|
|
56
|
+
declare function checkBuildFreshness(options?: CheckOptions): Promise<CheckResult>;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/lockfile.d.ts
|
|
59
|
+
declare function serializeHashFile(data: TsdownStaleGuardData): string;
|
|
60
|
+
declare function parseHashFile(content: string): TsdownStaleGuardData;
|
|
61
|
+
declare function readHashFile(path: string): Promise<TsdownStaleGuardData>;
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/plugin.d.ts
|
|
64
|
+
declare function TsdownStaleGuard(options?: TsdownStaleGuardPluginOptions): Plugin;
|
|
65
|
+
//#endregion
|
|
66
|
+
export { type CheckChange, type CheckOptions, type CheckResult, TsdownStaleGuard, type TsdownStaleGuardData, type TsdownStaleGuardEntry, type TsdownStaleGuardPluginOptions, checkBuildFreshness, parseHashFile, readHashFile, serializeHashFile };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { a as writeHashFile, c as hashFiles, i as serializeHashFile, n as parseHashFile, o as computeCompositeHash, r as readHashFile, s as hashFile, t as checkBuildFreshness } from "./check-YFLy7mcd.mjs";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
4
|
+
import process from "node:process";
|
|
5
|
+
import { readdir } from "node:fs/promises";
|
|
6
|
+
//#region src/detect.ts
|
|
7
|
+
const PACKAGE_LOCK_FILES = [
|
|
8
|
+
"pnpm-lock.yaml",
|
|
9
|
+
"yarn.lock",
|
|
10
|
+
"package-lock.json",
|
|
11
|
+
"bun.lockb",
|
|
12
|
+
"bun.lock"
|
|
13
|
+
];
|
|
14
|
+
const TSDOWN_CONFIG_FILES = [
|
|
15
|
+
"tsdown.config.ts",
|
|
16
|
+
"tsdown.config.mts",
|
|
17
|
+
"tsdown.config.cts",
|
|
18
|
+
"tsdown.config.js",
|
|
19
|
+
"tsdown.config.mjs",
|
|
20
|
+
"tsdown.config.cjs",
|
|
21
|
+
"tsdown.config.json"
|
|
22
|
+
];
|
|
23
|
+
function findUp(names, cwd) {
|
|
24
|
+
let dir = resolve(cwd);
|
|
25
|
+
while (true) {
|
|
26
|
+
for (const name of names) {
|
|
27
|
+
const candidate = join(dir, name);
|
|
28
|
+
if (existsSync(candidate)) return candidate;
|
|
29
|
+
}
|
|
30
|
+
const parent = dirname(dir);
|
|
31
|
+
if (parent === dir) break;
|
|
32
|
+
dir = parent;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function detectPackageLock(cwd) {
|
|
36
|
+
return findUp(PACKAGE_LOCK_FILES, cwd);
|
|
37
|
+
}
|
|
38
|
+
function detectTsdownConfig(cwd) {
|
|
39
|
+
return findUp(TSDOWN_CONFIG_FILES, cwd);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/plugin.ts
|
|
43
|
+
const RE_QUERY = /\?.*$/;
|
|
44
|
+
const RE_WINDOWS_DRIVE = /^[a-z]:\\/i;
|
|
45
|
+
const RE_NODE_MODULES = /node_modules/;
|
|
46
|
+
const DEFAULT_HASH_FILE = "node_modules/.cache/tsdown-stale-guard/hash.yaml";
|
|
47
|
+
function TsdownStaleGuard(options = {}) {
|
|
48
|
+
const { hashFile: hashFilePath = DEFAULT_HASH_FILE, hashOutputs = true } = options;
|
|
49
|
+
const sourceIds = /* @__PURE__ */ new Set();
|
|
50
|
+
let root;
|
|
51
|
+
return {
|
|
52
|
+
name: "tsdown-stale-guard",
|
|
53
|
+
buildStart() {
|
|
54
|
+
root = options.root || process.cwd();
|
|
55
|
+
},
|
|
56
|
+
transform: {
|
|
57
|
+
filter: { id: { exclude: [RE_NODE_MODULES] } },
|
|
58
|
+
handler(_code, id) {
|
|
59
|
+
const cleanId = id.replace(RE_QUERY, "");
|
|
60
|
+
if (cleanId.startsWith("/") || RE_WINDOWS_DRIVE.test(cleanId)) sourceIds.add(cleanId);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
async writeBundle(opts) {
|
|
64
|
+
const outDir = opts.dir || resolve(root, "dist");
|
|
65
|
+
const resolvedHashFile = resolve(root, hashFilePath);
|
|
66
|
+
const sources = await hashFiles([...sourceIds].filter((f) => existsSync(f)), root);
|
|
67
|
+
let outputs = [];
|
|
68
|
+
if (hashOutputs && existsSync(outDir)) {
|
|
69
|
+
const files = await readdir(outDir, {
|
|
70
|
+
recursive: true,
|
|
71
|
+
withFileTypes: true
|
|
72
|
+
});
|
|
73
|
+
const outputPaths = [];
|
|
74
|
+
for (const file of files) if (file.isFile()) outputPaths.push(resolve(file.parentPath, file.name));
|
|
75
|
+
outputs = await hashFiles(outputPaths, root);
|
|
76
|
+
}
|
|
77
|
+
let config;
|
|
78
|
+
const configPath = detectTsdownConfig(root);
|
|
79
|
+
if (configPath) {
|
|
80
|
+
const hash = await hashFile(configPath);
|
|
81
|
+
config = {
|
|
82
|
+
file: toForwardSlash(relative(root, configPath)),
|
|
83
|
+
hash
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
let lockfileEntry;
|
|
87
|
+
const lockfilePath = detectPackageLock(root);
|
|
88
|
+
if (lockfilePath) {
|
|
89
|
+
const hash = await hashFile(lockfilePath);
|
|
90
|
+
lockfileEntry = {
|
|
91
|
+
file: toForwardSlash(relative(root, lockfilePath)),
|
|
92
|
+
hash
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
await writeHashFile(resolvedHashFile, {
|
|
96
|
+
version: 1,
|
|
97
|
+
hash: computeCompositeHash([
|
|
98
|
+
...sources,
|
|
99
|
+
...outputs,
|
|
100
|
+
...config ? [config] : [],
|
|
101
|
+
...lockfileEntry ? [lockfileEntry] : []
|
|
102
|
+
]),
|
|
103
|
+
config,
|
|
104
|
+
lockfile: lockfileEntry,
|
|
105
|
+
sources,
|
|
106
|
+
outputs
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function toForwardSlash(p) {
|
|
112
|
+
return p.replace(/\\/g, "/");
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
export { TsdownStaleGuard, checkBuildFreshness, parseHashFile, readHashFile, serializeHashFile };
|
package/package.json
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tsdown-stale-guard",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.0",
|
|
5
|
+
"description": "Build freshness validation for tsdown",
|
|
6
|
+
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"funding": "https://github.com/sponsors/antfu",
|
|
9
|
+
"homepage": "https://github.com/antfu/tsdown-stale-guard#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/antfu/tsdown-stale-guard.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/antfu/tsdown-stale-guard/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"tsdown",
|
|
17
|
+
"build",
|
|
18
|
+
"stale",
|
|
19
|
+
"hash",
|
|
20
|
+
"freshness"
|
|
21
|
+
],
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": "./dist/index.mjs",
|
|
25
|
+
"./cli": "./dist/cli.mjs",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"bin": {
|
|
30
|
+
"tsdown-stale-guard": "./dist/cli.mjs"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"yaml": "^2.8.3"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@antfu/eslint-config": "^8.2.0",
|
|
40
|
+
"@antfu/ni": "^30.0.0",
|
|
41
|
+
"@antfu/utils": "^9.3.0",
|
|
42
|
+
"@types/node": "^25.6.0",
|
|
43
|
+
"bumpp": "^11.0.1",
|
|
44
|
+
"eslint": "^10.2.0",
|
|
45
|
+
"lint-staged": "^16.4.0",
|
|
46
|
+
"publint": "^0.3.18",
|
|
47
|
+
"rolldown": "1.0.0-rc.15",
|
|
48
|
+
"simple-git-hooks": "^2.13.1",
|
|
49
|
+
"tsdown": "^0.21.8",
|
|
50
|
+
"tsnapi": "^0.2.1",
|
|
51
|
+
"tsx": "^4.21.0",
|
|
52
|
+
"typescript": "^6.0.2",
|
|
53
|
+
"vite": "^8.0.8",
|
|
54
|
+
"vitest": "^4.1.4"
|
|
55
|
+
},
|
|
56
|
+
"simple-git-hooks": {
|
|
57
|
+
"pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && pnpm run build && npx lint-staged"
|
|
58
|
+
},
|
|
59
|
+
"lint-staged": {
|
|
60
|
+
"*": "eslint --fix"
|
|
61
|
+
},
|
|
62
|
+
"scripts": {
|
|
63
|
+
"build": "tsdown --config-loader unrun",
|
|
64
|
+
"dev": "tsdown --watch",
|
|
65
|
+
"lint": "eslint",
|
|
66
|
+
"release": "bumpp",
|
|
67
|
+
"start": "tsx src/index.ts",
|
|
68
|
+
"test": "vitest",
|
|
69
|
+
"typecheck": "tsc"
|
|
70
|
+
}
|
|
71
|
+
}
|