fix(vite): get radix-ui working (#3344)
This commit is contained in:
@@ -9,6 +9,7 @@ export function cjsPlugin(
|
||||
const EXPORTED = "exported";
|
||||
const EXPORTED_NAMESPACES = "exported_namespaces";
|
||||
const ALIASED = "aliased";
|
||||
const REEXPORT = "re-export";
|
||||
const NEEDS_REQUIRE_IMPORT = "needsRequireImport";
|
||||
|
||||
return {
|
||||
@@ -19,6 +20,7 @@ export function cjsPlugin(
|
||||
state.set(ROOT_SCOPE, path.scope);
|
||||
state.set(EXPORTED, new Set<string>());
|
||||
state.set(EXPORTED_NAMESPACES, new Set<string>());
|
||||
state.set(REEXPORT, null);
|
||||
},
|
||||
exit(path, state) {
|
||||
const body = path.get("body");
|
||||
@@ -36,6 +38,7 @@ export function cjsPlugin(
|
||||
}
|
||||
}
|
||||
|
||||
const reexport = state.get(REEXPORT);
|
||||
const exported = state.get(EXPORTED);
|
||||
const exportedNs = state.get(EXPORTED_NAMESPACES);
|
||||
const needsRequireImport = state.get(NEEDS_REQUIRE_IMPORT);
|
||||
@@ -73,6 +76,13 @@ export function cjsPlugin(
|
||||
);
|
||||
}
|
||||
|
||||
if (reexport !== null) {
|
||||
path.unshiftContainer(
|
||||
"body",
|
||||
t.exportAllDeclaration(t.cloneNode(reexport, true)),
|
||||
);
|
||||
}
|
||||
|
||||
const mappedNs: string[] = [];
|
||||
|
||||
for (const spec of exportedNs.values()) {
|
||||
@@ -448,6 +458,22 @@ export function cjsPlugin(
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
// Check: module.exports = require(...)
|
||||
t.isAssignmentExpression(path.node.expression) &&
|
||||
t.isMemberExpression(path.node.expression.left) &&
|
||||
t.isIdentifier(path.node.expression.left.object) &&
|
||||
t.isIdentifier(path.node.expression.left.property) &&
|
||||
path.node.expression.left.object.name === "module" &&
|
||||
path.node.expression.left.property.name === "exports" &&
|
||||
t.isCallExpression(path.node.expression.right) &&
|
||||
t.isIdentifier(path.node.expression.right.callee) &&
|
||||
path.node.expression.right.callee.name === "require" &&
|
||||
path.node.expression.right.arguments.length === 1 &&
|
||||
t.isStringLiteral(path.node.expression.right.arguments[0])
|
||||
) {
|
||||
const source = path.node.expression.right.arguments[0];
|
||||
state.set(REEXPORT, source);
|
||||
} else {
|
||||
let depth = 0;
|
||||
let current = path.node.expression;
|
||||
@@ -524,16 +550,6 @@ export function cjsPlugin(
|
||||
path.remove();
|
||||
}
|
||||
},
|
||||
VariableDeclarator(path) {
|
||||
// Esbuild CJS shims
|
||||
if (
|
||||
t.isIdentifier(path.node.id) &&
|
||||
(path.node.id.name === "__exportStar" ||
|
||||
path.node.id.name === "__createBinding")
|
||||
) {
|
||||
path.remove();
|
||||
}
|
||||
},
|
||||
ConditionalExpression(path) {
|
||||
if (
|
||||
t.isBinaryExpression(path.node.test) &&
|
||||
|
||||
@@ -439,6 +439,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
__exportStar(require("./node"), exports);`,
|
||||
expected: `${INIT}
|
||||
import * as _ns from "./node";
|
||||
var __createBinding = this && this.__createBinding || (Object.create ? function (o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
Object.defineProperty(o, k2, {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return m[k];
|
||||
}
|
||||
});
|
||||
} : function (o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
});
|
||||
var __exportStar = this && this.__exportStar || function (m, exports) {
|
||||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
||||
};
|
||||
export * from "./node";
|
||||
${DEFAULT_EXPORT}
|
||||
Object.assign(_default, _ns);
|
||||
@@ -470,6 +485,13 @@ const pkg = require(path.join(basedir, "package.json"));`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - keep binding", () => {
|
||||
runTest({
|
||||
input: `export var __createBinding = Object.create ? 1 : 2;`,
|
||||
expected: `export var __createBinding = Object.create ? 1 : 2;`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - require lazy import", () => {
|
||||
runTest({
|
||||
input: `if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
|
||||
@@ -533,3 +555,32 @@ ${DEFAULT_EXPORT}
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - wrapped iife binding", () => {
|
||||
runTest({
|
||||
input: `"production" !== process.env.NODE_ENV && (function() {
|
||||
exports.foo = 123
|
||||
})()`,
|
||||
expected: `${INIT}
|
||||
"production" !== process.env.NODE_ENV && function () {
|
||||
exports.foo = 123;
|
||||
}();
|
||||
var _foo = exports.foo;
|
||||
export { _foo as foo };
|
||||
${DEFAULT_EXPORT}
|
||||
_default.foo = _foo;
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("commonjs - re-export", () => {
|
||||
runTest({
|
||||
input: `module.exports = require("foo");`,
|
||||
expected: `${INIT}
|
||||
export * from "foo";
|
||||
import * as _mod from "foo";
|
||||
module.exports = _mod;
|
||||
${DEFAULT_EXPORT}
|
||||
${DEFAULT_EXPORT_END}`,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user