-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add support for generating TypeScript declarations, and named pipelines #3613
Conversation
We were registering the declaration but not finding all references to it.
Benchmark Resultspackages/benchmarks/kitchen-sink
Timings
Cold Bundles
Cached Bundles
packages/benchmarks/react-hn
Timings
Cold BundlesNo bundle changes detected. Cached BundlesNo bundles found, this is probably a failed build... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me. Would be interested to hear from @jamiebuilds on the named pipeline work.
@@ -0,0 +1,23 @@ | |||
// @flow strict-local |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this only for packaging type definitions? Maybe the name of the file could include "types"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I could work for any TS... but we could go more specific... It's not even really just TS, all it is needed for is inserting the source mapping url, which the raw packager doesn't do.
@@ -1,6 +1,7 @@ | |||
{ | |||
"bundler": "@parcel/bundler-default", | |||
"transforms": { | |||
"types:*.{ts,tsx}": ["@parcel/transformer-typescript-types"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be the syntax for named pipelines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
This adds support for generating TypeScript declaration files as a library target, along with the beginnings of named pipeline support. This allows the same input file to be sent through multiple different pipelines to produce different outputs (a
.js
file and a.d.ts
file in this case). Apipeline
parameter has been added to dependencies, asset requests, and config requests to support this in core. Related: #3477.Eventually, resolvers should be able to return pipeline names parsed from import specifiers, so you could reference pipelines from code. For now, the pipeline is set on entries based on the target name (e.g.
types
from package.json). So, you can have a package.json like this:And a
.parcelrc
like this (actually, this is in the default config):Parcel will resolve the targets in package.json, and apply the appropriate pipeline based on the target name. If no named pipeline is found for that target (e.g. no
main
pipeline), then it will fall back to the non-named pipeline that matches.In terms of actually generating TS declarations, we use the TypeScript compiler and a custom TypeScript transformer to do that. TypeScript normally generates separate
.d.ts
file for each file that you import in your code, but if you specify theoutFile
option it will generate a "bundle" file instead with all modules inlined. Unfortunately, this still includes all types, not only ones that are exported by the package.So, we have a custom TS transformer which uses the AST to tree shake and flatten these modules into one top-level
.d.ts
file containing only exported types. It works in three passes, similar to the JS tree shaking implementation: first collecting information about the imported, exported, and local declarations in each module, then propagating exports from the entry module through to the children and marking used nodes, and finally removing unused nodes and flattening into the top-level scope, renaming declarations as needed.