Add transform support for JSON modules imports#16172
Add transform support for JSON modules imports#16172nicolo-ribaudo merged 8 commits intobabel:mainfrom
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/56344 |
| @@ -0,0 +1,6 @@ | |||
| // TODO: Not supported yet | |||
There was a problem hiding this comment.
I'm not sure how to support this, given that:
- if it's not a JSON import, we need to fallback to an import with two arguments
- that is not known statically, so we need to always emit code for that fallback
- the browser might only support import with a single argument
I'd not block this feature on solving that problem, but rather document it as a known limitation.
400b2ab to
ee49aa4
Compare
ee49aa4 to
a71f8f8
Compare
There was a problem hiding this comment.
The transform looks good to me.
Can you add a test case of integration between this plugin and polyfill providers? For example the web output depends on the fetch API, if users specify chrome: 41 as target, the Fetch API should be polyfilled then.
As for docs, can you also add docs for the proposal-import-wasm-source plugin?
| require("fs").readFileSync(require.resolve(${specifier})) | ||
| `); | ||
| buildFetchAsync = specifier => template.expression.ast` | ||
| require("fs").promises.readFile(require.resolve(${specifier})) |
There was a problem hiding this comment.
The fs Promises API is available on Node.js 10. If we would like to support older versions, we should manually promisify the callback-style fs.readFile call. Ideally we can output the most compatible form based on the input target.
https://babeljs.io/docs/babel-plugin-proposal-import-wasm-source 😛 |
f05faa9 to
f234acc
Compare
core-js does not polyfill |
aaf1b9e to
74c17a1
Compare
|
Hey @nicolo-ribaudo ! Thank you for all your hard work! ❤️ Quick question (didn't find it anywhere). Are there any plans for |
https://github.com/tc39/proposal-json-modules has been in Stage 3 for three years and we do not support it yet :)
Now that we already have some logic to converts imports to
fetch/fs.readFileforimport source, we can just reuse it. The first commit extracts that logic to a shared helper; the second commit implements transform forimport ... with { type: "json" }.