Skip to content

Commit eeb1f5e

Browse files
robertmclawsclaude
andcommitted
Eliminate hardcoded idb version duplication with build-time code generation
The idb version was hardcoded in both package.json and the CDN import URL. Add a prebuild script that reads the version from package.json and generates a TypeScript constant, so the version is defined in one place only. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3a9f599 commit eeb1f5e

8 files changed

Lines changed: 34 additions & 7 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8'));
5+
const idbVersion = packageJson.dependencies.idb;
6+
7+
const outputDir = path.join(__dirname, 'generated');
8+
if (!fs.existsSync(outputDir)) {
9+
fs.mkdirSync(outputDir, { recursive: true });
10+
}
11+
12+
const content = `// AUTO-GENERATED from package.json - DO NOT EDIT\nexport const IDB_VERSION = "${idbVersion}";\n`;
13+
14+
fs.writeFileSync(path.join(outputDir, 'idb-version.ts'), content);
15+
console.log(`Generated idb-version.ts with version ${idbVersion}`);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// AUTO-GENERATED from package.json - DO NOT EDIT
2+
export const IDB_VERSION = "8.0.3";

src/CloudNimble.BlazorEssentials.IndexedDb/Scripts/idb-loader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IDB_VERSION } from './generated/idb-version';
2+
13
let idbModule: any = null;
24
let isLoading = false;
35
let loadPromise: Promise<any> | null = null;
@@ -34,7 +36,7 @@ async function loadIdbWithFallback() {
3436
try {
3537
// Try online CDN first
3638
// @ts-ignore - Dynamic import from CDN
37-
const onlineModule = await import('https://cdn.jsdelivr.net/npm/idb@8.0.3/+esm');
39+
const onlineModule = await import(`https://cdn.jsdelivr.net/npm/idb@${IDB_VERSION}/+esm`);
3840
console.log('Loaded idb from online CDN');
3941
return onlineModule;
4042
} catch (onlineError) {

src/CloudNimble.BlazorEssentials.IndexedDb/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"author": "CloudNimble, Inc.",
88
"license": "ISC",
99
"scripts": {
10+
"prebuild": "node Scripts/generate-idb-version.js",
1011
"build": "tsc",
1112
"watch": "tsc --watch",
12-
"clean": "rimraf wwwroot/*.js wwwroot/*.js.map"
13+
"clean": "rimraf wwwroot/*.js wwwroot/*.js.map Scripts/generated"
1314
},
1415
"devDependencies": {
1516
"@microsoft/dotnet-js-interop": "10.0.0",

src/CloudNimble.BlazorEssentials.IndexedDb/wwwroot/generated/idb-version.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CloudNimble.BlazorEssentials.IndexedDb/wwwroot/generated/idb-version.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CloudNimble.BlazorEssentials.IndexedDb/wwwroot/idb-loader.js

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CloudNimble.BlazorEssentials.IndexedDb/wwwroot/idb-loader.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)