Skip to content

Commit

Permalink
v1.40.0: [#375] Improvements of Babel & Webpack configs
Browse files Browse the repository at this point in the history
  • Loading branch information
birdofpreyru committed Aug 4, 2024
1 parent 04f0def commit 713e213
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 97 deletions.
12 changes: 12 additions & 0 deletions __tests__/config/babel/__snapshots__/node-ssr.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ exports[`"development" environment "baseAssetsOutputPath" option 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -130,6 +131,7 @@ exports[`"development" environment "baseAssetsOutputPath" option 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -186,6 +188,7 @@ exports[`"development" environment "noStyling" variation 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -248,6 +251,7 @@ exports[`"development" environment "noStyling" variation 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -317,6 +321,7 @@ exports[`"development" environment Default config 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -392,6 +397,7 @@ exports[`"development" environment Default config 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -461,6 +467,7 @@ exports[`"production" environment "baseAssetsOutputPath" option 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -536,6 +543,7 @@ exports[`"production" environment "baseAssetsOutputPath" option 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -592,6 +600,7 @@ exports[`"production" environment "noStyling" variation 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -654,6 +663,7 @@ exports[`"production" environment "noStyling" variation 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -723,6 +733,7 @@ exports[`"production" environment Default config 1`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down Expand Up @@ -798,6 +809,7 @@ exports[`"production" environment Default config 2`] = `
[
"@babel/env",
{
"modules": "cjs",
"targets": "current node",
},
],
Expand Down
8 changes: 8 additions & 0 deletions __tests__/config/babel/__snapshots__/webpack.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports[`"development" environment "noStyling" variation 1`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -70,6 +71,7 @@ exports[`"development" environment "noStyling" variation 2`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -121,6 +123,7 @@ exports[`"development" environment Default config 1`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -178,6 +181,7 @@ exports[`"development" environment Default config 2`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -216,6 +220,7 @@ exports[`"production" environment "noStyling" variation 1`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -260,6 +265,7 @@ exports[`"production" environment "noStyling" variation 2`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -310,6 +316,7 @@ exports[`"production" environment Default config 1`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down Expand Up @@ -366,6 +373,7 @@ exports[`"production" environment Default config 2`] = `
[
"@babel/env",
{
"modules": false,
"targets": "defaults or chrome >= 69",
},
],
Expand Down
18 changes: 10 additions & 8 deletions config-ts-src/babel/node-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@ type OptionsT = WebpackConfigOptionsT & {

/**
* Creates a new base config.
* @param {object} babel Babel compiler.
* @param {object} [options] It supports all options of
* {@link module:babel/webpack.getPreset babel/webpack's getPreset()},
* but it overrides `targets` option with `current node` value, and further
* @param babel Babel compiler.
* @param [options] It supports all options of our Babel config for Webpack,
* but it overrides `targets` option with "current node" value, and further
* accepts the following:
* @param {string} [options.baseAssetsOutputPath] Path prefix for emitted
* @param [options.baseAssetsOutputPath] Path prefix for emitted
* image assets.
* @return {object} Created config object.
* @ignore
* @return Created config object.
*/
function newBase(babel: BabelCompilerI, options: OptionsT = {}) {
const config = getWebpackBabelConfig(
babel,
{ ...options, targets: 'current node' },
{
modules: 'cjs',
targets: 'current node',
...options,
},
);

const baseAssetsOutputPath = options.baseAssetsOutputPath || '';
Expand Down
14 changes: 12 additions & 2 deletions config-ts-src/babel/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export type ConfigT = {
plugins: PresetOrPluginT[];
};

// Valid values for `module` option of @babel/preset-env,
// as per https://babeljs.io/docs/babel-preset-env#modules
type ModuleT = 'amd' | 'auto' | 'cjs' | 'commonjs' | 'systemjs' | 'umd' | false;

export type OptionsT = {
modules?: ModuleT;
noRR?: boolean;
noStyling?: boolean;
targets?: string | string[] | { [key: string]: string };
Expand All @@ -53,8 +58,13 @@ export type OptionsT = {
function newBaseConfig(options: OptionsT): ConfigT {
return {
presets: [
// Chrome 69 is the browser for Android API 28.
['@babel/env', { targets: options.targets || 'defaults or chrome >= 69' }],
['@babel/env', {
// Leaves it to the Webpack to deal with modules.
modules: options.modules ?? false,

// Chrome 69 is the browser/WebView for Android 9 (API level 28).
targets: options.targets || 'defaults or chrome >= 69',
}],

// TODO: Starting from Babel 8, "automatic" will be the default runtime,
// thus once upgraded to Babel 8, runtime should be removed from
Expand Down
24 changes: 18 additions & 6 deletions config-ts-src/webpack/app-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import path from 'path';

import SM from 'sitemap';

import { type Configuration, type WebpackPluginInstance } from 'webpack';

import {
clone,
defaults,
Expand All @@ -23,7 +21,14 @@ import autoprefixer from 'autoprefixer';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import forge from 'node-forge';

import { DefinePlugin, ProgressPlugin } from 'webpack';
import {
type Configuration,
type RuleSetRule,
type WebpackPluginInstance,
DefinePlugin,
ProgressPlugin,
} from 'webpack';

import WorkboxPlugin from 'workbox-webpack-plugin';

import {
Expand All @@ -41,7 +46,11 @@ export type BuildInfoT = {

export type OptionsT = {
babelEnv: string;
babelLoaderExclude?: RuleSetRule['exclude'];

// TODO: Find and use the actual type for Babel options object.
babelLoaderOptions?: object;

context: string;
cssLocalIdent?: string;
dontEmitBuildInfo?: boolean;
Expand All @@ -65,6 +74,8 @@ export type OptionsT = {
* directly with the created config object.
* @param {string} ops.babelEnv Babel environment to use for the Babel
* compilation step.
* @param [ops.babelLoaderExclude] Overrides the default value of `exclude`
* option of babel-loader, which is [/node_modules/].
* @param {object} [ops.babelLoaderOptions] Overrides for default Babel options
* of JSX and SVG files loader.
* @param ops.context Base URL for resolution of relative config paths.
Expand Down Expand Up @@ -320,9 +331,10 @@ export default function configFactory(ops: OptionsT): Configuration {
enforce: 'pre',
use: ['source-map-loader'],
}, {
/* Loads JS and JSX moudles, and inlines SVG assets. */
test: ops.typescript ? /\.((j|t)sx?|svg)$/ : /\.(jsx?|svg)$/,
exclude: [/node_modules/],
// Loads JS modules (.cjs, .js, .jsx); TS modules (.ts, .tsx);
// and SVG assets (.svg).
test: ops.typescript ? /\.(cjs|(j|t)sx?|svg)$/ : /\.(cjs|jsx?|svg)$/,
exclude: ops.babelLoaderExclude ?? [/node_modules/],
loader: 'babel-loader',
options: {
babelrc: false,
Expand Down
5 changes: 5 additions & 0 deletions config-ts-src/webpack/app-production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export default function configFactory(ops: OptionsT): Configuration {
];

const res = merge(baseFactory({
babelLoaderExclude: [],
...ops,
babelEnv: 'production',
babelLoaderOptions: {
compact: true,
...ops.babelLoaderOptions,
},
entry,
mode: 'production',
}), {
Expand Down
1 change: 1 addition & 0 deletions config-ts-src/webpack/lib-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default function configFactory(ops: OptionsT): Configuration {
'@dr.pogodin/react-themes',
'@dr.pogodin/react-utils',
'axios',
'cookie',
'dayjs',
'lodash',
/node-forge/,
Expand Down
Loading

0 comments on commit 713e213

Please sign in to comment.