Skip to content

Import modules before others are loaded? #11

@wmertens

Description

@wmertens

The node flag -r doesn't work in ESM mode, it translates it to a require() but it should be import.

Would it be possible to use a loader to inject a module? However, the loaded module needs to pass through babel. I was thinking to maybe add the imports in front of the translated code of the first file that @node-loader/babel loads?

The use case is a repl that includes some extra globals. In pre-ESM it was just a matter of adding -r ./inject-globals but now the only way is to type await import('./inject-globals.js') manually (*).

(*): and I'm actually doing that by calling a ioctl from Perl like so:

$ perl -e 'ioctl(STDIN, 0x5412, $_) for split "", "await import('"'"'$ARGV[0]'"'"')\n";' ./inject-globals.js; node --loader @node-loader/babel
await import('./inject-globals.js')
(node:495354) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Welcome to Node.js v16.14.2.
Type ".help" for more information.
> await import('./inject-globals.js')
[... it loads]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions