cli: use commonjs compat mode for .cts files

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-12-24 12:41:26 +01:00
parent e1d50e2ce5
commit d36f7fd6f8
3 changed files with 9 additions and 14 deletions
+5 -3
View File
@@ -246,11 +246,13 @@ export async function load(url, context, nextLoad) {
// all that well though, and can lead to module loading issues in many cases,
// especially for older code.
// This `if` block opts-out of using CommonJS compatibility mode, and instead
// leaves it to our existing loader to transform CommonJS.
// This `if` block opts-out of using CommonJS compatibility mode by default,
// and instead leaves it to our existing loader to transform CommonJS. We do
// however use compatibility mode for the more explicit .cts file extension,
// allows for a way to opt-in to the new behavior.
//
// TODO(Rugvip): Once the synchronous hooks API is available for us to use, we might be able to adopt that instead
if (format === 'commonjs') {
if (format === 'commonjs' && ext !== '.cts') {
return nextLoad(url, { ...context, format });
}
@@ -21,13 +21,12 @@ import * as depModule from 'dep-module';
import * as depDefault from 'dep-default';
import { value as namedA } from './a-named';
import { value as namedB } from './b-named';
import * as cNamed from './c-named';
import { value as namedC } from './c-named';
import defaultA from './a-default';
import defaultB from './b-default';
import * as cDefault from './c-default';
import cDefault from './c-default';
const { default: defaultC } = cDefault;
const { value: namedC } = cNamed;
async function resolveAll(obj): Promise<unknown> {
const val = await obj;
@@ -96,12 +96,7 @@ describe('node runtime module transforms', () => {
dynCommonJs: expectedExports.commonJs,
dynDefault: expectedExports.commonJs,
dynModule: expectedExports.module,
// TODO(Rugvip): Fix CommonJS import compat from modules
dep: {
...exportValues.all,
defaultC: { default: 'c' },
namedC: undefined,
},
dep: exportValues.all,
dyn: exportValues.all,
});
});
@@ -226,8 +221,7 @@ describe('package build transforms', () => {
dynCommonJs: expectedExports.commonJs,
dynDefault: expectedExports.commonJs,
dynModule: expectedExports.module,
// TODO(Rugvip): Fix CommonJS import compat from modules
dep: { ...exportValues.all, defaultC: { default: 'c' } },
dep: exportValues.all,
dyn: exportValues.all,
});
});