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 package resolution in legacy Node.js #3816

Merged
merged 3 commits into from
Jun 19, 2023
Merged

Fix package resolution in legacy Node.js #3816

merged 3 commits into from
Jun 19, 2023

Conversation

colinrotherham
Copy link
Contributor

@colinrotherham colinrotherham commented Jun 16, 2023

As described in #3755 this PR fixes point 2)

We've identified two scenarios when trying to require() or import GOV.UK Frontend

  1. Node.js >= 17 errors with GOV.UK Frontend v4 folder mapping in "exports"
  2. Node.js <= 12.18 errors with GOV.UK Frontend v5 (unreleased) subpath patterns in "exports"

This may also fix issues with bundlers that also follow Node.js package exports resolution

@colinrotherham colinrotherham added 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) javascript labels Jun 16, 2023
@colinrotherham colinrotherham requested a review from a team as a code owner June 16, 2023 12:42
@colinrotherham colinrotherham linked an issue Jun 16, 2023 that may be closed by this pull request
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3816 June 16, 2023 12:42 Inactive
@colinrotherham colinrotherham changed the title Fix Node.js <= 12.18 require + import Fix package resolution in legacy Node.js Jun 16, 2023
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3816 June 16, 2023 13:31 Inactive
Copy link
Member

@romaricpascal romaricpascal left a comment

Choose a reason for hiding this comment

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

Neat! And nice one for the testing. If the answer to my question is "no", then all good to go 🙌🏻

Comment on lines +247 to +257
- name: Export ${{ matrix.conditions }}, Node.js ${{ matrix.node-version }}
env:
# Node.js conditions override from "require" to "import" etc
# https://nodejs.org/api/cli.html#-c-condition---conditionscondition
FLAGS: ${{ matrix.conditions != 'require' && format(' --conditions {0}', matrix.conditions) || '' }}

run: |
node --eval "console.log(require.resolve('govuk-frontend'))"${{ env.FLAGS }}
node --eval "console.log(require.resolve('govuk-frontend/package.json'))"${{ env.FLAGS }}
node --eval "console.log(require.resolve('govuk-frontend/dist/govuk/i18n.js'))"${{ env.FLAGS }}
node --eval "console.log(require.resolve('govuk-frontend/dist/govuk-esm/i18n.mjs'))"${{ env.FLAGS }}
Copy link
Member

Choose a reason for hiding this comment

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

Brilliant! That's going to be neat to check we're not breaking things by changing package.json 😍

Comment on lines 21 to 23
"./dist/": "./dist/",
"./package.json": "./package.json"
Copy link
Member

Choose a reason for hiding this comment

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

question: Is there a way we can allow "./" in a similar fashion as "./*"? That'd let us not worry about which folders are specifically accessible 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah sorry, I started with that but Visual Studio Code gives me this:

Property ./ is not allowed.

Are there any new paths in v5 we should add for legacy Node.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was my thinking process by the way:

  1. Take the compatibility subpath exports from v4.6.0:
"./govuk/": "./govuk/",
"./govuk-esm/": "./govuk-esm/",
"./package.json": "./package.json"
  1. Convert compatibility subpath exports for v5
"./dist/govuk/": "./dist/govuk/",
"./dist/govuk-esm/": "./dist/govuk-esm/",
"./package.json": "./package.json"
  1. Simplify compatibility subpath exports:
"./dist/": "./dist/",
"./package.json": "./package.json"

Copy link
Contributor Author

@colinrotherham colinrotherham Jun 19, 2023

Choose a reason for hiding this comment

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

If you take our package.json files list (what gets published) we have these that can't be resolved:

govuk-prototype-kit.config.json
README.md

Copy link
Member

Choose a reason for hiding this comment

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

Property ./ is not allowed.

That's shame 😭

If you take our package.json files list (what gets published) we have these that can't be resolved:

govuk-prototype-kit.config.json
README.md

Could we add them to the list of exports for completedness, please and then we can send it through ⛵

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mind if I just do govuk-prototype-kit.config.json as Node.js can't require/import Markdown?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done it, but let me know

For context Sass does not resolve package.json fields or exports
sass/sass#2739

But bundlers like webpack use “style” and “sass” fields using loaders:

https://www.npmjs.com/package/css-loader
https://www.npmjs.com/package/sass-loader

Note: With “exports” present in package.json the stylesheet exports must appear there too to maintain both backwards and forwards compatibility
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3816 June 19, 2023 16:04 Inactive
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@govuk-design-system-ci govuk-design-system-ci temporarily deployed to govuk-frontend-pr-3816 June 19, 2023 16:28 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) javascript
Projects
Development

Successfully merging this pull request may close these issues.

Error resolving GOV.UK Frontend subpath exports in Node.js 17+
3 participants