From 95934905b0e3fea30163c273a84a9051aa4ee4c4 Mon Sep 17 00:00:00 2001 From: hulxv Date: Mon, 23 Sep 2024 01:07:03 +0300 Subject: [PATCH] docs(bundler): add some docs to `bundler.js` --- crates/metassr-bundler/src/bundle.js | 49 +++++++++++++++++----------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/crates/metassr-bundler/src/bundle.js b/crates/metassr-bundler/src/bundle.js index 2d0bb45..98ffa18 100644 --- a/crates/metassr-bundler/src/bundle.js +++ b/crates/metassr-bundler/src/bundle.js @@ -1,14 +1,20 @@ const { rspack } = require('@rspack/core'); const path = require('path'); +/** + * Safely parses a JSON string, returning undefined if parsing fails. + * @param {string} json - The JSON string to parse. + * @returns {Object|undefined} - Parsed object or undefined if parsing fails. + */ function safelyParseJSON(json) { try { - return JSON.parse(json) + return JSON.parse(json); } catch (_) { - return undefined + return undefined; } } +// Default configuration object for rspack bundling process let config = { output: { @@ -82,39 +88,44 @@ let config = { }, ], - } -} - +/** + * Bundles web resources using rspack. + * @param {Object|string} entry - The entry point(s) for the bundling process (can be a string or JSON object). + * @param {string} dist - The distribution path where bundled files will be output. + * @returns {Promise} - Resolves when bundling is successful, rejects if there is an error. + */ async function web_bundling(entry, dist) { - + // Create a bundler instance using the config and parameters const compiler = rspack( { - ...config, - entry: safelyParseJSON(entry) ?? entry, + ...config, // Merge with the default config + entry: safelyParseJSON(entry) ?? entry, // Parse entry if it's JSON, otherwise use it as is output: dist ? { ...config.output, - path: path.join(process.cwd(), dist) + path: path.join(process.cwd(), dist), // Use current working directory and output path } : config.output, - - name: 'Client', - mode: 'development', - devtool: 'source-map', - stats: { preset: 'errors-warnings', timings: true, colors: true }, - target: 'web', + // minimize: true, + name: 'Client', // Name of the bundle (Client) + mode: 'production', // Set mode to development (for non-minimized builds) + devtool: 'source-map', // Enable source maps for better debugging + stats: { preset: 'errors-warnings', timings: true, colors: true }, // Customize bundling stats output + target: 'web', // Set the target environment to web (for browser usage) } - ); + // Return a promise that runs the bundling process and resolves or rejects based on the result return new Promise((resolve, reject) => { return compiler.run((error, stats) => { + // Handle errors during the bundling process if (error) { - reject(error.message); + reject(error.message); // Reject with the error message if bundling fails } + // Check if there are any errors in the bundling stats if (error || stats?.hasErrors()) { - reject(stats.toString("errors-only")); + reject(stats.toString("errors-only")); // Reject with errors-only details from stats } - resolve(0); + resolve(0); // Resolve successfully when bundling is complete }); }); }