Skip to content

Commit

Permalink
feat: update code to support Node 20.10+ (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer committed May 7, 2024
1 parent b3faf66 commit 39ebbbe
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest, windows-latest, macOS-latest ]
node: [ '16', '18', '20' ]
node: [ '20', '22' ]
runs-on: ${{ matrix.os }}
steps:
- name: Git checkout
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
---

## [0.6.0]
### Added
* [#27]: support for Node 22.

### Changed
* [#26]: **BREAKING CHANGE**: updated dependencies:

Expand All @@ -18,6 +21,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Dependencies with major version change are marked with the "⚠️" emoji.

### Removed
* [#27]: **BREAKING CHANGE**: support for Node < 20.10.0.


## [0.5.1] – 2023-08-02
### Fixed
Expand Down Expand Up @@ -71,6 +77,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#20]: https://github.com/Comandeer/esm-loader-manager/issues/20
[#24]: https://github.com/Comandeer/esm-loader-manager/issues/24
[#26]: https://github.com/Comandeer/esm-loader-manager/issues/26
[#27]: https://github.com/Comandeer/esm-loader-manager/issues/27

[0.6.0]: https://github.com/Comandeer/esm-loader-manager/compare/v0.5.1...v0.6.0
[0.5.1]: https://github.com/Comandeer/esm-loader-manager/compare/v0.5.0...v0.5.1
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

Like [Pirates](https://github.com/danez/pirates) but for ESM.

**Works with Node 16.12.0+**.
**Works with Node 20.10.0+**.

⚠️ As ESM loaders are still experimental in Node.js, this package should also be considered experimental. **Use in production environment at your own risk!**

## How does it work?

This package allows to use several [ESM loaders](https://nodejs.org/api/esm.html#loaders) inside one Node.js application. It basically provides an ESM loader that can be configured to pass some of importing modules to user-provided transformation functions (see [Examples](#Examples) section for more info).
This package allows to use several [module customization hooks](https://nodejs.org/docs/latest-v20.x/api/module.html#customization-hooks) inside one Node.js application. It basically provides an ESM loader that can be configured to pass some of importing modules to user-provided transformation functions (see [Examples](#Examples) section for more info).

**Built-in Node.js modules and modules located inside `node_modules` are ignored by the manager!**

Expand All @@ -26,10 +26,10 @@ The loader manager needs to be added as an ESM loader to your Node application.

### Via Node.js flag

This is probably the easiest way to use the loader manager. You can add it to your application using [the `--experimental-loader` flag](https://nodejs.org/api/cli.html#--experimental-loadermodule):
This is probably the easiest way to use the loader manager. You can add it to your application using [the `--import` flag](https://nodejs.org/api/cli.html#--importmodule):

```shell
node --experimental-loader=esm-loader-manager
node --import=esm-loader-manager/register
```

### Via `esmlm` executable
Expand Down Expand Up @@ -84,7 +84,7 @@ The configuration file needs to export an object as a default export. This objec

Each loader is an object with two methods: `matcher()` and `loader()`.

#### matcher( url: string, context: [LoaderContext ](https://nodejs.org/api/esm.html#resolvespecifier-context-defaultresolve)): boolean
#### matcher( url: string, context: [ResolveContext](https://nodejs.org/api/module.html#resolvespecifier-context-nextresolve) ): boolean

The task of this method is to check if the module identified by the passed URL should be handled by this particular loader. If yes, it should return `true` and `false` otherwise.

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"description": "Allows to use several ESM loaders in one Node.js application.",
"type": "module",
"exports": {
"types": "./dist/esm-loader-manager.d.ts",
"import": "./dist/esm-loader-manager.mjs"
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs"
},
"./register": {
"import": "./dist/register.mjs"
}
},
"bin": {
"esmlm": "./bin/esmlm.js"
Expand All @@ -15,7 +20,7 @@
"dist"
],
"engines": {
"node": ">=16.12.0"
"node": ">=20.10.0"
},
"engineStrict": true,
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions src/__bin__/esmlm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import { pathToFileURL } from 'node:url';
import { execa } from 'execa';

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const loaderPath = resolvePath( __dirname, '..', 'dist', 'esm-loader-manager.mjs' );
const loaderURL = pathToFileURL( loaderPath );
const loaderPath = resolvePath( __dirname, '..', 'dist', 'register.mjs' );
const registerModuleUrl = pathToFileURL( loaderPath );
const cwd = processCWD();
const [ ,, passedEntryPoint, ...args ] = argv;
const entryPoint = passedEntryPoint !== undefined ? resolvePath( cwd, passedEntryPoint ) : cwd;
const cmd = 'node';
const params = [
'--experimental-loader',
loaderURL.href,
'--import',
registerModuleUrl.href,
entryPoint,
...args
];
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import resolveProjectRoot from './utilities/resolveProjectRoot.js';
import loadURL from './utilities/loadURL.js';

type ModuleFormat = 'builtin' | 'commonjs' | 'json' | 'module' | 'wasm';
type ImportAssertions = Record<string, unknown>;
type ImportAttributes = Record<string, unknown>;
type TypedArray =
| BigInt64Array
| BigUint64Array
Expand All @@ -29,13 +29,13 @@ type TypedArray =

interface ResolverContext {
conditions: Array<string>;
importAssertions: ImportAssertions;
importAttributes: ImportAttributes;
parentURL?: string | undefined;
}

interface ResolverResult {
format: ModuleFormat | null | undefined;
importAssertions?: ImportAssertions;
importAssertions?: ImportAttributes;
shortCircuit?: boolean;
url: string;
}
Expand All @@ -49,7 +49,7 @@ type NextResolver = (
interface LoaderContext {
conditions: Array<string>;
format: ModuleFormat | null | undefined;
importAssertions: ImportAssertions;
importAttributes: ImportAttributes;
}

interface LoaderResult {
Expand All @@ -71,7 +71,7 @@ export interface ModuleInfo {

export interface MatcherContext {
conditions: Array<string>;
importAssertions: ImportAssertions;
importAttributes: ImportAttributes;
parentURL?: string | undefined;
}

Expand Down
3 changes: 3 additions & 0 deletions src/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { register } from 'node:module';

register( './index.mjs', import.meta.url );
8 changes: 4 additions & 4 deletions tests/__helpers__/macros/testLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const testLoader = test.macro( ( t: ExecutionContext, {
env = {},
callback: userCallback
}: TestLoaderOptions ): Promise<void> => {
const loaderPath = resolvePath( __dirname, '..', '..', '..', 'dist', 'esm-loader-manager.mjs' );
const loaderURL = pathToFileURL( loaderPath );
const registerPath = resolvePath( __dirname, '..', '..', '..', 'dist', 'register.mjs' );
const registerURL = pathToFileURL( registerPath );
const cmd = 'tsx';
const params = [
'--experimental-loader',
loaderURL.href,
'--import',
registerURL.href,
entryPoint
];

Expand Down

0 comments on commit 39ebbbe

Please sign in to comment.