Offline Bundler Plugin
The `offline-bundler-plugin` is a JavaScript/TypeScript utility designed to prepare web applications for offline access. It automates the process of compressing static assets (including JavaScript, CSS, images, and HTML) into a single zip archive and simultaneously generates a resource mapping JSON file. This manifest maps remote URLs of assets to their corresponding local paths within the generated bundle, facilitating efficient offline caching and retrieval. Currently at version 1.0.3, the plugin boasts compatibility with both Webpack and Vite (including UniApp environments), offering broad utility across modern frontend build setups. Its key differentiator lies in its dual-bundler support and its specialized focus on creating structured resource manifests, which goes beyond simple asset compression to enable robust offline-first strategies.
Common errors
-
TypeError: OfflinePackagePlugin is not a constructor
cause The Vite plugin entry point exports a function, not a class. It was incorrectly invoked with the `new` keyword.fixRemove the `new` keyword when using the plugin in Vite. Correct usage: `OfflinePackagePlugin({...options})`. -
Error: Cannot find module 'offline-bundler-plugin/vite'
cause Incorrect import path used in a Webpack configuration, or the package was not installed.fixFor Webpack, use `require('offline-bundler-plugin')`. For Vite, ensure `import OfflinePackagePlugin from 'offline-bundler-plugin/vite'` and that `offline-bundler-plugin` is installed as a dev dependency. -
Error: Cannot find module 'offline-bundler-plugin'
cause Incorrect import path used in a Vite configuration, or the package was not installed.fixFor Vite, use `import OfflinePackagePlugin from 'offline-bundler-plugin/vite'`. For Webpack, ensure `require('offline-bundler-plugin')` and that `offline-bundler-plugin` is installed as a dev dependency.
Warnings
- breaking When integrating `offline-bundler-plugin` into a Vite project, the plugin function must be invoked directly, without the `new` keyword. This contrasts with its usage in Webpack, where `new` is required to instantiate the plugin class. Failure to omit `new` in Vite configurations will result in a `TypeError`.
- gotcha The plugin exposes different entry points for Webpack and Vite. Attempting to use the Webpack-specific import path (`offline-bundler-plugin`) in a Vite configuration, or vice-versa with (`offline-bundler-plugin/vite`), will lead to module resolution errors or incorrect plugin behavior.
- gotcha Webpack configuration files are typically CommonJS modules (`require` syntax), while Vite configurations commonly use ESM (`import` syntax). Mixing these module systems without proper transpilation or configuration can cause runtime errors during the build process.
Install
-
npm install offline-bundler-plugin -
yarn add offline-bundler-plugin -
pnpm add offline-bundler-plugin
Imports
- OfflinePackagePlugin
import { OfflinePackagePlugin } from 'offline-bundler-plugin';const OfflinePackagePlugin = require('offline-bundler-plugin'); - OfflinePackagePlugin
const OfflinePackagePlugin = require('offline-bundler-plugin/vite');import OfflinePackagePlugin from 'offline-bundler-plugin/vite';
Quickstart
// For Webpack: webpack.config.js (or similar, requiring commonjs)
const OfflinePackagePluginWebpack = require('offline-bundler-plugin');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'production',
entry: './src/index.js',
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname, 'dist-webpack'),
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
}),
new OfflinePackagePluginWebpack({
packageNameKey: 'packageId',
packageNameValue: 'my-app-webpack',
version: 1,
baseUrl: 'http://localhost:8080/', // Replace with your actual deployment base URL
fileTypes: ['html', 'js', 'css', 'png'],
folderName: 'offline-bundle-webpack',
}),
],
};
// For Vite: vite.config.ts (or similar, using ESM)
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react'; // Example framework plugin
import OfflinePackagePluginVite from 'offline-bundler-plugin/vite';
export default defineConfig({
plugins: [
react(),
OfflinePackagePluginVite({
packageNameKey: 'packageId',
packageNameValue: 'my-app-vite',
version: 1,
baseUrl: 'http://localhost:3000/', // Replace with your actual deployment base URL
fileTypes: ['html', 'js', 'css', 'png'],
folderName: 'offline-bundle-vite',
}),
],
build: {
outDir: 'dist-vite',
},
});