vite-plugin-rebundle 1.2.6 → 1.2.8
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.d.ts +11 -5
- package/dist/rebundle.js +115 -100
- package/package.json +2 -4
package/dist/rebundle.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import * as $utils from '@eposlabs/utils';
|
|
2
2
|
import type { BuildOptions } from 'esbuild';
|
|
3
|
+
import type { OutputBundle, OutputChunk } from 'rollup';
|
|
3
4
|
import type { Plugin } from 'vite';
|
|
5
|
+
export declare const _code_: unique symbol;
|
|
6
|
+
export declare const _sourcemap_: unique symbol;
|
|
4
7
|
export type Options = {
|
|
5
8
|
[chunkName: string]: BuildOptions;
|
|
6
9
|
};
|
|
@@ -19,12 +22,15 @@ export declare class Rebundle extends $utils.Unit {
|
|
|
19
22
|
private onConfigResolved;
|
|
20
23
|
private onBuildEnd;
|
|
21
24
|
private onWriteBundle;
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
private
|
|
25
|
-
private
|
|
25
|
+
rebundleChunk(chunk: OutputChunk, bundle: OutputBundle): Promise<boolean | undefined>;
|
|
26
|
+
private removeChunk;
|
|
27
|
+
private readChunkFiles;
|
|
28
|
+
private get dist();
|
|
29
|
+
private resolve;
|
|
30
|
+
private read;
|
|
31
|
+
private write;
|
|
32
|
+
private remove;
|
|
26
33
|
private ensureWs;
|
|
27
34
|
private getOptions;
|
|
28
|
-
private readChunkFiles;
|
|
29
35
|
private removeDirectoryIfEmpty;
|
|
30
36
|
}
|
package/dist/rebundle.js
CHANGED
|
@@ -6,6 +6,8 @@ import * as $utils from '@eposlabs/utils';
|
|
|
6
6
|
import * as $ws from 'ws';
|
|
7
7
|
import $chalk from 'chalk';
|
|
8
8
|
import $portfinder from 'portfinder';
|
|
9
|
+
export const _code_ = Symbol('rebundle:code');
|
|
10
|
+
export const _sourcemap_ = Symbol('rebundle:sourcemap');
|
|
9
11
|
export class Rebundle extends $utils.Unit {
|
|
10
12
|
options;
|
|
11
13
|
config = null;
|
|
@@ -30,7 +32,7 @@ export class Rebundle extends $utils.Unit {
|
|
|
30
32
|
};
|
|
31
33
|
}
|
|
32
34
|
// ---------------------------------------------------------------------------
|
|
33
|
-
// HOOKS
|
|
35
|
+
// VITE HOOKS
|
|
34
36
|
// ---------------------------------------------------------------------------
|
|
35
37
|
onConfig = async () => {
|
|
36
38
|
this.port = await $portfinder.getPort({ port: 3100 });
|
|
@@ -43,7 +45,7 @@ export class Rebundle extends $utils.Unit {
|
|
|
43
45
|
onConfigResolved = async (config) => {
|
|
44
46
|
// Save resolved config
|
|
45
47
|
this.config = config;
|
|
46
|
-
// Hide
|
|
48
|
+
// Hide js files from output logs (rollup only, not supported in rolldown)
|
|
47
49
|
const info = this.config.logger.info;
|
|
48
50
|
this.config.logger.info = (message, options) => {
|
|
49
51
|
const path = message.split(/\s+/)[0];
|
|
@@ -58,118 +60,138 @@ export class Rebundle extends $utils.Unit {
|
|
|
58
60
|
onWriteBundle = async (_output, bundle) => {
|
|
59
61
|
if (this.hasError)
|
|
60
62
|
return;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
// Delete chunk from bundle to hide log in vite-rolldown.
|
|
73
|
-
// Call for rollup as well for consistency.
|
|
74
|
-
delete bundle[chunk.fileName];
|
|
75
|
-
const chunkPath = this.outPath(chunk.fileName);
|
|
76
|
-
const chunkBuildOptions = options[chunk.name] ?? {};
|
|
77
|
-
const chunkFiles = await this.readChunkFiles(chunk);
|
|
78
|
-
const chunkFilePaths = Object.keys(chunkFiles);
|
|
79
|
-
const chunkChanged = chunkFilePaths.some(path => chunkFiles[path] !== this.chunkFiles[path]);
|
|
80
|
-
// Update files cache
|
|
81
|
-
Object.assign(this.chunkFiles, chunkFiles);
|
|
82
|
-
// Modified? -> Rebundle
|
|
83
|
-
if (chunkChanged) {
|
|
84
|
-
// Build with esbuild
|
|
85
|
-
const result = await $esbuild.build({
|
|
86
|
-
sourcemap: Boolean(this.config.build.sourcemap),
|
|
87
|
-
...chunkBuildOptions,
|
|
88
|
-
outfile: chunkPath,
|
|
89
|
-
entryPoints: [chunkPath],
|
|
90
|
-
bundle: true,
|
|
91
|
-
minify: false,
|
|
92
|
-
allowOverwrite: true,
|
|
93
|
-
});
|
|
94
|
-
if (result.errors.length > 0)
|
|
95
|
-
return;
|
|
96
|
-
// Log successful build
|
|
97
|
-
const { size } = await $fs.stat(chunkPath);
|
|
98
|
-
const _outDir_ = $chalk.dim(`${this.outDir}/`);
|
|
99
|
-
const _fileName_ = $chalk.cyan(chunk.fileName);
|
|
100
|
-
const _rebundle_ = $chalk.dim.cyan('[rebundle]');
|
|
101
|
-
const _size_ = $chalk.bold.dim(`${filesize(size)}`);
|
|
102
|
-
console.log(`${_outDir_}${_fileName_} ${_rebundle_} ${_size_}`);
|
|
103
|
-
// Mark chunk as changed
|
|
104
|
-
changedChunkNames.push(chunk.name);
|
|
105
|
-
// Save chunk content
|
|
106
|
-
this.rebundledContent[chunk.fileName] = await this.outRead(chunk.fileName);
|
|
107
|
-
// Save sourcemap content
|
|
108
|
-
if (chunk.sourcemapFileName) {
|
|
109
|
-
this.rebundledContent[chunk.sourcemapFileName] = await this.outRead(chunk.sourcemapFileName);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
// Overwrite chunk
|
|
113
|
-
await this.outWrite(chunk.fileName, this.rebundledContent[chunk.fileName]);
|
|
114
|
-
chunk.code = this.rebundledContent[chunk.fileName];
|
|
115
|
-
// Overwrite sourcemap
|
|
116
|
-
if (chunk.sourcemapFileName) {
|
|
117
|
-
const sourcemapAsset = bundle[chunk.sourcemapFileName];
|
|
118
|
-
if (sourcemapAsset.type !== 'asset')
|
|
119
|
-
throw this.never;
|
|
120
|
-
await this.outWrite(chunk.sourcemapFileName, this.rebundledContent[chunk.sourcemapFileName]);
|
|
121
|
-
sourcemapAsset.source = this.rebundledContent[chunk.sourcemapFileName];
|
|
122
|
-
}
|
|
63
|
+
if (!this.config)
|
|
64
|
+
throw this.never;
|
|
65
|
+
const chunks = Object.values(bundle).filter(chunkOrAsset => chunkOrAsset.type === 'chunk');
|
|
66
|
+
const entryChunks = chunks.filter(chunk => chunk.isEntry);
|
|
67
|
+
const nonEntryChunks = chunks.filter(chunk => !chunk.isEntry);
|
|
68
|
+
// Rebundle entry chunks
|
|
69
|
+
const modifiedChunkNames = [];
|
|
70
|
+
await Promise.all(entryChunks.map(async (chunk) => {
|
|
71
|
+
const modified = await this.rebundleChunk(chunk, bundle);
|
|
72
|
+
if (modified)
|
|
73
|
+
modifiedChunkNames.push(chunk.name);
|
|
123
74
|
}));
|
|
124
|
-
//
|
|
125
|
-
const nonEntryChunks = Object.values(bundle)
|
|
126
|
-
.filter(chunkOrAsset => chunkOrAsset.type === 'chunk')
|
|
127
|
-
.filter(chunk => !chunk.isEntry);
|
|
128
|
-
// Remove all non-entry chunks
|
|
75
|
+
// Remove non-entry chunks
|
|
129
76
|
for (const chunk of nonEntryChunks) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
77
|
+
await this.removeChunk(chunk, bundle);
|
|
78
|
+
}
|
|
79
|
+
// Notify about modified chunks
|
|
80
|
+
if (this.config.build.watch && modifiedChunkNames.length > 0) {
|
|
81
|
+
const ws = await this.ensureWs();
|
|
82
|
+
ws.clients.forEach(client => client.send(JSON.stringify(modifiedChunkNames)));
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// CHUNK METHODS
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
async rebundleChunk(chunk, bundle) {
|
|
89
|
+
if (!this.config)
|
|
90
|
+
throw this.never;
|
|
91
|
+
// Delete chunk from bundle to hide log for vite-rolldown.
|
|
92
|
+
// Call for rollup as well for consistency.
|
|
93
|
+
delete bundle[chunk.fileName];
|
|
94
|
+
const options = await this.getOptions();
|
|
95
|
+
const chunkPath = this.resolve(chunk.fileName);
|
|
96
|
+
const chunkBuildOptions = options[chunk.name] ?? {};
|
|
97
|
+
const chunkFiles = await this.readChunkFiles(chunk);
|
|
98
|
+
const chunkFilePaths = Object.keys(chunkFiles);
|
|
99
|
+
const chunkChanged = chunkFilePaths.some(path => chunkFiles[path] !== this.chunkFiles[path]);
|
|
100
|
+
// Update files cache
|
|
101
|
+
Object.assign(this.chunkFiles, chunkFiles);
|
|
102
|
+
// Not modified? -> Use pervious content
|
|
103
|
+
if (!chunkChanged) {
|
|
104
|
+
// Overwrite vite's code
|
|
105
|
+
const code = this.rebundledContent[chunk.fileName];
|
|
106
|
+
await this.write(chunk.fileName, code);
|
|
107
|
+
// Overwrite vite's sourcemap
|
|
134
108
|
if (chunk.sourcemapFileName) {
|
|
135
|
-
|
|
136
|
-
|
|
109
|
+
const sourcemap = this.rebundledContent[chunk.sourcemapFileName];
|
|
110
|
+
if (sourcemap)
|
|
111
|
+
await this.write(chunk.sourcemapFileName, sourcemap);
|
|
137
112
|
}
|
|
138
|
-
|
|
139
|
-
const dir = $path.dirname(this.outPath(chunk.fileName));
|
|
140
|
-
await this.removeDirectoryIfEmpty(dir);
|
|
113
|
+
return false;
|
|
141
114
|
}
|
|
142
|
-
//
|
|
143
|
-
|
|
144
|
-
|
|
115
|
+
// Build with esbuild
|
|
116
|
+
const result = await $esbuild.build({
|
|
117
|
+
sourcemap: Boolean(this.config.build.sourcemap),
|
|
118
|
+
...chunkBuildOptions,
|
|
119
|
+
outfile: chunkPath,
|
|
120
|
+
entryPoints: [chunkPath],
|
|
121
|
+
bundle: true,
|
|
122
|
+
allowOverwrite: true,
|
|
123
|
+
});
|
|
124
|
+
// Errors? -> Ignore, esbuild will show errors in console
|
|
125
|
+
if (result.errors.length > 0)
|
|
126
|
+
return;
|
|
127
|
+
// Log successful build
|
|
128
|
+
const { size } = await $fs.stat(chunkPath);
|
|
129
|
+
const _dist_ = $chalk.dim(`${this.dist}/`);
|
|
130
|
+
const _fileName_ = $chalk.cyan(chunk.fileName);
|
|
131
|
+
const _rebundle_ = $chalk.dim.cyan('[rebundle]');
|
|
132
|
+
const _size_ = $chalk.bold.dim(`${filesize(size)}`);
|
|
133
|
+
console.log(`${_dist_}${_fileName_} ${_rebundle_} ${_size_}`);
|
|
134
|
+
// Save code
|
|
135
|
+
const code = await this.read(chunk.fileName);
|
|
136
|
+
if (!code)
|
|
137
|
+
throw this.never;
|
|
138
|
+
this.rebundledContent[chunk.fileName] = code;
|
|
139
|
+
// Save sourcemap
|
|
140
|
+
if (chunk.sourcemapFileName) {
|
|
141
|
+
const sourcemap = await this.read(chunk.sourcemapFileName);
|
|
142
|
+
if (sourcemap)
|
|
143
|
+
this.rebundledContent[chunk.sourcemapFileName] = sourcemap;
|
|
145
144
|
}
|
|
146
|
-
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
async removeChunk(chunk, bundle) {
|
|
148
|
+
await this.remove(chunk.fileName);
|
|
149
|
+
delete bundle[chunk.fileName];
|
|
150
|
+
if (chunk.sourcemapFileName) {
|
|
151
|
+
await this.remove(chunk.sourcemapFileName);
|
|
152
|
+
delete bundle[chunk.sourcemapFileName];
|
|
153
|
+
}
|
|
154
|
+
// Recursively remove containing directory if empty
|
|
155
|
+
const dir = $path.dirname(this.resolve(chunk.fileName));
|
|
156
|
+
await this.removeDirectoryIfEmpty(dir);
|
|
157
|
+
}
|
|
158
|
+
async readChunkFiles(chunk) {
|
|
159
|
+
const files = {};
|
|
160
|
+
const usedPaths = [chunk.fileName, ...chunk.imports];
|
|
161
|
+
await Promise.all(usedPaths.map(async (path) => {
|
|
162
|
+
const content = await this.read(path);
|
|
163
|
+
if (!content)
|
|
164
|
+
throw this.never;
|
|
165
|
+
files[path] = content;
|
|
166
|
+
}));
|
|
167
|
+
return files;
|
|
168
|
+
}
|
|
147
169
|
// ---------------------------------------------------------------------------
|
|
148
170
|
// HELPERS
|
|
149
171
|
// ---------------------------------------------------------------------------
|
|
150
|
-
get
|
|
172
|
+
get dist() {
|
|
151
173
|
if (!this.config)
|
|
152
174
|
throw this.never;
|
|
153
175
|
return this.config.build.outDir;
|
|
154
176
|
}
|
|
155
|
-
|
|
156
|
-
return $path.join(this.
|
|
177
|
+
resolve(path) {
|
|
178
|
+
return $path.join(this.dist, path);
|
|
157
179
|
}
|
|
158
|
-
async
|
|
159
|
-
|
|
180
|
+
async read(path) {
|
|
181
|
+
const [content] = await $utils.safe($fs.readFile(this.resolve(path), 'utf-8'));
|
|
182
|
+
return content;
|
|
160
183
|
}
|
|
161
|
-
async
|
|
162
|
-
await $fs.writeFile(this.
|
|
184
|
+
async write(path, content) {
|
|
185
|
+
await $fs.writeFile(this.resolve(path), content, 'utf-8');
|
|
186
|
+
}
|
|
187
|
+
async remove(path) {
|
|
188
|
+
await $utils.safe($fs.unlink(this.resolve(path)));
|
|
163
189
|
}
|
|
164
190
|
async ensureWs() {
|
|
165
191
|
if (this.ws)
|
|
166
192
|
return this.ws;
|
|
167
193
|
if (!this.port)
|
|
168
194
|
throw this.never;
|
|
169
|
-
if (!this.config)
|
|
170
|
-
throw this.never;
|
|
171
|
-
if (!this.config.build.watch)
|
|
172
|
-
return null;
|
|
173
195
|
this.ws = new $ws.WebSocketServer({ port: this.port });
|
|
174
196
|
return this.ws;
|
|
175
197
|
}
|
|
@@ -178,13 +200,6 @@ export class Rebundle extends $utils.Unit {
|
|
|
178
200
|
return this.options;
|
|
179
201
|
return await this.options();
|
|
180
202
|
}
|
|
181
|
-
async readChunkFiles(chunk) {
|
|
182
|
-
const files = {};
|
|
183
|
-
await Promise.all([chunk.fileName, ...chunk.imports].map(async (path) => {
|
|
184
|
-
files[path] = await this.outRead(path);
|
|
185
|
-
}));
|
|
186
|
-
return files;
|
|
187
|
-
}
|
|
188
203
|
async removeDirectoryIfEmpty(dir) {
|
|
189
204
|
const files = await $fs.readdir(dir);
|
|
190
205
|
if (files.length > 0)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-rebundle",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "imkost",
|
|
@@ -13,9 +13,7 @@
|
|
|
13
13
|
"dev": "rm -rf dist && tsc --watch",
|
|
14
14
|
"build": "rm -rf dist && tsc",
|
|
15
15
|
"lint": "tsc --noEmit",
|
|
16
|
-
"release": "npm version patch && npm run
|
|
17
|
-
"release:raw": "npm run build && npm publish",
|
|
18
|
-
"release:minor": "npm version minor && npm run release:raw"
|
|
16
|
+
"release": "sh -c 'npm version ${1:-patch} && npm run build && npm publish' --"
|
|
19
17
|
},
|
|
20
18
|
"exports": {
|
|
21
19
|
"import": "./dist/index.js"
|