| Status | DRAFT |
|---|---|
| Authors | @jdalton @bmeck |
| Date | June 14, 2016 |
- CJS and ES modules just work without new extensions, extra ceremony, or excessive scaffolding
- Performance is generally on par with existing CJS module loading
- Performance is significantly improved for ES modules over transpilation workflows
- Change JS grammars for Script and Module to be unambiguous
- Determine grammar of
.jsfiles by parsing as one grammar and falling back to others
Note: This proposal would replace section 5.1 of the Node ES6 Module Interoperability proposal.
The Script and Module goal of ECMA262 have a grammatical ambiguity where some
code can run in both goals, having the exact same source text, but produce
different results. Unlike "use strict", the signal to have a specific behavior
is not in the code, thus the code has a multitude of possible effects which are
not controlled by the programmer.
Note: The following example highlights a few of the effects of ambiguous grammar and is by no means exhaustive.
function foo(value) {
value = value || '';
var args = [].slice.call(arguments);
args.unshift(this);
return args;
}
foo(null);Differences:
| script | module
--- | --- | --- | ---
variable scope of foo | global | local
arguments object | modified | unmodified
this binding of foo | global | undefined
Since there is no way in source text to enforce the goal with the current grammar; this leads to the behavior of certain constructs being undefined by the programmer, and defined by the host environment. In turn, existing code could be run in the wrong goal and partially function, or function without errors but produce incorrect results.
Require a structure in the Module goal that does not parse as the Script goal. Having this requirement would prevent any source text written for the Module goal from being executed in the Script goal by removing the ambiguity at parse time.
While the ES2015 specification does not forbid this extension, Node wants to avoid acting as a rogue agent. This proposal is on the agenda for the July 2016 ECMA TC39 meeting for further discussion. A specification change or at least an official endorsement of this proposal for Node would be welcomed.
The proposal is to require that Module source text has at least one import or
export declaration. This should feel natural to developers as most modules import
dependencies and or export APIs. A module with only an import declaration and
no export declaration is valid. However, it is our recommendation that modules
are explicit with export. Modules, that do not export anything, should specify
an export {} to make their intentions clear and avoid accidental parse errors
while removing import declarations.
Note: The export {} is not new syntax and does not export an empty
object. It is the standard ES2015 way to specify exporting nothing.
function foo(value) {
value = value || '';
var args = [].slice.call(arguments);
args.unshift(this);
return args;
}
foo(null);| script | module (cannot parse)
--- | --- | --- | ---
variable scope of foo | global | n/a
arguments object | modified | n/a
this binding of foo | global | n/a
function foo(value) {
value = value || '';
var args = [].slice.call(arguments);
args.unshift(this);
return args;
}
foo(null);
export {};| script (cannot parse) | module
--- | --- | --- | ---
variable scope of foo | n/a | local
arguments object | n/a | unmodified
this binding of foo | n/a | undefined
Node currently requires a means for programmers to signal what goal their code is written to run in.
Leading solutions have either hefty ecosystem tolls, ceremony, or scaffolding. They lack a way to define the intent of the source text from the ECMA262 standard.
A package opts-in to the Module goal by specifying "module" as the parse goal
field (name not final) in its package.json. Package dependencies are not affected
by the opt-in and may be a mix of CJS and ES module packages. If a parse goal is
not specified, then parse source text as either goal. If there is a parse error
that may allow the other goal to parse, then parse as the other goal. After this,
the goal is known unambiguously and the environment can safely perform initialization
without the possibility of the source text being run in the wrong goal.
The abstract operation to parse source text as a given goal.
- Bootstrap
sourceforgoal. - Parse
sourceasgoal. - If success, return
true. - If
throws, throw exception. - Return
false.
-
If a package parse goal is specified, then
-
Let
goalbe the resolved parse goal. -
Call
Parse(source, goal, true)and return. -
Fallback to multiple parse.
-
If
Parse(Source, Script, false)istrue, then 1. Return. -
Else 1. Call
Parse(Source, Module, true).
Note: A host can choose either goal to parse first and may change their order over time or as new parse goals are added. Feel free to swap the order of Script and Module.
To improve performance, host environments may want to specify a goal to parse
first. This could be done in several ways:
cache on disk, a command line flag, a manifest file, HTTP header, file extension, etc.
Some tools, outside of Node, may not have access to a JS parser (Bash programs, some asset pipelines, etc.). These tools generally operate on files as opaque blobs / plain text files and can use the techniques, listed under Implementation, to get parse goal information.
-
Esprima relies on a
--moduleflag to signal the Module goal. However, this has proven to be unintuitive for many users. Unambiguous Script and Module goals would enable things to “just work” without flags. -
Facebook Flow performs a series of inferences to detect CJS and ES modules. Unambiguous Script and Module goals would improve its ability to determine module types.
-
JSCS double parses, first as the Script goal, then as the Module goal. With unambiguous Script and Module goals it could parse as the Module goal first. JSCS also accepts input through stdin, so identification of goals through source text is ideal.
-
Linters, like xo, could use unambiguous Script and Module goals to enable module specific linting rules without extra configuration.
-
Microsoft packaged web applications can benefit from unambiguous Script and Module goals. The bytecode cache for a packaged web application is generated upon installation. When the application is running, files are loaded by script tags so their intended parse goals are understood. However, bytecode cache generation is done without running the application so the intended parse goals are unknown. Because of this, the bytecode cache is generated for the Script goal and ignored for ES modules.
-
TypeScript has taken the stance from early on that a script becomes a module when it has at least one
importorexportdeclaration. Over the years they have experienced very few user issues with this approach.
This proposal would not have been possible without the tireless effort, conviction,
and collaboration of
@bmeck, @dherman,
and @wycats.
While iterating on this proposal we have reached out to several people from areas
affected by it.
Although opinions on this proposal are varied, I am grateful for
feedback from:
- @AtOMiCNebula (Microsoft Edge)
- @brendaneich (TC39)
- @bterlson (Microsoft Chakra / TC39)
- @chrisdickinson (Node)
- @hzoo (Babel)
- @indutny (Node)
- @leobalter (jQuery Foundation / TC39)
- @ljharb (TC39)
- @loganfsmyth (Babel)
- @mathiasbynens (Lodash)
- @rvagg (Node)
- @sheerun (Bower)
- @sindresorhus (AVA / Chalk / Yeoman)
- @travisleithead (Microsoft Edge / W3C)
- @trevnorris (Node)
Thank you! ❤️
— @jdalton