-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Import assertions #5391
Import assertions #5391
Conversation
Does anyone mind reviewing this? @vendethiel? @edemaine? |
Maybe a regression test for an identifier being |
Like |
Yes, and |
Done. |
It looks like the new tests somehow broke
It looks like I need to add |
Line 479 in f557c05
Once JSON modules are unflagged in Node, which should happen soon, those tests will automatically start running in Node too, and they’ll pass. I tested by setting the flag ahead of time via |
My guess (based on the experiments below) is that the > process.version
'v17.3.0'
> new Function('async () => { await import(\'data:application/json,{"foo":"bar"}\', { assert: { type: "json" } }) }')
[Function: anonymous]
> new Function('import(\'data:application/json,{"foo":"bar"}\', { assert: { type: "json" } })')
[Function: anonymous]
> await new Function('return import(\'data:application/json,{"foo":"bar"}\', { assert: { type: "json" } })')()
Uncaught:
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "data:application/json,{"foo":"bar"}" has an unsupported MIME type "application/json"
at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
at new NodeError (node:internal/errors:371:5)
at ESMLoader.load (node:internal/modules/esm/loader:380:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:280:47)
at async link (node:internal/modules/esm/module_job:70:21) {
code: 'ERR_INVALID_MODULE_SPECIFIER'
} Here's the output I get with > await new Function('return import(\'data:application/json,{"foo":"bar"}\', { assert: { type: "json" } })')()
[Module: null prototype] { default: { foo: 'bar' } }
> (node:41788) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created) So now we just need to figure out a different |
Our CI doesn’t include 17 as it’s unstable; we stop at 16. I was testing in 16. JSON modules will get unflagged before 18, so I don’t think it’ll matter much whether this works in 17. For now either just set |
This PR adds support for the import assertions syntax, which permits
import
orimport()
of JSON files (browsers and Node) and CSS files (browsers). It looks like this (identical in CoffeeScript and JavaScript):The Node feature is flagged behind
--experimental-json-modules
; I was partly responsible for adding it. The syntax is supported without flags in at least Chrome. You can test in latest Node by running the tests after settingexport NODE_OPTIONS=--experimental-json-modules
, or runcake build:browser
to update the browser build and runhttp-server docs/v2
and go to http://127.0.0.1:8080/test.html in Chrome. Only theimport()
syntax is tested by running, since we don’t yet have support for ESM syntax in our tests (import()
is supported in CommonJS and classic Script modes for Node and browsers, respectively) and so the staticimport
andexport
syntaxes are tested via string comparisons, like our existingimport
andexport
tests. (I’d love to get proper ESM support in our tests, but that’s another PR.)I followed the AST I saw in https://astexplorer.net/ for
@babel/parser
. @helixbass please let me know if it looks okay.