Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Implement "exports" proposal #72

Closed
wants to merge 5 commits into from

Conversation

guybedford
Copy link
Contributor

@guybedford guybedford commented Jul 2, 2019

This implements the package.json "exports" proposal (https://github.com/jkrems/proposal-pkg-exports/) with the following features:

  • When "exports" are defined for a package ({ "exports": { "./x": "./x.js" }), resolving that package import 'pkg/x', will resolve the exports path (/path/to/pkg/x.js).
  • When a subpath does not match "exports", an error is thrown (see test case).
  • Support for "exports": false to indicate a package has no exports at all.
  • Support for the "." export as an alias of the main.
  • Support for folder exports ({ "./folder/": "./other-folder/" })

Support for "exports" when resolving from CommonJS is not currently implemented.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@MylesBorins
Copy link
Contributor

Amazing work!

I think that we should remove "Support for the "." export as an alias of the main."... this recreates the issue with dual mode.

@devsnek
Copy link
Member

devsnek commented Jul 2, 2019

this repo is two months behind master, you may want to re-open this in nodejs/node

@targos
Copy link
Member

targos commented Jul 2, 2019

This repo is not behind master. It's updated once per day by a Jenkins job

@guybedford
Copy link
Contributor Author

I've also added some documentation and included a folder mapping feature test.

// Loads ./node_modules/es-module-package/src/features/x.js
```

If a package has no exports, setting `"exports": false` can be used instead of
Copy link
Member

Choose a reason for hiding this comment

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

I think this should just be “any non-nullish value is passed through Object.keys to get the export list; any nullish value disables the feature”, because then “false” is just a convention instead of a magic special value.

Copy link
Contributor

Choose a reason for hiding this comment

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

So:

  1. null and undefined: Same as { './': './' }.
  2. Anything else: Normalized as an object (e.g. Object.assign({}, pkgExports)).

Makes sense!

Copy link
Member

Choose a reason for hiding this comment

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

I’d use { ...pkgExports } but yes, exactly!

Copy link
Contributor

Choose a reason for hiding this comment

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

I think guiding people towards a convention here may still have value. How about adding the following sentence:

This is just a convention that works because false, just like {}, has no iterable own properties.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed and that text sounds good to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Done!

test/fixtures/pkgexports.mjs Outdated Show resolved Hide resolved
@jkrems
Copy link
Contributor

jkrems commented Jul 4, 2019

Support for "." is gone as is the special treatment of false.

@jkrems
Copy link
Contributor

jkrems commented Jul 4, 2019

Afaict this now has all the pieces for the upstream PR, with CJS support and general polish / edge case hunting being follow-ups while it's experimental.

@jkrems
Copy link
Contributor

jkrems commented Jul 5, 2019

I turned this into a PR against core: nodejs/node#28568

Should we close this PR?

@ljharb
Copy link
Member

ljharb commented Jul 5, 2019

@jkrems i thought we were going to remove Support for the "." export as an alias of the main.

@jkrems
Copy link
Contributor

jkrems commented Jul 5, 2019

@ljharb May be a misunderstanding: What I removed (and there's a test for it not working) is setting the un-postfixed package name export via the exports field. The test to verify that adding a . exports key doesn't work is here: https://github.com/nodejs/node/pull/28568/files#diff-a512c0b2a7207547e7e8e8fb17bd1aa5R9

@jkrems
Copy link
Contributor

jkrems commented Jul 5, 2019

@Trott
Copy link
Member

Trott commented Apr 9, 2020

Should this be closed at this point?

@MylesBorins MylesBorins closed this Apr 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants