-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Runtime Compiler API #3442
Runtime Compiler API #3442
Conversation
bikeshedding: |
Well it isn't just TS, you can "compile" JavaScript and JSON with the current implementation. I think when we added the ability to register a custom compiler, that we would want to be able to just throw any of the supported media types at this and just have it return the results, irrespective of it being built in or not. The only limitation I can see of the current API, is that when sources are provided (and they aren't resolved outside of Deno) the media type is determined by extension. There isn't a way to express a media type outside of that. An alternative version of the |
c491f49
to
e64ecdf
Compare
Ok, the |
e64ecdf
to
83d855f
Compare
Ok, the PR is functionally complete now, but needs DRYing, clean-up, tests and docs. |
f24c9fb
to
0047cc9
Compare
It turned out it was a strong one, where root module specifiers sometimes included relative paths in the absolute URL (e.g. "file://foo/bar/../bar/baz.ts") but the resolved modules didn't, so the ability to determine the common root which is needed to figure out what module to instantiate in the bundle wasn't working. 🤷♂ |
a2eadfb
to
ed065bd
Compare
920b6f7
to
a16d4cd
Compare
* @param sources An optional key/value map of sources to be used when resolving | ||
* modules, where the key is the module name, and the value is | ||
* the source content. The extension of the key will determine | ||
* the media type of the file when processing. If supplied, | ||
* Deno will not attempt to resolve any modules externally. |
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.
This API allows to programatically create TS programs programatically without writing file to disk before compilation 👍 nice
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.
One more small nitpick: js/compiler_*.ts
- maybe move all compiler related files under common js/compiler/
directory (or js/compilers/
to resemble layout in Rust)?
All in all I think this PR brings a lot of great functionality that users are waiting for. I'm for landing this PR, but after Tokio 0.2 PR (#3418)
a16d4cd
to
bb05b2b
Compare
I thought about that, as that was my first instinct but then I remembered that @ry doesn't like sub-directories... 😁 so I was going to wait for some feedback before making that change. |
export function compile( | ||
rootName: string, | ||
sources?: Record<string, string>, | ||
options?: CompilerOptions | ||
): Promise<[Diagnostic[] | undefined, Record<string, string>]> { |
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.
Diagnostics are null when there are none, so what would empty array diagnostics mean?
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.
Are you getting an empty array? I believe I coded it so that it doesn't resolve with an empty array.
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.
Why that kind of API? Empty array for no diagnostics follows naturally and is just as easy to check for. Unless this is some convention?
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.
Empty array for no diagnostics follows naturally and is just as easy to check for.
Is it? Technically it is slightly slower, because property access is slower than a value check.
Unless this is some convention?
There are two conventions. One is the arguments to a callback convention, where err
would be the first argument and undefined
if there was no error. The second convention is the Rust enum Result
type, where you have Err
and Ok
. While not directly translatable, a presence of a value would indicate that something didn't go ok.
So why would an empty array be preferable?
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.
There are two conventions. One is the arguments to a callback convention, where
err
would be the first argument andundefined
if there was no error.
When there is a single potential error, the no-error case is modelled with nullability. When there is (explicitly) a list of errors, the no-error case is modelled naturally as an empty list and nullability is redundant, isn't it? null | non-empty list
is generally strange to see.
My first instinct compares it more to an stderr
stream which is simply empty or not (especially when it's referred to as a diagnostic).
da30f57
to
a755177
Compare
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully. Fixes denoland#2927
67a66f1
to
6e60373
Compare
@ry rebased post rusty_v8 merge and still going strong. |
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.
Hey @kitsonk, I really apologize for taking so long to get this in. My insufficient excuses for taking so long is that w've been refactoring lower-level code and it's difficult to switch modes when thinking about that.
My concerns with this patch are:
- I keep thinking it would be nice to have the TypeScript compiler be less privileged and instead run as normal user code. This is clearly impossible in the near future - and it's not even clear if it would be possible under any circumstances. But it bothers me that we're unable to run the normal TypeScript compiler as user code still (like we do with, for example, prettier).
- I'm slightly concerned with the mismatch between subcommand names and the APIs. "Deno.bundle" matches "deno bundle" but "Deno.compile" corresponds to "deno fetch".
That said, we do have a privileged compiler facilities which deviate from TS's in functionality. Currently they are only accessible from the command-line by executing Deno - certainly it's better to be able to programmatically access these APIs, as is achieved in this patch. These APIs which you've added are all accessed through the op interface, so I think this is all good.
This patch does add a bit of runtime code, so let's watch the benchmarks for changes in snapshot size or execution time regressions.
All that said - it LGTM - it's a well written patch with tests and docs.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Also restructures the compiler TypeScript files to make them easier to manage and eventually integrate deno_typescript fully.
Fixes #2927
Fixes #3054
This PR provides 3 new
Deno
APIs which provide user/runtime access to the TypeScript compiler that is built into Deno. This should be a good base for creating interesting workloads on top of Deno.