Felt: On-demand ES6/CSS Bundler
Felt is an on-demand bundler designed for modern JavaScript (ES6 and beyond, typically transpiled via Buble/Rollup) and CSS (CSS Next), serving primarily as a development middleware or a static asset exporter. Currently at version 0.3.2, it requires Node.js 6 or above to run. The package offers three main modes of operation: a standalone CLI web server for quick local development, an Express.js middleware for integration into existing Node.js applications, and a static file exporter for pre-compiling assets for deployment. Its core functionality is highly extensible through 'recipes' and a plugin architecture, exemplified by modules like `felt-rollup`, which allows developers to define custom handler logic for different file types. This on-demand compilation approach is a key differentiator, focusing on speed during development rather than exhaustive pre-bundling.
Common errors
-
Error: Cannot find module 'felt-recipe-minimal'
cause The `felt-recipe-minimal` package (or another specified recipe) was not installed, or its path is incorrect.fixInstall the required recipe: `npm install felt-recipe-minimal` or `npm install -g felt felt-recipe-minimal` if using the CLI globally. -
Error: listen EADDRINUSE: address already in use :::3000
cause The default port 3000 (or a specified port) is already being used by another process on your system.fixChange the port Felt uses with the `--port` CLI option (e.g., `felt --port 3333`) or by setting the `port` option in your `felt.config.js`. -
ReferenceError: require is not defined (at file:///path/to/server.js:...) or SyntaxError: Cannot use import statement outside a module
cause Attempting to use CommonJS `require()` in an ES module context or vice-versa, indicating a module system mismatch.fixEnsure your Node.js environment and file types are correctly configured for CommonJS (`.js` files without `"type": "module"` in package.json, or `.cjs` files) when using Felt, as it is a CommonJS library.
Warnings
- breaking Felt is a CommonJS-only package. Attempting to use `import` statements directly in an ES module environment will result in runtime errors. It predates widespread native ESM support in Node.js.
- gotcha Felt itself does not bundle files out of the box; it requires 'recipes' (like `felt-recipe-minimal`) or custom `handlers` specified in its configuration to define how different file types (e.g., `.js`, `.css`) are processed.
- gotcha Felt explicitly requires Node.js 6.x or above. While it may run on newer versions, its older dependencies (e.g., Rollup 0.x, Buble) might lead to compatibility issues or missing features compared to modern bundling setups.
- gotcha Felt's 'on-demand' bundling means assets are compiled dynamically upon request. This is great for development hot-reloading but can introduce latency for initial requests or heavy load in production environments compared to pre-compiled static assets.
Install
-
npm install felt -
yarn add felt -
pnpm add felt
Imports
- felt
import felt from 'felt'
const felt = require('felt') - recipe
import recipe from 'felt-recipe-minimal'
const recipe = require('felt-recipe-minimal') - feltConfig
import config from './felt.config.js'
const config = require('./felt.config.js')
Quickstart
const express = require('express');
const felt = require('felt');
const path = require('path');
const config = require('./felt.config.js'); // Assuming felt.config.js exists and exports a Felt config object
const app = express();
// Integrate Felt as an on-demand bundling middleware
app.use(felt(config));
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
console.log(`Felt server running at http://localhost:${PORT}`);
console.log('Serving static files from ./public and bundling JavaScript/CSS on-demand.');
});
// To set up:
// 1. npm install express felt felt-rollup rollup-plugin-buble rollup-plugin-node-resolve rollup-plugin-commonjs
// 2. Create 'felt.config.js' (as shown in the README example)
// 3. Create 'public/index.html' and 'public/main.js' (e.g., 'console.log("Hello from Felt!")')
// 4. Run with 'node server.js'