kibana/packages/kbn-ui-shared-deps-npm/webpack.config.js
Clint Andrew Hall d0fde5f8e1
[React@18] Env variable to use React@18 (#193113)
Part of
https://github.com/elastic/kibana-team/issues/1016#issuecomment-2454845292

Our plan for React@18 packages upgrade is to let kibana contributors now
that we're going to bump React packages couple weeks in advance. In
addtion to the final PR with green tests and Kibana deployed, we want to
give simple instructions on how to run React@18 locally easilly:

This PR allows to quickly toggle between version of React locally
without having to do anything beyond an environment variable.

`REACT_18=true yarn bootstrap` will alias `react` and `react-dom` to v18
in the build.

I check that this works as expected when starting from: 

- local dev server `yarn start` 
- local ftr `node scripts/functional_tests_server.js`
- local unit tests `REACT_18=true yarn test:jest ...`

Please note: 
- **This PR doesn't implement this switch for dist build, as I don't
think we need this for our purposes.**
- The plan is that we remove this switch soon after we merge React@18
upgrade to main.

In addition to the switch this PR mutes a very noisy warning from
React@18 about legacy root `Warning: ReactDOM.render is no longer
supported in React 18. Use createRoot instead. Until you switch to the
new API, your app will behave as if it's running React 17.`. This
warning is expected as after we upgrade to React@18 packages (Phase 1)
we will be in the process of migrating to the new createRoot API (Phase
2). However, it is very noisy and we want to mute it for now.


Co-authored-by: Anton Dosov <anton.dosov@elastic.co>
2024-12-09 13:55:14 +01:00

183 lines
5.6 KiB
JavaScript

/*
* 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".
*/
const Path = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const UiSharedDepsNpm = require('.');
const MOMENT_SRC = require.resolve('moment/min/moment-with-locales.js');
const WEBPACK_SRC = require.resolve('webpack');
const REPO_ROOT = Path.resolve(__dirname, '..', '..');
module.exports = (_, argv) => {
const outputPath = argv.outputPath ? Path.resolve(argv.outputPath) : UiSharedDepsNpm.distDir;
return {
node: {
child_process: 'empty',
fs: 'empty',
},
externals: {
module: 'module',
},
mode: process.env.NODE_ENV || 'development',
entry: {
'kbn-ui-shared-deps-npm': [
// polyfill code
'core-js/stable',
'whatwg-fetch',
'symbol-observable',
// Parts of node-libs-browser that are used in many places across Kibana
'buffer',
'punycode',
'util',
/**
* babel runtime helpers referenced from entry chunks
* determined by running:
*
* node scripts/build_kibana_platform_plugins --dist --profile
* node scripts/find_babel_runtime_helpers_in_use.js
*/
'@babel/runtime/helpers/assertThisInitialized',
'@babel/runtime/helpers/classPrivateFieldGet',
'@babel/runtime/helpers/classPrivateFieldSet',
'@babel/runtime/helpers/defineProperty',
'@babel/runtime/helpers/extends',
'@babel/runtime/helpers/inheritsLoose',
'@babel/runtime/helpers/taggedTemplateLiteralLoose',
'@babel/runtime/helpers/wrapNativeSuper',
// modules from npm
'@elastic/apm-rum-core',
'@elastic/charts',
'@elastic/eui',
'@elastic/eui/optimize/es/components/provider/nested',
'@elastic/eui/optimize/es/services',
'@elastic/eui/optimize/es/services/format',
'@elastic/eui/dist/eui_theme_amsterdam_light.json',
'@elastic/eui/dist/eui_theme_amsterdam_dark.json',
'@elastic/eui/dist/eui_theme_borealis_light.json',
'@elastic/eui/dist/eui_theme_borealis_dark.json',
'@elastic/numeral',
'@emotion/cache',
'@emotion/react',
'@hello-pangea/dnd/dist/dnd.js',
'@reduxjs/toolkit',
'redux',
'react-redux',
'immer',
'@tanstack/react-query',
'@tanstack/react-query-devtools',
'classnames',
'fflate',
'fastest-levenshtein',
'history',
'io-ts',
'jquery',
'lodash',
'lodash/fp',
'moment-timezone/moment-timezone',
'moment-timezone/data/packed/latest.json',
'moment',
'react-dom',
'react-dom/server',
'react-router-dom',
'react-router-dom-v5-compat',
'react-router',
'react',
'reselect',
'rxjs',
'styled-components',
'tslib',
'uuid',
],
},
context: __dirname,
devtool: 'cheap-source-map',
output: {
path: outputPath,
filename: '[name].dll.js',
chunkFilename: 'kbn-ui-shared-deps-npm.chunk.[id].js',
devtoolModuleFilenameTemplate: (info) =>
`kbn-ui-shared-deps-npm/${Path.relative(REPO_ROOT, info.absoluteResourcePath)}`,
library: '__kbnSharedDeps_npm__',
futureEmitAssets: true,
},
module: {
noParse: [MOMENT_SRC, WEBPACK_SRC],
rules: [
{
include: [require.resolve('jquery')],
use: [
{
loader: UiSharedDepsNpm.publicPathLoader,
options: {
key: 'kbn-ui-shared-deps-npm',
},
},
],
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader'],
},
],
},
resolve: {
alias: {
'@elastic/eui$': '@elastic/eui/optimize/es',
moment: MOMENT_SRC,
// NOTE: Used to include react profiling on bundles
// https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977#webpack-4
'react-dom$':
process.env.REACT_18 === 'true' ? 'react-dom-18/profiling' : 'react-dom/profiling',
'scheduler/tracing': 'scheduler/tracing-profiling',
react: process.env.REACT_18 === 'true' ? 'react-18' : 'react',
},
extensions: ['.js', '.ts'],
},
optimization: {
minimize: false,
noEmitOnErrors: true,
},
performance: {
// NOTE: we are disabling this as those hints
// are more tailored for the final bundles result
// and not for the webpack compilations performance itself
hints: false,
},
plugins: [
new CleanWebpackPlugin({
protectWebpackAssets: false,
cleanAfterEveryBuildPatterns: [
'kbn-ui-shared-deps-npm.v8.{dark,light}.{dll.js,dll.js.map}',
'kbn-ui-shared-deps-npm.v8.{dark,light}-manifest.json',
],
}),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
new webpack.DllPlugin({
context: REPO_ROOT,
path: Path.resolve(outputPath, '[name]-manifest.json'),
name: '__kbnSharedDeps_npm__',
}),
],
};
};