// Copyright 2026 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import path from 'node:path'; import { rspack } from '@rspack/core'; import type { Compilation, Compiler, RspackOptions } from '@rspack/core'; import { describe, expect, it } from '@rstest/core'; import { LynxEncodePlugin, LynxTemplatePlugin, } from '@lynx-js/template-webpack-plugin'; // @ts-expect-error – JS config has no type declarations import lazyBundleRawConfig from './cases/main-thread/lazy-bundle-sourcemap/rspack.config.js'; // @ts-expect-error – JS helper has no type declarations import { createConfig as rawCreateConfig } from './create-react-config.js'; type CreateConfig = ( loaderOptions?: unknown, pluginOptions?: { mainThreadChunks?: string[]; experimental_isLazyBundle?: boolean; }, ) => RspackOptions; const lazyBundleConfig = lazyBundleRawConfig as unknown as RspackOptions; const createConfig = rawCreateConfig as unknown as CreateConfig; const nonLazyBaseConfig = createConfig(undefined, { mainThreadChunks: ['main__main-thread.js'], }); const nonLazyAsyncConfig = { ...nonLazyBaseConfig, devtool: 'source-map', optimization: { ...nonLazyBaseConfig.optimization, minimize: true, }, plugins: [ ...(nonLazyBaseConfig.plugins ?? []), new LynxEncodePlugin(), new LynxTemplatePlugin({ ...LynxTemplatePlugin.defaultOptions, chunks: ['main__main-thread', 'main__background'], filename: 'main/template.js', intermediate: '.rspeedy/main', }), ], } satisfies RspackOptions; async function captureAssets( config: RspackOptions, context: string, matcher: (name: string) => boolean, ) { const capturedAssets = new Map(); const capturedMaps = new Map< string, { mappings: string; sources: string[] } >(); const plugins: NonNullable = [ ...(config.plugins ?? []), { apply(compiler: Compiler) { compiler.hooks.compilation.tap( 'TestPlugin', (compilation: Compilation) => { compilation.hooks.processAssets.tap( { name: 'TestPlugin', // Hook in after wrapper injection, before final cleanup. stage: compiler.rspack.Compilation .PROCESS_ASSETS_STAGE_DEV_TOOLING, }, (assets) => { const jsFiles = Object.keys(assets).filter(name => matcher(name) ); for (const file of jsFiles) { capturedAssets.set(file, assets[file]!.source().toString()); const mapName = file + '.map'; if (assets[mapName]) { capturedMaps.set( file, JSON.parse( assets[mapName].source().toString(), ) as { mappings: string; sources: string[] }, ); } } }, ); }, ); }, }, ]; const testCompiler = rspack({ ...config, context, plugins, }); await new Promise((resolve, reject) => { testCompiler.run((err, stats) => { const runErr = err ?? (stats?.hasErrors() ? new Error(stats.toString()) : undefined); testCompiler.close(closeErr => { if (runErr ?? closeErr) { reject(runErr ?? closeErr!); return; } resolve(); }); }); }); return { capturedAssets, capturedMaps }; } function expectWrappedAssets( capturedAssets: Map, capturedMaps: Map, ) { for (const [filename, asset] of capturedAssets) { expect(asset, `${filename} should include wrapper header`).toContain( '(function (globDynamicComponentEntry)', ); expect(asset, `${filename} should include wrapper footer`).toContain( ';return module.exports', ); const capturedMap = capturedMaps.get(filename); expect(capturedMap, `${filename} sourcemap should exist`).toBeTruthy(); // In a VLQ sourcemap, semicolons separate lines. Our wrapper is: // Line 1: (function (globDynamicComponentEntry) { // Line 2: const module = { exports: {} } // Line 3: const exports = module.exports; // Real code starts on line 4 or later. So the first *non-empty* segment // group (split by ';') must be at index >= 3. const segments = capturedMap!.mappings.split(';'); const firstNonEmpty = segments.findIndex(s => s.length > 0); expect( firstNonEmpty, `${filename} should shift mappings by at least 3 lines`, ).toBeGreaterThanOrEqual(3); expect(segments[firstNonEmpty]!.length).toBeGreaterThan(0); } } describe('Lazy Bundle Sourcemap Verification', () => { it('should verify main-thread.js has wrapper and a correctly-shifted sourcemap', async () => { const { capturedAssets, capturedMaps } = await captureAssets( lazyBundleConfig, path.join(__dirname, 'cases/main-thread/lazy-bundle-sourcemap'), name => name.includes('main-thread') && name.endsWith('.js'), ); const mainThreadFiles = [...capturedAssets.keys()]; expect( mainThreadFiles, 'Should capture the initial main-thread asset', ).toContain('main__main-thread.js'); expect( mainThreadFiles.some(name => name.includes('.rspeedy/lazy-bundle/') && name.endsWith('main-thread.js') ), 'Should capture at least one async main-thread asset from dynamic import', ).toBe(true); expectWrappedAssets(capturedAssets, capturedMaps); }); it('should verify non-lazy async main-thread chunks also shift sourcemaps after wrapper injection', async () => { const { capturedAssets, capturedMaps } = await captureAssets( nonLazyAsyncConfig, path.join(__dirname, 'cases/main-thread/lazy-bundle-sourcemap'), name => name.includes('.rspeedy/lazy-bundle/') && name.endsWith('main-thread.js'), ); const assetNames = [...capturedAssets.keys()]; expect( assetNames.some(name => name.endsWith('main-thread.js') && name.includes('lazy') ), 'Should capture the async lazy main-thread chunk', ).toBe(true); expectWrappedAssets(capturedAssets, capturedMaps); }); });