Skip to content

Commit

Permalink
Use esm dir package.json fix mui-tiptap nodejs import
Browse files Browse the repository at this point in the history
Should resolve #256
  • Loading branch information
sjdemartini committed Aug 19, 2024
1 parent 0ec02e7 commit 54808bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"node": true
},
"parser": "@typescript-eslint/parser",
"ignorePatterns": ["create-esm-package-json.js"], // Not in the TS project
"parserOptions": {
"ecmaFeatures": {
"jsx": true
Expand Down
19 changes: 19 additions & 0 deletions create-esm-package-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Create a `package.json` file in the `dist/esm` build output directory with
// {"type: "module"}, which should ensure that mui-tiptap can be imported
// properly as ESM in a node.js context (fixing
// https://github.com/sjdemartini/mui-tiptap/issues/256).
// See:
// - https://github.com/microsoft/TypeScript/issues/18442#issuecomment-1377529164
// - https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html
// - https://github.com/nodejs/node/issues/34515
const fs = require("fs");
const path = require("path");

const dir = path.join(__dirname, "dist", "esm");
const filePath = path.join(dir, "package.json");
const content = JSON.stringify({ type: "module" }, null, 2);

fs.writeFileSync(filePath, content, "utf8");

// Print the path relative to __dirname:
console.log(`Created ${path.relative(__dirname, filePath)}`);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"sideEffects": false,
"scripts": {
"build": "pnpm clean && tsc --project tsconfig.build.json && tsc --project tsconfig.build-esm.json",
"build": "pnpm clean && tsc --project tsconfig.build.json && tsc --project tsconfig.build-esm.json && node create-esm-package-json.js",
"clean": "rimraf dist coverage",
"example": "pnpm build && rimraf ./example/node_modules && pnpm --dir ./example install && pnpm --dir ./example run dev",
"dev": "vite",
Expand Down

0 comments on commit 54808bc

Please sign in to comment.