# vite-plugin-dynamic-publicpath

> A vite plugin for dynamic publicpath, like __webpack_public_path__

Latest version **1.1.2** (published 2022-02-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-plugin-dynamic-publicpath
pnpm add vite-plugin-dynamic-publicpath
yarn add vite-plugin-dynamic-publicpath
bun add vite-plugin-dynamic-publicpath
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-02-26 |
| First published | 2021-07-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 46 |
| Author | Jy |
| Maintainers | pure-jy |
| Keywords | vite, publicpath, dynamic publicpath |

## Links

- npm: https://www.npmjs.com/package/vite-plugin-dynamic-publicpath
- Repository: https://github.com/jy0529/vite-plugin-dynamic-publicpath
- Homepage: https://github.com/jy0529/vite-plugin-dynamic-publicpath#readme
- Issues: https://github.com/jy0529/vite-plugin-dynamic-publicpath/issues
- npm.io page: https://npm.io/package/vite-plugin-dynamic-publicpath

## Dependencies (1)

- [es-module-lexer](https://npm.io/package/es-module-lexer.md) ^0.6.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.1.2 (latest) — 2022-02-26
- 1.1.1 — 2022-01-15
- 1.1.0 — 2021-11-05
- 1.0.2 — 2021-07-09
- 1.0.1 — 2021-07-08
- 1.0.0 — 2021-07-05

## README

# vite-plugin-dynamic-publicpath

## Why
Use dynamic import handler to change runtime public base path, like webpack's `__webpack_public_path__`.

## Usage

- run `yarn add vite-plugin-dynamic-publicpath --dev`
- add the plugin into `vite.config.ts`
``` ts
// vite.config.ts

import { useDynamicPublicPath } from 'vite-plugin-dynamic-publicpath'
export default defineConfig({
  plugins: [
    useDynamicPublicPath(/** option */),
  ]
})
```
e.g. [basic demo](https://github.com/jy0529/vite-plugin-dynamic-publicpath/tree/main/examples/dynamic-publicpath-demo)


```ts
// main.ts

// Your dynamic cdn
const dynamicCdn = oneOf(['cdn.xxx.com', 'cdn1.xxx.com']);
window.__dynamicImportHandler__ = function(importer) {
    return dynamicCdn + importer;
}
window.__dynamicImportPreload__ = function(preloads) {
    return preloads.map(preload => dynamicCdn + preload);
}

```
### For legacy browser
e.g. [legacy browser demo](https://github.com/jy0529/vite-plugin-dynamic-publicpath/tree/main/examples/legacy-demo)
```ts
// vite.config.ts

import legacy from '@vitejs/plugin-legacy'
import { useDynamicPublicPath } from 'vite-plugin-dynamic-publicpath'
export default defineConfig({
  plugins: [
    /**
     * @see https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
     * /
    legacy({
      targets: ['defaults', 'not ie <= 8'] 
    }),
    /** Attention! The legacy plugin must before loaded than dynamic-publicpath plugin */
    useDynamicPublicPath({
      dynamicImportHandler: 'window.__dynamic_handler__',
      dynamicImportPreload: 'window.__dynamic_preload__'
    }),
  ]
})

```

## [Options](https://github.com/jy0529/vite-plugin-dynamic-publicpath/blob/main/index.d.ts)

### `dynamicImportHandler`

Type: `string`<br>
Default: `window.__dynamicImportHandler__`

Register dynamic import handler

### `dynamicImportPreload`

Type: `string`<br>
Default: `window.__dynamicImportPreload__`

Register dynamic import preload handler

### `assetsBase`

Type: `string`<br>
Default: `assets`

Custom the assets directory.

---
_Source: https://npm.io/package/vite-plugin-dynamic-publicpath · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
