@pugpigbolt/importmap-plugin
v0.1.4
Published
Vite plugin that automatically injects `<script type="importmap">` into HTML and handles shared package resolution for both dev and production builds.
Readme
@pugpigbolt/importmap-plugin
Vite plugin that automatically injects <script type="importmap"> into HTML and handles shared package resolution for both dev and production builds.
Installation
npm install --save-dev @pugpigbolt/importmap-pluginUsage
// vite.config.ts
import { defineConfig } from 'vite-plus'
import { importMapPlugin } from '@pugpigbolt/importmap-plugin'
export default defineConfig({
...,
plugins: [importMapPlugin()],
})The plugin discovers importmap.ts in the Vite root automatically.
importmap.ts config file
Create importmap.ts next to vite.config.ts:
import { defineImportMap } from '@pugpigbolt/importmap-plugin'
export default defineImportMap({
entries: [
{ name: 'vue', importmapOnly: true }, // shared singleton — importmap mode only
'@pugpigbolt/bridge', // bare string → auto-expands to all JS exports
'@pugpigbolt/components',
'@pugpigbolt/widgets',
],
dev: {
mode: 'alias', // workspace packages aliased to TypeScript source for HMR
},
build: {
mode: 'importmap',
manifest: '../shared/dist/assets/importmap.json',
baseUrl: '/',
},
})Modes
| Mode | Dev | Build | Description |
| ----------- | --- | ----- | ---------------------------------------------------------------------------------- |
| alias | yes | - | Workspace packages aliased to TypeScript source via resolve.alias — full HMR |
| importmap | yes | yes | Entries served through a module server and injected as <script type="importmap"> |
mode must be set explicitly per command. If omitted, the plugin is inactive for that command.
ImportMapPluginOptions
| Option | Default | Description |
| ------------------------- | ------- | -------------------------------------------------------------- |
| mode | - | 'alias' or 'importmap'. Required to activate the plugin. |
| baseUrl | - | Base URL for pre-hosted shared assets (build only) |
| manifest | - | Path to an importmap.json from a sibling app's build |
| moduleServerPort | 5280 | Port for the dev module server |
| externalModuleServerUrl | - | Connect to an existing module server instead of starting one |
| hash | false | Include content hash in locally-bundled shared entry filenames |
