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.

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', '')'.
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.
npm install esbuild-svelte-inertia
yarn add esbuild-svelte-inertia
pnpm add esbuild-svelte-inertia

Minimal setup: esbuild config with both svelte and inertia plugins, then import pages via glob for Inertia resolve.

// 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 });
  },
});