Say that I have a library that relies on non-strict semantics, like coercing a top-level this
to the global object:
this.library = { foo: function () {} };
Say that I try to import this library using import
:
import './library';
library.foo();
Since I used import
, does that mean the library code implicitly runs in strict mode (and breaks, since this
is undefined
in strict code)? The ECMAScript spec says,
Module code is always strict mode code.
and for the definition of "module code,"
Module code is source text that is code that is provided as a
ModuleBody
.
And a ModuleBody
consists of a ModuleItemList
, which consists of ModuleItem
's, which could include ImportDeclaration
, ExportDeclaration
and StatementListItem
, and a StatementListItem
could be a Statement
or Declaration
. Which implies that any code could be "module code" depending on the context it is loaded, even without the prescence of an import
or export
declaration.
Also, the definition of [[RequestedModules]]
field from 15.2.1.16 implies that the ModuleSpecifier
used in ImportDeclaration
indeed specifies the name of a "module:"
A List of all the
ModuleSpecifier
strings used by the module represented by this record to request the importation of a module.
It would appear that the module system is backwards-incompatible with non-strict code. Is this true?
From this conversation:
A module is not a JavaScript program that includes import/export statements. Just because they MAY contain import/export statements does not mean that they MUST have them. You cannot always look at a program and determine whether or not it is a module. . . .
Since ModuleBody is optional, the empty program is a Module. The empty program does not contain an export statement.
So, yes, the module system is backwards-incompatible with non-strict code. I suppose code not using the module system is not really fit for participation in the system, because it's likely exporting global variables, which defeats the purpose of it.
Therefore, in order to interoperate, old code will have to be updated either to provide an export
declaration or to use strict mode (and/or "guaranteed" non-strict techniques like Function('return this')()
, instead of a supposedly non-strict top-level this
).