Skip to content

Commit

Permalink
chore: use node hooks to fix coverage issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Aug 30, 2024
1 parent d091207 commit 50e785c
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
104 changes: 104 additions & 0 deletions config/babel-register/babel-hooks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { fileURLToPath } from 'url';
import * as path from 'path';
import * as babel from '@babel/core';

/**
* This script is a [node:module hook][https://nodejs.org/api/module.html] to
* override module resolution and source-code loading to transpile files with
* babel.
*/

// Regular expressions from
// https://github.com/babel/babel/blob/main/packages/babel-register/src/worker/transform.js
function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}

const nmRE = escapeRegExp(`${path.sep}node_modules${path.sep}`);
const cwd = path.resolve('.');
const cwdRE = escapeRegExp(cwd);

/**
* @param {string | SharedArrayBuffer | Uint8Array} source
* @param {Object} context
* @param {string} context.url
* @param {string} context.format
*/
async function transpile(source, context) {
const { url, format } = context;
if (format !== 'module' && format !== 'commonjs') {
return source;
}

let input;
if (typeof source === 'string') {
input = source;
} else if (Buffer.isBuffer(source)) {
input = source.toString('utf-8');
} else {
input = Buffer.from(source).toString('utf-8');
}

// transformAsync merges options in .babelrc with the ones provided below
const transform = await babel.transformAsync(input, {
sourceType: 'module',
filename: fileURLToPath(url),
sourceMaps: 'inline',
ast: false,
ignore: [
// do not transpile files in node_modules
new RegExp(`^${cwdRE}(?:${path.sep}.*)?${nmRE}`, 'i'),
],
});

if (!transform?.code) {
return;
}

return transform.code;
}

/**
* @param {string} specifier
* @param {Object} context
* @param {string[]} context.conditions - Export conditions of the relevant
* package.json
* @param {Object} context.importAttributes - An object whose key-value pairs
* represent the attributes for the module to import
* @param {string | undefined} context.parentURL - The module importing this
* one, or undefined if this is the Node.js entry point
* @param {Function} nextResolve - The subsequent resolve hook in the chain, or
* the Node.js default resolve hook after the last user-supplied resolve hook
*/
export async function resolve(specifier, context, nextResolve) {
return nextResolve(specifier, context);
}

/**
* @param {string} url - The URL returned by the resolve chain
* @param {Object} context
* @param {string[]} context.conditions - Export conditions of the relevant
* package.json
* @param {string | null | undefined} context.format - The format optionally
* supplied by the resolve hook chain
* @param {Object} context.importAttributes
* @param {function(string, object): Promise<{ format: string, shortCircuit: boolean, source: string }>} nextLoad -
* The subsequent load hook in the chain, or the Node.js default load hook after
* the last user-supplied load hook
*/
export async function load(url, context, nextLoad) {
const { format, shortCircuit, source } = await nextLoad(url, context);

if (format !== 'module' && format !== 'commonjs') {
return { format, shortCircuit, source };
}

if (source) {
const code = await transpile(source, { format, url });
if (code) {
return { source: code, format };
}
}

return { format, source };
}
3 changes: 3 additions & 0 deletions config/babel-register/register.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { register } from 'node:module';

register('./babel-hooks.mjs', import.meta.url);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"test-functional": "mocha -t 100000 --require test/hooks_functional.js --recursive test/functional",
"test-with-coverage": "c8 -n src -r html cross-env npm run test-unit",
"test-with-coverage_lcov": "c8 -n src --reporter=lcov cross-env npm run test-unit",
"base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 mocha --file test/unit/bootstrap.js --loader=babel-register-esm",
"base-test-unit": "cross-env BABEL_DISABLE_CACHE=1 mocha --file test/unit/bootstrap.js --import=./config/babel-register/register.mjs",
"build": "cross-env NODE_ENV=production webpack",
"build-dev": "cross-env NODE_ENV=development webpack",
"transpile": "cross-env BABEL_DISABLE_CACHE=1 babel src --out-dir lib",
Expand Down

0 comments on commit 50e785c

Please sign in to comment.