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

refactor: Be stricter about imports #485

Merged
merged 18 commits into from
Apr 10, 2022
Merged

refactor: Be stricter about imports #485

merged 18 commits into from
Apr 10, 2022

Conversation

fb55
Copy link
Collaborator

@fb55 fb55 commented Apr 1, 2022

Addresses #453 (comment). Changes are in three categories:

  1. Adds exports to the main parse5 package (many of them marked as @internal, which hides them in the generated documentation). This way, we won't need deep exports. I'm not entirely happy with the amount of API surface this exposes, but marking these as @internal should allow us to introduce some potentially breaking changes in non-major versions.
  2. Moves some code that made more sense in another place. This is for serialising doctypes (only used in the htmlparser2-adapter, as well as some tests), and hasUnescapedText, which made sense as part of the HTML file.
  3. Standardises imports. This means we don't import * as parse5 in most places, and we are explicit with type imports.

Additionally, all package.json files have been updated.

Fixes #301

@@ -1,4 +1,5 @@
export enum NAMESPACES {
/** All valid namespaces in HTML. */
export enum NS {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We always aliased NAMESPACES as NS, even in this file. This makes it the actual name.

@@ -18,7 +18,9 @@
],
"license": "MIT",
"main": "dist/index.js",
"exports": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
Copy link
Collaborator

Choose a reason for hiding this comment

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

by setting this, does that mean importing an exact file will fail in typescript? since it'd look for the definition in here rather than {whatever-you-imported}.d.ts. maybe thats your intention? still trying to understand the overall change tbh so could've just misunderstood

Copy link
Collaborator Author

@fb55 fb55 Apr 2, 2022

Choose a reason for hiding this comment

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

With this PR, exact file imports are no longer allowed — all you can do is import the modules. This is done through the exports field.

As for the types field — from my understanding, this is for TS to figure out how to resolve a plain module import (eg. import ... from 'parse5'). TS should use co-located .d.ts files when you actually import a file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

because we don't bundle anything, i feel like we don't need it. typescript will already detect that there's an index.d.ts for the index.js. using a bare import will resolve main or whatever else, and then typescript will find the parallel d.ts file iirc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am honestly not sure if we need it or not. Eg. tsc might be able to pick up the type definitions, but will NPM show the TS badge if there is no types field in the package.json?

My gut instinct is to keep this field, just to be safe.

Copy link
Collaborator

@43081j 43081j Apr 4, 2022

Choose a reason for hiding this comment

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

i just tried it on a package, with types set using a direct import of a module. it seems to work fine, so i think you're ok to leave this here & yeah npm checks explicitly for types/typings AFAIK

@fb55 fb55 merged commit 8e22fe4 into master Apr 10, 2022
@fb55 fb55 deleted the stricter-imports branch April 10, 2022 13:58
jmbpwtw pushed a commit to jmbpwtw/parse5 that referenced this pull request Feb 16, 2023
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.

Expose default treeadapter
2 participants