Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: improve astro compatibilty #172

Merged
merged 1 commit into from
Aug 7, 2023
Merged

fix: improve astro compatibilty #172

merged 1 commit into from
Aug 7, 2023

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Aug 6, 2023

When this pattern is used:

{
  ".": {
    "source": "./src/index.js",
    "require": "./dist/index.cjs",
    "node": {
      "import": "./dist/index.cjs.js"
    },
    "import": "./dist/index.js",
    "default": "./dist/index.js"
  }
}

astro throws an error as it will use the node.import condition and isn't prepared to handle a re-export of CJS:

error   "default" is not exported by "../dummy-commonjs/dist/index.js", imported by "../dummy-commonjs/node/index.mjs".
  File:
    /Users/cody/Developer/GitHub/pkg-utils/playground/dummy-commonjs/node/index.mjs:1:7
  Code:
    1: import cjs from '../dist/index.js'
              ^
    2: 
    3: export const format = cjs.format
  Stacktrace:
RollupError: "default" is not exported by "../dummy-commonjs/dist/index.js", imported by "../dummy-commonjs/node/index.mjs".

The solution is to add a node.module condition before node.import, that has the same value as import:

{
  ".": {
    "source": "./src/index.js",
    "require": "./dist/index.cjs",
    "node": {
      "module": "./dist/index.js",
      "import": "./dist/index.cjs.js"
    },
    "import": "./dist/index.js",
    "default": "./dist/index.js"
  }
}

This PR adds validation that ensures this pattern, and other pkg.exports ordering best practices, are used. And it will throw errors on common mistakes.

@stipsan stipsan force-pushed the fix-allow-wrapper-without-require branch from 5a7a6ad to d020cff Compare August 6, 2023 01:55
@stipsan stipsan force-pushed the fix-allow-wrapper-without-require branch from d020cff to 00b65d5 Compare August 6, 2023 03:10
@stipsan stipsan force-pushed the fix-astro branch 3 times, most recently from c107659 to 0ad2431 Compare August 6, 2023 06:21
Base automatically changed from fix-allow-wrapper-without-require to main August 7, 2023 10:50
@stipsan stipsan marked this pull request as ready for review August 7, 2023 11:00
@stipsan stipsan requested a review from a team as a code owner August 7, 2023 11:00
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant