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
This commit is contained in:
Dominik Prokop
2025-12-16 10:34:46 +01:00
parent 3fe8e70436
commit 1c94a7ddd5
3 changed files with 83 additions and 0 deletions
+40
View File
@@ -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,
},
];
@@ -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';
+40
View File
@@ -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