fix: handle function exports in webpack module wrapper (#19609)

This commit is contained in:
Xiao 2025-06-20 21:40:01 +08:00 committed by GitHub
parent 574a88736d
commit 0af28df145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 50 additions and 5 deletions

View File

@ -49,7 +49,7 @@ class CreateFakeNamespaceObjectRuntimeModule extends HelperRuntimeModule {
`${RuntimeGlobals.makeNamespaceObject}(ns);`, `${RuntimeGlobals.makeNamespaceObject}(ns);`,
"var def = {};", "var def = {};",
"leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];", "leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)];",
"for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) {", "for(var current = mode & 2 && value; (typeof current == 'object' || typeof current == 'function') && !~leafPrototypes.indexOf(current); current = getProto(current)) {",
Template.indent([ Template.indent([
`Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.expressionFunction( `Object.getOwnPropertyNames(current).forEach(${runtimeTemplate.expressionFunction(
`def[key] = ${runtimeTemplate.returningFunction("value[key]", "")}`, `def[key] = ${runtimeTemplate.returningFunction("value[key]", "")}`,

View File

@ -190,10 +190,10 @@ describe("Stats", () => {
"assets": Array [ "assets": Array [
Object { Object {
"name": "entryB.js", "name": "entryB.js",
"size": 3081, "size": 3105,
}, },
], ],
"assetsSize": 3081, "assetsSize": 3105,
"auxiliaryAssets": undefined, "auxiliaryAssets": undefined,
"auxiliaryAssetsSize": 0, "auxiliaryAssetsSize": 0,
"childAssets": undefined, "childAssets": undefined,
@ -238,10 +238,10 @@ describe("Stats", () => {
"info": Object { "info": Object {
"javascriptModule": false, "javascriptModule": false,
"minimized": true, "minimized": true,
"size": 3081, "size": 3105,
}, },
"name": "entryB.js", "name": "entryB.js",
"size": 3081, "size": 3105,
"type": "asset", "type": "asset",
}, },
Object { Object {

View File

@ -0,0 +1,13 @@
it("Should work with export a function", function(done) {
const myModule = require("module");
expect(typeof myModule).toBe("function");
expect(myModule.builtinModules).toBeDefined();
done()
});
it("should work with export a object", function(done) {
const myFs = require("fs");
expect(typeof myFs).toBe("object");
expect(myFs.readFileSync).toBeDefined();
done()
});

View File

@ -0,0 +1,7 @@
/** @type {import("../../../../types").Configuration} */
module.exports = {
externals: {
module: "commonjs module",
fs: "commonjs fs"
}
};

View File

@ -0,0 +1 @@
export const foo = 'foo'

View File

@ -0,0 +1,17 @@
import { foo } from "./foo.mjs"
it("Should work with export a function when using CreateFakeNamespaceObjectRuntimeModule", async function(done) {
expect(foo).toBe("foo")
const myModule = await import("module");
// namespace object
expect(typeof myModule).toBe("object");
expect(myModule.builtinModules).toBeDefined();
done()
});
it("Should work with export a object when using CreateFakeNamespaceObjectRuntimeModule", async function(done) {
const myFs = await import("fs");
expect(typeof myFs).toBe("object");
expect(myFs.readFileSync).toBeDefined();
done()
});

View File

@ -0,0 +1,7 @@
/** @type {import("../../../../types").Configuration} */
module.exports = {
externals: {
module: "commonjs module",
fs: "commonjs fs"
}
};