Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
refactor: improve main module loading resolution
Browse files Browse the repository at this point in the history
Instead of rewritting main files we simply add some logic in then loader -- cf:
nodejs/node#41465

A la tchusss abomination
  • Loading branch information
lachrist committed Oct 18, 2022
1 parent 3a02a9a commit d6df3e5
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 131 deletions.
1 change: 0 additions & 1 deletion components/.ordering
Expand Up @@ -59,6 +59,5 @@ receptor
batch
questionnaire
setup
abomination
init
status
1 change: 0 additions & 1 deletion components/abomination/default/.env

This file was deleted.

63 changes: 0 additions & 63 deletions components/abomination/default/index.mjs

This file was deleted.

22 changes: 0 additions & 22 deletions components/abomination/default/index.test.mjs

This file was deleted.

6 changes: 0 additions & 6 deletions components/configuration-accessor/default/index.mjs
Expand Up @@ -231,12 +231,6 @@ export const compileConfigurationCommand = (configuration, env) => {
...env,
NODE_OPTIONS: [
coalesce(env, "NODE_OPTIONS", ""),
// abomination: https://github.com/mochajs/mocha/issues/4720
`--require=${pathifyURL(
appendURLSegmentArray(directory, ["lib", "node", "abomination.js"]),
base,
true,
)}`,
`--experimental-loader=${generateEscape(exec)(
pathifyURL(
appendURLSegmentArray(directory, [
Expand Down
2 changes: 0 additions & 2 deletions components/configuration-accessor/default/index.test.mjs
Expand Up @@ -315,7 +315,6 @@ assertDeepEqual(
env: {
NODE_OPTIONS: [
"--node-key=node-value",
"--require=../agent/lib/node/abomination.js",
"--experimental-loader=../agent/lib/node/recorder-process.mjs",
].join(" "),
VAR1: "VAL1",
Expand Down Expand Up @@ -400,7 +399,6 @@ assertDeepEqual(
env: {
NODE_OPTIONS: [
"",
"--require=../agent/lib/node/abomination.js",
"--experimental-loader=../agent/lib/node/mocha-loader.mjs",
].join(" "),
},
Expand Down
33 changes: 0 additions & 33 deletions lib/node/abomination.js

This file was deleted.

29 changes: 29 additions & 0 deletions lib/node/loader.mjs
@@ -1,4 +1,5 @@
const {
process: { stdout },
Error,
undefined,
parseInt,
Expand All @@ -18,9 +19,30 @@ const parseMajorVersion = (version) => {
}
};

let is_main = true;

const message =
"Treating main file as commonjs, this might not always be appropriate -- cf: https://github.com/nodejs/node/issues/41465\n";

export default (node_version, esm_hook_variable) => {
const major_node_version = parseMajorVersion(node_version);
return {
getFormat:
major_node_version >= 16
? undefined
: (url, context, next) => {
if (is_main) {
is_main = false;
try {
return next(url, context, next);
} catch {
stdout.write(message);
return { format: "commonjs" };
}
} else {
return next(url, context, next);
}
},
transformSource:
major_node_version >= 16
? undefined
Expand All @@ -39,6 +61,13 @@ export default (node_version, esm_hook_variable) => {
major_node_version < 16
? undefined
: (url, context, next) => {
if (is_main) {
is_main = false;
if (context.format === undefined) {
stdout.write(message);
context.format = "commonjs";
}
}
if (hasOwn(globalThis, esm_hook_variable)) {
return globalThis[esm_hook_variable].load(url, context, next);
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/node/mocha-loader.mjs
Expand Up @@ -6,7 +6,7 @@ import Mocha from "mocha";
import Loader from "./loader.mjs";

import { params, configuration } from "./configuration.mjs";
export const { transformSource, load } = Loader(
export const { getFormat, transformSource, load } = Loader(
version,
configuration.hooks.esm,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/node/recorder-process.mjs
Expand Up @@ -6,7 +6,7 @@ const {
import Loader from "./loader.mjs";
import { configuration, params } from "./configuration.mjs";

export const { transformSource, load } = Loader(
export const { getFormat, transformSource, load } = Loader(
version,
configuration.hooks.esm,
);
Expand Down
2 changes: 1 addition & 1 deletion lib/node/recorder-remote.mjs
Expand Up @@ -6,7 +6,7 @@ const {
import Loader from "./loader.mjs";
import { configuration, params } from "./configuration.mjs";

export const { transformSource, load } = Loader(
export const { getFormat, transformSource, load } = Loader(
version,
configuration.hooks.esm,
);
Expand Down

0 comments on commit d6df3e5

Please sign in to comment.