-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve node polyfill error handling even better
- Loading branch information
1 parent
097d532
commit 171ae8e
Showing
2 changed files
with
50 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import chalk from 'chalk'; | ||
import isNodeBuiltin from 'is-builtin-module'; | ||
import {Plugin} from 'rollup'; | ||
|
||
/** | ||
* rollup-plugin-catch-unresolved | ||
* | ||
* Catch any unresolved imports to give proper warnings (Rollup default is to ignore). | ||
*/ | ||
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; | ||
} | ||
|
||
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; | ||
}, | ||
}; | ||
} |