(entryPath, { globals = {} } = {})
| 28 | } |
| 29 | |
| 30 | export async function importBundledModule(entryPath, { globals = {} } = {}) { |
| 31 | const previous = new Map(); |
| 32 | for (const [key, value] of Object.entries(globals)) { |
| 33 | previous.set(key, { |
| 34 | exists: Object.prototype.hasOwnProperty.call(globalThis, key), |
| 35 | value: globalThis[key], |
| 36 | }); |
| 37 | globalThis[key] = value; |
| 38 | } |
| 39 | |
| 40 | const outDir = mkdtempSync(path.join(tmpdir(), "td-module-loader-")); |
| 41 | try { |
| 42 | await runRspack({ |
| 43 | mode: "development", |
| 44 | entry: { bundled: entryPath }, |
| 45 | experiments: { outputModule: true }, |
| 46 | output: { |
| 47 | module: true, |
| 48 | library: { type: "module" }, |
| 49 | path: outDir, |
| 50 | filename: "bundled.mjs", |
| 51 | }, |
| 52 | resolve: { extensions: [".tsx", ".ts", ".jsx", ".js", ".json"] }, |
| 53 | module: { |
| 54 | rules: [ |
| 55 | { |
| 56 | test: /\.(tsx?|jsx)$/, |
| 57 | exclude: /node_modules/, |
| 58 | use: { |
| 59 | loader: "builtin:swc-loader", |
| 60 | options: { |
| 61 | jsc: { |
| 62 | parser: { |
| 63 | syntax: "typescript", |
| 64 | tsx: true, |
| 65 | }, |
| 66 | transform: { react: { runtime: "automatic" } }, |
| 67 | }, |
| 68 | }, |
| 69 | }, |
| 70 | }, |
| 71 | ], |
| 72 | }, |
| 73 | // The tests need real React elements (e.g. sdk.jsx's `import React`), |
| 74 | // so bundle `react` from node_modules rather than externalizing it. |
| 75 | optimization: { minimize: false, splitChunks: false, runtimeChunk: false }, |
| 76 | performance: { hints: false }, |
| 77 | stats: { preset: "errors-only" }, |
| 78 | }); |
| 79 | |
| 80 | const outFile = path.join(outDir, "bundled.mjs"); |
| 81 | return await import(pathToFileURL(outFile).href + "?t=" + Date.now()); |
| 82 | } finally { |
| 83 | restoreGlobals(previous); |
| 84 | try { |
| 85 | rmSync(outDir, { recursive: true, force: true }); |
| 86 | } catch { |
| 87 | // best-effort cleanup |
no test coverage detected