vite-plugin-rebundle 1.8.0 → 1.10.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.
@@ -9,18 +9,19 @@ type RolldownOptions = {
9
9
  input?: InputOptions;
10
10
  output?: OutputOptions;
11
11
  };
12
- type Options = {
12
+ type BundleOptions = {
13
13
  [bundleName: string]: RolldownOptions;
14
14
  };
15
15
  declare class Rebundle extends Unit {
16
- private options;
16
+ private generalOptions;
17
+ private bundleOptions;
17
18
  private config;
18
19
  private originalFiles;
19
20
  private rebundledFiles;
20
21
  private hasError;
21
22
  private port;
22
23
  private ws;
23
- constructor(options: Options);
24
+ constructor(generalOptions: RolldownOptions | null, bundleOptions?: BundleOptions);
24
25
  get vite(): Plugin;
25
26
  private onConfig;
26
27
  private onConfigResolved;
@@ -37,7 +38,8 @@ declare class Rebundle extends Unit {
37
38
  private removeFromDist;
38
39
  private ensureWs;
39
40
  private removeDirectoryIfEmpty;
41
+ private merge;
40
42
  }
41
- declare function rebundle(options?: Options): Plugin<any>;
43
+ declare function rebundle(generalOptions: RolldownOptions | null, bundleOptions?: BundleOptions): Plugin<any>;
42
44
 
43
- export { type Options, Rebundle, type RolldownOptions, _code_, _sourcemap_, rebundle as default, rebundle };
45
+ export { type BundleOptions, Rebundle, type RolldownOptions, _code_, _sourcemap_, rebundle as default, rebundle };
package/dist/rebundle.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/rebundle.ts
2
- import { safe, Unit } from "@eposlabs/utils";
2
+ import { safe, Unit, is } from "@eposlabs/utils";
3
3
  import chalk from "chalk";
4
4
  import { filesize } from "filesize";
5
5
  import { readdir, readFile, rmdir, stat, unlink } from "fs/promises";
@@ -10,16 +10,18 @@ import { WebSocketServer } from "ws";
10
10
  var _code_ = Symbol("rebundle:code");
11
11
  var _sourcemap_ = Symbol("rebundle:sourcemap");
12
12
  var Rebundle = class extends Unit {
13
- options;
13
+ generalOptions;
14
+ bundleOptions;
14
15
  config = null;
15
16
  originalFiles = {};
16
17
  rebundledFiles = {};
17
18
  hasError = false;
18
19
  port = null;
19
20
  ws = null;
20
- constructor(options) {
21
+ constructor(generalOptions, bundleOptions) {
21
22
  super();
22
- this.options = options;
23
+ this.generalOptions = generalOptions ?? {};
24
+ this.bundleOptions = bundleOptions ?? {};
23
25
  }
24
26
  get vite() {
25
27
  return {
@@ -78,8 +80,6 @@ var Rebundle = class extends Unit {
78
80
  if (chunk.isEntry) continue;
79
81
  await this.removeFromDist(chunk.fileName);
80
82
  delete bundle[chunk.fileName];
81
- const dir = dirname(join(this.dist, chunk.fileName));
82
- await this.removeDirectoryIfEmpty(dir);
83
83
  }
84
84
  if (this.config.build.watch && modifiedChunkNames.length > 0) {
85
85
  const ws = await this.ensureWs();
@@ -100,12 +100,18 @@ var Rebundle = class extends Unit {
100
100
  await this.removeFromDist(chunk.fileName);
101
101
  return false;
102
102
  }
103
- const options = this.options[chunk.name] ?? {};
104
103
  const [result] = await safe(async () => {
105
104
  const inputPath = join(this.dist, chunk.fileName);
106
105
  const outputPath = join(this.dist, this.unprefixed(chunk.fileName));
107
- const build = await rolldown({ ...options.input, input: inputPath });
108
- const result2 = await build.write({ ...options.output, sourcemap: false, file: outputPath });
106
+ const build = await rolldown({
107
+ ...this.merge(this.generalOptions.input ?? {}, this.bundleOptions[chunk.name]?.input ?? {}),
108
+ input: inputPath
109
+ });
110
+ const result2 = await build.write({
111
+ ...this.merge(this.generalOptions.output ?? {}, this.bundleOptions[chunk.name]?.output ?? {}),
112
+ sourcemap: false,
113
+ file: outputPath
114
+ });
109
115
  return result2;
110
116
  });
111
117
  if (!result) return;
@@ -153,7 +159,10 @@ var Rebundle = class extends Unit {
153
159
  return content;
154
160
  }
155
161
  async removeFromDist(path) {
156
- await safe(unlink(join(this.dist, path)));
162
+ path = join(this.dist, path);
163
+ const dir = dirname(path);
164
+ await safe(unlink(path));
165
+ await this.removeDirectoryIfEmpty(dir);
157
166
  }
158
167
  async ensureWs() {
159
168
  if (this.ws) return this.ws;
@@ -167,9 +176,20 @@ var Rebundle = class extends Unit {
167
176
  await rmdir(dir);
168
177
  await this.removeDirectoryIfEmpty(dirname(dir));
169
178
  }
179
+ merge(obj1, obj2) {
180
+ const result = { ...obj1 };
181
+ for (const key in obj2) {
182
+ if (is.object(obj1[key]) && is.object(obj2[key])) {
183
+ result[key] = this.merge(obj1[key], obj2[key]);
184
+ } else {
185
+ result[key] = obj2[key];
186
+ }
187
+ }
188
+ return result;
189
+ }
170
190
  };
171
- function rebundle(options = {}) {
172
- return new Rebundle(options).vite;
191
+ function rebundle(generalOptions, bundleOptions) {
192
+ return new Rebundle(generalOptions, bundleOptions).vite;
173
193
  }
174
194
  var rebundle_default = rebundle;
175
195
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-rebundle",
3
- "version": "1.8.0",
3
+ "version": "1.10.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "imkost",
@@ -27,14 +27,14 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@eposlabs/utils": "^1.8.0",
30
+ "@eposlabs/utils": "^1.9.0",
31
31
  "@types/ws": "^8.18.1",
32
32
  "chalk": "^5.6.2",
33
33
  "filesize": "^11.0.13",
34
34
  "portfinder": "^1.0.38",
35
35
  "rolldown": "^1.0.0-beta.41",
36
- "rollup": "^4.52.3",
37
- "vite": "^7.1.7",
36
+ "rollup": "^4.52.4",
37
+ "vite": "^7.1.9",
38
38
  "ws": "^8.18.3"
39
39
  }
40
40
  }
package/src/rebundle.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { safe, Unit } from '@eposlabs/utils'
1
+ import { safe, Unit, is } from '@eposlabs/utils'
2
2
  import chalk from 'chalk'
3
3
  import { filesize } from 'filesize'
4
4
  import { readdir, readFile, rmdir, stat, unlink } from 'node:fs/promises'
@@ -17,12 +17,13 @@ export type RolldownOptions = {
17
17
  output?: OutputOptions
18
18
  }
19
19
 
20
- export type Options = {
20
+ export type BundleOptions = {
21
21
  [bundleName: string]: RolldownOptions
22
22
  }
23
23
 
24
24
  export class Rebundle extends Unit {
25
- private options: Options
25
+ private generalOptions: RolldownOptions
26
+ private bundleOptions: BundleOptions
26
27
  private config: ResolvedConfig | null = null
27
28
  private originalFiles: Record<string, string> = {}
28
29
  private rebundledFiles: Record<string, string> = {}
@@ -30,9 +31,10 @@ export class Rebundle extends Unit {
30
31
  private port: number | null = null
31
32
  private ws: WebSocketServer | null = null
32
33
 
33
- constructor(options: Options) {
34
+ constructor(generalOptions: RolldownOptions | null, bundleOptions?: BundleOptions) {
34
35
  super()
35
- this.options = options
36
+ this.generalOptions = generalOptions ?? {}
37
+ this.bundleOptions = bundleOptions ?? {}
36
38
  }
37
39
 
38
40
  get vite(): Plugin {
@@ -110,10 +112,6 @@ export class Rebundle extends Unit {
110
112
  // Remove from dist and bundle
111
113
  await this.removeFromDist(chunk.fileName)
112
114
  delete bundle[chunk.fileName]
113
-
114
- // Recursively remove containing directory if empty
115
- const dir = dirname(join(this.dist, chunk.fileName))
116
- await this.removeDirectoryIfEmpty(dir)
117
115
  }
118
116
 
119
117
  // Notify about modified chunks
@@ -150,12 +148,21 @@ export class Rebundle extends Unit {
150
148
  }
151
149
 
152
150
  // Build with rolldown
153
- const options = this.options[chunk.name] ?? {}
154
151
  const [result] = await safe(async () => {
155
152
  const inputPath = join(this.dist, chunk.fileName)
156
153
  const outputPath = join(this.dist, this.unprefixed(chunk.fileName))
157
- const build = await rolldown({ ...options.input, input: inputPath })
158
- const result = await build.write({ ...options.output, sourcemap: false, file: outputPath })
154
+
155
+ const build = await rolldown({
156
+ ...this.merge(this.generalOptions.input ?? {}, this.bundleOptions[chunk.name]?.input ?? {}),
157
+ input: inputPath,
158
+ })
159
+
160
+ const result = await build.write({
161
+ ...this.merge(this.generalOptions.output ?? {}, this.bundleOptions[chunk.name]?.output ?? {}),
162
+ sourcemap: false,
163
+ file: outputPath,
164
+ })
165
+
159
166
  return result
160
167
  })
161
168
  if (!result) return
@@ -221,7 +228,10 @@ export class Rebundle extends Unit {
221
228
  }
222
229
 
223
230
  private async removeFromDist(path: string) {
224
- await safe(unlink(join(this.dist, path)))
231
+ path = join(this.dist, path)
232
+ const dir = dirname(path)
233
+ await safe(unlink(path))
234
+ await this.removeDirectoryIfEmpty(dir)
225
235
  }
226
236
 
227
237
  private async ensureWs() {
@@ -237,10 +247,23 @@ export class Rebundle extends Unit {
237
247
  await rmdir(dir)
238
248
  await this.removeDirectoryIfEmpty(dirname(dir))
239
249
  }
250
+
251
+ private merge(obj1: Record<string, any>, obj2: Record<string, any>) {
252
+ const result: Record<string, any> = { ...obj1 }
253
+ for (const key in obj2) {
254
+ if (is.object(obj1[key]) && is.object(obj2[key])) {
255
+ result[key] = this.merge(obj1[key], obj2[key])
256
+ } else {
257
+ result[key] = obj2[key]
258
+ }
259
+ }
260
+
261
+ return result
262
+ }
240
263
  }
241
264
 
242
- export function rebundle(options: Options = {}) {
243
- return new Rebundle(options).vite
265
+ export function rebundle(generalOptions: RolldownOptions | null, bundleOptions?: BundleOptions) {
266
+ return new Rebundle(generalOptions, bundleOptions).vite
244
267
  }
245
268
 
246
269
  export default rebundle