vite-plugin-html-template-mpa
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import { HtmlTagDescriptor, Plugin } from 'vite'; | ||
import { HtmlTagDescriptor, Plugin, PluginOption } from 'vite'; | ||
import { Options as Options$1 } from 'html-minifier-terser'; | ||
@@ -19,2 +19,3 @@ import { Options as Options$2 } from 'ejs'; | ||
mpaAutoAddMainTs?: boolean; | ||
onlyUseEjsAndMinify?: boolean; | ||
}; | ||
@@ -44,3 +45,11 @@ interface Options extends PageOptions { | ||
declare function htmlTemplate(userOptions?: HtmlTemplateMpaOptions): Plugin; | ||
declare function createMinifyHtmlPlugin(userOptions?: HtmlTemplateMpaOptions): { | ||
name: string; | ||
enforce: string; | ||
configResolved(resolvedConfig: any): void; | ||
generateBundle(_: any, bundle: any): Promise<void>; | ||
}; | ||
export { type HtmlTemplateMpaOptions, htmlTemplate as default }; | ||
declare function createHtmlPlugin(userOptions?: HtmlTemplateMpaOptions): PluginOption[]; | ||
export { type HtmlTemplateMpaOptions, createMinifyHtmlPlugin, createHtmlPlugin as default, htmlTemplate }; |
@@ -32,3 +32,5 @@ var __create = Object.create; | ||
__export(src_exports, { | ||
default: () => htmlTemplate | ||
createMinifyHtmlPlugin: () => createMinifyHtmlPlugin, | ||
default: () => createHtmlPlugin, | ||
htmlTemplate: () => htmlTemplate | ||
}); | ||
@@ -67,3 +69,4 @@ module.exports = __toCommonJS(src_exports); | ||
addEntryScript, | ||
mpaAutoAddMainTs | ||
mpaAutoAddMainTs, | ||
onlyUseEjsAndMinify | ||
} = payload; | ||
@@ -100,13 +103,15 @@ let content = ""; | ||
}) : []; | ||
if (pageName2 === "index" && links?.length) { | ||
content = content.replace( | ||
"</body>", | ||
`${links.join("").replace(/,/g, " ")} | ||
if (!onlyUseEjsAndMinify) { | ||
if (pageName2 === "index" && links?.length) { | ||
content = content.replace( | ||
"</body>", | ||
`${links.join("").replace(/,/g, " ")} | ||
</body>` | ||
); | ||
} else if (isMPA && mpaAutoAddMainTs || addEntryScript) { | ||
content = content.replace( | ||
"</body>", | ||
`<script type="module" src="${entryJsPath}"></script></body>` | ||
); | ||
); | ||
} else if (isMPA && mpaAutoAddMainTs || addEntryScript) { | ||
content = content.replace( | ||
"</body>", | ||
`<script type="module" src="${entryJsPath}"></script></body>` | ||
); | ||
} | ||
} | ||
@@ -234,3 +239,3 @@ const { data, ejsOptions } = injectOptions || { | ||
const assetDir = resolvedConfig.build.assetsDir || "assets"; | ||
if (isMpa(resolvedConfig)) { | ||
if (!options.onlyUseEjsAndMinify && isMpa(resolvedConfig)) { | ||
const _output = resolvedConfig.build.rollupOptions.output; | ||
@@ -308,3 +313,3 @@ if (buildPrefixName) { | ||
const templateOption = page.template; | ||
const templatePath = templateOption ? resolve(templateOption) : isMpa(config) ? resolve("public/index.html") : resolve("index.html"); | ||
const templatePath = options.onlyUseEjsAndMinify ? config.build?.rollupOptions?.input?.[pageName] : templateOption ? resolve(templateOption) : isMpa(config) ? resolve("public/index.html") : resolve("index.html"); | ||
let content = await getHtmlContent({ | ||
@@ -327,3 +332,4 @@ pagesDir: options.pagesDir, | ||
pages: options.pages || {}, | ||
jumpTarget: options.jumpTarget | ||
jumpTarget: options.jumpTarget, | ||
onlyUseEjsAndMinify: options.onlyUseEjsAndMinify | ||
}); | ||
@@ -340,3 +346,3 @@ content = await server.transformIndexHtml?.( | ||
resolveId(id) { | ||
if (id.endsWith(".html")) { | ||
if (!options.onlyUseEjsAndMinify && id.endsWith(".html")) { | ||
id = (0, import_vite.normalizePath)(id); | ||
@@ -407,2 +413,39 @@ if (!isMpa(config)) { | ||
}, | ||
closeBundle() { | ||
if (isMpa(config)) { | ||
import_shelljs.default.rm( | ||
"-rf", | ||
resolve(`${config.build?.outDir || "dist"}/index.html`) | ||
); | ||
} | ||
} | ||
}; | ||
} | ||
function createMinifyHtmlPlugin(userOptions = {}) { | ||
const options = { | ||
pagesDir: "src/views", | ||
pages: {}, | ||
jumpTarget: "_self", | ||
buildCfg: { | ||
moveHtmlTop: true, | ||
moveHtmlDirTop: false, | ||
buildPrefixName: "", | ||
htmlHash: false, | ||
buildAssetDirName: "", | ||
buildChunkDirName: "", | ||
buildEntryDirName: "", | ||
htmlPrefixSearchValue: "", | ||
htmlPrefixReplaceValue: "" | ||
}, | ||
minify: true, | ||
mpaAutoAddMainTs: true, | ||
...userOptions | ||
}; | ||
let config; | ||
return { | ||
name: "vite:minify-html", | ||
enforce: "post", | ||
configResolved(resolvedConfig) { | ||
config = resolvedConfig; | ||
}, | ||
async generateBundle(_, bundle) { | ||
@@ -442,19 +485,15 @@ const htmlFiles = Object.keys(bundle).filter((i) => i.endsWith(".html")); | ||
} | ||
}, | ||
closeBundle() { | ||
if (isMpa(config)) { | ||
import_shelljs.default.rm( | ||
"-rf", | ||
resolve(`${config.build?.outDir || "dist"}/index.html`) | ||
); | ||
} | ||
if (!isMpa(config) && isBuild) { | ||
const root = config.root || process.cwd(); | ||
const dest = config.build && config.build.outDir || "dist"; | ||
const resolve2 = (p) => import_path.default.resolve(root, p); | ||
import_shelljs.default.mv(resolve2(`${dest}/${PREFIX}/*.html`), resolve2(dest)); | ||
import_shelljs.default.rm("-rf", resolve2(`${dest}/${PREFIX}`)); | ||
} | ||
} | ||
}; | ||
} | ||
function createHtmlPlugin(userOptions = {}) { | ||
return [ | ||
htmlTemplate(userOptions), | ||
createMinifyHtmlPlugin(userOptions) | ||
]; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
createMinifyHtmlPlugin, | ||
htmlTemplate | ||
}); |
{ | ||
"name": "vite-plugin-html-template-mpa", | ||
"version": "1.0.27", | ||
"version": "1.0.28", | ||
"description": "html template map for vite", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51338
5.07%987
8.94%