[w3c/webcomponents] JSON and CSS modules dynamic import (#835)

When preparing the initial [implementation of JSON modules](https://chromium-review.googlesource.com/c/chromium/src/+/1731108) in Blink, we noticed a potentially interesting issue with dynamic import.

The original [JSON module WPT test](https://github.com/web-platform-tests/wpt/blob/master/html/semantics/scripting-1/the-script-element/json-module/non-object.any.js) for dynamic import expected this behavior:
```JavaScript
const result = await import(`./resource.json`);
assert_equals(result, value); // where 'value' is the same as the JSON object in resource.json
```

However, given the existing behavior of Synthetic Modules with dynamic import(), `result` actually ends up being the namespace object for the imported module, and the importer needs to check the `default` property to get the actual value:
```JavaScript
const result = await import(`./resource.json`);
assert_equals(result.default, value); // where 'value' is the JSON object in resource.json
```
One can argue that this was just a bug in the initial test, and we did in fact end up [changing the test](https://github.com/web-platform-tests/wpt/commit/a8a1303f3dadc7392a426ca6525e23b6114bb066) as part of submitting the Blink code change.

It doesn't quite seem ideal ideal to make the importer dig into the namespace object's `default` property, but it is consistent with the existing semantics for default exports.  I'm not sure whether it's a serious enough problem to consider changing the semantics of how these work.  A solution would either require reimplementing JSON modules without Synthetic Modules, or extending Synthetic Modules in some way, perhaps with an addition of a custom [FinishModuleImport](https://tc39.es/ecma262/#sec-finishdynamicimport)-like callback where the implementer of the Synthetic Module can customize what gets returned from an `import()` of the module.

All of the above applies to [CSS modules](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/css-modules-v1-explainer.md) in the same way, as these too are built as a Synthetic Module with a single default export of the stylesheet.

Thoughts?
cc: @littledan @ms2ger @travisleithead @bocupp @samsebree @domenic 

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/835

Received on Monday, 9 September 2019 22:04:05 UTC