rollup-plugin-base-url

raw JSON →
0.0.2 verified Mon Apr 27 auth: no javascript

A Rollup plugin that rewrites dynamic (and optionally static) import paths to use an absolute base URL, such as '/' or '/forum'. This prevents chunk loading failures on sub-routes (e.g., /login loads chunks from the wrong path). Version 0.0.2 is the current release. It avoids AST parsing for performance, doing a naive string replacement of chunk paths. No known release cadence. Differentiators: lightweight, simple, no AST overhead, compatible with plugins like rollup-plugin-hoist-import-deps. Alternatives (e.g., @rollup/plugin-url) target assets, not imports.

error Error [ERR_REQUIRE_ESM]: require() of ES Module
cause Using require('rollup-plugin-base-url') in a CommonJS context.
fix
Change to import { baseUrl } from 'rollup-plugin-base-url' and ensure your project is ESM (e.g., use 'type': 'module' in package.json or .mjs extension).
error TypeError: (0 , _baseUrl.baseUrl) is not a function
cause Attempting to call baseUrl() with incorrect arguments, e.g., baseUrl('/forum', true) instead of baseUrl({url: '/forum', staticImports: true}).
fix
Invoke baseUrl with a single options object: baseUrl({ url: '/forum', staticImports: true }).
error Error: Could not resolve import ... in file ...
cause Plugin didn't rewrite the import path correctly, possibly because the chunk name doesn't match the expected pattern or staticImports is false but a static import is present.
fix
Check that output.chunkFileNames uses '[name]-[hash].js' pattern. If using static imports, set staticImports: true.
gotcha Does not parse AST; uses naive string replacement of import paths. May incorrectly replace strings in comments, strings, or variable names that match the chunk pattern.
fix Avoid using file names or imports that contain patterns like './some-chunk-hash.js' in string literals or comments.
gotcha Only rewrites imports that match the exact output chunk pattern; non-standard chunk file names or custom hash functions may not be recognized.
fix Ensure output.chunkFileNames uses the default '[name]-[hash].js' pattern, or adapt plugin logic.
breaking Version 0.0.2 is the only release; no breaking changes yet, but be aware of possible future changes.
fix None needed.
deprecated Package is experimental and may be abandoned; no recent updates.
fix Consider migrating to a more maintained alternative if needed.
npm install rollup-plugin-base-url
yarn add rollup-plugin-base-url
pnpm add rollup-plugin-base-url

Basic Rollup config using baseUrl plugin to serve dynamic imports from '/' root, preventing route-specific loading errors.

// rollup.config.js
import { baseUrl } from 'rollup-plugin-base-url';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
    entryFileNames: '[name]-[hash].js',
    chunkFileNames: '[name]-[hash].js',
  },
  plugins: [
    baseUrl({
      url: '/',
      staticImports: false,
    }),
  ],
};