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 format should not replace "exports.default =" to "module.exports = " when "exports.default =" in last line #192

Open
gaoletian opened this issue Jan 8, 2024 · 3 comments

Comments

@gaoletian
Copy link

gaoletian commented Jan 8, 2024

a reproduction or simple code @pi0

https://github.com/gaoletian/mkdist-issues-192

// src/Foo.ts
export enum Type {
    Foo,
    Bar
}

export default class Action {}
// dist/Foo.js
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
module.exports = exports.Type = void 0;
var Type = exports.Type = /* @__PURE__ */(Type2 => {
  Type2[Type2["Foo"] = 0] = "Foo";
  Type2[Type2["Bar"] = 1] = "Bar";
  return Type2;
})(Type || {});
class Action {}
module.exports = Action;
// test.js
const foo = require('./dist/Foo');
console.log(foo);
console.log(foo.Type);
$ node test.js
foo =  [class Action]
foo.Type = undefined

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/mkdist/dist/index.mjs b/node_modules/mkdist/dist/index.mjs
index 1403db8..7b1582a 100644
--- a/node_modules/mkdist/dist/index.mjs
+++ b/node_modules/mkdist/dist/index.mjs
@@ -59,7 +59,9 @@ const jsLoader = async (input, { options }) => {
   }
   const isCjs = options.format === "cjs";
   if (isCjs) {
-    contents = jiti("").transform({ source: contents, retainLines: false }).replace(/^exports.default = /gm, "module.exports = ").replace(/^var _default = exports.default = /gm, "module.exports = ").replace("module.exports = void 0;", "");
+    contents = jiti("")
+      .transform({ source: contents, retainLines: false })
+      .replace("module.exports = void 0;", "");
   }
   let extension = isCjs ? ".js" : ".mjs";
   if (options.ext) {

This issue body was partially generated by patch-package.

@gaoletian gaoletian changed the title cjs format should not replace exports.default to module.exports = cjs format should not replace "exports.default =" to "module.exports = " when "exports.default =" in last line Jan 8, 2024
@pi0
Copy link
Member

pi0 commented Jan 8, 2024

Hi dear @gaoletian. The syntax change is intentional to fix issues with certain bundlers. Would you please share your case which replacing exports.default to module.exports make issues? (a reproduction or simple code) 🙏🏼

@gaoletian
Copy link
Author

a reproduction or simple code

https://github.com/gaoletian/mkdist-issues-192

@pi0
Copy link
Member

pi0 commented Jan 21, 2024

Thanks for making reproduction and sorry for checking late.

For reference, this was the original hotfix 94444df to address IPX/Unstorage regression with .cjs bundling as a result of a behavior change in Jiti (babel) changes that broke Netlify builder.

[email protected]/drivers/fs.cjs (before - fine) > [email protected]/drivers/fs.cjs (problematic) > [email protected]/drivers/fs.cjs (fixed with hotfix)

I will see if can find smaller workaround only for that case and also working with your reproduction 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants