GeezerVison is showing github.com — esm: use import attributes instead of import assertions · nodejs/node@a193be3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc.js +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,10 +44,7 @@ module.exports = { | 44 | 44 | parserOptions: { | 45 | 45 | babelOptions: { | 46 | 46 | plugins: [ | 47 | | - [ | 48 | | - Module._findPath('@babel/plugin-syntax-import-attributes'), | 49 | | - { deprecatedAssertSyntax: true }, | 50 | | - ], | | 47 | + Module._findPath('@babel/plugin-syntax-import-attributes'), | 51 | 48 | ], | 52 | 49 | }, | 53 | 50 | requireConfigFile: false, | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/esm.md +11-9Lines changed: 11 additions & 9 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | <!-- YAML | 8 | 8 | added: v8.5.0 | 9 | 9 | changes: | | 10 | + - version: REPLACEME | | 11 | + pr-url: https://github.com/nodejs/node/pull/50140 | | 12 | + description: Add experimental support for import attributes. | 10 | 13 | - version: v18.19.0 | 11 | 14 | pr-url: https://github.com/nodejs/node/pull/44710 | 12 | 15 | description: Module customization hooks are executed off the main thread. | @@ -202,7 +205,7 @@ added: v12.10.0 | 202 | 205 |
| 203 | 206 | ```js | 204 | 207 | import 'data:text/javascript,console.log("hello!");'; | 205 | | -import _ from 'data:application/json,"world!"' assert { type: 'json' }; | | 208 | +import _ from 'data:application/json,"world!"' with { type: 'json' }; | 206 | 209 | ``` | 207 | 210 |
| 208 | 211 | `data:` URLs only resolve [bare specifiers][Terminology] for builtin modules | @@ -243,26 +246,25 @@ added: | 243 | 246 | - v17.1.0 | 244 | 247 | - v16.14.0 | 245 | 248 | changes: | 246 | | - - version: v18.19.0 | | 249 | + - version: REPLACEME | 247 | 250 | pr-url: https://github.com/nodejs/node/pull/50140 | 248 | 251 | description: Switch from Import Assertions to Import Attributes. | 249 | 252 | --> | 250 | 253 |
| 251 | 254 | > Stability: 1.1 - Active development | 252 | 255 |
| 253 | 256 | > This feature was previously named "Import assertions", and using the `assert` | 254 | | -> keyword instead of `with`. Because the version of V8 on this release line does | 255 | | -> not support the `with` keyword, you need to keep using `assert` to support | 256 | | -> this version of Node.js. | | 257 | +> keyword instead of `with`. Any uses in code of the prior `assert` keyword | | 258 | +> should be updated to use `with` instead. | 257 | 259 |
| 258 | 260 | The [Import Attributes proposal][] adds an inline syntax for module import | 259 | 261 | statements to pass on more information alongside the module specifier. | 260 | 262 |
| 261 | 263 | ```js | 262 | | -import fooData from './foo.json' assert { type: 'json' }; | | 264 | +import fooData from './foo.json' with { type: 'json' }; | 263 | 265 |
| 264 | 266 | const { default: barData } = | 265 | | - await import('./bar.json', { assert: { type: 'json' } }); | | 267 | + await import('./bar.json', { with: { type: 'json' } }); | 266 | 268 | ``` | 267 | 269 |
| 268 | 270 | Node.js supports the following `type` values, for which the attribute is | @@ -555,10 +557,10 @@ separate cache. | 555 | 557 | JSON files can be referenced by `import`: | 556 | 558 |
| 557 | 559 | ```js | 558 | | -import packageConfig from './package.json' assert { type: 'json' }; | | 560 | +import packageConfig from './package.json' with { type: 'json' }; | 559 | 561 | ``` | 560 | 562 |
| 561 | | -The `assert { type: 'json' }` syntax is mandatory; see [Import Attributes][]. | | 563 | +The `with { type: 'json' }` syntax is mandatory; see [Import Attributes][]. | 562 | 564 |
| 563 | 565 | The imported JSON only exposes a `default` export. There is no support for named | 564 | 566 | exports. A cache entry is created in the CommonJS cache to avoid duplication. | |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -745,6 +745,13 @@ int ProcessGlobalArgs(std::vector<std::string>* args, | 745 | 745 | "--no-harmony-import-assertions") == v8_args.end()) { | 746 | 746 | v8_args.emplace_back("--harmony-import-assertions"); | 747 | 747 | } | | 748 | + // TODO(aduh95): remove this when the harmony-import-attributes flag | | 749 | + // is removed in V8. | | 750 | + if (std::find(v8_args.begin(), | | 751 | + v8_args.end(), | | 752 | + "--no-harmony-import-attributes") == v8_args.end()) { | | 753 | + v8_args.emplace_back("--harmony-import-attributes"); | | 754 | + } | 748 | 755 |
| 749 | 756 | auto env_opts = per_process::cli_options->per_isolate->per_env; | 750 | 757 | if (std::find(v8_args.begin(), v8_args.end(), | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-assertionless-json-import.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,7 +9,7 @@ async function test() { | 9 | 9 | import('../fixtures/experimental.json'), | 10 | 10 | import( | 11 | 11 | '../fixtures/experimental.json', | 12 | | - { assert: { type: 'json' } } | | 12 | + { with: { type: 'json' } } | 13 | 13 | ), | 14 | 14 | ]); | 15 | 15 |
| @@ -24,7 +24,7 @@ async function test() { | 24 | 24 | import('../fixtures/experimental.json?test'), | 25 | 25 | import( | 26 | 26 | '../fixtures/experimental.json?test', | 27 | | - { assert: { type: 'json' } } | | 27 | + { with: { type: 'json' } } | 28 | 28 | ), | 29 | 29 | ]); | 30 | 30 |
| @@ -39,7 +39,7 @@ async function test() { | 39 | 39 | import('../fixtures/experimental.json#test'), | 40 | 40 | import( | 41 | 41 | '../fixtures/experimental.json#test', | 42 | | - { assert: { type: 'json' } } | | 42 | + { with: { type: 'json' } } | 43 | 43 | ), | 44 | 44 | ]); | 45 | 45 |
| @@ -54,7 +54,7 @@ async function test() { | 54 | 54 | import('../fixtures/experimental.json?test2#test'), | 55 | 55 | import( | 56 | 56 | '../fixtures/experimental.json?test2#test', | 57 | | - { assert: { type: 'json' } } | | 57 | + { with: { type: 'json' } } | 58 | 58 | ), | 59 | 59 | ]); | 60 | 60 |
| @@ -69,7 +69,7 @@ async function test() { | 69 | 69 | import('data:application/json,{"ofLife":42}'), | 70 | 70 | import( | 71 | 71 | 'data:application/json,{"ofLife":42}', | 72 | | - { assert: { type: 'json' } } | | 72 | + { with: { type: 'json' } } | 73 | 73 | ), | 74 | 74 | ]); | 75 | 75 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-data-urls.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,36 +60,36 @@ function createBase64URL(mime, body) { | 60 | 60 | } | 61 | 61 | { | 62 | 62 | const ns = await import('data:application/json;foo="test,"this"', | 63 | | - { assert: { type: 'json' } }); | | 63 | + { with: { type: 'json' } }); | 64 | 64 | assert.deepStrictEqual(Object.keys(ns), ['default']); | 65 | 65 | assert.strictEqual(ns.default, 'this'); | 66 | 66 | } | 67 | 67 | { | 68 | 68 | const ns = await import(`data:application/json;foo=${ | 69 | 69 | encodeURIComponent('test,') | 70 | | - },0`, { assert: { type: 'json' } }); | | 70 | + },0`, { with: { type: 'json' } }); | 71 | 71 | assert.deepStrictEqual(Object.keys(ns), ['default']); | 72 | 72 | assert.strictEqual(ns.default, 0); | 73 | 73 | } | 74 | 74 | { | 75 | 75 | await assert.rejects(async () => | 76 | 76 | import('data:application/json;foo="test,",0', | 77 | | - { assert: { type: 'json' } }), { | | 77 | + { with: { type: 'json' } }), { | 78 | 78 | name: 'SyntaxError', | 79 | 79 | message: /Unexpected end of JSON input/ | 80 | 80 | }); | 81 | 81 | } | 82 | 82 | { | 83 | 83 | const body = '{"x": 1}'; | 84 | 84 | const plainESMURL = createURL('application/json', body); | 85 | | - const ns = await import(plainESMURL, { assert: { type: 'json' } }); | | 85 | + const ns = await import(plainESMURL, { with: { type: 'json' } }); | 86 | 86 | assert.deepStrictEqual(Object.keys(ns), ['default']); | 87 | 87 | assert.strictEqual(ns.default.x, 1); | 88 | 88 | } | 89 | 89 | { | 90 | 90 | const body = '{"default": 2}'; | 91 | 91 | const plainESMURL = createURL('application/json', body); | 92 | | - const ns = await import(plainESMURL, { assert: { type: 'json' } }); | | 92 | + const ns = await import(plainESMURL, { with: { type: 'json' } }); | 93 | 93 | assert.deepStrictEqual(Object.keys(ns), ['default']); | 94 | 94 | assert.strictEqual(ns.default.default, 2); | 95 | 95 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-dynamic-import-attribute.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,7 @@ const { strictEqual } = require('assert'); | 5 | 5 | async function test() { | 6 | 6 | { | 7 | 7 | const results = await Promise.allSettled([ | 8 | | - import('../fixtures/empty.js', { assert: { type: 'json' } }), | | 8 | + import('../fixtures/empty.js', { with: { type: 'json' } }), | 9 | 9 | import('../fixtures/empty.js'), | 10 | 10 | ]); | 11 | 11 |
| @@ -16,7 +16,7 @@ async function test() { | 16 | 16 | { | 17 | 17 | const results = await Promise.allSettled([ | 18 | 18 | import('../fixtures/empty.js'), | 19 | | - import('../fixtures/empty.js', { assert: { type: 'json' } }), | | 19 | + import('../fixtures/empty.js', { with: { type: 'json' } }), | 20 | 20 | ]); | 21 | 21 |
| 22 | 22 | strictEqual(results[0].status, 'fulfilled'); | @@ -25,7 +25,7 @@ async function test() { | 25 | 25 |
| 26 | 26 | { | 27 | 27 | const results = await Promise.allSettled([ | 28 | | - import('../fixtures/empty.json', { assert: { type: 'json' } }), | | 28 | + import('../fixtures/empty.json', { with: { type: 'json' } }), | 29 | 29 | import('../fixtures/empty.json'), | 30 | 30 | ]); | 31 | 31 |
| @@ -36,7 +36,7 @@ async function test() { | 36 | 36 | { | 37 | 37 | const results = await Promise.allSettled([ | 38 | 38 | import('../fixtures/empty.json'), | 39 | | - import('../fixtures/empty.json', { assert: { type: 'json' } }), | | 39 | + import('../fixtures/empty.json', { with: { type: 'json' } }), | 40 | 40 | ]); | 41 | 41 |
| 42 | 42 | strictEqual(results[0].status, 'rejected'); | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-dynamic-import-attribute.mjs +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import { strictEqual } from 'assert'; | 3 | 3 |
| 4 | 4 | { | 5 | 5 | const results = await Promise.allSettled([ | 6 | | - import('../fixtures/empty.js', { assert: { type: 'json' } }), | | 6 | + import('../fixtures/empty.js', { with: { type: 'json' } }), | 7 | 7 | import('../fixtures/empty.js'), | 8 | 8 | ]); | 9 | 9 |
| @@ -14,7 +14,7 @@ import { strictEqual } from 'assert'; | 14 | 14 | { | 15 | 15 | const results = await Promise.allSettled([ | 16 | 16 | import('../fixtures/empty.js'), | 17 | | - import('../fixtures/empty.js', { assert: { type: 'json' } }), | | 17 | + import('../fixtures/empty.js', { with: { type: 'json' } }), | 18 | 18 | ]); | 19 | 19 |
| 20 | 20 | strictEqual(results[0].status, 'fulfilled'); | @@ -23,7 +23,7 @@ import { strictEqual } from 'assert'; | 23 | 23 |
| 24 | 24 | { | 25 | 25 | const results = await Promise.allSettled([ | 26 | | - import('../fixtures/empty.json', { assert: { type: 'json' } }), | | 26 | + import('../fixtures/empty.json', { with: { type: 'json' } }), | 27 | 27 | import('../fixtures/empty.json'), | 28 | 28 | ]); | 29 | 29 |
| @@ -34,7 +34,7 @@ import { strictEqual } from 'assert'; | 34 | 34 | { | 35 | 35 | const results = await Promise.allSettled([ | 36 | 36 | import('../fixtures/empty.json'), | 37 | | - import('../fixtures/empty.json', { assert: { type: 'json' } }), | | 37 | + import('../fixtures/empty.json', { with: { type: 'json' } }), | 38 | 38 | ]); | 39 | 39 |
| 40 | 40 | strictEqual(results[0].status, 'rejected'); | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import '../common/index.mjs'; | 2 | 2 | import { strictEqual } from 'assert'; | 3 | 3 |
| 4 | | -import secret from '../fixtures/experimental.json' assert { type: 'json' }; | | 4 | +import secret from '../fixtures/experimental.json' with { type: 'json' }; | 5 | 5 |
| 6 | 6 | strictEqual(secret.ofLife, 42); |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-import-attributes-2.mjs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import '../common/index.mjs'; | 2 | 2 | import { strictEqual } from 'assert'; | 3 | 3 |
| 4 | | -import secret0 from '../fixtures/experimental.json' assert { type: 'json' }; | | 4 | +import secret0 from '../fixtures/experimental.json' with { type: 'json' }; | 5 | 5 | const secret1 = await import('../fixtures/experimental.json', { | 6 | | - assert: { type: 'json' }, | | 6 | + with: { type: 'json' }, | 7 | 7 | }); | 8 | 8 |
| 9 | 9 | strictEqual(secret0.ofLife, 42); | |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-esm-import-attributes-3.mjs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import '../common/index.mjs'; | 2 | 2 | import { strictEqual } from 'assert'; | 3 | 3 |
| 4 | | -import secret0 from '../fixtures/experimental.json' assert { type: 'json' }; | | 4 | +import secret0 from '../fixtures/experimental.json' with { type: 'json' }; | 5 | 5 | const secret1 = await import('../fixtures/experimental.json', | 6 | | - { assert: { type: 'json' } }); | | 6 | + { with: { type: 'json' } }); | 7 | 7 |
| 8 | 8 | strictEqual(secret0.ofLife, 42); | 9 | 9 | strictEqual(secret1.default.ofLife, 42); | |
|