diff --git a/src/commands/install.ts b/src/commands/install.ts index 89b91d6f9a..4445882814 100644 --- a/src/commands/install.ts +++ b/src/commands/install.ts @@ -16,6 +16,7 @@ import {EnvVarReplacements, SnowpackConfig} from '../config.js'; import {resolveTargetsFromRemoteCDN} from '../resolve-remote.js'; import {rollupPluginCss} from '../rollup-plugin-css'; import {rollupPluginEntrypointAlias} from '../rollup-plugin-entrypoint-alias.js'; +import {rollupPluginCatchUnresolved} from '../rollup-plugin-catch-unresolved.js'; import {rollupPluginWrapInstallTargets} from '../rollup-plugin-wrap-install-targets'; import {rollupPluginDependencyCache} from '../rollup-plugin-remote-cdn.js'; import {DependencyStatsOutput, rollupPluginDependencyStats} from '../rollup-plugin-stats.js'; @@ -337,6 +338,7 @@ export async function install( rollupPluginWrapInstallTargets(!!isTreeshake, CJS_PACKAGES_TO_AUTO_DETECT, installTargets), rollupPluginDependencyStats((info) => (dependencyStats = info)), ...userDefinedRollup.plugins, // load user-defined plugins last + rollupPluginCatchUnresolved(), ].filter(Boolean) as Plugin[], onwarn(warning, warn) { if (warning.code === 'CIRCULAR_DEPENDENCY') { @@ -346,35 +348,6 @@ export async function install( } return; } - if (warning.code === 'UNRESOLVED_IMPORT') { - logError( - `'${warning.source}' is imported by '${warning.importer}', but could not be resolved.`, - ); - if (isNodeBuiltin(warning.source)) { - console.log( - chalk.dim( - ` '${warning.source}' is a Node.js builtin module that won't exist in the browser.`, - ), - ); - console.log( - chalk.dim( - ` Search pika.dev for a web-friendly alternative to ${chalk.bold(warning.importer)}`, - ), - ); - console.log( - chalk.dim( - ` Or, add ${chalk.bold( - '"rollup-plugin-node-polyfills"', - )} to installOptions.rollup.plugins in your Snowpack config file.`, - ), - ); - } else { - console.log( - chalk.dim(` Make sure that the package is installed and that the file exists.`), - ); - } - return; - } warn(warning); }, }; diff --git a/src/rollup-plugin-catch-unresolved.ts b/src/rollup-plugin-catch-unresolved.ts new file mode 100644 index 0000000000..0a52274768 --- /dev/null +++ b/src/rollup-plugin-catch-unresolved.ts @@ -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; + }, + }; +}