Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement/issue 684 improve import map generation diagnostics #1389

Merged
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
20 changes: 15 additions & 5 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'fs';
import { isBuiltin } from 'node:module';

// priority if from L -> R
const SUPPORTED_EXPORT_CONDITIONS = ['import', 'module-sync', 'default'];
const IMPORT_MAP_RESOLVED_PREFIX = '/~';
const importMap = new Map();
const diagnostics = {};
const diagnostics = new Map();

function updateImportMap(key, value, resolvedRoot) {
importMap.set(
Expand All @@ -23,7 +24,7 @@ function resolveBareSpecifier(specifier) {
try {
resolvedPath = import.meta.resolve(specifier);
} catch (e) {
diagnostics[specifier] = `ERROR (${e.code}): unable to resolve specifier => \`${specifier}\` \n${e.message}`;
diagnostics.set(specifier, `ERROR (${e.code}): unable to resolve specifier => \`${specifier}\`\n${e.message}`);
}

return resolvedPath;
Expand Down Expand Up @@ -68,7 +69,9 @@ function derivePackageRoot(resolved) {
root = root.substring(0, root.lastIndexOf(segment));
}

return root;
return root !== ''
? root
: null;
}

// helper function to convert export patterns to a regex (thanks ChatGPT :D)
Expand Down Expand Up @@ -186,7 +189,7 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {

if (!matched) {
// ex. https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `no supported export conditions (\`${SUPPORTED_EXPORT_CONDITIONS.join(', ')}\`) for dependency => \`${dependency}\``;
diagnostics.set(dependency, `no supported export conditions (\`${SUPPORTED_EXPORT_CONDITIONS.join(', ')}\`) for dependency => \`${dependency}\``);
}
} else {
// handle (unconditional) subpath exports
Expand All @@ -208,7 +211,7 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
updateImportMap(dependency, 'index.js', resolvedRoot);
} else {
// ex: https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `WARNING: No supported entry point detected for => \`${dependency}\``;
diagnostics.set(dependency, `WARNING: No supported entry point detected for => \`${dependency}\``);
}
}

Expand All @@ -235,6 +238,13 @@ async function walkPackageJson(packageJson = {}, walkedPackages = new Set()) {

await walkPackageJson(resolvedPackageJson, walkedPackages);
}
} else {
// ignore built-ins since NodeJS resolves them automatically
// https://github.com/nodejs/node/issues/56652
// https://nodejs.org/api/modules.html#built-in-modules
if (!isBuiltin(resolved)) {
diagnostics.set(dependency, `WARNING: No package.json resolved for => \`${dependency}\`, resolved to \`${resolved}\``);
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ class NodeModulesResource extends ResourceInterface {
console.log('Generating import map from project dependencies...');
const { importMap, diagnostics } = await walkPackageJson(userPackageJson);

if (Object.keys(diagnostics).length > 0) {
if (diagnostics.size > 0) {
console.log('****************************************************************************');

Object.keys(diagnostics).forEach((diagnostic) => {
console.warn(diagnostics[diagnostic]);
diagnostics.forEach((value) => {
console.warn(`- ${value}\n`);
});

console.log('Learn more about these warnings at => https://greenwoodjs.dev/docs/introduction/web-standards/#import-maps');
console.log('\n>>> Some issue were detected, learn more about these warnings at https://greenwoodjs.dev/docs/introduction/web-standards/#import-maps');
console.log('****************************************************************************');
}

Expand Down