kibana/examples/field_formats_example/public/app.tsx
Tiago Costa 203bc28478
chore(NA): upgrade to webpack 5 (#191106)
### Summary

- Closes #89741

This PR contains the resulting work of a massive effort that ports our
on top bundler abstraction (called @kbn/optimizer) from Webpack v4 into
Webpack v5. It's essential in terms of long term maintenance since v4
was not receiving updates any longer but will also unblock some new
features that could be beneficial for our future DevEx endeavours.

Next you can find a small list of all the accomplished tasks on this
journey.

### Completed Tasks
- [x] Upgrade dependencies to match the ones on webpack v5
- [x] Fix null-loader usages
- [x] Fix raw-loader usages
- [x] Fix file-loader usages
- [x] Fix url-loader usages
- [x] Fix `@kbn/optimizer-webpack-helpers` to support webpack v5 
- [x] Adopt previous webpack v4 polyfill-all strategy with
node-polyfill-webpack-plugin
- [x] Fix theme-loader on @kbn/optimizer
- [x] Migrate configurations and ad-hoc loader options on all webpack
configs from v4 to v5
- [x] Fix @kbn/test jest resolver for file-loader cases
- [x] Migrate public-path loader on UiSharedDeps
- [x] Fix all usages of webpack-merge
- [x] Migrate BundleRemoteModule
- [x] Migrate BundleRemotesPlugin
- [x] Correctly migrate PopulateBundleCachePlugin
- [x] Correctly migrate BundleMetricsPlugin
- [x] Check if the profiling plugins still work (--profile flag)
- [x] Recover if possible the previous webpack v4 cacheGroup chunks
rename to something like `data.plugin.chunk.0.js`
- [x] Run `/ci` and make sure we get our first green CI, otherwise work
on the errors until we do
- [x] Profile and solve bottlenecks until we get a cold build
performance similar to the one we had on webpack v4 (`node
scripts/build_kibana_platform_plugins --no-cache`).
- [x] OpenSSL Legacy Warnings: try to remove `--openssl-legacy-provider
` flags
- [x] Add Webpack to Renovate config
- [x] Explore removing `NodePolyfillPlugin`
([here](https://www.npmjs.com/package/node-polyfill-webpack-plugin)) and
add each polyfill needed individually per each webpack config to check
if we get smaller bundles. If we do it's better to go with the case by
case need approach instead of deploying a bunch of polyfills with
NodePolyfillPlugin. As another alternative, create a custom smaller
plugin with only the union of all needed polyfills.
- [x] Evaluate if we want to touch the resolutions on mainFields and
conditionNames
- [x] Understand why `@import 'src/core/public/mixins'` does not work
anymore (not a problem, we should use relative paths anyway but we want
to track why it changed from v4 to v5)
- [x] BUG: Child compilers are having errors hidden and/or changed from
error to warning
- [x] Fix license check for
[Artistic-2.0](https://spdx.org/licenses/Artistic-2.0.html) is the
license for
[domain-browser](https://github.com/bevry/domain-browser?tab=License-1-ov-file).
This package is a dependency of
[NodePolyfillPlugin](https://www.npmjs.com/package/node-polyfill-webpack-plugin).
Artistic 2.0 license is [classified as
yellow](https://github.com/elastic/open-source/blob/main/elastic-product-policy.md#yellow-list)
and should only be used for dev dependencies.
- [x] Make sure `resourceQuery: { not: /raw/ }` is not necessary on
other webpack configs like storybook one
- [x] Find what is being wrongly removed by usedExports optimization;
hint: I believe it is identifying a lot of exports inside the sync entry
of plugins as unused exports and removing them. Then `__kbnBootstrap__`
can't be found
- [x] Rebalance @kbn/optimizer pickMaxWorkerCount
- [x] Re-open the issue to fix sass-warnings
[#190345](https://github.com/elastic/kibana/issues/190345) or downgrade
sass-loader to v10
- [x] Remove previous esm no parse rules
- [x] Confirm esm support is working
- [x] Confirm console override is needed
- [x] Confirm react prod builds on ui shared deps for distributable
- [x] Remove customization for
[xyflow](https://github.com/xyflow/xyflow) from webpack configs
- [x] Clean all the code
- [x] Make sure collected metrics from stats are still aligned with what
we were collecting before; also verify if the modules used for optimizer
caches etc are well generated (@kbn/node-libs-browser)
- [x] Fix watch performance

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Brad White <brad.white@elastic.co>
2025-02-14 03:01:36 +00:00

198 lines
6.5 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import React from 'react';
import {
EuiBasicTable,
EuiCallOut,
EuiCode,
EuiCodeBlock,
EuiLink,
EuiPageTemplate,
EuiProvider,
EuiSpacer,
EuiText,
EuiTitle,
} from '@elastic/eui';
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
import * as example1 from './examples/1_using_existing_format';
import * as example2 from './examples/2_creating_custom_formatter';
// @ts-ignore
import example1SampleCode from './examples/1_using_existing_format?raw';
// @ts-ignore
import example2SampleCodePart1 from '../common/example_currency_format?raw';
// @ts-ignore
import example2SampleCodePart2 from './examples/2_creating_custom_formatter?raw';
// @ts-ignore
import example2SampleCodePart3 from '../server/examples/2_creating_custom_formatter?raw';
// @ts-ignore
import example3SampleCode from './examples/3_creating_custom_format_editor?raw';
export interface Deps {
fieldFormats: FieldFormatsStart;
/**
* Just for demo purposes
*/
openDateViewNumberFieldEditor: () => void;
}
const UsingAnExistingFieldFormatExample: React.FC<{ deps: Deps }> = (props) => {
const sample = example1.getSample(props.deps.fieldFormats);
return (
<>
<EuiText>
<p>
This example shows how to use existing field formatter to format values. As an example, we
have a following sample configuration{' '}
<EuiCode>{JSON.stringify(example1.sampleSerializedFieldFormat)}</EuiCode> representing a{' '}
<EuiCode>bytes</EuiCode>
field formatter with a <EuiCode>0.00b</EuiCode> pattern.
</p>
</EuiText>
<EuiSpacer size={'s'} />
<EuiCodeBlock language="jsx">{example1SampleCode}</EuiCodeBlock>
<EuiSpacer size={'s'} />
<EuiBasicTable
data-test-subj={'example1 sample table'}
items={sample}
columns={[
{
field: 'raw',
name: 'Raw value',
'data-test-subj': 'example1 sample raw',
},
{
field: 'formatted',
name: 'Formatted value',
'data-test-subj': 'example1 sample formatted',
},
]}
/>
</>
);
};
const CreatingCustomFieldFormat: React.FC<{ deps: Deps }> = (props) => {
const sample = example2.getSample(props.deps.fieldFormats);
return (
<>
<EuiText>
<p>
This example shows how to create a custom field formatter. As an example, we create a
currency formatter and then display some values as <EuiCode>USD</EuiCode>. Custom field
formatter has to be registered both client and server side.
</p>
</EuiText>
<EuiSpacer size={'s'} />
<EuiCodeBlock language="jsx">{example2SampleCodePart1}</EuiCodeBlock>
<EuiSpacer size={'xs'} />
<EuiCodeBlock language="jsx">{example2SampleCodePart2}</EuiCodeBlock>
<EuiSpacer size={'xs'} />
<EuiCodeBlock language="jsx">{example2SampleCodePart3}</EuiCodeBlock>
<EuiSpacer size={'s'} />
<EuiBasicTable
items={sample}
data-test-subj={'example2 sample table'}
columns={[
{
field: 'raw',
name: 'Raw value',
'data-test-subj': 'example2 sample raw',
},
{
field: 'formatted',
name: 'Formatted value',
'data-test-subj': 'example2 sample formatted',
},
]}
/>
<EuiSpacer size={'s'} />
<EuiCallOut
title="Seamless integration with data views!"
color="success"
iconType="indexManagementApp"
>
<p>
Currency formatter that we&apos;ve just created is already integrated with data views. It
can be applied to any <EuiCode>numeric</EuiCode> field of any data view.{' '}
<EuiLink onClick={() => props.deps.openDateViewNumberFieldEditor()}>
Open data view field editor to give it a try.
</EuiLink>
</p>
</EuiCallOut>
</>
);
};
const CreatingCustomFieldFormatEditor: React.FC<{ deps: Deps }> = (props) => {
return (
<>
<EuiText>
<p>
This example shows how to create a custom field formatter editor. As an example, we will
create a format editor for the currency formatter created in the previous section. This
custom editor will allow to select either <EuiCode>USD</EuiCode> or <EuiCode>EUR</EuiCode>{' '}
currency.
</p>
</EuiText>
<EuiSpacer size={'s'} />
<EuiCodeBlock language="jsx">{example3SampleCode}</EuiCodeBlock>
<EuiSpacer size={'s'} />
<EuiCallOut
title="Check the result in the data view field editor!"
color="primary"
iconType="indexManagementApp"
>
<p>
Currency formatter and its custom editor are integrated with data views. It can be applied
to any <EuiCode>numeric</EuiCode> field of any data view.{' '}
<EuiLink onClick={() => props.deps.openDateViewNumberFieldEditor()}>
Open date view field editor to give it a try.
</EuiLink>
</p>
</EuiCallOut>
</>
);
};
export const App: React.FC<{ deps: Deps }> = (props) => {
return (
<EuiProvider>
<EuiPageTemplate offset={0}>
<EuiPageTemplate.Header pageTitle="Field formats examples" />
<EuiPageTemplate.Section grow={false}>
<EuiTitle size="m">
<h2>Using an existing field format</h2>
</EuiTitle>
<EuiSpacer />
<UsingAnExistingFieldFormatExample deps={props.deps} />
</EuiPageTemplate.Section>
<EuiPageTemplate.Section grow={false}>
<EuiTitle size="m">
<h2>Creating a custom field format</h2>
</EuiTitle>
<EuiSpacer />
<CreatingCustomFieldFormat deps={props.deps} />
</EuiPageTemplate.Section>
<EuiPageTemplate.Section grow={false}>
<EuiTitle size="m">
<h2>Creating a custom field format editor</h2>
</EuiTitle>
<EuiSpacer />
<CreatingCustomFieldFormatEditor deps={props.deps} />
</EuiPageTemplate.Section>
</EuiPageTemplate>
</EuiProvider>
);
};