Skip to content

Commit

Permalink
improve node polyfill error handling even better
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jun 5, 2020
1 parent 097d532 commit 171ae8e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
31 changes: 2 additions & 29 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
},
};
Expand Down
48 changes: 48 additions & 0 deletions src/rollup-plugin-catch-unresolved.ts
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;
},
};
}

0 comments on commit 171ae8e

Please sign in to comment.