Snowpack Rollup Bundling Plugin
snowpack-plugin-rollup-bundle is a Snowpack plugin designed to integrate Rollup for production bundling of web applications. The package, currently at version 0.4.4, provides functionality to process specified entrypoints (relative to the Snowpack `build/` directory), bundling them along with their corresponding CSS files and generating a `manifest.json` for hashed asset locations. It was initially developed with a focus on seamless integration with Rails applications, particularly through the `Snowpacker` gem. While it fulfills its purpose for Snowpack projects, it's important to note that Snowpack itself has largely been unmaintained since 2021, with its creators now recommending alternatives like Vite for new development. Consequently, this plugin is primarily relevant for maintaining or optimizing existing Snowpack-based projects rather than serving new build tooling initiatives.
Common errors
-
Error: Cannot find module 'rollup'
cause The 'rollup' package, a peer dependency, has not been installed in your project.fixInstall Rollup: `npm install --save-dev rollup` or `yarn add --dev rollup`. -
Error: [snowpack-plugin-rollup-bundle] Entrypoint 'src/index.js' not found. Entrypoints must refer to the build/ directory.
cause The `entrypoints` option specifies a path from the source directory (`src/`) instead of the Snowpack build output directory (`build/_dist_/`).fixAdjust the `entrypoints` in your `snowpack.config.js` to point to the built files, e.g., `"build/_dist_/index.js"`. -
TypeError: Cannot read properties of undefined (reading 'push') when extending Rollup config
cause Attempting to push to `config.inputOptions.plugins` or `config.outputOptions.plugins` when the `plugins` array does not exist or is undefined in the default config.fixInitialize the `plugins` array if it doesn't exist before pushing to it: `config.inputOptions.plugins = config.inputOptions.plugins || []; config.inputOptions.plugins.push(yourPlugin());`
Warnings
- breaking The plugin option `emitHtml` was renamed to `emitHtmlFiles` in `v0.3.3`. Older configurations using `emitHtml` will no longer function correctly.
- gotcha The `entrypoints` option is required and expects paths relative to your Snowpack `build/` directory (e.g., `build/_dist_/index.js`), not your source `src/` directory. Providing `src/` paths will result in files not being found.
- gotcha Both `rollup` and `snowpack` are peer dependencies. They must be installed separately in your project alongside `snowpack-plugin-rollup-bundle` for the plugin to function.
- gotcha Snowpack, the host tool for this plugin, is largely unmaintained by its original creators, who now recommend migration to Vite. This plugin is primarily for existing Snowpack projects and is unlikely to receive significant new features or extensive support.
- gotcha Early versions (prior to `v0.4.4`) had issues with path resolution on Windows operating systems due to incorrect path separators. This was fixed in `v0.4.4`.
Install
-
npm install snowpack-plugin-rollup-bundle -
yarn add snowpack-plugin-rollup-bundle -
pnpm add snowpack-plugin-rollup-bundle
Imports
- snowpack-plugin-rollup-bundle
import { snowpackPluginRollupBundle } from 'snowpack-plugin-rollup-bundle';// snowpack.config.js module.exports = { plugins: [ ['snowpack-plugin-rollup-bundle', { /* options */ }] ] }; - awesomeRollupPlugin
const awesomeRollupPlugin = require('awesome-rollup-plugin'); - SnowpackConfig
const plugins = [ // ... ["snowpack-plugin-rollup-bundle", { /* options */ }] ]; module.exports = { plugins: plugins };
Quickstart
// snowpack.config.js
const awesomeRollupPlugin = require('awesome-rollup-plugin'); // Example for extending config
module.exports = {
mount: {
'src': '/',
},
plugins: [
// Other Snowpack plugins...
[
"snowpack-plugin-rollup-bundle",
{
emitHtmlFiles: true, // Set to true to rewrite script tags in HTML
preserveSourceFiles: false,
entrypoints: [
"build/_dist_/index.js",
"build/_dist_/entrypoints/*.js",
], // Required: glob patterns or array of paths relative to build/
extendConfig: (config) => {
// Example: Add a custom Rollup plugin
config.inputOptions.plugins.push(awesomeRollupPlugin());
// Example: Override output options
config.outputOptions = {
...config.outputOptions,
chunkFileNames: 'chunks/[name]-[hash].js',
assetFileNames: 'assets/[name]-[hash][extname]'
};
return config;
}
}
]
],
build: {
out: 'dist',
metaDir: '_snowpack_',
},
optimize: {
bundle: false, // Ensure Snowpack's built-in bundler is off if using this plugin
},
};