Skip to content

Commit

Permalink
cleanup unresolved error messages (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 11, 2020
1 parent 18a213b commit 5adee6d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 31 deletions.
26 changes: 26 additions & 0 deletions docs/09-troubleshooting.md
@@ -0,0 +1,26 @@
## Troubleshooting

### Node built-in could not be resolved

```
✖ /my-application/node_modules/dep/index.js
"http" (Node.js built-in) could not be resolved.
```

Some packages are written with dependencies on Node.js built-in modules. This is a problem on the web, since Node.js built-in modules don't exist in the browser. For example, `import 'path'` will run just fine in Node.js but would fail in the browser.

To solve this issue, you can either replace the offending package ([pika.dev](https://pika.dev/) is a great resource for web-friendly packages) or add Node.js polyfill support:

```js
// snowpack.config.js
// Plugin: https://github.com/ionic-team/rollup-plugin-node-polyfills
module.exports = {
installOptions: {
rollup: {
plugins: [require("rollup-plugin-node-polyfills")()]
}
}
};
```


13 changes: 12 additions & 1 deletion src/commands/install.ts
Expand Up @@ -347,13 +347,24 @@ export async function install(
rollupPluginCatchUnresolved(),
].filter(Boolean) as Plugin[],
onwarn(warning, warn) {
// Warn about the first circular dependency, but then ignore the rest.
if (warning.code === 'CIRCULAR_DEPENDENCY') {
if (!isCircularImportFound) {
isCircularImportFound = true;
logUpdate(`Warning: 1+ circular dependencies found via "${warning.importer}".`);
}
return;
}
// Log "unresolved" import warnings as an error, causing Snowpack to fail at the end.
if (
warning.code === 'PLUGIN_WARNING' &&
warning.plugin === 'snowpack:rollup-plugin-catch-unresolved'
) {
// Display posix-style on all environments, mainly to help with CI :)
const fileName = warning.id!.replace(cwd + path.sep, '').replace(/\\/g, '/');
logError(`${fileName}\n ${warning.message}`);
return;
}
warn(warning);
},
};
Expand Down Expand Up @@ -457,7 +468,7 @@ export async function command({cwd, config, lockfile, pkgManifest}: CommandOptio
if (finalResult) {
spinner.succeed(
chalk.bold(`snowpack`) +
` install complete.` +
` install complete${spinnerHasError ? ' with errors.' : '.'}` +
chalk.dim(` [${((Date.now() - startTime) / 1000).toFixed(2)}s]`),
);
if (!!dependencyStats) {
Expand Down
40 changes: 10 additions & 30 deletions src/rollup-plugin-catch-unresolved.ts
Expand Up @@ -11,37 +11,17 @@ export function rollupPluginCatchUnresolved(): Plugin {
return {
name: 'snowpack:rollup-plugin-catch-unresolved',
resolveId(id, importer) {
if (!isNodeBuiltin(id)) {
console.error(
chalk.red(
chalk.bold(`! ${id}`) + ` is imported by '${importer}', but could not be resolved.`,
),
);
return false;
if (isNodeBuiltin(id)) {
this.warn({
id: importer,
message: `"${id}" (Node.js built-in) could not be resolved. (https://www.snowpack.dev/#node-built-in-could-not-be-resolved)`,
});
} else {
this.warn({
id: importer,
message: `"${id}" could not be resolved.`,
});
}

console.error(
chalk.red(
chalk.bold(`! ${id}`) +
` is a Node.js built-in module that does not exist in the browser.\n`,
),
);
console.error(
` 1. Search pika.dev for a more web-friendly package alternative${
importer ? ` to ${chalk.bold(importer)}.` : '.'
}`,
);
console.error(
` 2. Or, add this rollup plugin to your installer to polyfill Node.js packages:\n\n` +
chalk.dim(
` // snowpack.config.js\n` +
` module.exports = {\n` +
` installOptions: {\n` +
` rollup: {plugins: [require("rollup-plugin-node-polyfills")()]}\n` +
` }\n` +
` };\n`,
),
);
return false;
},
};
Expand Down
@@ -0,0 +1,8 @@
- snowpack installing...
⠼ snowpack installing... mock-test-package
✖ node_modules/mock-test-package/entrypoint.js
"http" (Node.js built-in) could not be resolved. (https://www.snowpack.dev/#node-built-in-could-not-be-resolved)
✔ snowpack install complete with errors.

⦿ web_modules/ size gzip brotli
└─ mock-test-package.js XXXX KB XXXX KB XXXX KB

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/integration/error-node-builtin-unresolved/package.json
@@ -0,0 +1,11 @@
{
"scripts": {
"TEST": "node ../../../pkg/dist-node/index.bin.js"
},
"dependencies": {
"mock-test-package": "^1.2.3"
},
"snowpack": {
"install": ["mock-test-package"]
}
}

0 comments on commit 5adee6d

Please sign in to comment.