From ac37e554a5fa9e5844f2bc8db7eabee7a5d8b1ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sat, 12 Oct 2024 13:21:09 +0200 Subject: [PATCH] esm: mark import attributes and JSON module as stable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two proposals reached stage 4 at the October 2024 meeting. PR-URL: https://github.com/nodejs/node/pull/55333 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel Reviewed-By: Marco Ippolito Reviewed-By: Michaƫl Zasso Reviewed-By: Matteo Collina Reviewed-By: Luigi Pinca --- doc/api/esm.md | 20 +++++++++----------- lib/internal/modules/esm/translators.js | 3 +-- test/es-module/test-esm-json.mjs | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index 5e6b643036d6a5..582e5a4eea471b 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -251,14 +251,10 @@ changes: description: Switch from Import Assertions to Import Attributes. --> -> Stability: 1.1 - Active development - -> This feature was previously named "Import assertions", and using the `assert` -> keyword instead of `with`. Any uses in code of the prior `assert` keyword -> should be updated to use `with` instead. +> Stability: 2 - Stable -The [Import Attributes proposal][] adds an inline syntax for module import -statements to pass on more information alongside the module specifier. +[Import attributes][Import Attributes MDN] are an inline syntax for module +import statements to pass on more information alongside the module specifier. ```js import fooData from './foo.json' with { type: 'json' }; @@ -267,13 +263,15 @@ const { default: barData } = await import('./bar.json', { with: { type: 'json' } }); ``` -Node.js supports the following `type` values, for which the attribute is -mandatory: +Node.js only supports the `type` attribute, for which it supports the following +values: | Attribute `type` | Needed for | | ---------------- | ---------------- | | `'json'` | [JSON modules][] | +The `type: 'json'` attribute is mandatory when importing JSON modules. + ## Builtin modules [Core modules][] provide named exports of their public API. A @@ -552,7 +550,7 @@ separate cache. ## JSON modules -> Stability: 1 - Experimental +> Stability: 2 - Stable JSON files can be referenced by `import`: @@ -1090,7 +1088,7 @@ success! [Dynamic `import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import [ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration [Import Attributes]: #import-attributes -[Import Attributes proposal]: https://github.com/tc39/proposal-import-attributes +[Import Attributes MDN]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with [JSON modules]: #json-modules [Module customization hooks]: module.md#customization-hooks [Node.js Module Resolution And Loading Algorithm]: #resolution-algorithm-specification diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 2c5efa61a6936c..27e49a7ea1e503 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -304,8 +304,7 @@ translators.set('builtin', async function builtinStrategy(url) { }); // Strategy for loading a JSON file -translators.set('json', async function jsonStrategy(url, source) { - emitExperimentalWarning('Importing JSON modules'); +translators.set('json', function jsonStrategy(url, source) { assertBufferSource(source, true, 'load'); debug(`Loading JSONModule ${url}`); const pathname = StringPrototypeStartsWith(url, 'file:') ? diff --git a/test/es-module/test-esm-json.mjs b/test/es-module/test-esm-json.mjs index 422a8f717594ab..91018378872de7 100644 --- a/test/es-module/test-esm-json.mjs +++ b/test/es-module/test-esm-json.mjs @@ -14,12 +14,12 @@ describe('ESM: importing JSON', () => { assert.strictEqual(secret.ofLife, 42); }); - it('should print an experimental warning', async () => { + it('should not print an experimental warning', async () => { const { code, signal, stderr } = await spawnPromisified(execPath, [ fixtures.path('/es-modules/json-modules.mjs'), ]); - assert.match(stderr, /ExperimentalWarning: Importing JSON modules/); + assert.strictEqual(stderr, ''); assert.strictEqual(code, 0); assert.strictEqual(signal, null); });