|
| 1 | +import fs from "node:fs"; |
| 2 | +import { sync as resolveSync, transformSync } from "@rspack/binding"; |
| 3 | +import type { Compiler } from "../Compiler"; |
| 4 | + |
| 5 | +type BrowserRequire = typeof Compiler.prototype.__internal_browser_require; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents the runtime context for CommonJS modules in a browser environment. |
| 9 | + */ |
| 10 | +interface CommonJsRuntime { |
| 11 | + module: any; |
| 12 | + exports: any; |
| 13 | + require: BrowserRequire; |
| 14 | +} |
| 15 | + |
| 16 | +interface BrowserRequirePluginOptions { |
| 17 | + /** |
| 18 | + * This function defines how to execute CommonJS code. |
| 19 | + */ |
| 20 | + execute: (code: string, runtime: CommonJsRuntime) => void; |
| 21 | +} |
| 22 | + |
| 23 | +const unsafeExecute: BrowserRequirePluginOptions["execute"] = ( |
| 24 | + code, |
| 25 | + runtime |
| 26 | +) => { |
| 27 | + const wrapper = new Function("module", "exports", "require", code); |
| 28 | + wrapper(runtime.module, runtime.exports, runtime.require); |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * This plugin inject browser-compatible `require` function to the `Compiler`. |
| 33 | + * 1. It resolves the JavaScript in the memfs with Node.js resolution algorithm rather than in the host filesystem. |
| 34 | + * 2. It transform ESM to CommonJS which will be executed with a user-defined `execute` function. |
| 35 | + */ |
| 36 | +export class BrowserRequirePlugin { |
| 37 | + /** |
| 38 | + * This is an unsafe way to execute code in the browser using `new Function`. |
| 39 | + * It is your responsibility to ensure that your application is not vulnerable to attacks due to this function. |
| 40 | + */ |
| 41 | + static unsafeExecute = unsafeExecute; |
| 42 | + |
| 43 | + constructor(private options: BrowserRequirePluginOptions) {} |
| 44 | + |
| 45 | + apply(compiler: Compiler) { |
| 46 | + const execute = this.options.execute; |
| 47 | + compiler.__internal_browser_require = function browserRequire(id: string) { |
| 48 | + const { path: loaderPath } = resolveSync("", id); |
| 49 | + if (!loaderPath) { |
| 50 | + throw new Error(`Cannot find loader of ${id}`); |
| 51 | + } |
| 52 | + |
| 53 | + const data = fs.readFileSync(loaderPath); |
| 54 | + const code = data?.toString() || ""; |
| 55 | + |
| 56 | + const module: any = { exports: {} }; |
| 57 | + const exports = module.exports; |
| 58 | + |
| 59 | + const cjs = transformSync( |
| 60 | + code, |
| 61 | + JSON.stringify({ |
| 62 | + module: { type: "commonjs" } |
| 63 | + }) |
| 64 | + ); |
| 65 | + |
| 66 | + execute(cjs.code, { exports, module, require: browserRequire }); |
| 67 | + return exports.default ?? module.exports; |
| 68 | + }; |
| 69 | + } |
| 70 | +} |
0 commit comments