Skip to content

Commit fe56019

Browse files
committed
require and import
1 parent f05cc51 commit fe56019

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

src/commands/check.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const checkCommand = createCommand<{}, {}>(api => {
9393
cwd: path.join(cwd, 'dist'),
9494
packageJSON: distPackageJSON,
9595
skipExports: new Set<string>(config?.check?.skip ?? []),
96-
includesCommonJS: config?.commonjs ?? true,
96+
dual: config?.commonjs ?? true,
9797
});
9898
await checkEngines({
9999
packageJSON: distPackageJSON,
@@ -119,11 +119,11 @@ async function checkExportsMapIntegrity(args: {
119119
cwd: string;
120120
packageJSON: {
121121
name: string;
122-
exports: unknown;
122+
exports: any;
123123
bin: unknown;
124124
};
125125
skipExports: Set<string>;
126-
includesCommonJS: boolean;
126+
dual: boolean;
127127
}) {
128128
const exportsMapResult = ExportsMapModel.safeParse(args.packageJSON['exports']);
129129
if (exportsMapResult.success === false) {
@@ -140,7 +140,7 @@ async function checkExportsMapIntegrity(args: {
140140
const cjsSkipExports = new Set<string>();
141141
const esmSkipExports = new Set<string>();
142142
for (const definedExport of args.skipExports) {
143-
if (args.includesCommonJS) {
143+
if (args.dual) {
144144
const cjsResult = resolve.resolve(args.packageJSON, definedExport, {
145145
require: true,
146146
})?.[0];
@@ -155,7 +155,7 @@ async function checkExportsMapIntegrity(args: {
155155
}
156156

157157
for (const key of Object.keys(exportsMap)) {
158-
if (args.includesCommonJS) {
158+
if (args.dual) {
159159
const cjsResult = resolve.resolve(args.packageJSON, key, {
160160
require: true,
161161
})?.[0];
@@ -208,9 +208,8 @@ async function checkExportsMapIntegrity(args: {
208208
}
209209

210210
const esmResult = resolve.resolve({ exports: exportsMap }, key)?.[0];
211-
212211
if (!esmResult) {
213-
throw new Error(`Could not resolve CommonJS import '${key}' for '${args.packageJSON.name}'.`);
212+
throw new Error(`Could not resolve export '${key}' in '${args.packageJSON.name}'.`);
214213
}
215214

216215
if (esmResult.match(/.(js|mjs)$/)) {
@@ -247,48 +246,46 @@ async function checkExportsMapIntegrity(args: {
247246
}
248247
}
249248

250-
const legacyRequire = resolve.legacy(args.packageJSON, {
251-
fields: ['main'],
252-
});
253-
if (!legacyRequire || typeof legacyRequire !== 'string') {
254-
throw new Error(`Could not resolve legacy CommonJS entrypoint.`);
249+
const exportsRequirePath = resolve.resolve({ exports: exportsMap }, '.', { require: true })?.[0];
250+
if (!exportsRequirePath || typeof exportsRequirePath !== 'string') {
251+
throw new Error('Could not resolve default CommonJS entrypoint in a Module project.');
255252
}
256253

257-
if (args.includesCommonJS) {
258-
const legacyRequireResult = await runRequireJSFileCommand({
259-
path: legacyRequire,
254+
if (args.dual) {
255+
const requireResult = await runRequireJSFileCommand({
256+
path: exportsRequirePath,
260257
cwd: args.cwd,
261258
});
262259

263-
if (legacyRequireResult.exitCode !== 0) {
260+
if (requireResult.exitCode !== 0) {
264261
throw new Error(
265-
`Require of file '${legacyRequire}' failed with error:\n` + legacyRequireResult.stderr,
262+
`Require of file '${exportsRequirePath}' failed with error:\n` + requireResult.stderr,
266263
);
267264
}
268265
} else {
269-
const legacyRequireResult = await runImportJSFileCommand({
270-
path: legacyRequire,
266+
const importResult = await runImportJSFileCommand({
267+
path: exportsRequirePath,
271268
cwd: args.cwd,
272269
});
273270

274-
if (legacyRequireResult.exitCode !== 0) {
271+
if (importResult.exitCode !== 0) {
275272
throw new Error(
276-
`Require of file '${legacyRequire}' failed with error:\n` + legacyRequireResult.stderr,
273+
`Import of file '${exportsRequirePath}' failed with error:\n` + importResult.stderr,
277274
);
278275
}
279276
}
280277

281278
const legacyImport = resolve.legacy(args.packageJSON);
282279
if (!legacyImport || typeof legacyImport !== 'string') {
283-
throw new Error(`Could not resolve legacy ESM entrypoint.`);
280+
throw new Error('Could not resolve default ESM entrypoint.');
284281
}
285282
const legacyImportResult = await runImportJSFileCommand({
286283
path: legacyImport,
287284
cwd: args.cwd,
288285
});
289286
if (legacyImportResult.exitCode !== 0) {
290287
throw new Error(
291-
`Require of file '${legacyRequire}' failed with error:\n` + legacyImportResult.stderr,
288+
`Require of file '${exportsRequirePath}' failed with error:\n` + legacyImportResult.stderr,
292289
);
293290
}
294291

0 commit comments

Comments
 (0)