Skip to content

Commit

Permalink
make resolution failures more appealing (#5551)
Browse files Browse the repository at this point in the history
* make resolution failures more appealing - closes #5543

* lint
  • Loading branch information
Rich-Harris committed Jul 15, 2022
1 parent a38040c commit bbb8a8e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-eagles-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': patch
---

Print friendlier message if adapter-vercel fails to resolve dependencies
35 changes: 33 additions & 2 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export default function ({ external = [], edge, split } = {}) {
);

await create_function_bundle(
builder,
`${tmp}/index.js`,
`${dirs.functions}/${name}.func`,
`nodejs${node_version.major}.x`
Expand Down Expand Up @@ -288,22 +289,52 @@ function get_node_version() {
}

/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {string} entry
* @param {string} dir
* @param {string} runtime
*/
async function create_function_bundle(entry, dir, runtime) {
async function create_function_bundle(builder, entry, dir, runtime) {
let base = entry;
while (base !== (base = path.dirname(base)));

const traced = await nodeFileTrace([entry], { base });

/** @type {Map<string, string[]>} */
const resolution_failures = new Map();

traced.warnings.forEach((error) => {
// pending https://github.com/vercel/nft/issues/284
if (error.message.startsWith('Failed to resolve dependency node:')) return;
console.error(error);

if (error.message.startsWith('Failed to resolve dependency')) {
const match = /Cannot find module '(.+?)' loaded from (.+)/;
const [, module, importer] = match.exec(error.message);

if (!resolution_failures.has(importer)) {
resolution_failures.set(importer, []);
}

resolution_failures.get(importer).push(module);
} else {
throw error;
}
});

if (resolution_failures.size > 0) {
const cwd = process.cwd();
builder.log.warn(
'The following modules failed to locate dependencies that may (or may not) be required for your app to work:'
);

for (const [importer, modules] of resolution_failures) {
console.error(` ${path.relative(cwd, importer)}`);
for (const module of modules) {
console.error(` - \u001B[1m\u001B[36m${module}\u001B[39m\u001B[22m`);
}
}
}

// find common ancestor directory
let common_parts;

Expand Down

0 comments on commit bbb8a8e

Please sign in to comment.