Skip to content

Commit 171cdf9

Browse files
committed
refactor: disable cache by default
1 parent b1f554c commit 171cdf9

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- **refactor**: Disable `--cache` by default, it might be too aggressive.
6+
37
## 0.10.10
48

59
- **feat**: Add `--cwd` to change the working directory, it also affects the cache file logic.

src/build.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ export interface CacheOptions {
6464
shims?: boolean;
6565
}
6666

67+
function normalize(path: string) {
68+
if (path.startsWith("./")) {
69+
path = path.slice(2);
70+
}
71+
return path.replace(/[\/\\]/g, "+");
72+
}
73+
6774
export async function build(
6875
entry: string,
6976
options: BuildOptions & { format: Format },
@@ -84,7 +91,8 @@ export async function build(
8491
sourcemap: true,
8592
sourcesContent: false,
8693
treeShaking: true,
87-
outfile: join(tmpdir, entry.replace(/[\/\\]/g, "+") + extname[options.format]),
94+
outfile: join(tmpdir, normalize(entry) + extname[options.format]),
95+
metafile: cacheOptions?.cache,
8896
...options,
8997
};
9098
if (cacheOptions?.cache && mtime(options.outfile!) > mtime(entry)) {

src/commands/default.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { ChildProcess, spawn, spawnSync } from "child_process";
22
import { BuildFailure, BuildOptions } from "esbuild";
3-
import { readFile } from "fs/promises";
4-
import { dirname } from "path";
5-
import { pathToFileURL } from "url";
63
import { EsbuildDevFlags, EsbuildDevOptions, EsbuildFlags, parse } from "../args.js";
74
import { Format, build, delay, loadPlugins, loaderPath } from "../index.js";
85
import { resolveMangleCache } from "./utils";
@@ -24,15 +21,12 @@ export async function defaultCommand(entry: string, argsBeforeEntry: string[], a
2421

2522
const argsToEntry = [..._2, ...argsAfterEntry];
2623

27-
const devOptions = { shims: true, cache: true, cwd: process.cwd(), ...devOptionsRaw } as EsbuildDevOptions;
24+
const devOptions = { shims: true, cwd: process.cwd(), ...devOptionsRaw } as EsbuildDevOptions;
2825
const buildOptions = buildOptionsRaw as BuildOptions & { format: Format };
2926
if (devOptions.import) devOptions.loader = true;
3027
if (devOptions.loader && (devOptions.cjs || devOptions.plugin || devOptions.watch || devOptions.include)) {
3128
throw new Error(`--cjs, --plugin, --watch and --include are not supported with --loader`);
3229
}
33-
if (devOptions.loader || devOptions.plugin || devOptions.watch) {
34-
devOptions.cache = false;
35-
}
3630

3731
buildOptions.format = devOptions.cjs ? "cjs" : "esm";
3832
resolveMangleCache(buildOptions);

src/help.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ Options:
1818
can only be accessed in ESM. This option cannot be used
1919
with `--import`.
2020

21+
--cwd=... Set the current working directory, it also affects the
22+
logic to find the temp directory.
23+
2124
--shims Replace `import.meta.url` and `__dirname` with absolute
2225
path in .[tj]s files. This is enabled by default.
2326
You can pass `--shims=false` to disable it.
2427

2528
--cache Skip build if the output file is newer than the input.
26-
This option cannot be used with plugins or `--import`.
27-
It is enabled by default, You can pass `--cache=false`
28-
to disable it.
29+
It is disabled by default.
2930

3031
--watch Enable watch mode. This is built on top of the
3132
alias: -w `context.watch()` function of esbuild.
@@ -36,7 +37,7 @@ Options:
3637

3738
--include:name Force include a package in the bundle.
3839

39-
--node:node-options Append node options to the command line. For example,
40+
--node:options Append node options to the command line. For example,
4041
`--node:--env-file=.env` will result in the final
4142
command be `node --env-file=.env outfile.js`.
4243

@@ -53,3 +54,6 @@ Sub Commands:
5354
alias: -b
5455

5556
temp Show the temp directory of the current project.
57+
alias: tmp
58+
59+
--cwd=... Set the current working directory.

test/cache.dep.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const dep = 1;

test/cache.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// node dist/bin.js --cache test/cache.ts
2+
import * as dep from "./cache.dep";
3+
console.log(structuredClone(dep));

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"esModuleInterop": true,
1616
"resolveJsonModule": true,
1717
"lib": ["ESNext"],
18-
"module": "ESNext",
18+
"module": "Preserve",
1919
"target": "ESNext",
20-
"moduleResolution": "Node",
20+
"moduleDetection": "force",
2121
"types": ["node"]
2222
}
2323
}

0 commit comments

Comments
 (0)