UNPKG

@nx/rspack

Version:

The Nx Plugin for Rspack contains executors and generators that support building applications using Rspack.

122 lines (121 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = runExecutor; const devkit_1 = require("@nx/devkit"); const async_iterable_1 = require("@nx/devkit/src/utils/async-iterable"); const js_1 = require("@nx/js"); const ts_solution_setup_1 = require("@nx/js/src/utils/typescript/ts-solution-setup"); const core_1 = require("@rspack/core"); const path_1 = require("path"); const create_compiler_1 = require("../../utils/create-compiler"); const mode_utils_1 = require("../../utils/mode-utils"); const normalize_options_1 = require("./lib/normalize-options"); async function* runExecutor(options, context) { process.env.NODE_ENV ??= options.mode ?? 'production'; options.target ??= 'web'; const metadata = context.projectsConfigurations.projects[context.projectName]; const sourceRoot = (0, ts_solution_setup_1.getProjectSourceRoot)(metadata); const normalizedOptions = (0, normalize_options_1.normalizeOptions)(options, context.root, metadata.root, sourceRoot); if ((0, mode_utils_1.isMode)(process.env.NODE_ENV)) { normalizedOptions.mode = process.env.NODE_ENV; } if (!normalizedOptions.skipTypeChecking) { await executeTypeCheck(normalizedOptions, context); } const compiler = await (0, create_compiler_1.createCompiler)(normalizedOptions, context); const iterable = (0, async_iterable_1.createAsyncIterable)(async ({ next, done }) => { const watch = (compiler instanceof core_1.Compiler ? compiler.options.watch : compiler.options[0].watch) ?? options.watch; if (watch) { const watcher = compiler.watch({}, async (err, stats) => { if (err) { devkit_1.logger.error(err); next({ success: false }); return; } if (!compiler || !stats) { devkit_1.logger.error(new Error('Compiler or stats not available')); next({ success: false }); return; } const statsOptions = getStatsOptions(compiler); const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'` if (printedStats) { console.error(printedStats); } next({ success: !stats.hasErrors(), outfile: (0, path_1.resolve)(context.root, normalizedOptions.outputPath, 'main.js'), }); }); registerCleanupCallback(() => { watcher.close(() => { devkit_1.logger.info('Watcher closed'); }); }); } else { compiler.run(async (err, stats) => { compiler.close(() => { if (err) { devkit_1.logger.error(err); next({ success: false }); return; } if (!compiler || !stats) { devkit_1.logger.error(new Error('Compiler or stats not available')); next({ success: false }); return; } const statsOptions = getStatsOptions(compiler); const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'` if (printedStats) { console.error(printedStats); } next({ success: !stats.hasErrors(), outfile: (0, path_1.resolve)(context.root, normalizedOptions.outputPath, 'main.js'), }); done(); }); }); } }); yield* iterable; } // copied from packages/esbuild/src/executors/esbuild/esbuild.impl.ts function registerCleanupCallback(callback) { const wrapped = () => { callback(); process.off('SIGINT', wrapped); process.off('SIGTERM', wrapped); process.off('exit', wrapped); }; process.on('SIGINT', wrapped); process.on('SIGTERM', wrapped); process.on('exit', wrapped); } async function executeTypeCheck(options, context) { const projectConfiguration = context.projectGraph.nodes[context.projectName].data; const result = await (0, js_1.runTypeCheck)({ workspaceRoot: (0, path_1.resolve)(projectConfiguration.root), tsConfigPath: options.tsConfig, mode: 'noEmit', }); await (0, js_1.printDiagnostics)(result.errors, result.warnings); if (result.errors.length > 0) { throw new Error('Found type errors. See above.'); } } function getStatsOptions(compiler) { return (0, create_compiler_1.isMultiCompiler)(compiler) ? { children: compiler.compilers.map((compiler) => compiler.options ? compiler.options.stats : undefined), } : compiler.options ? compiler.options.stats : undefined; }