Skip to content

Tricks vite into inlining modules, helpful when code should be centralized but cannot be imported during runtime due to arbitrary constraints

License

Notifications You must be signed in to change notification settings

soultice/vite-plugin-force-inline-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vite-plugin-force-inline-module

This plugin is tricking Vite into inlining contents of files/imports that match the inlineModules argument. Somehow Vite is not making use of the shouldTransformCachedModules hook of rollup. We trick Vite into busting its own cache, therefore inlining the modules code.

Usage (also see example package)

in vite.config.ts

import { forceInlineModule } from 'vite-plugin-force-inline-module'

export default defineConfig{
  ...
  plugins: [forceInlineModule({ inlineModules: [MODULES_TO_INLINE]})
  ...
}

Example

compare the build output of the example package in dist with plugins used/deactivated in vite.config.ts

Without Plugin:

build1.js / build2.js

import { foobar } from './import_from.js';

console.log("entry_point1", foobar);

With Plugin:

build1.js:

const foobar = "Foobar";

console.log("entry_point1", foobar);

build2.js:

const foobar = "Foobar";

console.log("entry_point2", foobar);

About

Tricks vite into inlining modules, helpful when code should be centralized but cannot be imported during runtime due to arbitrary constraints

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published