fix(vite): improve commonjs handling (#3357)
This commit is contained in:
@@ -243,6 +243,17 @@ export function cjsPlugin(
|
||||
}
|
||||
|
||||
path.pushContainer("body", t.exportDefaultDeclaration(id));
|
||||
path.pushContainer(
|
||||
"body",
|
||||
t.exportNamedDeclaration(
|
||||
t.variableDeclaration("var", [
|
||||
t.variableDeclarator(
|
||||
t.identifier("__require"),
|
||||
t.identifier("exports"),
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (body.length === 0 && state.get(HAS_ES_MODULE)) {
|
||||
@@ -304,9 +315,16 @@ export function cjsPlugin(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("default"),
|
||||
t.identifier("__require"),
|
||||
),
|
||||
t.logicalExpression(
|
||||
"??",
|
||||
t.memberExpression(
|
||||
t.cloneNode(id, true),
|
||||
t.identifier("default"),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
),
|
||||
t.cloneNode(id, true),
|
||||
),
|
||||
);
|
||||
return;
|
||||
|
||||
@@ -25,7 +25,8 @@ Object.defineProperty(module, "exports", {
|
||||
});`;
|
||||
|
||||
const DEFAULT_EXPORT = `const _default = exports.default ?? exports;`;
|
||||
const DEFAULT_EXPORT_END = `export default _default;`;
|
||||
const DEFAULT_EXPORT_END = `export default _default;
|
||||
export var __require = exports;`;
|
||||
const IMPORT_REQUIRE = `import { createRequire } from "node:module";
|
||||
const require = createRequire(import.meta.url);`;
|
||||
|
||||
@@ -162,7 +163,7 @@ Deno.test("commonjs - require", () => {
|
||||
console.log(foo);
|
||||
`,
|
||||
expected: `import * as _mod from "tape";
|
||||
var foo = _mod.default ?? _mod;
|
||||
var foo = _mod.__require ?? _mod.default ?? _mod;
|
||||
console.log(foo);`,
|
||||
});
|
||||
});
|
||||
@@ -206,7 +207,7 @@ Deno.test("commonjs - require function call", () => {
|
||||
runTest({
|
||||
input: `var a = require('./a')()`,
|
||||
expected: `import * as _mod from './a';
|
||||
var a = (_mod.default ?? _mod)();`,
|
||||
var a = (_mod.__require ?? _mod.default ?? _mod)();`,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -214,7 +215,7 @@ Deno.test("commonjs - require var decls", () => {
|
||||
runTest({
|
||||
input: `var a = require('./a'), b = 42;`,
|
||||
expected: `import * as _mod from './a';
|
||||
var a = _mod.default ?? _mod,
|
||||
var a = _mod.__require ?? _mod.default ?? _mod,
|
||||
b = 42;`,
|
||||
});
|
||||
});
|
||||
@@ -256,7 +257,7 @@ Deno.test("commonjs - define exports", () => {
|
||||
Object.defineProperty(exports, "foo", { enumerable: true, get: function () { return utils_1.foo; } });`,
|
||||
expected: `${INIT}
|
||||
import * as _mod from "./bar";
|
||||
var utils_1 = _mod.default ?? _mod;
|
||||
var utils_1 = _mod.__require ?? _mod.default ?? _mod;
|
||||
exports.foo = utils_1.foo;
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
@@ -272,7 +273,7 @@ Deno.test("commonjs - define exports #2", () => {
|
||||
Object.defineProperty(exports, "foo", { enumerable: true, get() { return utils_1.foo; } });`,
|
||||
expected: `${INIT}
|
||||
import * as _mod from "./bar";
|
||||
var utils_1 = _mod.default ?? _mod;
|
||||
var utils_1 = _mod.__require ?? _mod.default ?? _mod;
|
||||
exports.foo = utils_1.foo;
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
|
||||
@@ -371,6 +371,17 @@ Deno.test({
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - npm:ioredis",
|
||||
fn: async () => {
|
||||
const res = await fetch(`${demoServer.address()}/tests/ioredis`);
|
||||
const text = await res.text();
|
||||
expect(text).toContain("<h1>ioredis</h1>");
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "vite dev - radix",
|
||||
fn: async () => {
|
||||
|
||||
Reference in New Issue
Block a user