-
Notifications
You must be signed in to change notification settings - Fork 43
Flags functionality options #300
Comments
I think there's two different cases / functionalities that may be good to keep apart:
The first is a mostly binary choice between two modes. The latter is not. The content-type could be JSON, WASM, binary module, binary AST (forward looking), JS module, CJS module, or any future format. The first is "switch mime DB", the latter is "provide mime for this specific bit". If I say
For test runners (or |
Taking off my neutrality hat from the top post, the one thing I’m sure of is that I think I’m sympathetic to the argument that the use cases for
So basically if the choice was only between Switching from Even with So how would people feel about this approach? Replace |
I like this approach |
Given that being able to control the parse goal of non-file input is the only one of the three that is strictly necessary as opposed to being about convenience, sugar, etc, I wholeheartedly agree with this plan. It will neatly sidestep concerns about defaults, modes, consistency with package.json, etc; it makes something impossible possible; and it leaves open the design space to make something possible perhaps be simpler. |
Input from a user (me :-) ):
Thanks to all for your deep thought, and hard work, on this. I'm overbooked the next two months, but I hope to start giving back by pitching in on things a newbie can help with (so, probably not this) in a couple of months. |
Why is it a need to not have a package json when using multiple files? Do you often have multiple files, no package.json (and thus no non-builtin dependencies), and run things directly with node? |
@ljharb - (Replacing my earlier reply, hit send too soon.) My quick examples are usually just a single file, but I prefer ESM to |
With a single file, you can use the proper extension (mjs) and it will be interpreted as such; at the moment the intention is for multiple files to require a package.json if you want to use an alternative file extension. |
@ljharb - "the proper extension" can be a heated phrase. For me, the proper extension is |
I meant to say, and should have said, "can be a heated phrase," not "is". Sorry about that. (Didn't want to just silently edit.) |
Definitely let’s not debate it; but the reality is that i used an accurate term. It’s totally fine if you want to use a different extension, and i fully support (#283) everyone’s full freedom to do so - but that doesn’t change what the (not temporary) defaults are, and that you need a package.json to override the defaults. |
I'd use "default," it avoid the problems with "proper." The reality is indeed the
Or, ideally, |
--package-type still has some confusion due to it not being clear about
override boundary being the cwd, all the fs, etc. Perhaps we can use a
different name like --default-types , the usage of multiple formats covered
by these flags also makes me wish we would use the plural
…On Sat, Apr 13, 2019, 10:30 AM T.J. Crowder ***@***.***> wrote:
I'd use "default," it avoid the problems with "proper." The reality is
indeed the .mjs is the *default* extension for ESM on Node.js.
...and that you need a package.json to override the defaults.
Or, ideally, --package-type. :-)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#300 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAOUo2HIah-TZ7HV4QWzlGd_3wZzoFZUks5vgff_gaJpZM4cKuhd>
.
|
One of the use cases discussed was wanting to change Node's default type, i.e. making .js treated as ESM by default when there's no package.json or there is one but it lacks a @bmeck “types” already has meaning in TypeScript so it's probably not a good choice. I don't find the use of singular confusing, as the user is choosing only one type. And the browser attribute is just |
The flag does not act similar to the browser attribute though and that at a glance leads to the confusion of treatment similar to how --entry-type was compared to the attribute in an odd way. The browser flag just uses an alternative scripting infrastructure and does not affect how the browser runtime identifies how to interpret content. The flag in Node does not cause a different scripting system to be used to load the module, it only affects how the ESM infrastructure identifies how to interpret a files contents for ESM. The typescript argument also brings up verbiage problems (also see the previous issue that was brought) for dialogue that are being ignored here like: "What type of package is X" Also the flag can and should become general purpose due to needs of supporting other types such as .coffee, .ts, etc. for properly dealing with loader composition. We have had many talks about how the feature being described does not apply to a single type over time but must deal with further extension to handle existing types such as .jsx, .es6, etc. As well as portential new types. None of these need to really be addressed when considering --default-types if we call it default-type , but the usage of singular is something we should probably avoid. |
I thought it might be useful to have a post laying out in neutral terms how the various ESM flag proposals work and the implications of each, to help in discussion for choosing the best approach. If anything in this initial post is incorrect or incomplete or biased, I will edit as appropriate.
There are three options discussed so far for how what was previously called
--type
should behave. I’ll refer to the options by proposed new names based on how the flag would behave. They all take eithermodule
orcommonjs
as the single accepted and required argument.--input-type
would set the type of--eval
,--print
andSTDIN
input—and that’s it. If the initial entry point is a file, an error is thrown.--entry-type
would set the type of the initial entry point to the program, whether that be a file or one of the non-file input types (--eval
etc.); but setting of the type of that initial entry point implies nothing about any other files.--package-type
would set or override the"type"
field of thepackage.json
file that controls the parsing of the initial entry point (or of the virtualpackage.json
at the root of the volume, if there are nopackage.json
files up the path from the entry point). It would also apply to the non-file input types. Like the"type"
field, it would not override any package scopes beyond the one containing the initial entry point.This isn’t meant to be a discussion of names. I don’t feel that it’s a good use of GitHub issue threads to bikeshed what we should name things. Once we decide on which functionality we want, we can separately determine the best name for it. Please consider the names below to be placeholders.
Pros and cons of each option
--input-type
Pros:
--eval
,--print
andSTDIN
, where otherwise ESM syntax would be impossible to enable.Cons:
--entry-type
Pros:
Provides a way to use ESM in “loose”
.js
or extensionless files, that live outside of any project/package and don’t have a parentpackage.json
.Explicitly applies to the file or string being referenced in the
node
command, so is straightforward in that regard.Cons:
So far, all users who have read about this have assumed that setting the type of the entry also opts in to that type for all files imported by that entry point. As in, if you set the entry point to be ESM,
import
statements of.js
files should treat those.js
files also as ESM. This user expectation is likely to only become stronger as ESM in browsers becomes more widely used, as this is how<script type="module">
behaves in browsers.It is inconsistent for
entry.js
anddep.js
to be side by side, whereentry.js
import
sdep.js
, andnode --entry-type=module entry.js
loadsentry.js
as ESM but thendep.js
is treated as CommonJS. This is the only case where files with the same extension in the same folder are treated differently by Node.There’s no use case for overriding the initial entry point of a project while relying on file extensions or
package.json
to define the type of all other files in the project.--package-type
Pros:
Behaves as users expect the flag to behave, by setting the type for an entire project.
By referencing
package.json
"type"
in its name, this should be easier for users to understand as they should grasp that it behaves the same way aspackage.json
"type"
does.Allows “loose”
.js
or extensionless files toimport
other ESM.js
files, so “shell script”.js
files don’t need to be limited to a single file.Without this, we can’t have
--package-type=auto
, as it wouldn’t make sense to have type detection for an entry point only. The use case forauto
is a project that lacks an explicit"type"
field (and uses.js
), and it’s implausible to imagine a project with a.js
entry point where all other files are.mjs
(or in a subfolder under apackage.json
with a"type"
field).Cons:
node
, so users would need to be aware that it’s the equivalent topackage.json
"type"
.Both:
The only use case for needing this flag for a project is when a project is already using ESM in
.js
files but without"type": "module"
; but most such projects expect Babel or the like to transpile them, and may not be compatible with Node without changes. (For example, they may require refactoring to enable explicit extensions; though--es-module-specifier-resolution=node
might be sufficient for most such projects to run without changes.) If we build--package-type=auto
, regardless of its effectiveness for Babel ESM projectsauto
would work great for a CommonJS project without apackage.json
"type"
field.Allows opting into ESM mode by default system-wide via
NODE_OPTIONS=--package-type=module
. After setting such an option in a user’s environment, CommonJS projects would need to either have a"type": "commonjs"
in theirpackage.json
or be run via--package-type=commonjs
. Allowing changing Node to be ESM by default system-wide would be seen as a pro by some and as a con by others; it’s a pro for those who want to leave CommonJS behind and don’t plan on adopting.mjs
; and as a con for those who don’t want to encourage people to expect ESM by default and publish projects and packages that assume so. (The latter concern could presumably be addressed somewhat bynpm publish
checking forimport
/export
syntax in packages about to be published, and erroring if"type": "module"
is not present.)Other considerations
It is not clear to me how these flags would interact with loaders, test runners, stubs/mocks and the like. I’m not sure if any flags are better or worse than others with regard to such things. On the one hand
--input-type
or--entry-type
would seem to be simpler for such add-ons to handle, as they only apply to one string or file; yet if the add-ons need to know how to handlepackage.json
"type"
, it might be simpler for them to support that and--package-type
(which should behave identically) rather than needing to special-case the entry point.See also
upstream objection to --type #296 - Initial discussion of upstream objection
esm: scoped --type, cpp refactoring ecmascript-modules#57 - PR that implements
--package-type
functionality (though with--type
name)new ESM implementation node#26745 - Upstream PR, the code for which contains
--entry-type
currently; and the thread contains some discussion of--type
The text was updated successfully, but these errors were encountered: