Skip to content

Commit

Permalink
Different presets for different stages
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Apr 26, 2022
1 parent 288e124 commit 9c0b1cb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/kbn-babel-preset/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SOURCE_FILES = glob([
"istanbul_preset.js",
"node_preset.js",
"styled_components_files.js",
"web_preset.js",
"webpack_preset.js",
])

Expand Down
6 changes: 4 additions & 2 deletions packages/kbn-babel-preset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ To use our presets add the following to the devDependencies section of your pack

Then run `yarn kbn bootstrap` to properly link the package into your plugin/package.

Finally, add either `@kbn/babel-preset/node_preset` or `@kbn/babel-preset/webpack_preset` to your babel config.
Finally, add either `@kbn/babel-preset/node_preset`, `@kbn/babel-preset/web_preset` or `@kbn/babel-preset/webpack_preset` to your babel config.

`@kbn/babel-preset/node_preset` is usually placed in a [`babel.config.js` file](https://babeljs.io/docs/en/configuration#babelconfigjs).
`@kbn/babel-preset/node_preset` is usually placed in a [`babel.config.js` file](https://babeljs.io/docs/en/configuration#babelconfigjs) for server-side code.

`@kbn/babel-preset/web_preset` is used to build browser-friendly packages that will be bundled later on by `webpack` using the `@kbn/babel-preset/webpack_preset`. Refer to [the original PR for the discussion and metrics improvements](https://github.com/elastic/kibana/pull/130904).

`@kbn/babel-preset/webpack_preset` is usually placed directly in your `webpack` configuration.

Expand Down
16 changes: 16 additions & 0 deletions packages/kbn-babel-preset/web_preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

module.exports = (_, options = {}) => {
return {
// This preset is called from the /src/dev/bazel/jsts_transpiler.bzl.
// Ideally, it should simply use the `webpack_preset` with some opts but, since it is called via CLI options,
// Babel currently does not provide a way to pass options to the presets.
presets: [[require('./webpack_preset'), { ...options, esmodules: true }]],
};
};
15 changes: 6 additions & 9 deletions packages/kbn-babel-preset/webpack_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@

const { USES_STYLED_COMPONENTS } = require('./styled_components_files');

module.exports = (_, options = {}) => {
module.exports = (_, { esmodules, ...options } = {}) => {
return {
presets: [
[
require.resolve('@babel/preset-env'),
{
...(options.esmodules === false
? {}
: {
targets: {
esmodules: true,
},
modules: false,
}),
// When building the @kbn web packages, we want to emit the ES modules only.
// However, when generating the final bundles in webpack, we want `.browserslistrc` to be applied (specifying no targets gets this effect).
// This optimization has proven to generate a smaller bundle sizes overall. \o/
...(esmodules === true ? { targets: { esmodules: true } } : {}),
useBuiltIns: 'entry',
modules: false,
// Please read the explanation for this
// in node_preset.js
corejs: '3.21.1',
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-optimizer/src/worker/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ export function getWebpackConfig(bundle: Bundle, bundleRefs: BundleRefs, worker:
babelrc: false,
envName: worker.dist ? 'production' : 'development',
presets: IS_CODE_COVERAGE
? [ISTANBUL_PRESET_PATH, [BABEL_PRESET_PATH, { esmodules: false }]]
: [[BABEL_PRESET_PATH, { esmodules: false }]],
? [ISTANBUL_PRESET_PATH, [BABEL_PRESET_PATH]]
: [[BABEL_PRESET_PATH]],
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/dev/bazel/jsts_transpiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def jsts_transpiler(name, srcs, build_pkg_name, web = False, root_input_dir = "s

if web:
inline_presets += [
"@kbn/babel-preset/webpack_preset",
"@kbn/babel-preset/web_preset",
]
else:
inline_presets += [
Expand Down

0 comments on commit 9c0b1cb

Please sign in to comment.