Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CJS/ESM build fix #3

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
comments added
  • Loading branch information
Lesther Caballero authored and Lesther Caballero committed Mar 8, 2024
commit f3fed21a8739d122e838110c328edf3f201ea33a
12 changes: 6 additions & 6 deletions packages/sdk/rollup.config.cjs.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
// import multi from "@rollup/plugin-multi-entry";
import commonjs from "@rollup/plugin-commonjs";
import multiInput from "rollup-plugin-multi-input";

const esmCjsCompatible = ["file-type", "uint8arrays", "multiformats"];
// List of ESM dependencies that are not compatible with CJS
const esmInCompatible = ["file-type", "uint8arrays", "multiformats"];

/*
* Rollup configuration to build CJS compatible dependencies
*/
export default {
input: esmCjsCompatible.map((m) => `../../node_modules/${m}/**/*.js`),
input: esmInCompatible.map((m) => `../../node_modules/${m}/**/*.js`),
plugins: [
commonjs(),
// multi({preserveModules: true}),
multiInput.default({ relative: "../../node_modules" }),
nodeResolve(),
],
output: {
dir: "build/cjs/deps",
format: "cjs",
// preserveModules: true,
// entryFileNames: "[name].js",
},
};
5 changes: 5 additions & 0 deletions packages/sdk/src/esm-cjs-maps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

/*
`esmCjsMaps` is a map of ESM package names to their CJS equivalents.
This is used to resolve ESM imports to CJS imports for CommonJS build.
*/
export const esmCjsMaps = {
"file-type": "file-type",
uint8arrays: "uint8arrays/dist/src",
Expand Down
8 changes: 5 additions & 3 deletions packages/sdk/src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import fs from "fs";
import { esmCjsMaps } from "./esm-cjs-maps.js";


/*
This is a polyfill for the "require" Commonjs function
This is needed because there are some dependencies that are not ESM compatible
and we need to use the Commonjs version of the package
*/
if (fs.existsSync("package.json")) {
const packageJson = fs.readFileSync("package.json", "utf-8");
const packageJsonObj = JSON.parse(packageJson);
Expand All @@ -12,12 +17,9 @@ if (fs.existsSync("package.json")) {
var self = this;

if (
// fs.existsSync(pathModule.resolve(__dirname, "deps", modules[path])) &&
typeof esmCjsMaps[path] === "string"
) {
const dep = pathModule.resolve(__dirname, "deps", esmCjsMaps[path]);
// console.log("Loading from deps:", path);
// console.log("dep path:", dep);
return self.constructor._load(dep, self);
}
return self.constructor._load(path, self);
Expand Down