From 1c94a7ddd533ec4f3ef6acadb638aeeb7b759f6c Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Tue, 16 Dec 2025 10:34:46 +0100 Subject: [PATCH] Add versioned dashboard schema sub-path exports - Create dashboard/v0 sub-path for raw dashboard types - Create dashboard/v2beta1 sub-path for v2 schema types - Add exports and typesVersions to package.json via prepare-npm-package.js - typesVersions provides backwards compatibility for moduleResolution: node - Add rollup build targets for both sub-paths --- packages/grafana-schema/rollup.config.ts | 40 +++++++++++++++++++ .../src/schema/dashboard/v0/index.ts | 3 ++ scripts/prepare-npm-package.js | 40 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 packages/grafana-schema/src/schema/dashboard/v0/index.ts diff --git a/packages/grafana-schema/rollup.config.ts b/packages/grafana-schema/rollup.config.ts index 04928f75c47..5250e433d51 100644 --- a/packages/grafana-schema/rollup.config.ts +++ b/packages/grafana-schema/rollup.config.ts @@ -48,4 +48,44 @@ export default [ }, treeshake: false, }, + // Build sub-path exports for dashboard v0 + { + input: { + 'schema/dashboard/v0': fileURLToPath(new URL('src/schema/dashboard/v0/index.ts', import.meta.url)), + }, + plugins: [noderesolve, esbuild], + output: [ + { + format: 'esm', + dir: path.dirname(pkg.publishConfig.module), + entryFileNames: '[name].mjs', + }, + { + format: 'cjs', + dir: path.dirname(pkg.publishConfig.main), + entryFileNames: '[name].cjs', + }, + ], + treeshake: false, + }, + // Build sub-path exports for dashboard v2beta1 + { + input: { + 'schema/dashboard/v2beta1': fileURLToPath(new URL('src/schema/dashboard/v2beta1/index.ts', import.meta.url)), + }, + plugins: [noderesolve, esbuild], + output: [ + { + format: 'esm', + dir: path.dirname(pkg.publishConfig.module), + entryFileNames: '[name].mjs', + }, + { + format: 'cjs', + dir: path.dirname(pkg.publishConfig.main), + entryFileNames: '[name].cjs', + }, + ], + treeshake: false, + }, ]; diff --git a/packages/grafana-schema/src/schema/dashboard/v0/index.ts b/packages/grafana-schema/src/schema/dashboard/v0/index.ts new file mode 100644 index 00000000000..2fd4dac743b --- /dev/null +++ b/packages/grafana-schema/src/schema/dashboard/v0/index.ts @@ -0,0 +1,3 @@ +// Re-export raw dashboard types for v0 (legacy) dashboard schema +// This allows imports like: import { AnnotationPanelFilter, DashboardLink } from '@grafana/schema/dashboard/v0' +export * from '../../../raw/dashboard/x/dashboard_types.gen'; diff --git a/scripts/prepare-npm-package.js b/scripts/prepare-npm-package.js index 054b3d12609..6d23833d03f 100644 --- a/scripts/prepare-npm-package.js +++ b/scripts/prepare-npm-package.js @@ -29,6 +29,46 @@ try { types: './dist/*', default: './dist/*', }; + // Support @grafana/scenes that imports from dist/esm paths which webpack transforms to src paths + // (see webpack.common.js NormalModuleReplacementPlugin that replaces @grafana/schema/dist/esm with @grafana/schema/src) + exports['./src/*'] = { + types: './src/*', + default: './src/*', + }; + // Add sub-path exports for dashboard v0 + exports['./dashboard/v0'] = { + import: { + types: './dist/types/schema/dashboard/v0/index.d.ts', + default: './dist/esm/schema/dashboard/v0.mjs', + }, + require: { + types: './dist/types/schema/dashboard/v0/index.d.ts', + default: './dist/cjs/schema/dashboard/v0.cjs', + }, + }; + // Add sub-path exports for dashboard v2beta1 + exports['./dashboard/v2beta1'] = { + import: { + types: './dist/types/schema/dashboard/v2beta1/index.d.ts', + default: './dist/esm/schema/dashboard/v2beta1.mjs', + }, + require: { + types: './dist/types/schema/dashboard/v2beta1/index.d.ts', + default: './dist/cjs/schema/dashboard/v2beta1.cjs', + }, + }; + + // Add typesVersions for backwards compatibility with moduleResolution: "node" + // This allows TypeScript to resolve types for sub-path imports even without + // modern moduleResolution settings (bundler/node16/nodenext) + pkgJson.update({ + typesVersions: { + '*': { + 'dashboard/v0': ['./dist/types/schema/dashboard/v0/index.d.ts'], + 'dashboard/v2beta1': ['./dist/types/schema/dashboard/v2beta1/index.d.ts'], + }, + }, + }); } // Fix for @grafana/i18n so eslint-plugin can be imported by consumers