Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/resolve-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isAbsolute, resolve, sep } from 'path';
import { builtinModules } from 'module';
import { Job } from './node-file-trace';
import { getNodeMajorVersion } from './utils/node-version';

// node resolver
// custom implementation to emit only needed package.json files for resolver
Expand Down Expand Up @@ -168,6 +169,7 @@ function getExportsTarget(
condition === 'default' ||
(condition === 'require' && cjsResolve) ||
(condition === 'import' && !cjsResolve) ||
(condition === 'module-sync' && getNodeMajorVersion() >= 22) ||
conditions.includes(condition)
) {
const target = getExportsTarget(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/node-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Gets the major version of the current Node.js runtime
* @returns The major version number (e.g., 22 for Node.js v22.16.0)
*/
export function getNodeMajorVersion(): number {
return parseInt(process.versions.node.split('.')[0], 10);
}
11 changes: 11 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const skipOnWindows = [
'require-symlink',
];
const skipOnMac = [];
const skipOnNode20AndBelow = ['module-sync-condition-es'];
const skipOnNode22AndAbove = ['module-sync-condition-es-node20'];
if (process.platform === 'darwin' && process.arch === 'arm64') {
skipOnMac.push('microtime-node-gyp');
}
Expand Down Expand Up @@ -68,6 +70,7 @@ afterEach(resetFileIOMocks);

for (const { testName, isRoot } of unitTests) {
const testSuffix = `${testName} from ${isRoot ? 'root' : 'cwd'}`;
const nodeVersion = parseInt(process.versions.node.split('.')[0], 10);
if (
process.platform === 'win32' &&
(isRoot || skipOnWindows.includes(testName))
Expand All @@ -79,6 +82,14 @@ for (const { testName, isRoot } of unitTests) {
console.log(`Skipping unit test on macOS: ${testSuffix}`);
continue;
}
if (nodeVersion < 22 && skipOnNode20AndBelow.includes(testName)) {
console.log(`Skipping unit test on Node.js 20 or below: ${testSuffix}`);
continue;
}
if (nodeVersion >= 22 && skipOnNode22AndAbove.includes(testName)) {
console.log(`Skipping unit test on Node.js 22 or above: ${testSuffix}`);
continue;
}
const unitPath = join(__dirname, 'unit', testName);

it(`should correctly trace ${testSuffix}`, async () => {
Expand Down
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-cjs/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'fallback version';
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-cjs/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'import version';
2 changes: 2 additions & 0 deletions test/unit/module-sync-condition-cjs/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { test } from 'test-pkg-sync-es';
console.log(test);
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-cjs/module-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'module-sync version';
4 changes: 4 additions & 0 deletions test/unit/module-sync-condition-cjs/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"test/unit/module-sync-condition-cjs/input.js",
"test/unit/module-sync-condition-cjs/package.json"
]
11 changes: 11 additions & 0 deletions test/unit/module-sync-condition-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-pkg-sync-cjs",
"type": "commonjs",
"exports": {
".": {
"module-sync": "./module-sync.js",
"import": "./import.js",
"default": "./fallback.js"
}
}
}
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es-node20/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'fallback version';
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es-node20/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'import version';
2 changes: 2 additions & 0 deletions test/unit/module-sync-condition-es-node20/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { test } from 'test-pkg-sync-es';
console.log(test);
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es-node20/module-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'module-sync version';
5 changes: 5 additions & 0 deletions test/unit/module-sync-condition-es-node20/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"test/unit/module-sync-condition-es-node20/import.js",
"test/unit/module-sync-condition-es-node20/input.js",
"test/unit/module-sync-condition-es-node20/package.json"
]
11 changes: 11 additions & 0 deletions test/unit/module-sync-condition-es-node20/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-pkg-sync-es",
"type": "module",
"exports": {
".": {
"module-sync": "./module-sync.js",
"import": "./import.js",
"default": "./fallback.js"
}
}
}
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es/fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'fallback version';
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'import version';
2 changes: 2 additions & 0 deletions test/unit/module-sync-condition-es/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { test } from 'test-pkg-sync-es';
console.log(test);
1 change: 1 addition & 0 deletions test/unit/module-sync-condition-es/module-sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const test = 'module-sync version';
5 changes: 5 additions & 0 deletions test/unit/module-sync-condition-es/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"test/unit/module-sync-condition-es/input.js",
"test/unit/module-sync-condition-es/module-sync.js",
"test/unit/module-sync-condition-es/package.json"
]
11 changes: 11 additions & 0 deletions test/unit/module-sync-condition-es/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-pkg-sync-es",
"type": "module",
"exports": {
".": {
"module-sync": "./module-sync.js",
"import": "./import.js",
"default": "./fallback.js"
}
}
}