esbuild-svelte-inertia
raw JSON → 0.0.5 verified Fri May 01 auth: no javascript
A tiny esbuild plugin (v0.0.5, updated Dec 2022, no regular release schedule) that enables importing Svelte page components via glob patterns for use with Inertia.js. It removes the need for manual page registration by resolving dynamic imports. Intended for esbuild-based Svelte + Inertia projects. Minimal and focused, but unmaintained since initial release.
Common errors
error TypeError: require(...) is not a function ↓
cause Using ESM import syntax for a CJS-only module.
fix
Use 'const svelteInertiaPlugin = require("esbuild-svelte-inertia")' instead of import.
error Error: Cannot find module './Pages/**/*' ↓
cause Glob pattern not resolved; esbuild needs the plugin to handle the glob import.
fix
Add 'svelteInertiaPlugin()' to the plugins array. Also ensure the directory './Pages' exists relative to the entry point.
error TypeError: pages[name.replace(...)] is undefined ↓
cause Page name does not match key in the imported pages object (likely due to path prefix or extension).
fix
Check the exact keys: log 'pages' to see the shape. Common fix: use 'name.replace('/', '').replace('.svelte', '')'.
Warnings
gotcha The glob import pattern './Pages/**/*' may not work as expected if your project is bundled with esbuild without the 'copy' plugin for static files. Only .svelte files are picked up. ↓
fix Ensure all Svelte page components have .svelte extension and the pattern matches directory structure exactly.
gotcha Plugin is CommonJS only; importing via ESM 'import' will cause 'require is not defined' error if Node ES modules mode is used. ↓
fix Use require() in CommonJS context, or configure your project to convert CJS to ESM (e.g., esbuild's --format=esm with CJS shims).
gotcha The plugin does not handle nested page directories reliably; the replace('/','') may break if pages have subfolders (e.g., 'Admin/Dashboard'). ↓
fix Modify resolve: name => pages['./' + name.replace('./Pages/', '').replace('.svelte', '')] or use a custom resolver.
Install
npm install esbuild-svelte-inertia yarn add esbuild-svelte-inertia pnpm add esbuild-svelte-inertia Imports
- default export wrong
import svelteInertiaPlugin from 'esbuild-svelte-inertia'correctconst svelteInertiaPlugin = require('esbuild-svelte-inertia') - pages import wrong
import pages from './Pages/**/*'correctimport * as pages from './Pages/**/*' - plugin usage wrong
plugins: [svelteInertiaPlugin, sveltePlugin]correctplugins: [sveltePlugin(), svelteInertiaPlugin()]
Quickstart
// esbuild.config.js
const esbuild = require('esbuild');
const sveltePlugin = require('esbuild-svelte');
const svelteInertiaPlugin = require('esbuild-svelte-inertia');
// Ensure your svelte.config.js has the inertiakit adapter or handle SPA mode
async function build() {
await esbuild.build({
entryPoints: ['src/main.js'],
bundle: true,
outdir: 'public/build',
plugins: [sveltePlugin(), svelteInertiaPlugin()],
});
}
build().catch(() => process.exit(1));
// Example main.js using Inertia
import { createInertiaApp } from '@inertiajs/inertia-svelte';
import * as pages from './src/Pages/**/*';
createInertiaApp({
resolve: name => pages[name.replace('/', '')],
setup({ el, App, props }) {
new App({ target: el, props });
},
});