vite-plugin-rebundle 1.22.0 → 1.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rebundle.js +7 -1
- package/dist/rebundle.js.map +1 -0
- package/package.json +5 -13
- package/src/rebundle.ts +0 -192
package/dist/rebundle.js
CHANGED
|
@@ -50,6 +50,7 @@ var RebundleVite = class {
|
|
|
50
50
|
const info = this.config.logger.info;
|
|
51
51
|
this.config.logger.info = (message, options) => {
|
|
52
52
|
const path = message.split(/\s+/)[0];
|
|
53
|
+
if (is.absent(path)) return;
|
|
53
54
|
if (extname(path) === ".js") return;
|
|
54
55
|
info(message, options);
|
|
55
56
|
};
|
|
@@ -69,7 +70,11 @@ var RebundleVite = class {
|
|
|
69
70
|
onWriteBundle = async (_output, bundle) => {
|
|
70
71
|
const modifiedEntryChunks = this.getEntryChunks(bundle).filter((chunk) => {
|
|
71
72
|
const usedPaths = [chunk.fileName, ...chunk.imports];
|
|
72
|
-
return usedPaths.some((path) =>
|
|
73
|
+
return usedPaths.some((path) => {
|
|
74
|
+
if (!bundle[path]) return false;
|
|
75
|
+
if (!("code" in bundle[path])) return false;
|
|
76
|
+
return bundle[path].code !== this.originals[path];
|
|
77
|
+
});
|
|
73
78
|
});
|
|
74
79
|
await Promise.all(
|
|
75
80
|
modifiedEntryChunks.map(async (chunk) => {
|
|
@@ -141,3 +146,4 @@ export {
|
|
|
141
146
|
rebundle_default as default,
|
|
142
147
|
rebundle
|
|
143
148
|
};
|
|
149
|
+
//# sourceMappingURL=rebundle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rebundle.ts"],"sourcesContent":["import chalk from 'chalk'\nimport { is } from 'dropcap/utils'\nimport { filesize } from 'filesize'\nimport { rm, stat } from 'node:fs/promises'\nimport { extname, join } from 'node:path'\nimport { getPort } from 'portfinder'\nimport { rolldown, type InputOptions, type OutputOptions } from 'rolldown'\nimport type { NormalizedOutputOptions, OutputBundle } from 'rollup'\nimport type { Plugin, ResolvedConfig, UserConfig } from 'vite'\nimport { WebSocketServer } from 'ws'\n\nexport type RolldownOptions = {\n input?: InputOptions\n output?: OutputOptions\n}\n\nexport type BundleOptions = {\n [bundleName: string]: RolldownOptions\n}\n\nexport class RebundleVite {\n private commonOptions: RolldownOptions\n private bundleOptions: BundleOptions\n private config: ResolvedConfig | null = null\n private originals: Record<string, string> = {}\n private port: number | null = null\n private ws: WebSocketServer | null = null\n private isRollupVite = false\n private isRolldownVite = false\n private ORIGINALS_DIR = 'REBUNDLE_originals'\n\n constructor(commonOptions?: RolldownOptions | null, bundleOptions?: BundleOptions) {\n this.commonOptions = commonOptions ?? {}\n this.bundleOptions = bundleOptions ?? {}\n }\n\n get plugin(): Plugin {\n return {\n name: 'vite-plugin-rebundle',\n apply: 'build',\n enforce: 'post',\n config: this.onConfig,\n configResolved: this.onConfigResolved,\n generateBundle: this.onGenerateBundle,\n writeBundle: this.onWriteBundle,\n }\n }\n\n private onConfig = async (config: UserConfig) => {\n if (config.build?.watch) {\n this.port = await getPort({ port: 3100 })\n this.ws = new WebSocketServer({ port: this.port })\n }\n\n return {\n define: { 'import.meta.env.REBUNDLE_PORT': JSON.stringify(this.port) },\n build: { sourcemap: false },\n }\n }\n\n private onConfigResolved = async (config: ResolvedConfig) => {\n // Detect Vite variant\n this.isRollupVite = !('oxc' in config)\n this.isRolldownVite = !this.isRollupVite\n\n // Save resolved config\n this.config = config\n\n // Hide js files from output logs for rollup Vite\n if (this.isRollupVite) {\n const info = this.config.logger.info\n this.config.logger.info = (message, options) => {\n const path = message.split(/\\s+/)[0]\n if (is.absent(path)) return\n if (extname(path) === '.js') return\n info(message, options)\n }\n }\n }\n\n private onGenerateBundle = async (_options: NormalizedOutputOptions, bundle: OutputBundle) => {\n for (const chunk of this.getChunks(bundle)) {\n const originalFileName = chunk.fileName\n\n // Move all chunks to a temporary subfolder\n chunk.fileName = this.prefixed(originalFileName)\n chunk.imports = chunk.imports.map(name => this.prefixed(name))\n\n // Use prefixed names as bundle keys for rollup Vite (rolldown Vite does this automatically)\n if (this.isRollupVite) {\n bundle[chunk.fileName] = chunk\n delete bundle[originalFileName]\n }\n }\n }\n\n private onWriteBundle = async (_output: NormalizedOutputOptions, bundle: OutputBundle) => {\n // Get modified entry chunks\n const modifiedEntryChunks = this.getEntryChunks(bundle).filter(chunk => {\n const usedPaths = [chunk.fileName, ...chunk.imports]\n return usedPaths.some(path => {\n if (!bundle[path]) return false\n if (!('code' in bundle[path])) return false\n return bundle[path].code !== this.originals[path]\n })\n })\n\n // Rebundle modified entry chunks\n await Promise.all(\n modifiedEntryChunks.map(async chunk => {\n const originalFileName = this.unprefixed(chunk.fileName)\n\n // Build with rolldown\n const build = await rolldown({\n ...this.merge(this.commonOptions.input ?? {}, this.bundleOptions[chunk.name]?.input ?? {}),\n input: join(this.dist, chunk.fileName),\n })\n await build.write({\n ...this.merge(this.commonOptions.output ?? {}, this.bundleOptions[chunk.name]?.output ?? {}),\n sourcemap: false,\n file: join(this.dist, originalFileName),\n })\n\n // Log successful build\n const { size } = await stat(join(this.dist, originalFileName))\n const $dist = chalk.dim(`${this.dist}/`)\n const $fileName = chalk.cyan(originalFileName)\n const $rebundle = chalk.dim.cyan('[rebundle]')\n const $size = chalk.bold.dim(`${filesize(size)}`)\n console.log(`${$dist}${$fileName} ${$rebundle} ${$size}`)\n }),\n )\n\n for (const chunk of this.getChunks(bundle)) {\n // Save original chunk code\n this.originals[chunk.fileName] = chunk.code\n\n // Delete chunk from the bundle to hide Vite's output log\n if (this.isRolldownVite) delete bundle[chunk.fileName]\n }\n\n // Remove folder with original chunks\n await rm(join(this.dist, this.ORIGINALS_DIR), { recursive: true })\n\n // Notify about modified chunks\n if (this.ws && modifiedEntryChunks.length > 0) {\n const names = modifiedEntryChunks.map(chunk => chunk.name)\n this.ws.clients.forEach(client => client.send(JSON.stringify(names)))\n }\n }\n\n // ---------------------------------------------------------------------------\n // HELPERS\n // ---------------------------------------------------------------------------\n\n private get dist() {\n if (!this.config) throw 'never'\n return this.config.build.outDir\n }\n\n private prefixed(path: string) {\n return join(this.ORIGINALS_DIR, path)\n }\n\n private unprefixed(path: string) {\n return path.replace(`${this.ORIGINALS_DIR}/`, '')\n }\n\n private getChunks(bundle: OutputBundle) {\n return Object.values(bundle).filter(item => item.type === 'chunk')\n }\n\n private getEntryChunks(bundle: OutputBundle) {\n return Object.values(bundle)\n .filter(item => item.type === 'chunk')\n .filter(chunk => chunk.isEntry)\n }\n\n private merge(obj1: Record<string, any>, obj2: Record<string, any>) {\n const result: Record<string, any> = { ...obj1 }\n for (const key in obj2) {\n if (is.object(obj1[key]) && is.object(obj2[key])) {\n result[key] = this.merge(obj1[key], obj2[key])\n } else {\n result[key] = obj2[key]\n }\n }\n\n return result\n }\n}\n\nexport function rebundle(commonOptions?: RolldownOptions | null, bundleOptions?: BundleOptions) {\n return new RebundleVite(commonOptions, bundleOptions).plugin\n}\n\nexport default rebundle\n"],"mappings":";AAAA,OAAO,WAAW;AAClB,SAAS,UAAU;AACnB,SAAS,gBAAgB;AACzB,SAAS,IAAI,YAAY;AACzB,SAAS,SAAS,YAAY;AAC9B,SAAS,eAAe;AACxB,SAAS,gBAAuD;AAGhE,SAAS,uBAAuB;AAWzB,IAAM,eAAN,MAAmB;AAAA,EAChB;AAAA,EACA;AAAA,EACA,SAAgC;AAAA,EAChC,YAAoC,CAAC;AAAA,EACrC,OAAsB;AAAA,EACtB,KAA6B;AAAA,EAC7B,eAAe;AAAA,EACf,iBAAiB;AAAA,EACjB,gBAAgB;AAAA,EAExB,YAAY,eAAwC,eAA+B;AACjF,SAAK,gBAAgB,iBAAiB,CAAC;AACvC,SAAK,gBAAgB,iBAAiB,CAAC;AAAA,EACzC;AAAA,EAEA,IAAI,SAAiB;AACnB,WAAO;AAAA,MACL,MAAM;AAAA,MACN,OAAO;AAAA,MACP,SAAS;AAAA,MACT,QAAQ,KAAK;AAAA,MACb,gBAAgB,KAAK;AAA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AAAA,MACxB;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;AAEO,SAAS,SAAS,eAAwC,eAA+B;AAC9F,SAAO,IAAI,aAAa,eAAe,aAAa,EAAE;AACxD;AAEA,IAAO,mBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-rebundle",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "imkost",
|
|
@@ -16,27 +16,19 @@
|
|
|
16
16
|
"release": "sh -c 'npm version ${1:-minor} && npm run build && npm publish' --"
|
|
17
17
|
},
|
|
18
18
|
"exports": {
|
|
19
|
-
".":
|
|
20
|
-
"source": "./src/rebundle.ts",
|
|
21
|
-
"import": "./dist/rebundle.js",
|
|
22
|
-
"types": "./dist/rebundle.d.ts"
|
|
23
|
-
},
|
|
24
|
-
"./ts": {
|
|
25
|
-
"import": "./src/rebundle.ts"
|
|
26
|
-
}
|
|
19
|
+
".": "./dist/rebundle.js"
|
|
27
20
|
},
|
|
28
21
|
"files": [
|
|
29
|
-
"src",
|
|
30
22
|
"dist"
|
|
31
23
|
],
|
|
32
24
|
"dependencies": {
|
|
33
25
|
"@types/ws": "^8.18.1",
|
|
34
26
|
"chalk": "^5.6.2",
|
|
35
|
-
"dropcap": "^1.
|
|
27
|
+
"dropcap": "^1.4.0",
|
|
36
28
|
"filesize": "^11.0.13",
|
|
37
29
|
"portfinder": "^1.0.38",
|
|
38
|
-
"rolldown": "^1.0.0-beta.
|
|
39
|
-
"rollup": "^4.
|
|
30
|
+
"rolldown": "^1.0.0-beta.58",
|
|
31
|
+
"rollup": "^4.54.0",
|
|
40
32
|
"vite": "^7.3.0",
|
|
41
33
|
"ws": "^8.18.3"
|
|
42
34
|
}
|
package/src/rebundle.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
import { is } from 'dropcap/utils'
|
|
3
|
-
import { filesize } from 'filesize'
|
|
4
|
-
import { rm, stat } from 'node:fs/promises'
|
|
5
|
-
import { extname, join } from 'node:path'
|
|
6
|
-
import { getPort } from 'portfinder'
|
|
7
|
-
import { rolldown, type InputOptions, type OutputOptions } from 'rolldown'
|
|
8
|
-
import type { NormalizedOutputOptions, OutputBundle } from 'rollup'
|
|
9
|
-
import type { Plugin, ResolvedConfig, UserConfig } from 'vite'
|
|
10
|
-
import { WebSocketServer } from 'ws'
|
|
11
|
-
|
|
12
|
-
export type RolldownOptions = {
|
|
13
|
-
input?: InputOptions
|
|
14
|
-
output?: OutputOptions
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export type BundleOptions = {
|
|
18
|
-
[bundleName: string]: RolldownOptions
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export class RebundleVite {
|
|
22
|
-
private commonOptions: RolldownOptions
|
|
23
|
-
private bundleOptions: BundleOptions
|
|
24
|
-
private config: ResolvedConfig | null = null
|
|
25
|
-
private originals: Record<string, string> = {}
|
|
26
|
-
private port: number | null = null
|
|
27
|
-
private ws: WebSocketServer | null = null
|
|
28
|
-
private isRollupVite = false
|
|
29
|
-
private isRolldownVite = false
|
|
30
|
-
private ORIGINALS_DIR = 'REBUNDLE_originals'
|
|
31
|
-
|
|
32
|
-
constructor(commonOptions?: RolldownOptions | null, bundleOptions?: BundleOptions) {
|
|
33
|
-
this.commonOptions = commonOptions ?? {}
|
|
34
|
-
this.bundleOptions = bundleOptions ?? {}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get plugin(): Plugin {
|
|
38
|
-
return {
|
|
39
|
-
name: 'vite-plugin-rebundle',
|
|
40
|
-
apply: 'build',
|
|
41
|
-
enforce: 'post',
|
|
42
|
-
config: this.onConfig,
|
|
43
|
-
configResolved: this.onConfigResolved,
|
|
44
|
-
generateBundle: this.onGenerateBundle,
|
|
45
|
-
writeBundle: this.onWriteBundle,
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private onConfig = async (config: UserConfig) => {
|
|
50
|
-
if (config.build?.watch) {
|
|
51
|
-
this.port = await getPort({ port: 3100 })
|
|
52
|
-
this.ws = new WebSocketServer({ port: this.port })
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
define: { 'import.meta.env.REBUNDLE_PORT': JSON.stringify(this.port) },
|
|
57
|
-
build: { sourcemap: false },
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
private onConfigResolved = async (config: ResolvedConfig) => {
|
|
62
|
-
// Detect Vite variant
|
|
63
|
-
this.isRollupVite = !('oxc' in config)
|
|
64
|
-
this.isRolldownVite = !this.isRollupVite
|
|
65
|
-
|
|
66
|
-
// Save resolved config
|
|
67
|
-
this.config = config
|
|
68
|
-
|
|
69
|
-
// Hide js files from output logs for rollup Vite
|
|
70
|
-
if (this.isRollupVite) {
|
|
71
|
-
const info = this.config.logger.info
|
|
72
|
-
this.config.logger.info = (message, options) => {
|
|
73
|
-
const path = message.split(/\s+/)[0]
|
|
74
|
-
if (extname(path) === '.js') return
|
|
75
|
-
info(message, options)
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private onGenerateBundle = async (_options: NormalizedOutputOptions, bundle: OutputBundle) => {
|
|
81
|
-
for (const chunk of this.getChunks(bundle)) {
|
|
82
|
-
const originalFileName = chunk.fileName
|
|
83
|
-
|
|
84
|
-
// Move all chunks to a temporary subfolder
|
|
85
|
-
chunk.fileName = this.prefixed(originalFileName)
|
|
86
|
-
chunk.imports = chunk.imports.map(name => this.prefixed(name))
|
|
87
|
-
|
|
88
|
-
// Use prefixed names as bundle keys for rollup Vite (rolldown Vite does this automatically)
|
|
89
|
-
if (this.isRollupVite) {
|
|
90
|
-
bundle[chunk.fileName] = chunk
|
|
91
|
-
delete bundle[originalFileName]
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
private onWriteBundle = async (_output: NormalizedOutputOptions, bundle: OutputBundle) => {
|
|
97
|
-
// Get modified entry chunks
|
|
98
|
-
const modifiedEntryChunks = this.getEntryChunks(bundle).filter(chunk => {
|
|
99
|
-
const usedPaths = [chunk.fileName, ...chunk.imports]
|
|
100
|
-
return usedPaths.some(path => 'code' in bundle[path] && bundle[path].code !== this.originals[path])
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
// Rebundle modified entry chunks
|
|
104
|
-
await Promise.all(
|
|
105
|
-
modifiedEntryChunks.map(async chunk => {
|
|
106
|
-
const originalFileName = this.unprefixed(chunk.fileName)
|
|
107
|
-
|
|
108
|
-
// Build with rolldown
|
|
109
|
-
const build = await rolldown({
|
|
110
|
-
...this.merge(this.commonOptions.input ?? {}, this.bundleOptions[chunk.name]?.input ?? {}),
|
|
111
|
-
input: join(this.dist, chunk.fileName),
|
|
112
|
-
})
|
|
113
|
-
await build.write({
|
|
114
|
-
...this.merge(this.commonOptions.output ?? {}, this.bundleOptions[chunk.name]?.output ?? {}),
|
|
115
|
-
sourcemap: false,
|
|
116
|
-
file: join(this.dist, originalFileName),
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
// Log successful build
|
|
120
|
-
const { size } = await stat(join(this.dist, originalFileName))
|
|
121
|
-
const $dist = chalk.dim(`${this.dist}/`)
|
|
122
|
-
const $fileName = chalk.cyan(originalFileName)
|
|
123
|
-
const $rebundle = chalk.dim.cyan('[rebundle]')
|
|
124
|
-
const $size = chalk.bold.dim(`${filesize(size)}`)
|
|
125
|
-
console.log(`${$dist}${$fileName} ${$rebundle} ${$size}`)
|
|
126
|
-
}),
|
|
127
|
-
)
|
|
128
|
-
|
|
129
|
-
for (const chunk of this.getChunks(bundle)) {
|
|
130
|
-
// Save original chunk code
|
|
131
|
-
this.originals[chunk.fileName] = chunk.code
|
|
132
|
-
|
|
133
|
-
// Delete chunk from the bundle to hide Vite's output log
|
|
134
|
-
if (this.isRolldownVite) delete bundle[chunk.fileName]
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
// Remove folder with original chunks
|
|
138
|
-
await rm(join(this.dist, this.ORIGINALS_DIR), { recursive: true })
|
|
139
|
-
|
|
140
|
-
// Notify about modified chunks
|
|
141
|
-
if (this.ws && modifiedEntryChunks.length > 0) {
|
|
142
|
-
const names = modifiedEntryChunks.map(chunk => chunk.name)
|
|
143
|
-
this.ws.clients.forEach(client => client.send(JSON.stringify(names)))
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ---------------------------------------------------------------------------
|
|
148
|
-
// HELPERS
|
|
149
|
-
// ---------------------------------------------------------------------------
|
|
150
|
-
|
|
151
|
-
private get dist() {
|
|
152
|
-
if (!this.config) throw 'never'
|
|
153
|
-
return this.config.build.outDir
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
private prefixed(path: string) {
|
|
157
|
-
return join(this.ORIGINALS_DIR, path)
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private unprefixed(path: string) {
|
|
161
|
-
return path.replace(`${this.ORIGINALS_DIR}/`, '')
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private getChunks(bundle: OutputBundle) {
|
|
165
|
-
return Object.values(bundle).filter(item => item.type === 'chunk')
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
private getEntryChunks(bundle: OutputBundle) {
|
|
169
|
-
return Object.values(bundle)
|
|
170
|
-
.filter(item => item.type === 'chunk')
|
|
171
|
-
.filter(chunk => chunk.isEntry)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
private merge(obj1: Record<string, any>, obj2: Record<string, any>) {
|
|
175
|
-
const result: Record<string, any> = { ...obj1 }
|
|
176
|
-
for (const key in obj2) {
|
|
177
|
-
if (is.object(obj1[key]) && is.object(obj2[key])) {
|
|
178
|
-
result[key] = this.merge(obj1[key], obj2[key])
|
|
179
|
-
} else {
|
|
180
|
-
result[key] = obj2[key]
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return result
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export function rebundle(commonOptions?: RolldownOptions | null, bundleOptions?: BundleOptions) {
|
|
189
|
-
return new RebundleVite(commonOptions, bundleOptions).plugin
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export default rebundle
|