Skip to content

Commit

Permalink
chore: release v0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Nov 28, 2024
1 parent d7e72cc commit e70346a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.1.5.0

# Improves

- Support vite6.
- Reduce bundle size.
- Improve plugin integration support

## 0.14.3

Bump client deps.
Expand Down
2 changes: 1 addition & 1 deletion global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type DeepPartial<T> = T extends object ? {
T

declare module 'html.mjs' {
export const html: string
export function html(title: string, module: string): string
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
41 changes: 38 additions & 3 deletions pre-compile.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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}</${d.kind}>`
}
return `<${d.kind}>${d.text}</${d.kind}>`
})
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) => {
Expand All @@ -26,14 +49,26 @@ async function main() {
html,
injectTo: 'head',
descriptors: [
{ text: '<--title-->', kind: 'title' },
...assets.map(({ fileType, content }) => ({
kind: fileType === 'js' ? 'script' : 'style',
text: content,
attrs: fileType === 'js' ? ['type="module"'] : []
})) 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()
37 changes: 2 additions & 35 deletions src/server/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}</${d.kind}>`
}
return `<${d.kind}>${d.text}</${d.kind}>`
})
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
Expand All @@ -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() {
Expand Down

0 comments on commit e70346a

Please sign in to comment.