|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -/* eslint-disable import/extensions */ |
| 3 | +/** |
| 4 | + * @file Compilation configuration |
| 5 | + * |
| 6 | + * Library is compiled to two targets before publishing, a CommonJS version and |
| 7 | + * an ESModules version. Both are compiled to work across the maximum number of |
| 8 | + * active browsers using Browserslist "defaults" target |
| 9 | + * (https://github.com/browserslist/browserslist#best-practices). |
| 10 | + * |
| 11 | + * Polyfills are _not_ included in the compilation: There is no clear guidance |
| 12 | + * on whether they should be included and most libraries do not include them. |
| 13 | + * This is probably because most current frameworks, like Next.js and Create |
| 14 | + * React App, include their own set of sensible polyfills. |
| 15 | + */ |
4 | 16 |
|
5 |
| -module.exports = { |
6 |
| - // Base configs are used by ESLint babel parser |
7 |
| - presets: ['@babel/preset-react', '@babel/preset-typescript'], |
| 17 | +const { BABEL_ENV } = process.env |
8 | 18 |
|
9 |
| - env: { |
10 |
| - /** |
11 |
| - * Test env mimics production, but uses commonjs modules because Jest |
12 |
| - * doesn't support ESModules and operates directly on source code. |
13 |
| - */ |
14 |
| - test: { |
15 |
| - presets: [ |
16 |
| - ['@babel/preset-env', { modules: 'commonjs', targets: 'node 16' }], |
17 |
| - '@babel/preset-react', |
18 |
| - '@babel/preset-typescript', |
19 |
| - ], |
20 |
| - plugins: [], |
21 |
| - }, |
| 19 | +const targets = BABEL_ENV === 'test' ? 'node 16' : 'defaults' // Testing runs in Node |
| 20 | +const useESM = BABEL_ENV === 'browser' |
22 | 21 |
|
23 |
| - // Publish targets |
24 |
| - // --------------------------------------------------------------------------- |
25 |
| - |
26 |
| - // CommonJS - ES5 syntax with commonJS modules |
27 |
| - commonjs: { |
28 |
| - presets: [ |
29 |
| - [ |
30 |
| - '@babel/preset-env', |
31 |
| - { |
32 |
| - corejs: '3', |
33 |
| - modules: 'commonjs', |
34 |
| - targets: 'node 14', |
35 |
| - useBuiltIns: 'usage', |
36 |
| - }, |
| 22 | +module.exports = { |
| 23 | + // Base configs are used by ESLint babel parser |
| 24 | + presets: [ |
| 25 | + [ |
| 26 | + '@babel/preset-env', |
| 27 | + { |
| 28 | + bugfixes: true, |
| 29 | + modules: useESM ? false : 'commonjs', |
| 30 | + targets, |
| 31 | + exclude: [ |
| 32 | + 'transform-typeof-symbol', // https://github.com/facebook/create-react-app/issues/5277 |
37 | 33 | ],
|
38 |
| - '@babel/preset-react', |
39 |
| - '@babel/preset-typescript', |
40 |
| - ], |
41 |
| - plugins: [ |
42 |
| - [ |
43 |
| - '@babel/plugin-transform-runtime', |
| 34 | + }, |
| 35 | + ], |
| 36 | + ['@babel/preset-react', { runtime: 'automatic' }], |
| 37 | + '@babel/preset-typescript', |
| 38 | + ], |
44 | 39 |
|
45 |
| - { |
46 |
| - corejs: '3', |
47 |
| - helpers: true, |
48 |
| - regenerator: true, |
49 |
| - useESModules: false, |
50 |
| - version: '^7.17.0', // Include version for smaller bundle |
51 |
| - }, |
52 |
| - ], |
53 |
| - ], |
54 |
| - }, |
55 |
| - // ESM - ES5 syntax with ESModules |
56 |
| - browser: { |
57 |
| - presets: [ |
58 |
| - [ |
59 |
| - '@babel/preset-env', |
60 |
| - { |
61 |
| - corejs: '3', |
62 |
| - modules: false, |
63 |
| - targets: 'defaults', |
64 |
| - useBuiltIns: 'usage', |
65 |
| - }, |
66 |
| - ], |
67 |
| - '@babel/preset-react', |
68 |
| - '@babel/preset-typescript', |
69 |
| - ], |
70 |
| - plugins: [ |
71 |
| - [ |
72 |
| - '@babel/plugin-transform-runtime', |
73 |
| - { |
74 |
| - corejs: '3', |
75 |
| - helpers: true, |
76 |
| - regenerator: true, |
77 |
| - useESModules: true, |
78 |
| - version: '^7.17.0', // Include version for smaller bundle |
79 |
| - }, |
80 |
| - ], |
81 |
| - ], |
82 |
| - }, |
83 |
| - // ℹ️ Local dev uses the default Storybook Babel configs |
84 |
| - }, |
| 40 | + plugins: [ |
| 41 | + [ |
| 42 | + '@babel/plugin-transform-runtime', |
| 43 | + { |
| 44 | + useESModules: useESM, |
| 45 | + version: '^7.17.0', // Default is 7.0, include current version for smaller bundle improvements |
| 46 | + }, |
| 47 | + ], |
| 48 | + ], |
85 | 49 | }
|
0 commit comments