Skip to content

Commit

Permalink
feat: support Node 22 (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilpatrick authored Jan 14, 2025
1 parent e2d6225 commit 07b9600
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
working-directory: "./packages/${{ inputs.package }}"
strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]
steps:
- name: "Checkout"
uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/playwright.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]
runs-on: ${{ matrix.os }}
steps:
- name: "Checkout"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
working-directory: ./packages/${{ inputs.package }}
strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]
steps:
- name: "Checkout"
uses: actions/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"*.{ts,js,tsx,jsx}": "eslint --cache --fix"
},
"engines": {
"node": "^18 || ^20.2.0"
"node": "^18 || ^20.2.0 || ^22"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -75,4 +75,4 @@
}
}
}
}
}
5 changes: 4 additions & 1 deletion packages/pages/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.env
tsdoc-metadata.json
docs/.vitepress/cache
docs/.vitepress/cache

# Output of 'pnpm pack'
*.tgz
4 changes: 2 additions & 2 deletions packages/pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
},
"engines": {
"node": "^18 || ^20.2.0"
"node": "^18 || ^20.2.0 || ^22"
},
"scripts": {
"watch": "node src/bundler --watch",
Expand Down Expand Up @@ -117,4 +117,4 @@
"react-dom": "^17.0.2 || ^18.2.0",
"vite": "^4.3.0 || ^5.0.2"
}
}
}
9 changes: 5 additions & 4 deletions packages/pages/src/bin/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { spawnSync } from "child_process";
import { fileURLToPath } from "url";
import process from "process";
import path from "path";
import { pathToFileURL } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const filePrefix = path.sep === path.win32.sep ? "file:\\\\" : "";
const pathToPagesScript = path.resolve(__dirname, "./pages.js");
const pathToLoader = filePrefix + path.resolve(__dirname, "./loader.js");
const pathToLoader = pathToFileURL(path.resolve(__dirname, "./loader.js")).href;

const nodeVersion = Number(
spawnSync("node", ["-v"], { encoding: "utf-8" })
Expand All @@ -20,8 +20,9 @@ const experimentalFlags = ["--experimental-vm-modules"];
if (nodeVersion === 18) {
experimentalFlags.push("--experimental-specifier-resolution=node");
} else {
experimentalFlags.push("--experimental-loader");
experimentalFlags.push(pathToLoader);
// Coercion to any is necessary because @types/node is not properly recognizing when dymanically importing
const { register }: any = await import("node:module");
register(pathToLoader);
}

const results = spawnSync(
Expand Down
13 changes: 12 additions & 1 deletion packages/pages/src/common/src/loader/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,21 @@ export const loadModules = async (
for (const modulePath of modulePaths) {
importedModules.push({
path: modulePath,
module: await import(pathToFileURL(modulePath).toString()),
module: await import(pathToDecodedFileURL(modulePath)),
});
}
}

return importedModules;
};

/**
* Node 22 changed pathToFileUrl to automatically encode characters that are not valid in URLs,
* while earlier versions did not. This function decodes the file URL to match the behavior of
* earlier versions.
*/
const pathToDecodedFileURL = (path: string): string => {
const fileUrlString = pathToFileURL(path).toString();
const decodedFileUrlString = decodeURI(fileUrlString);
return decodedFileUrlString.slice(7); // Remove "file://"
};
6 changes: 2 additions & 4 deletions packages/pages/tests/fixtures/src/functions/http/[param].ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { HttpFunctionArgument, HttpFunctionResponse } from "../../../../../src";
import { PagesHttpRequest, PagesHttpResponse } from "../../../../../src";

export default function helloWorld(
_: HttpFunctionArgument
): HttpFunctionResponse {
export default function helloWorld(_: PagesHttpRequest): PagesHttpResponse {
return {
body: "Hello World",
headers: {},
Expand Down

0 comments on commit 07b9600

Please sign in to comment.