Skip to content

Commit

Permalink
chore(babel): modernize config and clean webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Jun 20, 2024
1 parent 02d141f commit 048eaec
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"babelrcRoots": [
"."
]
}
42 changes: 23 additions & 19 deletions webpack.config.cjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
const fs = require('fs');
const path = require('path');
const ESLintPlugin = require('eslint-webpack-plugin');

const mode = process.env.NODE_ENV;
const noInline = process.env.noInline;
const debugBuild = mode === 'development';

/*
configuring babel:
- when babel runs alone (for `test-unit` for instance), we let him deal with
ES6 modules, because node doesn't support them yet (planned for v10 lts).
- however, webpack also has ES6 module support and these 2 don't play well
together. When running webpack (either `build` or `start` script), we prefer
to rely on webpack loaders (much more powerful and gives more possibilities),
so let's disable modules for babel here.
- we also dynamise the value of __DEBUG__ according to the env var
*/
// Note that we don't support .babelrc in parent folders
const babelrc = fs.readFileSync(path.resolve(__dirname, '.babelrc'));
const babelConf = JSON.parse(babelrc);

babelConf.babelrc = false; // disabel babelrc reading, as we've just done it
const replacementPluginConf = babelConf.plugins.find(plugin => Array.isArray(plugin) && plugin[0] === 'minify-replace');
replacementPluginConf[1].replacements.find(decl => decl.identifierName === '__DEBUG__').replacement.value = debugBuild;
// For each file, this config is merged with its local babel configuration.
// See https://babeljs.io/docs/configuration#how-babel-merges-config-items
const babelConf = {
rootMode: 'upward',
plugins: [
['minify-replace', {
replacements: [{
identifierName: '__DEBUG__',
replacement: {
type: 'booleanLiteral',
value: debugBuild,
},
}],
}],
],
};

const include = [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'test'),
path.resolve(__dirname, 'utils'),
];

const exclude = [
path.resolve(__dirname, '.git'),
path.resolve(__dirname, 'node_modules'),
];

module.exports = () => {
const babelLoaderOptions = [];
if (!noInline) {
Expand Down Expand Up @@ -72,6 +75,7 @@ module.exports = () => {
rules: [
{
test: /\.js$/,
exclude,
include,
use: babelLoaderOptions,
},
Expand All @@ -89,7 +93,7 @@ module.exports = () => {
static: {
directory: path.resolve(__dirname, './'),
watch: {
ignored: [path.resolve(__dirname, '.git'), path.resolve(__dirname, 'node_modules')],
ignored: exclude,
},
},
client: {
Expand Down

0 comments on commit 048eaec

Please sign in to comment.