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

Use export default for main exported classes #1631

Merged
merged 1 commit into from
Jul 26, 2024
Merged

Use export default for main exported classes #1631

merged 1 commit into from
Jul 26, 2024

Conversation

tidoust
Copy link
Member

@tidoust tidoust commented Jul 25, 2024

With the switch to ECMAScript modules, main Reffy functions were exported as named exports but there was no default export, meaning Reffy could be imported through:

import * as reffy from 'reffy';
import { crawlSpecs } from 'reffy';

... but there was no way to write:

import reffy from 'reffy';

This update makes it possible to do either:

import reffy from 'reffy';
import { crawlSpecs } from 'reffy';

Now, the dark side of this feature requires lines some might consider to be unnatural... In particular, that requires writing similar things 3 times in index.js:

  1. First, to import named exports from the different modules
  2. Second, to create named exports
  3. Third, to create a default Reffy export, a sort of static class that exposes the functions.

There's no way to reduce that (if there is a way, I don't know how :)). That makes me wonder whether this approach is good practice?

For example, there is a specific syntax in JavaScript for re-exporting values from other modules:

export { crawlSpecs } from "./src/lib/specs-crawler.js";

That is tempting! But that syntax does not import crawlSpecs and so cannot be used to also create a default export with crawlSpecs as a property.

Also, while they are written the same way, the values of the export and export default syntaxes cannot be combined because they are conceptually different beasts: the former is a list of names, the latter an object with properties.

The update does similar magic for the parse-webidl.js module. The post-processor module only exports the processor as a sort of static class and does not create named exports, because that's how the module is used in practice.

With the switch to ECMAScript modules, main Reffy functions were exported as
**named** exports but there was no **default** export, meaning Reffy could be
imported through:

```js
import * as reffy from 'reffy';
import { crawlSpecs } from 'reffy';
```

... but there was no way to write:

```js
import reffy from 'reffy';
```

This update makes it possible to do either:

```js
import reffy from 'reffy';
import { crawlSpecs } from 'reffy';
```

Now, the dark side of this feature requires lines some might consider to be
unnatural... In particular, that requires writing similar things 3 times in
`index.js`:

1. First, to import named exports from the different modules
2. Second, to create named exports
3. Third, to create a default Reffy export

There's no way to reduce that. That makes me wonder whether this approach is
good practice. For example, there is a specific syntax in JavaScript for
re-exporting values from other modules:

```js
export { crawlSpecs } from "./src/lib/specs-crawler.js";
```

That is tempting! But that syntax does not import `crawlSpecs` and so cannot
be used to also create a default export with `crawlSpecs` as a property.

Also, while they are written the same way, the values of the `export` and
`export default` syntaxes cannot be combined because they are conceptually
different beasts: the former is a list of names, the latter an object with
properties.

The update does similar magic for the `parse-webidl.js` module. The
post-processor module only exports the processor as a sort of static class and
does not create named exports, because that's how the module is used in
practice.
@tidoust tidoust merged commit ef0d198 into main Jul 26, 2024
1 check passed
@tidoust tidoust deleted the export branch July 26, 2024 12:33
tidoust added a commit that referenced this pull request Jul 26, 2024
New feature:
- Allow apps to use `import reffy from 'reffy';` (#1631)

Dependencies bumped:
- Bump web-specs from 3.13.1 to 3.14.0 (#1632)
- Bump puppeteer from 22.13.1 to 22.14.0 (#1630)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants