Skip to content

Commit

Permalink
chore: release v0.12.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Sep 27, 2024
1 parent beb8092 commit c5c42c2
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.12.1

Better debug log info.

## 0.12.0

# Improves
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build-server:

dev-server:
@echo "Starting server in development mode..."
@pnpm exec rollup --config rollup.config.mts --configPlugin swc3 --watch
@export NODE_ENV=development && pnpm exec rollup --config rollup.config.mts --configPlugin swc3 --watch

dev-client:
@echo "Starting client in development mode..."
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vite-bundle-analyzer",
"version": "0.12.0",
"description": "a vite bundle analyzer",
"version": "0.12.1",
"description": "a modern vite bundle analyzer tool",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand All @@ -14,7 +14,7 @@
},
"scripts": {
"dev": "vite src/client",
"dev:plugin": "rollup --config rollup.config.mts --configPlugin swc3 --watch",
"dev:plugin": "export NODE_ENV=development && rollup --config rollup.config.mts --configPlugin swc3 --watch",
"build": "pnpm run build:client && pnpm run build:plugin",
"build:client": "vite build src/client",
"build:plugin": "rollup --config rollup.config.mts --configPlugin swc3",
Expand Down Expand Up @@ -92,7 +92,8 @@
"rollup",
"rollup-plugin",
"vite-plugin",
"bundle-analyzer"
"bundle-analyzer",
"rollup-bundle-analyzer"
],
"ava": {
"files": [
Expand Down
8 changes: 5 additions & 3 deletions rollup.config.mts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { builtinModules } from 'module'
import { defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'
import { swc } from 'rollup-plugin-swc3'
import { minify, swc } from 'rollup-plugin-swc3'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import shim from '@rollup/plugin-esm-shim'
import commonjs from '@rollup/plugin-commonjs'

const external = [...builtinModules]

const env = process.env.NODE_ENV

export default defineConfig([
{
input: 'src/server/index.ts',
Expand All @@ -20,8 +22,8 @@ export default defineConfig([
commonjs(),
nodeResolve(),
shim(),
swc()
// minify({ mangle: true, module: true, compress: true, sourceMap: true })
swc(),
env !== 'development' && minify({ mangle: true, module: true, compress: true, sourceMap: true })
]
},
{
Expand Down
25 changes: 12 additions & 13 deletions src/server/analyzer-module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path'
import type { ZlibOptions } from 'zlib'
import ansis from 'ansis'
import { analyzerDebug, createGzip, slash, stringToByte } from './shared'
import { createFileSystemTrie } from './trie'
import type { ChunkMetadata, GroupWithNode, KindSource, KindStat } from './trie'
Expand Down Expand Up @@ -48,6 +49,10 @@ function wrapBundleChunk(bundle: OutputChunk | OutputAsset, chunks: OutputBundle
return wrapped
}

function printDebugLog(namespace: string, id: string, total: number) {
analyzerDebug(`[${namespace}]: ${ansis.yellow(id)} find ${ansis.bold(ansis.green(total + ''))} relative modules.`)
}

export class AnalyzerNode {
originalId: string
filename: string
Expand Down Expand Up @@ -111,17 +116,20 @@ export class AnalyzerNode {
stats.insert(generateNodeId(info.id, workspaceRoot), { kind: 'stat', meta: { statSize } })
}

analyzerDebug('Start analyze stats: module ' + "'" + this.originalId + "'" + ' find ' + infomations.length + ' modules')
printDebugLog('stats', this.originalId, infomations.length)

// We use sourcemap to restore the corresponding chunk block
// Don't using rollup context `resolve` function. If the relatived id is not live in rollup graph
// It's will cause dead lock.(Altough this is a race case.)
const chunks = pickupMappingsFromCodeBinary(bundle.code, map, (id: string) => {
const { grouped: chunks, files } = pickupMappingsFromCodeBinary(bundle.code, map, (id: string) => {
const relatived = path.relative(workspaceRoot, id)
return path.join(workspaceRoot, relatived)
})

let count = 0
if (!files.size) {
chunks[this.originalId] = bundle.code
files.add(this.originalId)
}

for (const id in chunks) {
if (!KNOWN_EXT_NAME.includes(path.extname(id))) continue
Expand All @@ -130,18 +138,9 @@ export class AnalyzerNode {
const { byteLength: gzipSize } = await compress(b)
const parsedSize = b.byteLength
sources.insert(generateNodeId(id, workspaceRoot), { kind: 'source', meta: { gzipSize, parsedSize } })
count++
}

if (count === 0 && KNOWN_EXT_NAME.includes(path.extname(this.originalId))) {
const b = bundle.code
const { byteLength: gzipSize } = await compress(b)
const parsedSize = b.byteLength
sources.insert(generateNodeId(this.originalId, workspaceRoot), { kind: 'source', meta: { gzipSize, parsedSize } })
count++
}

analyzerDebug('Start analyze source: module ' + "'" + this.originalId + "'" + ' find ' + count + ' chunks')
printDebugLog('source', this.originalId, files.size)

stats.mergePrefixSingleDirectory()
stats.walk(stats.root, (c, p) => p.groups.push(c))
Expand Down
11 changes: 1 addition & 10 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function analyzer(opts?: AnalyzerPluginOptions): Plugin {
// force set sourcemap to ensure the result as accurate as possible.
config.build.sourcemap = 'hidden'
}
analyzerDebug('Sourcemap option is set to ' + "'" + config.build.sourcemap + "'")
analyzerDebug(`plugin status is ${config.build.sourcemap ? ansis.green('ok') : ansis.red('invalid')}`)
}
},
async generateBundle(_, outputBundle) {
Expand All @@ -137,11 +137,6 @@ function analyzer(opts?: AnalyzerPluginOptions): Plugin {
for (const bundleName in outputBundle) {
const bundle = outputBundle[bundleName]
const [pass, sourcemapFileName] = validateChunk(bundle, outputBundle)
const sourceMapStatus = sourcemapFileName ? true : false

analyzerDebug(
'Processing chunk ' + "'" + bundle.fileName + "'." + 'Chunk status: ' + pass + '. ' + 'Sourcemap status: ' + sourceMapStatus + '.'
)
if (pass) {
// For classical
await analyzerModule.addModule(bundle, sourcemapFileName)
Expand All @@ -163,7 +158,6 @@ function analyzer(opts?: AnalyzerPluginOptions): Plugin {
if (opts.summary && !hasViteReporter) {
logger.info(generateSummaryMessage(analyzerModule.modules))
}
analyzerDebug('Finish analyze bundle.' + analyzerModule.modules.length + ' chunks found.')
const analyzeModule = analyzerModule.processModule()

if (preferSilent) {
Expand Down Expand Up @@ -195,10 +189,7 @@ function analyzer(opts?: AnalyzerPluginOptions): Plugin {
const address = `http://localhost:${port}`
openBrowser(address)
}
return
}

throw new Error('Invalidate Option `analyzerMode`')
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/server/opener.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// MIT LICENSE
// Copyright (c) Domenic Denicola
// https://github.com/domenic/opener/blob/master/LICENSE.txt

// This implementation is based on the `opener` package (It's no longer maintained)
// But we fixed the wsl/wsl2 (maybe)
// In past versions. The wsl/wsl2 os release might be `Microsoft` but now it's full lowercase `microsoft`

import os from 'os'
import child_process from 'child_process'

Expand Down
6 changes: 4 additions & 2 deletions src/server/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export function pickupContentFromSourcemap(rawSourcemap: string) {

export function pickupMappingsFromCodeBinary(bytes: Uint8Array, rawSourcemap: string, formatter: (id: string) => string) {
const consumer = new Sourcemap(rawSourcemap)
const grouped: Record<string, string> = {}
const grouped: Record<string, string | Uint8Array> = {}
const files = new Set<string>()
let line = 1
let column = 0
const code = byteToString(bytes)
Expand All @@ -51,6 +52,7 @@ export function pickupMappingsFromCodeBinary(bytes: Uint8Array, rawSourcemap: st
grouped[id] = ''
}
grouped[id] += char
files.add(id)
}

if (code[i] === '\n') {
Expand All @@ -59,5 +61,5 @@ export function pickupMappingsFromCodeBinary(bytes: Uint8Array, rawSourcemap: st
}
}
consumer.destroy()
return grouped
return { grouped, files }
}

0 comments on commit c5c42c2

Please sign in to comment.