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

Improve CJS/ESM interoperability support #345

Merged
merged 6 commits into from
Jan 22, 2022

Conversation

MitchellCash
Copy link
Contributor

@MitchellCash MitchellCash commented Jan 7, 2022

Resolves #344 (hopefully).

The real main difference here is we have some additional CJS exports in index.ts, but these exports aren't compatible with ESM so have to be removed in the build-helmet.js process.

I made a local package with npm run build-helmet && npm pack and went through the few test cases in https://github.com/helmetjs/helmet/blob/main/MAINTAINER_README.md and all seemed to pass. It also resolved my issue detailed in the above issue thread without changing my tsconfig.json or the way I import helmet. Which on top of fixing the issue also means there is no longer anything breaking changed for me from v4 to v5.

I welcome any feedback and if it helps any of the other people in the above thread.

@EvanHahn
Copy link
Member

EvanHahn commented Jan 7, 2022

Thanks for this. It's a nontrivial change so it might be a few days before I review this, apologies!

Copy link
Member

@EvanHahn EvanHahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks reasonable at a first glance...thanks for doing this! I'll take a more thorough look soon.

I'll test it myself but I want to double-check that the following work:

  • Vanilla JS, let helmet = require('helmet') should work
  • Vanilla JS with "type": "module" in package.json, import helmet from 'helmet' and import * as helmet from 'helmet' should work
  • TypeScript, compiling to CommonJS without esModuleInterop
  • TypeScript, compiling to CommonJS with esModuleInterop
  • TypeScript, compiling to ECMAScript modules

package.json Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
@EvanHahn
Copy link
Member

I'll take another look at this in a few days. (Things are a bit busy for me and I want to make sure I don't skim over this!)

Copy link
Member

@EvanHahn EvanHahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great overall, and should fix a bunch of user issues. Thanks for doing this!

Left a few comments, but nothing major. Also, one more thing: could you run npm run format before you put this up for re-review?

bin/helpers.js Outdated
Comment on lines 61 to 66
export async function renameFile(oldPath, newPath) {
await fs.rename(oldPath, newPath)
.catch((error) => {
if (error) console.error(error);
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems strange that we'd drop errors here—I think I'd like it to crash. Could we delete this function and just use fs.rename?

bin/build-helmet.js Outdated Show resolved Hide resolved
index.ts Show resolved Hide resolved
bin/build-helmet.js Outdated Show resolved Hide resolved
);
await moveDir(path.join(esmDistDir, "middlewares"), path.join(typesDistDir, "middlewares"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just fs.rename this? Why do we need this fancy helper?

Copy link
Contributor Author

@MitchellCash MitchellCash Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fs.rename doesn't handle moving files to directories that don't exist, nor have any sort of recursive renaming AFAIK. So this function was created to recursively traverse a directory tree and create required directories before moving a file.

However, in commit c8e887c I've removed this function in an attempt to simplify things.

Instead of outputting the *.d.ts type files into dist/esm/* and then using the moveDir helper to get them over to dist/types/*. Now when outputting ESM I output the types straight to dist/types/* and then only need to worry about moving dist/index.js to dist/esm/index.js.

We get to the same place as before, but with less code for us to maintain and a little more readable as we only are moving a single file instead of traversing a directory tree moving many files. Happy for feedback!

@MitchellCash
Copy link
Contributor Author

@EvanHahn happy to help! Also, apologies I've been busy the past few days, I will go over your feedback today 😃

@EvanHahn
Copy link
Member

Looks like you've been making some changes—let me know when it's time for me to take another look.

@MitchellCash
Copy link
Contributor Author

@EvanHahn ready for the next review now, I believe all the previous feedback should now be resolved.

By the way, if you would like me to squash these commits at any point please let me know.

@EvanHahn
Copy link
Member

EvanHahn commented Jan 20, 2022 via email

Copy link
Member

@EvanHahn EvanHahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge thanks for all of your help here! I'll merge and release soon.

Feel free to add yourself to the contributors list, or let me know your preferred name and optionally a URL, and I'll put you on there myself.

@EvanHahn EvanHahn merged commit a584439 into helmetjs:main Jan 22, 2022
@EvanHahn
Copy link
Member

This has been released in helmet@5.0.2. Thanks again!

@MitchellCash MitchellCash deleted the cjs-esm-interop branch January 24, 2022 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

This expression is not callable. error on typescript helmet 5.0.1
2 participants