-
Notifications
You must be signed in to change notification settings - Fork 2k
Add more explicit sourceType in options and return of compile #5268
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
Conversation
This allows code passing the output on to other tools to correctly identify the `sourceType` of the output.
@@ -100,12 +100,16 @@ exports.compile = compile = withPrettyErrors (code, options = {}) -> | |||
) | |||
|
|||
# Check for import or export; if found, force bare mode. | |||
unless options.bare? and options.bare is yes | |||
sourceType = options.sourceType ? 'unambiguous' |
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 is based on the definition of sourceType
in babel. In both babel sourceType: 'unambiguous'
and in CoffeeScript, there's behavior based on the existence of import
/export
statements. In the future this could also fail properly when import
is used in scripts but that may fall out of the scope of coffeescript.
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.
FYI the original code here removes the “safety wrapper” (function() { ... }).call(this);
when the source code contains import
or export
, since per spec import
and export
must be at the top level and therefore such a wrapper would always break ESM code.
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.
Another problem with the IIFE is that .call(this)
in a module wouldn't work. So even without import
/export
it would break when the source type is module. :)
EDIT: "Wouldn't work" as in "doesn't really make sense" because there's no value for this
in a module unlike in scripts and CJS files.
For background: I was wishing for this kind of API when working on examples for fetch hooks / service workers. It allows post-processing to be a bit smarter about how much (if any) wrapping the JS generated by the compiler needs (without having to add a 2nd layer of guessing). |
Hi @jkrems thanks for this! So it seems like this PR adds a new config option to the Node API? As you might've noticed, the options for CoffeeScript are kept very minimal. In general, adding a new option should have a strong justification; and it should be mirrored in the CLI too unless there's a reason why it wouldn't apply there. I'm not saying that As part of the AST work we've been discussing how to handle So my first thought is, one thing we definitely could do is include If not, then we have to decide how to determine when output = CoffeeScript.compile src, {bare: true, sourceMap: true}
output.sourceType = 'module' |
From my use of the Coffeescript API, using the file system to determine parse goal would've had unwanted side effects. E.g. I used to maintain a service that compiled code in-memory that didn't exist on disk and having CS check the disk for the filenames would've been incorrect (if it would've worked at all).
It's better because it allows - in the future - to make it explicit what the expected parse goal is. The same reason that babel/acorn/etc. all added the option instead of sticking with a "guess from AST" approach. :) E.g. it would be nice if CoffeeScript allowed
That would mean that it's impossible to tell apart if the returned code is a script or if something went wrong / the code was used with an older version of CoffeeScript. I definitely considered just adding the |
I think I'm not fully understanding your use case, or what you're trying to achieve. So there seem to be two possibilities (at least) for providing
The first version seems rather pointless; since the CoffeeScript compiler doesn't do anything with this information other than pass it along as part of the output, it seems like whatever tool you're building that calls/wraps the CoffeeScript compiler could just add the source type as part of its output. So it seems like what you're asking for is the second version, correct? Which could be achieved today via So I guess the question is, what's the argument for providing an option to detect |
The CoffeeScript compiler isn't just used with files on disk. It's also used in-memory. And in those cases checking other files isn't really an option. I agree that right now making the E.g. the following is also clearly an ES module but CoffeeScript will generally not consider it ESM (as shown by the wrapper): # should be detected as ESM but isn't
console.log import.meta
Would it be possible to attach the
Right, but right now it's what CoffeeScript does. I would hope that eventually CoffeeScript would add proper checks for valid syntax and potentially even allow explicit For the CLI, I could imagine that |
For additional context: I'm looking at building a service worker to compile coffeescript on the fly. Right now there's only one mime type for coffeescript. So that's the only information I have. I can't check other files realistically, I have only the mime type and the coffee source text. I can handle coffeescript returning non-module code if I know that it's non-module code so I can wrap it. So I need to have a clear way to communicate to coffeescript: "I got this string, it has the coffeescript mime type, do your thing, and tell me if you'd consider your output to be a module or a script/non-module". Alternatively I could also work with "I can set |
And what about when there's JSX in there? https://coffeescript.org/#jsx. That's also valid in plain CoffeeScript source code. CoffeeScript just doesn't really exist absent its build chain. From the MIME type or file extension you don't know if it's CoffeeScript 1 or 2, for example, and there are some breaking changes between them: https://coffeescript.org/#breaking-changes. You certainly don't know if it's meant to be Script or Module, CommonJS or ESM, JSX or normal JS, etc. |
This isn't correct. CoffeeScript forces All the things you're describing about erroring on certain features in certain modes sound like errors that Babel could throw, and you can pass options to the Babel that can be used as part of CoffeeScript's compilation: use the |
I'm thinking that we should close this for age.
This is an interesting use case, and I’m not sure what the best solution would be. My first reaction is that a I assume this is/was more for demo purposes than anything else? Then perhaps just say up front that the service only support ESM JavaScript? |
This allows code passing the output on to other tools to correctly identify the
sourceType
of the output.This PR was started by: git wf pr