Skip to content

Commit 883d452

Browse files
committed
fix(vite): bake package version in at build time
getPackageVersion() read package.json at runtime via import.meta.url. When a consumer imports the plugin inside vite.config.ts, Vite bundles the config with esbuild and rewrites import.meta.url, so the read failed and the injected `data-rep-version` (and `_meta.version`) fell back to "0.0.0". Inject the version via a tsup `define` (tsup.config.ts) so it is baked into the bundle at build time, independent of how the consumer loads the plugin.
1 parent 289ae17 commit 883d452

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

plugins/vite/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"dist"
3535
],
3636
"scripts": {
37-
"build": "tsup src/index.ts --format cjs,esm --dts",
37+
"build": "tsup",
3838
"test": "vitest run",
3939
"test:watch": "vitest",
4040
"typecheck": "tsc --noEmit"

plugins/vite/src/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import { resolve, dirname } from 'node:path';
2-
import { readFileSync } from 'node:fs';
3-
import { fileURLToPath } from 'node:url';
1+
import { resolve } from 'node:path';
42
import type { Plugin, ViteDevServer, HtmlTagDescriptor } from 'vite';
53
import { generateKeys, type Keys } from './crypto.js';
64
import { readAndClassify, type ClassifiedVars } from './env.js';
75
import { scanValue } from './guardrails.js';
86
import { buildPayload, type PayloadResult } from './payload.js';
97

8+
// Injected at build time by tsup (see tsup.config.ts). Reading package.json at
9+
// runtime via import.meta.url is unreliable here: consumers import this plugin
10+
// inside vite.config.ts, which Vite bundles with esbuild — that rewrites
11+
// import.meta.url, so the runtime read fails and the version falls back to
12+
// 0.0.0. Baking the version in at build time avoids that entirely.
13+
declare const __REP_VITE_VERSION__: string;
14+
15+
function getPackageVersion(): string {
16+
return typeof __REP_VITE_VERSION__ === 'string' ? __REP_VITE_VERSION__ : '0.0.0';
17+
}
18+
1019
export interface RepPluginOptions {
1120
/** Path to env file, relative to project root. Default: '.env.local' */
1221
env?: string;
1322
/** Enable strict mode — guardrail warnings become errors. Default: false */
1423
strict?: boolean;
1524
}
1625

17-
function getPackageVersion(): string {
18-
try {
19-
const pkgPath = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'package.json');
20-
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
21-
return pkg.version;
22-
} catch {
23-
return '0.0.0';
24-
}
25-
}
26-
2726
export function repPlugin(options: RepPluginOptions = {}): Plugin {
2827
const envFile = options.env ?? '.env.local';
2928
const strict = options.strict ?? false;

plugins/vite/tsup.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { defineConfig } from 'tsup';
2+
import { readFileSync } from 'node:fs';
3+
4+
const { version } = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8'));
5+
6+
export default defineConfig({
7+
entry: ['src/index.ts'],
8+
format: ['cjs', 'esm'],
9+
dts: true,
10+
// Bake the package version in at build time so it survives consumers that
11+
// bundle their vite.config (which would otherwise rewrite import.meta.url).
12+
define: {
13+
__REP_VITE_VERSION__: JSON.stringify(version),
14+
},
15+
});

0 commit comments

Comments
 (0)