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

fix: allow deep imports #13795

Merged
merged 4 commits into from Dec 21, 2021
Merged

fix: allow deep imports #13795

merged 4 commits into from Dec 21, 2021

Conversation

ephys
Copy link
Member

@ephys ephys commented Dec 20, 2021

Pull Request Checklist

Please make sure to review and check all of these items:

  • Have you added new tests to prevent regressions?
  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • [N/A] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • [N/A] Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

Fixes #13787

The issue was that once exports is defined in package.json, node uses it as the source of truth for all valid exports of a package.

I've configured exports to replicate how the imports worked in v6.
I also had to remove the alias in registerEsbuild.js as it was redundant with exports.
It also matches reality better as the tests use the same export definition as the published build. :)

I recommend changing the export declaration for V7, see below


Breaking changes recommendations for V7

In v7, we could change the export definition to

{
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js"
    },
    "./package.json": "./package.json",
    "./*": "./dist/lib/*.js"
  },
}

This way, instead of importing sequelize/lib/model.js, users could import sequelize/model.

If we still want users to specify /lib in v7, we can use this one instead

{
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js"
    },
    "./lib/*": "./dist/lib/*.js",
    "./package.json": "./package.json",
  },
}

[recommended] If we want to prevent users from importing /lib which could result in unexpected breaking changes, we could instead use this one which only allows importing sequelize & sequelize/package.json

{
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js"
    },
    "./package.json": "./package.json",
  },
}

@ephys ephys marked this pull request as ready for review December 20, 2021 11:42
@ephys
Copy link
Member Author

ephys commented Dec 20, 2021

While working on this PR I realized the TypeScript migration introduced a breaking change:

Before the TS Migration, importing sequelize/lib/anything from user code would work.

After the TS Migration, it will usually fail as it will, most of the time, try to import a TS file.

This PR also fixes that by mapping the sequelize/lib/* exports to sequelize/dist/lib/*. A user doing require('sequelize/lib/operators') will in fact do require('sequelize/dist/lib/operators') without needing to update their code.

As such, I'd like to add /lib to .npmignore. This won't be a breaking change because of the above mapping.

Note: it only fixes it for node >= 12 as node 10 does not support export mapping. I have no way of fixing it for node 10 without reverting the whole TypeScript migration and delaying it to v7. :/


I think this unexpected breakage is a good argument for forbidding importing /lib in v7 (see 3rd suggestion in #13795 (comment)). They're internal APIs and prone to changes.

@sdepold
Copy link
Member

sdepold commented Dec 21, 2021

Completely agreed on the fact that people would better not import anything from the package other than the main entry point.

@ephys
Copy link
Member Author

ephys commented Dec 21, 2021

I'll PR v7 to completely forbid it once this is merged :)

@sdepold sdepold merged commit 1ecdaf9 into sequelize:main Dec 21, 2021
@sdepold
Copy link
Member

sdepold commented Dec 21, 2021

Thanks a bunch. I think I'll just release a new version straight away or was there anything else we need to fix?

@sdepold
Copy link
Member

sdepold commented Dec 21, 2021

@ephys Would you mind pinging me on Slack?

@ephys
Copy link
Member Author

ephys commented Dec 21, 2021

@sdepold Yep!

@github-actions
Copy link
Contributor

🎉 This PR is included in version 6.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

aliatsis pushed a commit to creditiq/sequelize that referenced this pull request Jun 2, 2022
* fix: allow deep imports

* fix: fix export declarations

* test: fix deep-exports test

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ERR_PACKAGE_PATH_NOT_EXPORTED error on startup with 6.12.0 on Node 12 or 14
3 participants