Skip to content

đŸ„¶ vite-plugin-stripper

The first need was to remove @BackendMethod from your browser bundle for remult.

Vite already gives you import.meta.env.SSR - a constant that’s true on the server and false in the client bundle. Wrapping a block in if (import.meta.env.SSR) { ... } lets the client tree-shaker drop the whole block at build time:

function readSecret() {
if (import.meta.env.SSR) {
// stripped from the client bundle
return process.env.AUTH_SECRET
}
}

vite-plugin-stripper automates that wrapping for decorator-based code (like remult’s @BackendMethod and @Entity server-only callbacks) so you don’t have to sprinkle if (import.meta.env.SSR) by hand on every method.

Terminal window
npm i -D vite-plugin-stripper
vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite'
import { defineConfig } from 'vite'
import { stripper } from 'vite-plugin-stripper'
export default defineConfig({
plugins: [
stripper(), // 👈 uses the default config
sveltekit(),
],
})

Calling stripper() with no arguments applies defaultStripConfig, which covers the common remult setup:

// equivalent to: stripper()
import { defaultStripConfig, stripper } from 'vite-plugin-stripper'
stripper({ strip: defaultStripConfig })

defaultStripConfig is:

;[
{ decorator: 'BackendMethod' },
{
decorator: 'Entity',
args_1: [
{ fn: 'backendPrefilter' },
{ fn: 'backendPreprocessFilter' },
{ fn: 'sqlExpression' },
{ fn: 'saved' },
],
},
]
import { defaultStripConfig, stripper } from 'vite-plugin-stripper'
stripper({
strip: [...defaultStripConfig, { decorator: 'MyCustomServerOnly' }],
})
stripper({
strip: [
{ decorator: 'BackendMethod' },
{
decorator: 'Entity',
args_1: [
{ fn: 'backendPrefilter' },
{ fn: 'sqlExpression' },
{
fn: 'saved',
// keep `saved` for these specific entities, strip it everywhere else
excludeEntityKeys: ['users'],
},
],
},
],
})

Imports from these modules become let X = null in the client bundle:

  • $app/env/private (SvelteKit 3)
  • $env/static/private and $env/dynamic/private (SvelteKit 2)

This always happens (exported as privateEnvModules). Add your own with nullify:

stripper({
nullify: ['mongodb'],
})