-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
JSON modules #4315
Comments
So concretely, for those not familiar with Node.js's support here, this would mean that if you As for next steps, the main one is implementer interest. /cc @whatwg/modules; does this seem like a reasonable addition? |
I think it is reasonable, but would note that this is for static data, whereas |
I'm happy with certain well-known and commonly-used content-types getting good module support, even if it's just a "trivial" wrapper around a constructor or function call. JSON, CSS, etc are all good. |
This came up at the last W3C TPAC too, I raised WICG/webcomponents#770 at the time. If the complexity is indeed branching on a JSON MIME type and then using parse JSON from bytes I think Mozilla would be supportive. If we also want to advertise support to servers in some manner the discussion might become a little more involved. (It seems apart from an object or array this could also return a primitive, by the way.) |
I don't know if it matters to the standards process, but there is some discussion of the topic already. If you look at the answers on that SO question (and at linked/related questions), you'll see there is some confusion as well. Because many (most?) people writing in ES6 or later end up transpiling via Babel -- which means using a module loader provided by the transpiler, rather than the browser-native one -- they can write |
There are some complexities if we are wishing to exactly match tooling as it exists today. To note, tools supporting JSON as an importable module type do allow both named and default exports. The JSON provided always assigns the result value to the {"x": {}} ~= export default {x: {}}
// export x = default.x // This kind of aliasing where updating Additionally, for non-Identifier properties, the only way to access values would be through the default export due to being unable to import non-Identifier bindings. import locales from './locales.json';
locales[navigator.language]; I think the simplest solution of not supporting named exports would be fine for now and encourage a more uniform usage. |
Conceptually a json file is a single thing; that babel allows named imports from it is merely a product of it conflating destructuring with named imports. I definitely agree json should be confined to default import. |
What are the next steps for getting this off the ground? Spec Text? Implementor interest? I'm happy to help drive this but need some mentorship on the process |
Implementer interest would be ideal, otherwise spec text might be wasted effort. But, you could start spec and test work ahead of implementer interest, if you're OK taking the risk. I'm happy to mentor on spec/test work when you're ready; let's discuss in a more high-bandwidth medium like IRC or offline. |
I will be at tc39 next week and can work on getting some implementer
interest.
|
As background information why this complexity was introduced: aliasing the top-level JSON keys to named exports makes the JSON file tree-shakable on the first level. So if only |
Tree shaking isn't really a good argument for named exports anymore. Build tools can check statically analyzable member expressions and determine if an object binding escapes the static analysis. |
Let's consider multiple modules that depend on the same remote source of static JSON. With today's approach of having each user fetch & JSON.parse the result, they get their own object which is pristine. If the module loader is now holding a shared mutable copy of the data, how can a single user/callsite confidently access the original data? Will there be a special way to request the untainted copy? Or should we consider JSON modules being deeply frozen by default? |
If anybody can come up with a reasonable use case for mutable JSON imports, I'd like to hear it, but otherwise freezing seems like a simple solution. |
These checks are very difficult and I think there are a lot of situations where the object binding would escape. Using named imports is certainly a lot easier to statically analyze. But I also see that allowing named imports of first-level JSON keys kind of abuses named imports for the sake of static analysis, so I'm not a strong advocate of this feature. Just wanted to give some background information.
I also thought about this. I would definitely prefer freezing it but I also don't see a strong reason why the host environment should enforce this. Maybe security reasons? |
Freezing sounds very sensible. Note that freezing would also help static analysis because then even in the case of a reference escaping the analysis, member expressions can still be inlined. |
an alternative to recursively freezing might be if the import was a thunk - a function that returns a new mutable object. |
I don't think this is the place to introduce any kind of freezing or recursive freezing into the web platform. The platform is full of shared mutable objects, e.g. window, or JS modules' namespace objects. There's no reason to treat JSON modules' namespace objects specially. As always, if you want to create a frozen copy of one of these shared mutable objects, your code needs to run first, and do the freezing itself. |
@domenic your argument seems to be nothing more than to state the status quo as canon. I'd have hoped for a more convincing point here. |
Consistency is valuable. JSON modules should not depart from JS modules. |
Perhaps this pushes the argument back towards being for named exports then, since that would ensure the consistency guarantees of JS modules? Although it does mean introducing a valid identifier filter unfortunately. |
No, JS modules that represent a single thing do use default exports, so the consistency is perfect there. |
This PR updates the current `--experimental-modules` implementation based on the work of the modules team and reflects Phase 2 of our new modules plan. The largest differences from the current implementation include * `packge.type` which can be either `module` or `commonjs` - `type: "commonjs"`: - `.js` is parsed as commonjs - default for entry point without an extension is commonjs - `type: "module"`: - `.js` is parsed as esm - does not support loading JSON or Native Module by default - default for entry point without an extension is esm * `--entry-type=[mode]` - allows you set the type on entry point. * A new file extension `.cjs`. - this is specifically to support importing commonjs in the `module` mode. - this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path. * `--es-module-specifier-resolution=[type]` - options are `explicit` (default) and `node` - by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one - by default our loader will not allow for importing directories that have an index file - developers can use `--es-module-specifier-resolution=node` to enable the commonjs specifier resolution algorithm - This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed * `--experimental-json-loader` - the only way to import json when `"type": "module"` - when enable all `import 'thing.json'` will go through the experimental loader independent of mode - based on whatwg/html#4315 * You can use `package.main` to set an entry point for a module - the file extensions used in main will be resolved based on the `type` of the module Refs: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md Refs: https://github.com/GeoffreyBooth/node-import-file-specifier-resolution-proposal Refs: nodejs/modules#180 Refs: nodejs/ecmascript-modules#6 Refs: nodejs/ecmascript-modules#12 Refs: nodejs/ecmascript-modules#28 Refs: nodejs/modules#255 Refs: whatwg/html#4315 Refs: WICG/webcomponents#770 Co-authored-by: Myles Borins <[email protected]> Co-authored-by: John-David Dalton <[email protected]> Co-authored-by: Evan Plaice <[email protected]> Co-authored-by: Geoffrey Booth <[email protected]> Co-authored-by: Michaël Zasso <[email protected]> PR-URL: nodejs#26745 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
FWIW, a new feature of webpack@4 was support for JSON-as-modules. When the JSON document is an object, properties are plucked (when they are valid identifiers) and made available as independent exports, with the |
This PR updates the current `--experimental-modules` implementation based on the work of the modules team and reflects Phase 2 of our new modules plan. The largest differences from the current implementation include * `packge.type` which can be either `module` or `commonjs` - `type: "commonjs"`: - `.js` is parsed as commonjs - default for entry point without an extension is commonjs - `type: "module"`: - `.js` is parsed as esm - does not support loading JSON or Native Module by default - default for entry point without an extension is esm * `--entry-type=[mode]` - allows you set the type on entry point. * A new file extension `.cjs`. - this is specifically to support importing commonjs in the `module` mode. - this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path. * `--es-module-specifier-resolution=[type]` - options are `explicit` (default) and `node` - by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one - by default our loader will not allow for importing directories that have an index file - developers can use `--es-module-specifier-resolution=node` to enable the commonjs specifier resolution algorithm - This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed * `--experimental-json-loader` - the only way to import json when `"type": "module"` - when enable all `import 'thing.json'` will go through the experimental loader independent of mode - based on whatwg/html#4315 * You can use `package.main` to set an entry point for a module - the file extensions used in main will be resolved based on the `type` of the module Refs: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md Refs: https://github.com/GeoffreyBooth/node-import-file-specifier-resolution-proposal Refs: nodejs/modules#180 Refs: nodejs/ecmascript-modules#6 Refs: nodejs/ecmascript-modules#12 Refs: nodejs/ecmascript-modules#28 Refs: nodejs/modules#255 Refs: whatwg/html#4315 Refs: WICG/webcomponents#770 Co-authored-by: Myles Borins <[email protected]> Co-authored-by: John-David Dalton <[email protected]> Co-authored-by: Evan Plaice <[email protected]> Co-authored-by: Geoffrey Booth <[email protected]> Co-authored-by: Michaël Zasso <[email protected]> PR-URL: #26745 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
I realize this feature never seems to have gotten concrete signs of implementer interest. Blink is interested in implementing; any thoughts from Gecko or WebKit? @annevk @jonco3 / @rniwa @Constellation. Concretely, #4407 has the pull request. |
Mozilla is interested. |
Microsoft is on board for implementing this in Blink. |
This patch provides JSON modules as a single default export, with parse errors checked before instantiating the module graph. Note, editorially, it's unclear whether JSON modules should be considered a type of "module script", with a settings object, fetch options, base URL, etc or not. This patch considers them "module scripts", but leaves those record fields unset (as they are unused). This patch is based on tc39/proposal-built-in-modules#44 which hasn't landed yet, so the references are a bit awkward, and this patch should not land until that one does. Closes whatwg#4315
- Tweaks to <script> element stuff - Add example - Add Myles to acks for raising whatwg#4315. - Tighten definition of JS and JSON module script a bit
- Tweaks to <script> element stuff - Add example - Add Myles to acks for raising whatwg#4315. - Tighten definition of JS and JSON module script a bit
This commit adds JSON modules as a single default export, with parse errors checked before instantiating the module graph. As infrastructure, this divides the "module script" concept into "JavaScript module scripts" and "JSON module scripts". Most of the spec's existing uses of "module script" become "JavaScript module script". JSON module scripts are fetched in the same way as JavaScript module scripts, e.g. with the "cors" mode and using strict MIME type checking. They use the Synthetic Module Record defined in whatwg/webidl#722. Closes #4315. Closes WICG/webcomponents#770.
This patch provides CSS modules as a single default export, of a CSSStyleSheet object, which is not added to the document. Edge cases which I didn't see discussed elsewhere: - @imports are recursively fetched together with the module graph, blocking script execution. Network errors reached prevent the execution of the entire module graph. - Any MIME type whose essence is "text/css" is accepted; this appears to be weaker checking than elsewhere in the specification. - Although the Constructable Stylesheet Objects proposal is used for infrastructure, the resulting CSSStyleSheet object acts as if it were not constructed (i.e., you can't call the replace() method). Note, the Constructable Stylesheet Objects proposal makes important steps to specifying loading of @import, but there may still be room for more precise plumbing with HTML. This text ensures that style sheet module scripts have a base URL and fetch options, which might be referenced by the definition of @import in the future. This patch is based on tc39/proposal-built-in-modules#44 Closes whatwg#4315 Closes WICG/webcomponents#759
Once someone takes the time to resolve WICG/webcomponents#839 I'm sure the conversation will find its way back to this repository somehow. Not sure we need a new tracking issue or reopen this one. |
Seems like treating JSON file content as a With JSON files containing content other than objects, for example true null "foo" 123 [1, "2", true] named imports don't work (obviously). The only way to keep usage consistent with JSON features is a default export, then in their code people can choose to detect whether the import is a boolean, an object, or something else, or what properties they want to pluck from the default import in case it is an object. import pkg from './package.json'
import child from 'child_process'
child.exec(pkg.scripts['build:dev'], ...) If we don't mind being less consistent across JSON features, then the next best option is for But the question is, is consistency across the JSON features important? Most JSON values are objects, and most keys in those objects are valid identifiers. |
- The pseudo JSON files are renamed to ".json.js" - There's another way to get JSON: use JS module's `import` directive. But the module cannot be loaded from `localhost` (due to CORS). So we keep the good old non-module JS so that we can run the ebook locally.
Reland JSON modules (#4315), originally found in db03474 but removed in a530f6f. Importing a JSON module now requires the module type to be specified at every import site with an import assertion, addressing the security concern that led to the revert. Additionally, some of the infrastructure now lives in the TC39 proposal at https://github.com/tc39/proposal-json-modules, and 3d45584 introduced the import assertions integration, so overall this patch is pretty small.
Reland JSON modules (whatwg#4315), originally found in db03474 but removed in a530f6f. Importing a JSON module now requires the module type to be specified at every import site with an import assertion, addressing the security concern that led to the revert. Additionally, some of the infrastructure now lives in the TC39 proposal at https://github.com/tc39/proposal-json-modules, and 3d45584 introduced the import assertions integration, so overall this patch is pretty small.
Hey All,
I'd like to explore support for importing
json
, similar to howNode.js
currently supportrequire('./some.json')
Expected behavior:
imported json would export an object or array with the content from the provided json file.
Why:
Currently the was to get JSON is via fetch +
import.meta.url
... and requires a bit of back and forth... the eventual result is a promise to resolve to the json object. Being able to statically import and parse json would allow us to import specific symbols, export symbols, and asynchronously get resources during the fetch phase.What are the next steps?:
I'm assuming a spec change in the HTML spec, but unsure what else we would need to do. Thoughts?
The text was updated successfully, but these errors were encountered: