UNPKG

6.58 kBTypeScriptView Raw
1import { PluginOption } from 'vite';
2import { Options } from 'html-minifier-terser';
3
4/**
5 * cdn module configuration
6 */
7interface NpmModule {
8 /**
9 * Package name
10 */
11 name: string;
12 /**
13 * Global variable name, defaults to camel case of the package name if not specified
14 */
15 var?: string;
16 /**
17 * Package version, defaults to the version in node_modules if not specified
18 */
19 version?: string;
20 /**
21 * Path to the resource js/css files to be loaded
22 */
23 file?: string | string[];
24 /**
25 * Set npm module path
26 */
27 modulePath?: string;
28 /**
29 * Dependency modules or modules files. For example, [{"dayjs":["plugin/isoWeek.js"]}]
30 */
31 deps?: DepModuleFiles[];
32 /**
33 * These codes will be inserted before the script/link tag of the current module
34 *
35 * The `{{url}}` keyword in the code will be replaced with relevant information about the current module.
36 */
37 injectBefore?: string | string[];
38 /**
39 * These codes will be inserted after the script/link tag of the current module
40 *
41 * The `{{url}}` keyword in the code will be replaced with relevant information about the current module.
42 */
43 injectAfter?: string | string[];
44 /**
45 * Local module
46 */
47 local?: boolean;
48}
49/**
50 * Dependency module name or files
51 *
52 * @example
53 * ```ts
54 * const deps: DepModuleFiles[] = [
55 'vue',
56 {
57 dayjs: [
58 'plugin/customParseFormat.js',
59 ],
60 'vue-demi': 'lib/index.iife.js'
61 },
62 ]
63 * ```
64 */
65type DepModuleFiles = string | Record<string, string | string[]>;
66type Union<T> = T | (string & {});
67/**
68 * Default supported types
69 */
70type PresetNpmModule = Union<'dayjs' | 'axios' | 'lodash' | 'vue' | 'vue-router' | 'vue-demi' | 'pinia' | 'ant-design-vue' | 'ant-design-vue3' | '@vueuse/core' | '@vueuse/shared' | 'element-plus' | 'react' | 'react-dom' | 'react-router-dom' | 'antd' | 'ahooks' | '@ant-design/charts'>;
71/**
72 * Injected code
73 */
74interface HtmlInjectCode {
75 /**
76 * Injected HTML code
77 */
78 code: string | string[];
79 [key: string]: any;
80}
81/**
82 * All types of cdn module configurations
83 */
84type HtmlCdnModule = PresetNpmModule | NpmModule | HtmlInjectCode;
85/**
86 * Local cdn configuration
87 */
88interface HtmlCdnLocal {
89 /**
90 * Local mode or specify modules as local modules. Default is false.
91 * @default false
92 */
93 modules: boolean | PresetNpmModule[];
94 /**
95 * Same as vite configuration base option. Default is "/".
96 * @default "/"
97 */
98 base?: string;
99 /**
100 * Local output directory, same as vite configuration build.outDir option. Default is "dist".
101 * @default 'dist'
102 */
103 outDir?: string;
104 /**
105 * Local output path, the module URL will also be replaced with this path. Default is "npm/{name}@{version}".
106 * @default "npm/{name}@{version}"
107 */
108 path?: string;
109 /**
110 * Whether to copy to local. Default is true.
111 * @default true
112 */
113 copy?: boolean;
114}
115/**
116 * cdn plugin configuration
117 */
118interface HtmlCdnOptions {
119 /**
120 * Module configuration
121 */
122 modules: HtmlCdnModule[];
123 /**
124 * Tag selector where the cdn will be added. Default is "title".
125 * @default "title"
126 */
127 selector?: string;
128 /**
129 * `CDN` source type, parameters `name`/`version`/`file` are taken from the modules configuration.
130 *
131 * When the OS language is `zh_CN` , the default value is `npmmirror`, otherwise it is `jsdelivr`.
132 *
133 * * npmmirror: url defaults to https://registry.npmmirror.com/{name}/{version}/files/{file}
134 * * jsdelivr: url defaults to https://cdn.jsdelivr.net/npm/{name}@{version}/{file}
135 * * unpkg: url defaults to https://unpkg.com/{name}@{version}/{file}
136 * * custom: custom url can be defined
137 *
138 * @default "npmmirror"
139 */
140 type?: 'npmmirror' | 'unpkg' | 'jsdelivr' | 'custom';
141 /**
142 * Used in conjunction with the type parameter, sets different urls, the final path will be {url}/{file}
143 *
144 * * npmmirror: url defaults to https://registry.npmmirror.com/{name}/{version}/files
145 * * jsdelivr: url defaults to https://cdn.jsdelivr.net/npm/{name}@{version}
146 * * unpkg: url defaults to https://unpkg.com/{name}@{version}
147 * * custom: custom url can be defined
148 */
149 url?: string;
150 /**
151 * Local mode or specify modules as local modules. Default is false.
152 * @default false
153 */
154 local?: boolean | PresetNpmModule[] | HtmlCdnLocal;
155}
156
157/**
158 * Vite HTML plugin with CDN and other support
159 * @returns
160 */
161declare function useHtmlCdnPlugin(options: HtmlCdnOptions): PluginOption;
162
163/**
164 * Detailed configuration for the loading plugin
165 */
166interface HtmlLoadingOptions {
167 /**
168 * Selector for the node to insert the loading into. Defaults to `#app`
169 * @default '#app'
170 */
171 selector?: string;
172 /**
173 * Custom style code
174 */
175 style?: string;
176 /**
177 * Code to add before the loading code
178 */
179 before?: string;
180 /**
181 * Code to add after the loading code
182 */
183 after?: string;
184}
185
186/**
187 * HTML loading plugin
188 * @param options Configuration options, default is true
189 * @returns
190 */
191declare function useHtmlLoadingPlugin(options?: boolean | HtmlLoadingOptions): PluginOption;
192
193/**
194 * Detailed configuration for html minification plugin
195 */
196type HtmlMinifyOptions = Options;
197/**
198 * Configuration for html minification plugin
199 */
200type HtmlMinifyPluginOptions = boolean | HtmlMinifyOptions;
201
202/**
203 * HTML compression plugin
204 * @param minify Configuration parameter, default is true
205 */
206declare function useHtmlMinifyPlugin(minify?: boolean | HtmlMinifyOptions): PluginOption;
207
208/**
209 * Html Plugin Configuration
210 */
211interface HtmlPluginOptions {
212 /**
213 * Minification plugin configuration, default is true
214 */
215 minify?: boolean | HtmlMinifyOptions;
216 /**
217 * Loading plugin configuration, default is false
218 */
219 loading?: boolean | HtmlLoadingOptions;
220 /**
221 * CDN configuration, default is true
222 */
223 cdn?: false | HtmlCdnOptions;
224}
225/**
226 * Html Plugin
227 */
228declare function useHtmlPlugin(options?: HtmlPluginOptions): PluginOption[];
229
230export { type DepModuleFiles, type HtmlCdnLocal, type HtmlCdnModule, type HtmlCdnOptions, type HtmlInjectCode, type HtmlLoadingOptions, type HtmlMinifyOptions, type HtmlMinifyPluginOptions, type HtmlPluginOptions, type NpmModule, type PresetNpmModule, type Union, useHtmlPlugin as default, useHtmlCdnPlugin, useHtmlLoadingPlugin, useHtmlMinifyPlugin, useHtmlPlugin };