diff --git a/packages/cli/snap-tests-global/command-staged-with-config/snap.txt b/packages/cli/snap-tests-global/command-staged-with-config/snap.txt index 7fcf705678..0425facb67 100644 --- a/packages/cli/snap-tests-global/command-staged-with-config/snap.txt +++ b/packages/cli/snap-tests-global/command-staged-with-config/snap.txt @@ -20,5 +20,8 @@ [COMPLETED] Cleaning up temporary files... > git add -A && git commit -m 'second' +> echo 'export const bar = 2;' >> src/index.ts && git add src/index.ts +> vp staged --debug # should succeed with debug enabled +> git add -A && git commit -m 'third' > printf 'eval("code");\n' > src/fail.js && git add src/fail.js [1]> vp staged > /dev/null 2>&1 # should fail when staged .js file has lint errors \ No newline at end of file diff --git a/packages/cli/snap-tests-global/command-staged-with-config/steps.json b/packages/cli/snap-tests-global/command-staged-with-config/steps.json index 0430c879d2..9bddcda0a4 100644 --- a/packages/cli/snap-tests-global/command-staged-with-config/steps.json +++ b/packages/cli/snap-tests-global/command-staged-with-config/steps.json @@ -9,6 +9,15 @@ }, "vp staged # should succeed with staged .ts files", { "command": "git add -A && git commit -m 'second'", "ignoreOutput": true }, + { + "command": "echo 'export const bar = 2;' >> src/index.ts && git add src/index.ts", + "ignoreOutput": true + }, + { + "command": "vp staged --debug # should succeed with debug enabled", + "ignoreOutput": true + }, + { "command": "git add -A && git commit -m 'third'", "ignoreOutput": true }, { "command": "printf 'eval(\"code\");\\n' > src/fail.js && git add src/fail.js", "ignoreOutput": true diff --git a/packages/cli/tsdown.config.ts b/packages/cli/tsdown.config.ts index 9b1f2e8bff..944111ecdf 100644 --- a/packages/cli/tsdown.config.ts +++ b/packages/cli/tsdown.config.ts @@ -1,5 +1,10 @@ +import { createRequire } from 'node:module'; + import { defineConfig } from 'tsdown'; +const require = createRequire(import.meta.url); +const lintStagedPackageJson = require('lint-staged/package.json') as { version: string }; + /** * Rewrite `../versions.js` → `./versions.js` at resolve time. * @@ -18,6 +23,22 @@ const fixVersionsPathPlugin = { }, }; +/** + * Replace lint-staged's lib/version.js with a build-time version value. + * + * The original module reads ../package.json at runtime when debug logging is enabled, + * but that file does not exist in the bundled dist/staged/bin.js. + */ +const inlineLintStagedVersionPlugin = { + name: 'inline-lint-staged-version', + load(id: string) { + if (id.replaceAll('\\', '/').endsWith('/lint-staged/lib/version.js')) { + return `export const getVersion = async () => ${JSON.stringify(lintStagedPackageJson.version)};\n`; + } + return undefined; + }, +}; + export default defineConfig([ // ESM — all entry points bundled to dist/ { @@ -56,7 +77,7 @@ export default defineConfig([ mainFields: ['module', 'main'], }, }, - plugins: [fixVersionsPathPlugin], + plugins: [fixVersionsPathPlugin, inlineLintStagedVersionPlugin], }, // CJS — dual-format entries