From e70346a1e1a528c9d78152e6de74be02a142b30e Mon Sep 17 00:00:00 2001 From: kanno <812137533@qq.com> Date: Thu, 28 Nov 2024 09:20:08 +0800 Subject: [PATCH] chore: release v0.15.0 --- CHANGELOG.md | 8 ++++++++ global.d.ts | 2 +- package.json | 2 +- pre-compile.mts | 41 ++++++++++++++++++++++++++++++++++++++--- src/server/render.ts | 37 ++----------------------------------- 5 files changed, 50 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee60127..2c73c68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.1.5.0 + +# Improves + +- Support vite6. +- Reduce bundle size. +- Improve plugin integration support + ## 0.14.3 Bump client deps. diff --git a/global.d.ts b/global.d.ts index b21b8a6..c2e994c 100644 --- a/global.d.ts +++ b/global.d.ts @@ -5,5 +5,5 @@ type DeepPartial = T extends object ? { T declare module 'html.mjs' { - export const html: string + export function html(title: string, module: string): string } diff --git a/package.json b/package.json index 15fe304..1c8e9ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-bundle-analyzer", - "version": "0.14.3", + "version": "0.15.0", "description": "a modern vite bundle analyzer tool", "main": "dist/index.js", "module": "dist/index.mjs", diff --git a/pre-compile.mts b/pre-compile.mts index 1143d0e..a7c95e6 100644 --- a/pre-compile.mts +++ b/pre-compile.mts @@ -5,14 +5,37 @@ import path from 'path' import fsp from 'fs/promises' import { readAll } from './src/server/shared' -import { injectHTMLTag } from './src/server/render' -import type { Descriptor } from './src/server/render' const defaultWd = process.cwd() const clientPath = path.join(defaultWd, 'dist', 'client') const clientAssetsPath = path.join(clientPath, 'assets') +export interface Descriptor { + kind: 'script' | 'style' | 'title' + text: string + attrs?: string[] +} + +interface InjectHTMLTagOptions { + html: string + injectTo: 'body' | 'head' + descriptors: Descriptor | Descriptor[] +} + +// Refactor this function +function injectHTMLTag(options: InjectHTMLTagOptions) { + const regExp = options.injectTo === 'head' ? /([ \t]*)<\/head>/i : /([ \t]*)<\/body>/i + options.descriptors = Array.isArray(options.descriptors) ? options.descriptors : [options.descriptors] + const descriptors = options.descriptors.map(d => { + if (d.attrs && d.attrs.length > 0) { + return `<${d.kind} ${d.attrs.join(' ')}>${d.text}` + } + return `<${d.kind}>${d.text}` + }) + return options.html.replace(regExp, (match) => `${descriptors.join('\n')}${match}`) +} + async function main() { const clientAssetsPaths = await readAll(clientAssetsPath) const clientAssets = await Promise.all(clientAssetsPaths.map(async (p) => { @@ -26,6 +49,7 @@ async function main() { html, injectTo: 'head', descriptors: [ + { text: '<--title-->', kind: 'title' }, ...assets.map(({ fileType, content }) => ({ kind: fileType === 'js' ? 'script' : 'style', text: content, @@ -33,7 +57,18 @@ async function main() { })) satisfies Descriptor[] ] }) - process.stdout.write(`export const html = ${JSON.stringify(html)}`) + html = injectHTMLTag({ + html, + injectTo: 'body', + descriptors: { + kind: 'script', + text: '<--module-->' + } satisfies Descriptor | Descriptor[] + }) + + process.stdout.write(`export function html (title, module) { + return ${JSON.stringify(html)}.replace('<--title-->', title).replace('<--module-->', module) + }`) } main() diff --git a/src/server/render.ts b/src/server/render.ts index bdb570e..d7efeac 100644 --- a/src/server/render.ts +++ b/src/server/render.ts @@ -20,24 +20,6 @@ export interface Descriptor { text: string attrs?: string[] } -interface InjectHTMLTagOptions { - html: string - injectTo: 'body' | 'head' - descriptors: Descriptor | Descriptor[] -} - -// Refactor this function -export function injectHTMLTag(options: InjectHTMLTagOptions) { - const regExp = options.injectTo === 'head' ? /([ \t]*)<\/head>/i : /([ \t]*)<\/body>/i - options.descriptors = Array.isArray(options.descriptors) ? options.descriptors : [options.descriptors] - const descriptors = options.descriptors.map(d => { - if (d.attrs && d.attrs.length > 0) { - return `<${d.kind} ${d.attrs.join(' ')}>${d.text}` - } - return `<${d.kind}>${d.text}` - }) - return options.html.replace(regExp, (match) => `${descriptors.join('\n')}${match}`) -} // https://tc39.es/ecma262/#sec-putvalue // Using var instead of set attr to window we can reduce 9 bytes @@ -50,23 +32,8 @@ export function generateInjectCode(analyzeModule: Module[], mode: string) { // In built mode according flag inject a fake chunk at the output export async function renderView(analyzeModule: Module[], options: RenderOptions) { - let { html } = await import('html.mjs') - html = injectHTMLTag({ - html, - injectTo: 'head', - descriptors: [ - { text: options.title, kind: 'title' } - ] - }) - html = injectHTMLTag({ - html, - injectTo: 'body', - descriptors: { - kind: 'script', - text: generateInjectCode(analyzeModule, options.mode) - } - }) - return html + const { html } = await import('html.mjs') + return html(options.title, generateInjectCode(analyzeModule, options.mode)) } export function arena() {