UNPKG

vite-plugin-public-path

Version:

Vite's equivalent of `__webpack_public_path__` in Webpack. Works for `index.html` and modern/legacy build.

68 lines (67 loc) 2.93 kB
import type { Plugin } from "vite"; export interface HtmlAdvancedOptions { /** * `(rel: string, href: string) => void` */ functionNameAddLinkTag: string; /** * This string in your HTML file will be replaced to `functionNameAddLinkTag` expressions. */ addLinkTagsPlaceholder: string; /** * `(attributes: Record<string, string>, inlineScriptCode?: string) => void` */ functionNameAddScriptTag: string; /** * This string in your HTML file will be replaced to `functionNameAddScriptTag` expressions. */ addScriptTagsPlaceholder: string; } export interface Options { /** * The expression to use as `config.base`, a.k.a. the value you assign to __webpack_public_path__ in * Webpack. * * The expression should evaluate to a string ending with "/". */ publicPathExpression: string; /** * Any script tags whose src or data-src attributes match the provided filter(s) will not be rewritten. */ excludeScripts?: ReadonlyArray<string | RegExp> | string | RegExp; /** * ### `string` * * Pass a string to NOT to use dynamic public path in `index.html` but remove the impact of placeholder * `config.base`. Use it if you want to handle processing of `index.html` yourself. * * This is NOT recommanded. See also `false` below. * * NOTE: For modern build, this doesn't match Webpack's behavior "load initial JS/CSS files from original * host but imported from dynamic public path", because ES module imports generated by Vite use ALL *relative* * URLs. For legacy build it matches that behavior. * * ### `boolean` * * Pass `true` to enable simple `index.html` substitution. All `<link>` tags will be added with * an inline `<script>` just after ALL existing `<script>` tags in `<head>`. All `<script>` tags * will be added with an inline `<script>` just after ALL existing `<script>` tags in `<body>`. * * You should initialize the value of your public path expression in a `<script>` in `<head>`. * * Pass `false` to disable `index.html` processing. *WARNING:* this will cause the output `index.html` * contains the base placeholder, which lead to unusable HTML. You should handle `index.html` processing * yourself. * * ### `HtmlAdvancedOptions` * * You will need to implement two functions `addLinkTag` and `addScriptTag`. And two placeholders * in contexts with access to those two functions. All `<link>` and `<script>` tags will be * transformed to calls to those two functions. * * You should initialize the value of your public path expression before your placeholders. */ html: string | boolean | HtmlAdvancedOptions; } export declare type ViteConfig = Parameters<Plugin["configResolved"]>[0]; export default function publicPath(options: Options): Plugin;