@shuvi/platform-web 1.0.62 → 2.0.0-dev.6

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.
@@ -0,0 +1,249 @@
1
+ import * as Rspack from '@shuvi/toolpack/lib/webpack';
2
+ interface RspackPlugin {
3
+ apply: (compiler: Rspack.Compiler) => void;
4
+ }
5
+ /**
6
+ * Configuration options for RspackBuildManifestPlugin
7
+ */
8
+ interface Options {
9
+ /** Output filename for the build manifest JSON file */
10
+ filename: string;
11
+ /** Whether to include module information in the manifest */
12
+ modules: boolean;
13
+ /** Whether to include chunk request information */
14
+ chunkRequest: boolean;
15
+ }
16
+ /**
17
+ * Rspack plugin that generates a build manifest JSON file containing mappings
18
+ * of entry filenames to their actual output filenames (which may be hashed in production).
19
+ *
20
+ * This plugin is adapted from the webpack version to work with Rspack's API.
21
+ * It provides the same functionality for frameworks that need to know the relationship
22
+ * between entry points and their corresponding built assets.
23
+ *
24
+ * ## Features
25
+ * - Maps entry points to their output files
26
+ * - Tracks bundle files and their relationships
27
+ * - Collects polyfill files
28
+ * - Supports loadable modules/chunks
29
+ * - Handles both development and production builds
30
+ *
31
+ * ## Generated Manifest Structure
32
+ * ```json
33
+ * {
34
+ * "entries": {
35
+ * "main": {
36
+ * "js": ["/static/js/main.abc123.js"],
37
+ * "css": ["/static/css/main.def456.css"]
38
+ * }
39
+ * },
40
+ * "bundles": {
41
+ * "main": "/static/js/main.abc123.js"
42
+ * },
43
+ * "chunkRequest": {
44
+ * "/static/js/chunk.xyz789.js": "/pages/about"
45
+ * },
46
+ * "loadble": {
47
+ * "/pages/about": {
48
+ * "files": ["/static/js/chunk.xyz789.js"],
49
+ * "children": [
50
+ * {
51
+ * "id": "module-id",
52
+ * "name": "AboutPage"
53
+ * }
54
+ * ]
55
+ * }
56
+ * },
57
+ * "polyfillFiles": ["/static/js/polyfills.ghi012.js"]
58
+ * }
59
+ * ```
60
+ *
61
+ * ## Usage Example
62
+ * ```typescript
63
+ * // In rspack configuration
64
+ * const RspackBuildManifestPlugin = require('./rspack-build-manifest-plugin');
65
+ *
66
+ * module.exports = {
67
+ * plugins: [
68
+ * new RspackBuildManifestPlugin({
69
+ * filename: 'build-manifest.json',
70
+ * modules: true,
71
+ * chunkRequest: true
72
+ * })
73
+ * ]
74
+ * };
75
+ * ```
76
+ *
77
+ * ## Use Cases
78
+ * - **SSR Frameworks**: Map entry points to built assets for server-side rendering
79
+ * - **Asset Loading**: Determine which files to load for specific routes
80
+ * - **Cache Management**: Track hashed filenames for cache invalidation
81
+ * - **Code Splitting**: Understand relationships between chunks and their requests
82
+ *
83
+ * ## Differences from Webpack Version
84
+ * - Uses Rspack's API instead of Webpack's
85
+ * - Simplified module collection (Rspack has different module APIs)
86
+ * - Adapted to Rspack's chunk and compilation structure
87
+ * - Some features are marked with TODO comments for future Rspack API support
88
+ */
89
+ export default class RspackBuildManifestPlugin implements RspackPlugin {
90
+ private _options;
91
+ private _manifest;
92
+ /**
93
+ * Creates a new RspackBuildManifestPlugin instance
94
+ * @param options - Configuration options for the plugin
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * // Basic usage with default options
99
+ * new RspackBuildManifestPlugin()
100
+ *
101
+ * // Custom configuration
102
+ * new RspackBuildManifestPlugin({
103
+ * filename: 'assets-manifest.json',
104
+ * modules: true,
105
+ * chunkRequest: true
106
+ * })
107
+ * ```
108
+ */
109
+ constructor(options?: Partial<Options>);
110
+ /**
111
+ * Creates the build manifest by analyzing compilation assets and chunks
112
+ * @param compiler - Rspack compiler instance
113
+ * @param compilation - Rspack compilation instance
114
+ * @returns The generated manifest object
115
+ *
116
+ * This method performs the following operations:
117
+ * 1. Initializes the manifest structure
118
+ * 2. Collects entry point information
119
+ * 3. Processes chunk groups and their assets
120
+ * 4. Identifies polyfill files
121
+ * 5. Sorts and organizes loadable modules
122
+ */
123
+ createAssets(compiler: Rspack.Compiler, compilation: Rspack.Compilation): {
124
+ entries: {};
125
+ bundles: {};
126
+ chunkRequest: {};
127
+ loadble: {};
128
+ };
129
+ /**
130
+ * Applies the plugin to the rspack compiler
131
+ * @param compiler - Rspack compiler instance
132
+ *
133
+ * This method hooks into the rspack compilation process to:
134
+ * 1. Listen for the 'make' hook to prepare for asset processing
135
+ * 2. Hook into 'processAssets' to generate the manifest file
136
+ * 3. Create the JSON file as a compilation asset
137
+ */
138
+ apply(compiler: Rspack.Compiler): void;
139
+ /**
140
+ * Get the raw request of the chunk
141
+ * @param compilation - Rspack compilation instance
142
+ * @param chunk - Rspack chunk instance
143
+ * @returns The raw request of the chunk
144
+ */
145
+ private _getFirstRawRequest;
146
+ /**
147
+ * Collects entry point information from chunk groups
148
+ * @param entrypoint - The entry point chunk group to process
149
+ *
150
+ * This method processes entry points and maps them to their output files,
151
+ * filtering out source maps and hot update files.
152
+ */
153
+ private _collectEntries;
154
+ /**
155
+ * Collects information from chunk groups including chunks and modules
156
+ * @param chunkGroup - The chunk group to process
157
+ * @param compiler - Rspack compiler instance
158
+ * @param compilation - Rspack compilation instance
159
+ *
160
+ * This method processes chunk groups to collect:
161
+ * - Chunk information (files, requests)
162
+ * - Module information (if enabled)
163
+ * - Loadable modules and their relationships
164
+ */
165
+ private _collect;
166
+ /**
167
+ * Collects information from individual chunks
168
+ * @param chunk - The rspack chunk to process
169
+ * @param request - The request that generated this chunk
170
+ *
171
+ * This method processes chunks to:
172
+ * - Identify bundle files
173
+ * - Map chunk requests to files
174
+ * - Filter out source maps and hot update files
175
+ */
176
+ private _collectChunk;
177
+ /**
178
+ * Collects module information from chunks (when modules option is enabled)
179
+ * @param chunk - The rspack chunk to process
180
+ * @param request - The request that generated this chunk
181
+ * @param compiler - Rspack compiler instance
182
+ * @param compilation - Rspack compilation instance
183
+ *
184
+ * This method processes chunks to collect:
185
+ * - Loadable module files (JS and CSS)
186
+ * - Module metadata (ID, name) - simplified for Rspack
187
+ * - Root modules for code splitting analysis
188
+ */
189
+ private _collectChunkModule;
190
+ /**
191
+ * Adds entry information to the manifest
192
+ * @param name - Entry point name
193
+ * @param ext - File extension
194
+ * @param value - File path
195
+ *
196
+ * @example
197
+ * ```typescript
198
+ * this._pushEntries('main', 'js', '/static/js/main.abc123.js')
199
+ * // Results in: { entries: { main: { js: ['/static/js/main.abc123.js'] } } }
200
+ * ```
201
+ */
202
+ private _pushEntries;
203
+ /**
204
+ * Adds bundle information to the manifest
205
+ * @param name - Bundle name
206
+ * @param file - Bundle file path
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * this._pushBundle({ name: 'main', file: '/static/js/main.abc123.js' })
211
+ * // Results in: { bundles: { main: '/static/js/main.abc123.js' } }
212
+ * ```
213
+ */
214
+ private _pushBundle;
215
+ /**
216
+ * Adds chunk request information to the manifest (when chunkRequest option is enabled)
217
+ * @param file - Chunk file path
218
+ * @param request - Request that generated the chunk
219
+ *
220
+ * @example
221
+ * ```typescript
222
+ * this._pushChunkRequest({
223
+ * file: '/static/js/chunk.xyz789.js',
224
+ * request: '/pages/about'
225
+ * })
226
+ * // Results in: { chunkRequest: { '/static/js/chunk.xyz789.js': '/pages/about' } }
227
+ * ```
228
+ */
229
+ private _pushChunkRequest;
230
+ /**
231
+ * Adds loadable module information to the manifest
232
+ * @param request - The request that generated the loadable module
233
+ * @param module - Module item with ID and name
234
+ *
235
+ * @example
236
+ * ```typescript
237
+ * // Adding a file
238
+ * this._pushLoadableModules('/pages/about', '/static/js/chunk.xyz789.js')
239
+ *
240
+ * // Adding a module
241
+ * this._pushLoadableModules('/pages/about', {
242
+ * id: 'module-id',
243
+ * name: 'AboutPage'
244
+ * })
245
+ * ```
246
+ */
247
+ private _pushLoadableModules;
248
+ }
249
+ export {};