|
| 1 | +import { createContext, runInContext } from 'node:vm'; |
| 2 | +import { plugins } from '@callstack/repack'; |
| 3 | +import type { Configuration } from '@rspack/core'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | +import { |
| 6 | + compile, |
| 7 | + createCompiler, |
| 8 | + createVirtualModulePlugin, |
| 9 | +} from '../helpers.js'; |
| 10 | + |
| 11 | +class ForceModuleFactoriesPlugin { |
| 12 | + apply(compiler: any) { |
| 13 | + compiler.hooks.compilation.tap( |
| 14 | + 'ForceModuleFactoriesPlugin', |
| 15 | + (compilation: any) => { |
| 16 | + compilation.hooks.additionalTreeRuntimeRequirements.tap( |
| 17 | + 'ForceModuleFactoriesPlugin', |
| 18 | + (_chunk: unknown, runtimeRequirements: Set<string>) => { |
| 19 | + runtimeRequirements.add(compiler.webpack.RuntimeGlobals.require); |
| 20 | + runtimeRequirements.add( |
| 21 | + compiler.webpack.RuntimeGlobals.moduleFactories |
| 22 | + ); |
| 23 | + } |
| 24 | + ); |
| 25 | + } |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +async function compileRuntime( |
| 31 | + virtualModules: Record<string, string>, |
| 32 | + entry = './index.js' |
| 33 | +) { |
| 34 | + const virtualPlugin = await createVirtualModulePlugin(virtualModules); |
| 35 | + const compiler = await createCompiler({ |
| 36 | + context: __dirname, |
| 37 | + mode: 'development', |
| 38 | + devtool: false, |
| 39 | + entry, |
| 40 | + output: { |
| 41 | + path: '/out', |
| 42 | + filename: 'main.js', |
| 43 | + }, |
| 44 | + plugins: [ |
| 45 | + virtualPlugin, |
| 46 | + new ForceModuleFactoriesPlugin(), |
| 47 | + new plugins.RepackTargetPlugin(), |
| 48 | + ], |
| 49 | + } satisfies Configuration); |
| 50 | + |
| 51 | + return compile(compiler); |
| 52 | +} |
| 53 | + |
| 54 | +function executeBundle(code: string) { |
| 55 | + const fatalErrors: unknown[] = []; |
| 56 | + const context = createContext({ |
| 57 | + ErrorUtils: { |
| 58 | + reportFatalError(error: unknown) { |
| 59 | + fatalErrors.push(error); |
| 60 | + }, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + runInContext(code, context); |
| 65 | + |
| 66 | + return { context, fatalErrors }; |
| 67 | +} |
| 68 | + |
| 69 | +describe('RepackTargetPlugin guarded require', () => { |
| 70 | + it('reports an uncaught startup module error as fatal', async () => { |
| 71 | + const { code } = await compileRuntime( |
| 72 | + { |
| 73 | + './index.cjs': 'throw new Error("startup module failed");', |
| 74 | + }, |
| 75 | + './index.cjs' |
| 76 | + ); |
| 77 | + |
| 78 | + const { fatalErrors } = executeBundle(code); |
| 79 | + |
| 80 | + expect(fatalErrors).toHaveLength(1); |
| 81 | + expect(fatalErrors[0]).toMatchObject({ |
| 82 | + name: 'Error', |
| 83 | + message: 'startup module failed', |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it('preserves optional require and regular module error behavior', async () => { |
| 88 | + const { code } = await compileRuntime( |
| 89 | + { |
| 90 | + './index.cjs': ` |
| 91 | + try { |
| 92 | + require('./optional.cjs'); |
| 93 | + } catch (error) { |
| 94 | + globalThis.optionalRequireError = error.message; |
| 95 | + } |
| 96 | +
|
| 97 | + globalThis.requireRegularModuleLater = function () { |
| 98 | + return require('./regular.cjs'); |
| 99 | + }; |
| 100 | + `, |
| 101 | + './optional.cjs': 'throw new Error("optional module failed");', |
| 102 | + './regular.cjs': 'throw new Error("regular module failed");', |
| 103 | + }, |
| 104 | + './index.cjs' |
| 105 | + ); |
| 106 | + |
| 107 | + const { context, fatalErrors } = executeBundle(code); |
| 108 | + |
| 109 | + expect(context.optionalRequireError).toBe('optional module failed'); |
| 110 | + expect(fatalErrors).toHaveLength(0); |
| 111 | + |
| 112 | + expect(context.requireRegularModuleLater()).toBeUndefined(); |
| 113 | + expect(fatalErrors).toHaveLength(1); |
| 114 | + expect(fatalErrors[0]).toMatchObject({ |
| 115 | + name: 'Error', |
| 116 | + message: 'regular module failed', |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('propagates ChunkLoadError to an asynchronous caller', async () => { |
| 121 | + const { code } = await compileRuntime( |
| 122 | + { |
| 123 | + './index.cjs': ` |
| 124 | + globalThis.importChunkLater = function () { |
| 125 | + return Promise.resolve().then(function () { |
| 126 | + return require('./chunk.cjs'); |
| 127 | + }); |
| 128 | + }; |
| 129 | + `, |
| 130 | + './chunk.cjs': ` |
| 131 | + var error = new Error('Loading chunk test failed'); |
| 132 | + error.name = 'ChunkLoadError'; |
| 133 | + throw error; |
| 134 | + `, |
| 135 | + }, |
| 136 | + './index.cjs' |
| 137 | + ); |
| 138 | + |
| 139 | + const { context, fatalErrors } = executeBundle(code); |
| 140 | + |
| 141 | + await expect(context.importChunkLater()).rejects.toMatchObject({ |
| 142 | + name: 'ChunkLoadError', |
| 143 | + message: 'Loading chunk test failed', |
| 144 | + }); |
| 145 | + expect(fatalErrors).toHaveLength(0); |
| 146 | + }); |
| 147 | +}); |
0 commit comments