|
1 | 1 | const { rspack } = require('@rspack/core');
|
2 | 2 | const path = require('path');
|
3 | 3 |
|
| 4 | +/** |
| 5 | + * Safely parses a JSON string, returning undefined if parsing fails. |
| 6 | + * @param {string} json - The JSON string to parse. |
| 7 | + * @returns {Object|undefined} - Parsed object or undefined if parsing fails. |
| 8 | + */ |
4 | 9 | function safelyParseJSON(json) {
|
5 | 10 | try {
|
6 |
| - return JSON.parse(json) |
| 11 | + return JSON.parse(json); |
7 | 12 | } catch (_) {
|
8 |
| - return undefined |
| 13 | + return undefined; |
9 | 14 | }
|
10 | 15 | }
|
11 | 16 |
|
| 17 | +// Default configuration object for rspack bundling process |
12 | 18 | let config = {
|
13 |
| - |
14 | 19 | output: {
|
15 |
| - filename: '[name].js', |
| 20 | + filename: '[name].js', // Output filename with the entry name |
16 | 21 | library: {
|
17 |
| - type: 'commonjs2', |
| 22 | + type: 'commonjs2', // Set library type to CommonJS2 (Node.js modules) |
18 | 23 | },
|
19 |
| - publicPath: '' |
| 24 | + publicPath: '' // Specify the base path for all assets within the application |
20 | 25 | },
|
21 | 26 | resolve: {
|
22 |
| - extensions: ['.js', '.jsx', '.tsx', '.ts'] |
| 27 | + extensions: ['.js', '.jsx', '.tsx', '.ts'] // Extensions that will be resolved |
23 | 28 | },
|
24 | 29 | optimization: {
|
25 |
| - minimize: false, |
| 30 | + minimize: false, // Disable minimization for easier debugging |
26 | 31 | },
|
27 | 32 | module: {
|
28 | 33 | rules: [
|
29 | 34 | {
|
30 |
| - test: /\.(jsx|js)$/, |
31 |
| - exclude: /node_modules/, |
| 35 | + test: /\.(jsx|js)$/, // Rule for JavaScript and JSX files |
| 36 | + exclude: /node_modules/, // Exclude node_modules directory |
32 | 37 | use: {
|
33 |
| - loader: 'builtin:swc-loader', |
| 38 | + loader: 'builtin:swc-loader', // Use the SWC loader to transpile ES6+ and JSX |
34 | 39 | options: {
|
35 |
| - sourceMap: true, |
| 40 | + sourceMap: true, // Enable source maps for easier debugging |
36 | 41 | jsc: {
|
37 | 42 | parser: {
|
38 |
| - syntax: 'ecmascript', |
39 |
| - jsx: true, |
| 43 | + syntax: 'ecmascript', // Set parser syntax to ECMAScript |
| 44 | + jsx: true, // Enable parsing JSX syntax |
40 | 45 | },
|
41 |
| - externalHelpers: false, |
42 |
| - preserveAllComments: false, |
| 46 | + externalHelpers: false, // Disable external helpers (use inline helpers) |
| 47 | + preserveAllComments: false, // Remove comments from output |
43 | 48 | transform: {
|
44 | 49 | react: {
|
45 |
| - runtime: 'automatic', |
46 |
| - throwIfNamespace: true, |
47 |
| - useBuiltins: false, |
| 50 | + runtime: 'automatic', // Use React's automatic JSX runtime |
| 51 | + throwIfNamespace: true, // Throw error if namespace is used |
| 52 | + useBuiltins: false, // Don't include built-in polyfills |
48 | 53 | },
|
49 | 54 | },
|
50 | 55 | },
|
51 | 56 | },
|
52 |
| - |
53 | 57 | },
|
54 |
| - type: 'javascript/auto', |
| 58 | + type: 'javascript/auto', // Specify the type as auto (for backward compatibility) |
55 | 59 | },
|
56 | 60 | {
|
57 |
| - test: /\.(tsx|ts)$/, |
58 |
| - exclude: /node_modules/, |
| 61 | + test: /\.(tsx|ts)$/, // Rule for TypeScript and TSX files |
| 62 | + exclude: /node_modules/, // Exclude node_modules directory |
59 | 63 | use: {
|
60 |
| - loader: 'builtin:swc-loader', |
| 64 | + loader: 'builtin:swc-loader', // Use the SWC loader to transpile TS and TSX |
61 | 65 | options: {
|
62 | 66 | jsc: {
|
63 | 67 | parser: {
|
64 |
| - syntax: 'typescript', |
65 |
| - tsx: true, |
| 68 | + syntax: 'typescript', // Set parser syntax to TypeScript |
| 69 | + tsx: true, // Enable parsing TSX syntax |
66 | 70 | },
|
67 | 71 | transform: {
|
68 | 72 | react: {
|
69 |
| - runtime: 'automatic', |
70 |
| - throwIfNamespace: true, |
71 |
| - useBuiltins: false, |
| 73 | + runtime: 'automatic', // Use React's automatic JSX runtime |
| 74 | + throwIfNamespace: true, // Throw error if namespace is used |
| 75 | + useBuiltins: false, // Don't include built-in polyfills |
72 | 76 | },
|
73 | 77 | },
|
74 | 78 | },
|
75 | 79 | },
|
76 | 80 | },
|
77 |
| - type: 'javascript/auto', |
| 81 | + type: 'javascript/auto', // Specify the type as auto |
78 | 82 | },
|
79 | 83 | {
|
80 |
| - test: /\.(png|svg|jpg)$/, |
81 |
| - type: 'asset/inline', |
| 84 | + test: /\.(png|svg|jpg)$/, // Rule for image files (PNG, SVG, JPG) |
| 85 | + type: 'asset/inline', // Inline assets as Base64 strings |
82 | 86 | },
|
83 | 87 | ],
|
| 88 | + }, |
| 89 | +}; |
84 | 90 |
|
85 |
| - } |
86 |
| -} |
87 |
| - |
| 91 | +/** |
| 92 | + * Bundles web resources using rspack. |
| 93 | + * @param {Object|string} entry - The entry point(s) for the bundling process (can be a string or JSON object). |
| 94 | + * @param {string} dist - The distribution path where bundled files will be output. |
| 95 | + * @returns {Promise} - Resolves when bundling is successful, rejects if there is an error. |
| 96 | + */ |
88 | 97 | async function web_bundling(entry, dist) {
|
89 |
| - |
| 98 | + // Create a bundler instance using the config and parameters |
90 | 99 | const compiler = rspack(
|
91 | 100 | {
|
92 |
| - ...config, |
93 |
| - entry: safelyParseJSON(entry) ?? entry, |
| 101 | + ...config, // Merge with the default config |
| 102 | + entry: safelyParseJSON(entry) ?? entry, // Parse entry if it's JSON, otherwise use it as is |
94 | 103 | output: dist ? {
|
95 | 104 | ...config.output,
|
96 |
| - path: path.join(process.cwd(), dist) |
| 105 | + path: path.join(process.cwd(), dist), // Use current working directory and output path |
97 | 106 | } : config.output,
|
98 |
| - |
99 |
| - name: 'Client', |
100 |
| - mode: 'development', |
101 |
| - devtool: 'source-map', |
102 |
| - stats: { preset: 'errors-warnings', timings: true, colors: true }, |
103 |
| - target: 'web', |
| 107 | + // minimize: true, |
| 108 | + name: 'Client', // Name of the bundle (Client) |
| 109 | + mode: 'production', // Set mode to development (for non-minimized builds) |
| 110 | + devtool: 'source-map', // Enable source maps for better debugging |
| 111 | + stats: { preset: 'errors-warnings', timings: true, colors: true }, // Customize bundling stats output |
| 112 | + target: 'web', // Set the target environment to web (for browser usage) |
104 | 113 | }
|
105 |
| - |
106 | 114 | );
|
107 | 115 |
|
| 116 | + // Return a promise that runs the bundling process and resolves or rejects based on the result |
108 | 117 | return new Promise((resolve, reject) => {
|
109 | 118 | return compiler.run((error, stats) => {
|
| 119 | + // Handle errors during the bundling process |
110 | 120 | if (error) {
|
111 |
| - reject(error.message); |
| 121 | + reject(error.message); // Reject with the error message if bundling fails |
112 | 122 | }
|
113 | 123 |
|
| 124 | + // Check if there are any errors in the bundling stats |
114 | 125 | if (error || stats?.hasErrors()) {
|
115 |
| - reject(stats.toString("errors-only")); |
| 126 | + reject(stats.toString("errors-only")); // Reject with errors-only details from stats |
116 | 127 | }
|
117 |
| - resolve(0); |
| 128 | + resolve(0); // Resolve successfully when bundling is complete |
118 | 129 | });
|
119 | 130 | });
|
120 | 131 | }
|
121 | 132 |
|
122 | 133 | module.exports = {
|
123 |
| - web_bundling |
| 134 | + web_bundling // Export the web_bundling function to call it via metacall |
124 | 135 | };
|
0 commit comments