Build: Introduce ESM and Treeshaking to NPM package builds (#51517)

* Revert "Chore: Bump terser to fix security vulnerability (#53052)"

This reverts commit 7ae74d2a18.

* feat: use tsc and rollup directly with esbuild and publishConfig, files props

* refactor(grafana-data): fix isolatedModules re-export type error

* refactor(grafana-data): import paths from src not package name

* refactor(rollup): fix dts output.file

* chore(grafana-schema): delete dashboard_experimental.gen.ts - cannot work with isolatedModules

* refactor(grafana-e2e-selectors): fix export types isolatedModules error

* refactor(grafana-runtime): fix isolatedModules re-export type error

* refactor(grafana-ui): fix isolatedModules re-export type error

* feat(grafana-ui): use named imports for treeshaking

* refactor(grafana-ui): use named imports for treeshaking

* feat: react and react-dom as peerDeps for packages

* feat(grafana-ui): emotion packages as peerDeps

* feat(grafana-e2e): use tsc, rollup, esbuild for bundling

* chore(packages): clean up redundant dependencies

* chore(toolkit): deprecate unused package:build task

* chore(schema): put back dashboard_experimental and exclude to prevent isolatedModules error

* docs(packages): update readme

* chore(storybook): disable isolatedModules for builds

* chore: relax peerDeps for emotion and react

* revert(grafana-ui): put @emotion dependencies back

* refactor: replace relative package imports with package name

* build(packages): set emitDeclaration false for typecheck scripts to work

* test(publicdashboarddatasource): move test next to implementation. try to appease the betterer gods

* chore(storybook): override ts-node config for storybook compilation

* refactor(grafana-data): use ternary so babel doesnt complain about expecting flow types

* chore(toolkit): prefer files and publishConfig package.json props over copying

* build(npm): remove --contents dist arg from publishing commands

* chore(packages): introduce sideEffects prop to package.json to hint package can be treeshaken

* chore(packages): remove redundant index.js files

* feat(packages): set publishConfig.access to public

* feat(packages): use yarn berry and npm for packaging and publishing

* refactor(packages): simplify rollup configs

* chore(schema): add comment explaining need to exclude dashboard_experimental

* revert(toolkit): put back clean to prevent cli failures

* ci(packages): run packages:pack before a canary publish

* chore(gitignore): add npm-artifacts directory to ignore list

* test(publicdashboarddatasource): fix module mocking

* chore(packages): delete package.tgz when running clean

* chore(grafana-data): move dependencies from devDeps to prevent build resolution errors
This commit is contained in:
Jack Westbrook
2022-08-03 15:47:09 +02:00
committed by GitHub
parent 610abc2af0
commit d87bf30e9e
106 changed files with 3005 additions and 1499 deletions

View File

@@ -0,0 +1,96 @@
import { of } from 'rxjs';
import { DataQueryRequest, DataSourceInstanceSettings, DataSourceRef } from '@grafana/data';
import { BackendSrvRequest, BackendSrv, DataSourceWithBackend } from '@grafana/runtime';
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
import { PublicDashboardDataSource, PUBLIC_DATASOURCE } from './PublicDashboardDataSource';
const mockDatasourceRequest = jest.fn();
const backendSrv = {
fetch: (options: BackendSrvRequest) => {
return of(mockDatasourceRequest(options));
},
} as unknown as BackendSrv;
jest.mock('@grafana/runtime', () => ({
...jest.requireActual('@grafana/runtime'),
getBackendSrv: () => backendSrv,
getDataSourceSrv: () => {
return {
getInstanceSettings: (ref?: DataSourceRef) => ({ type: ref?.type ?? '?', uid: ref?.uid ?? '?' }),
};
},
}));
describe('PublicDashboardDatasource', () => {
test('Fetches results from the pubdash query endpoint', () => {
mockDatasourceRequest.mockReset();
mockDatasourceRequest.mockReturnValue(Promise.resolve({}));
const ds = new PublicDashboardDataSource('public');
const panelId = 1;
const publicDashboardAccessToken = 'abc123';
ds.query({
maxDataPoints: 10,
intervalMs: 5000,
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
panelId,
publicDashboardAccessToken,
} as DataQueryRequest);
const mock = mockDatasourceRequest.mock;
expect(mock.calls.length).toBe(1);
expect(mock.lastCall[0].url).toEqual(
`/api/public/dashboards/${publicDashboardAccessToken}/panels/${panelId}/query`
);
});
test('returns public datasource uid when datasource passed in is null', () => {
let ds = new PublicDashboardDataSource(null);
expect(ds.uid).toBe(PUBLIC_DATASOURCE);
});
test('returns datasource when datasource passed in is a string', () => {
let ds = new PublicDashboardDataSource('theDatasourceUid');
expect(ds.uid).toBe('theDatasourceUid');
});
test('returns datasource uid when datasource passed in is a DataSourceRef implementation', () => {
const datasource = { type: 'datasource', uid: 'abc123' };
let ds = new PublicDashboardDataSource(datasource);
expect(ds.uid).toBe('abc123');
});
test('returns datasource uid when datasource passed in is a DatasourceApi instance', () => {
const settings: DataSourceInstanceSettings = { id: 1, uid: 'abc123' } as DataSourceInstanceSettings;
const datasource = new DataSourceWithBackend(settings);
let ds = new PublicDashboardDataSource(datasource);
expect(ds.uid).toBe('abc123');
});
test('isMixedDatasource returns true when datasource is mixed', () => {
const datasource = new DataSourceWithBackend({ id: 1, uid: MIXED_DATASOURCE_NAME } as DataSourceInstanceSettings);
let ds = new PublicDashboardDataSource(datasource);
expect(ds.meta.mixed).toBeTruthy();
});
test('isMixedDatasource returns false when datasource is not mixed', () => {
const datasource = new DataSourceWithBackend({ id: 1, uid: 'abc123' } as DataSourceInstanceSettings);
let ds = new PublicDashboardDataSource(datasource);
expect(ds.meta.mixed).toBeFalsy();
});
test('isMixedDatasource returns false when datasource is a string', () => {
let ds = new PublicDashboardDataSource('abc123');
expect(ds.meta.mixed).toBeFalsy();
});
test('isMixedDatasource returns false when datasource is null', () => {
let ds = new PublicDashboardDataSource(null);
expect(ds.meta.mixed).toBeFalsy();
});
});