Skip to content

Commit

Permalink
diagnostics tracking and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Nov 28, 2024
1 parent 55e6276 commit 86f9c3c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
14 changes: 8 additions & 6 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
// priority if from L -> R
const SUPPORTED_EXPORT_CONDITIONS = ['import', 'module-sync', 'default'];
const importMap = {};
const diagnostics = {};

function updateImportMap(key, value) {
importMap[key.replace('./', '')] = value.replace('./', '');
Expand All @@ -19,8 +20,8 @@ function resolveBareSpecifier(specifier) {
try {
resolvedPath = import.meta.resolve(specifier);
} catch (e) {
// console.log({ e });
// TODO console.log(`WARNING: unable to resolve specifier \`${specifier}\``);
// ex. https://unpkg.com/browse/@types/[email protected]/package.json
diagnostics[specifier] = `ERROR (${e.code}): unable to resolve specifier => \`${specifier}\` \n${e.message}`;
}

return resolvedPath;
Expand Down Expand Up @@ -158,8 +159,8 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
}

if (!matched) {
// TODO what to do here? what else is there besides default?
// console.log(`unsupported condition \`${exports[sub]}\` for dependency => \`${dependency}\``);
// ex. https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `no supported export conditions (\`${SUPPORTED_EXPORT_CONDITIONS.join(', ')}\`) for dependency => \`${dependency}\``;
}
} else {
// handle (unconditional) subpath exports
Expand All @@ -175,7 +176,8 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
} else if (module || main) {
updateImportMap(dependency, `/node_modules/${dependency}/${module ?? main}`);
} else {
// TODO warn about no exports found
// ex: https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `WARNING: No supported export detected for => \`${dependency}\``;
}
}

Expand Down Expand Up @@ -211,7 +213,7 @@ async function walkPackageJson(packageJson = {}) {
console.error('Error building up import map', e);
}

return importMap;
return { importMap, diagnostics };
}

// could probably go somewhere else, in a util?
Expand Down
32 changes: 20 additions & 12 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ResourceInterface } from '../../lib/resource-interface.js';
import { mergeImportMap } from '../../lib/walker-package-ranger.js';
import { walkPackageJson } from '../../lib/walker-package-ranger.js';

let importMap;
let generatedImportMap;

class NodeModulesResource extends ResourceInterface {
constructor(compilation, options) {
Expand All @@ -25,8 +25,6 @@ class NodeModulesResource extends ResourceInterface {
return url.pathname.indexOf('/node_modules/') === 0;
}

// TODO convert node modules util to URL
// https://github.com/ProjectEvergreen/greenwood/issues/953v
async resolve(url) {
const { projectDirectory } = this.compilation.context;
const { pathname, searchParams } = url;
Expand Down Expand Up @@ -88,15 +86,25 @@ class NodeModulesResource extends ResourceInterface {
const userPackageJson = await getPackageJsonForProject(context);

// if there are dependencies and we haven't generated the importMap already
// walk the project's package.json for all its direct dependencies
// for each entry found in dependencies, find its entry point
// then walk its entry point (e.g. index.js) for imports / exports to add to the importMap
// and then walk its package.json for transitive dependencies and all those import / exports
importMap = !importMap && Object.keys(userPackageJson.dependencies || []).length > 0
? await walkPackageJson(userPackageJson)
: importMap || {};

body = mergeImportMap(body, importMap, importMaps);
// walk the project's package.json for all its direct and transitive dependencies
if (!generatedImportMap && Object.keys(userPackageJson.dependencies || []).length > 0) {
console.log('Generating import map from project dependencies...');
const { importMap, diagnostics } = await walkPackageJson(userPackageJson);

if (Object.keys(diagnostics).length > 0) {
console.log('****************************************************************************');
Object.keys(diagnostics).forEach((diagnostic) => {
console.warn(diagnostics[diagnostic]);
});
console.log('****************************************************************************');
}

generatedImportMap = importMap;
} else {
generatedImportMap = generatedImportMap || {};
}

body = mergeImportMap(body, generatedImportMap, importMaps);
body = body.replace('<head>', `
<head>
${importMapShimScript}
Expand Down

0 comments on commit 86f9c3c

Please sign in to comment.