Skip to content

Commit 89c2a60

Browse files
authored
refactor: support Node.js 20.x.x (#8)
* fix: use optional chaining for accessing importAttributes otherwise Node 20.x will fail since it has been introduced after * feat: support node 20/21 * ci: remove 22 from ci for now * chore: add changeset
1 parent 2fc1a95 commit 89c2a60

File tree

10 files changed

+85
-63
lines changed

10 files changed

+85
-63
lines changed

.changeset/wise-scissors-breathe.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@hot-hook/dump-viewer": patch
3+
"hot-hook": patch
4+
"@hot-hook/runner": patch
5+
---
6+
7+
Add support for Node.js 20. We removed the direct usage of `importAttributes` that was only introduced in Node.js 20.9. Hot-hook should works fine with Node.js 20.0.0 and above.

.github/workflows/check.yml

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,74 +7,75 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- uses: actions/checkout@v4
10+
- uses: actions/checkout@v4
1111

12-
- name: Setup Node.js
13-
uses: actions/setup-node@v3
14-
with:
15-
node-version: 21
12+
- name: Setup Node.js
13+
uses: actions/setup-node@v3
14+
with:
15+
node-version: 21
1616

17-
- name: Install pnpm
18-
run: |
19-
corepack enable
20-
corepack prepare pnpm@latest --activate
17+
- name: Install pnpm
18+
run: |
19+
corepack enable
20+
corepack prepare pnpm@latest --activate
2121
22-
- name: Install dependencies
23-
run: pnpm install
22+
- name: Install dependencies
23+
run: pnpm install
2424

25-
- name: Lint
26-
run: pnpm lint
25+
- name: Lint
26+
run: pnpm lint
2727

2828
typecheck:
2929
runs-on: ubuntu-latest
3030

3131
steps:
32-
- uses: actions/checkout@v4
32+
- uses: actions/checkout@v4
3333

34-
- name: Setup Node.js
35-
uses: actions/setup-node@v3
36-
with:
37-
node-version: 21
34+
- name: Setup Node.js
35+
uses: actions/setup-node@v3
36+
with:
37+
node-version: 21
3838

39-
- name: Install pnpm
40-
run: |
41-
corepack enable
42-
corepack prepare pnpm@latest --activate
39+
- name: Install pnpm
40+
run: |
41+
corepack enable
42+
corepack prepare pnpm@latest --activate
4343
44-
- name: Install dependencies
45-
run: |
46-
pnpm install
47-
pnpm -r build
44+
- name: Install dependencies
45+
run: |
46+
pnpm install
47+
pnpm -r build
4848
49-
- name: Typecheck
50-
run: pnpm typecheck
49+
- name: Typecheck
50+
run: pnpm typecheck
5151

5252
tests:
5353
strategy:
5454
matrix:
5555
os: [ubuntu-latest, windows-latest]
56+
node: [20, 21]
5657
runs-on: ${{ matrix.os }}
5758
timeout-minutes: 10
5859

5960
steps:
60-
- uses: actions/checkout@v4
61-
62-
- name: Setup Node.js
63-
uses: actions/setup-node@v4
64-
with:
65-
node-version: 21
66-
67-
- name: Install pnpm
68-
run: |
69-
corepack enable
70-
corepack prepare pnpm@latest --activate
71-
72-
- name: Install dependencies
73-
run: |
74-
pnpm install
75-
pnpm -r build
76-
77-
- name: Run tests
78-
env:
79-
FORCE_COLOR: 1
80-
run: pnpm test
61+
- uses: actions/checkout@v4
62+
63+
- name: Setup Node.js
64+
uses: actions/setup-node@v4
65+
with:
66+
node-version: ${{ matrix.node }}
67+
68+
- name: Install pnpm
69+
run: |
70+
corepack enable
71+
corepack prepare pnpm@latest --activate
72+
73+
- name: Install dependencies
74+
run: |
75+
pnpm install
76+
pnpm -r build
77+
78+
- name: Run tests
79+
env:
80+
FORCE_COLOR: 1
81+
run: pnpm test

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@types/supertest": "^6.0.2",
2626
"c8": "^9.1.0",
2727
"del-cli": "^5.1.0",
28+
"desm": "^1.3.1",
2829
"eslint": "^8.57.0",
2930
"execa": "^8.0.1",
3031
"fs-extra": "^11.2.0",

packages/dump_viewer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"dependencies": {
2828
"@unocss/reset": "^0.59.0",
29+
"desm": "^1.3.1",
2930
"preact": "^10.19.6",
3031
"vis-data": "^7.1.9",
3132
"vis-network": "^9.1.9"

packages/dump_viewer/src/dump_viewer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'node:path'
1+
import { join } from 'desm'
22
import { readFile } from 'node:fs/promises'
33

44
/**
@@ -14,7 +14,7 @@ export async function dumpViewer() {
1414
/**
1515
* Load the HTML content and replace the placeholder with the dump
1616
*/
17-
const htmlLocation = join(import.meta.dirname, 'index.html')
17+
const htmlLocation = join(import.meta.url, 'index.html')
1818
let html = await readFile(htmlLocation, 'utf8')
1919
html = html.replace('$__hot_hook_placeholder__', JSON.stringify(dump))
2020

packages/hot_hook/bin/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { configure, processCLIArgs, run } from '@japa/runner'
1+
import { join } from 'desm'
22
import { assert } from '@japa/assert'
3-
import { fileSystem } from '@japa/file-system'
43
import { snapshot } from '@japa/snapshot'
5-
import { join } from 'node:path'
4+
import { fileSystem } from '@japa/file-system'
5+
import { configure, processCLIArgs, run } from '@japa/runner'
66

77
processCLIArgs(process.argv.splice(2))
88
configure({
99
files: ['tests/**/*.spec.ts'],
1010
plugins: [
1111
assert(),
12-
fileSystem({ basePath: join(import.meta.dirname, '../tmp'), autoClean: true }),
12+
fileSystem({ basePath: join(import.meta.url, '../tmp'), autoClean: true }),
1313
snapshot(),
1414
],
1515
})

packages/hot_hook/src/loader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class HotHookLoader {
143143
url = parsedUrl.href
144144
}
145145

146-
if (context.importAttributes.hot) {
146+
if (context.importAttributes?.hot) {
147147
delete context.importAttributes.hot
148148
}
149149

@@ -183,7 +183,7 @@ export class HotHookLoader {
183183
} else {
184184
const parentPath = fileURLToPath(parentUrl)
185185
const isHardcodedBoundary = this.#hardcodedBoundaryMatcher.match(resultPath)
186-
const reloadable = context.importAttributes.hot === 'true' ? true : isHardcodedBoundary
186+
const reloadable = context.importAttributes?.hot === 'true' ? true : isHardcodedBoundary
187187

188188
this.#dependencyTree.addDependency(parentPath, { path: resultPath, reloadable })
189189
}

packages/hot_hook/tests/helpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import fs from 'fs-extra'
2+
import { join } from 'desm'
3+
import path from 'node:path'
24
import pTimeout from 'p-timeout'
35
import { pEvent } from 'p-event'
4-
import path, { join } from 'node:path'
56
import { getActiveTest } from '@japa/runner'
67
import { NodeOptions, execaNode } from 'execa'
78

8-
export const projectRoot = join(import.meta.dirname, '../')
9+
export const projectRoot = join(import.meta.url, '../')
910

1011
export async function fakeInstall(destination: string) {
1112
const { name: packageName, bin = {} } = await fs.readJson(

packages/runner/bin/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { configure, processCLIArgs, run } from '@japa/runner'
1+
import { join } from 'desm'
22
import { assert } from '@japa/assert'
3-
import { fileSystem } from '@japa/file-system'
43
import { snapshot } from '@japa/snapshot'
5-
import { join } from 'node:path'
4+
import { fileSystem } from '@japa/file-system'
5+
import { configure, processCLIArgs, run } from '@japa/runner'
66

77
processCLIArgs(process.argv.splice(2))
88
configure({
99
files: ['tests/**/*.spec.ts'],
1010
plugins: [
1111
assert(),
12-
fileSystem({ basePath: join(import.meta.dirname, '../tmp'), autoClean: true }),
12+
fileSystem({ basePath: join(import.meta.url, '../tmp'), autoClean: true }),
1313
snapshot(),
1414
],
1515
})

pnpm-lock.yaml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)