Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ consults the most specific matching scope first (longest prefix wins), then
progressively less specific ones, then `imports` — so a scope can override a
global mapping for one subtree and fall through to it everywhere else. The
synchronous resolver, the graph walk, and `import()` all resolve through the
same cascade.
same cascade. The map rewrites only the specifiers a module imports, never the
importing module's own URL.

The whole map is parsed and validated before any of it is installed: a
rejected map throws a `TypeError` out of `configureLoader` and the previously
Expand Down Expand Up @@ -744,6 +745,49 @@ unmodified where a shim exists:

## Loading ES modules

### Specifier resolution

A module loaded over http(s) resolves the specifiers it imports, with `import`
and `import()` alike, as below. `ns:`/`node:` builtins resolve first, and the
[import map](#import-maps-and-scopes) is consulted next; the table covers a
specifier the map does not match.

| specifier | from an http(s) importer |
|---|---|
| `./x`, `../x` | the importer's URL, query kept |
| `/x` | the importer's origin, query kept |
| `//host/x` | the importer's scheme, query kept |
| `http(s)://…` | that URL |
| `file:///app/x` | the app root, on disk |
| `~/x` | refused |
| bare (`pkg`, `pkg/x`) | refused: it needs an import-map entry |

Only `/`, `./` and `../` prefixes are relative: `.x`, `.` and `..` are bare
specifiers and are refused without an import-map entry. Specifiers follow the
HTML module-specifier rules: the WHATWG URL parser, the one behind
`new URL()`, decides whether a specifier is relative, an absolute URL or bare,
and produces the path, query and fragment, so dot segments and `\` resolve and
the scheme is lowercased as in a browser. The authority (userinfo, host and
port) keeps its spelling from the importer's URL, or from the specifier when it
names one (`//host/x`, `http(s)://…`). A specifier that resolves to an http(s)
URL therefore names the same module as `new URL(specifier, importerUrl).href`
whenever that authority is already spelled the way `new URL()` spells it (a
lowercase host, no default port).

An import-map entry is never refused — it is explicit configuration, so it may
point an HTTP module at a `~/` path — though a relative or root-relative target
still resolves against the importer's URL. A specifier with any other URL
scheme (`data:`, `file:/x`) is never looked up on disk, and `import()` of a
`blob:nativescript/…` URL loads from the blob store. A refused specifier fails
before anything is fetched or read from disk, with an `Error` whose message
names the specifier, the importer and what to use instead;
`createRequire(<file path>)` is the explicit way for an HTTP module to reach
CommonJS or ES modules on disk.

From a file module, a static `import 'pkg?x'` and `import('pkg?x')` alike look
`pkg?x` up in the import map as written. An import-map target is never looked
up in the map a second time, by a static import or by `import()`.

### The `require()` specifier

Every `require` — the global one and any minted by `createRequire` /
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_bareDynamic = true;
export const name = "bare-dynamic";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_bareStatic = true;
export const name = "bare-static";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "file-url";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "mapped";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_selfMappedBare = true;
export const name = "self-mapped-bare";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_selfMappedTilde = true;
export const name = "self-mapped-tilde";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped";
2 changes: 2 additions & 0 deletions test-app/app/src/main/assets/app/esm/http-importer/tilde.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__nsHttpImporterDiskRead_tilde = true;
export const name = "tilde";
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/esm/query-map/literal.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "literal";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-pkg?x";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "stripped";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-bare";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const url = import.meta.url;
export function load(spec) {
return import(spec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-sibling";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-file-tilde";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-bare";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const url = import.meta.url;
export function load(spec) {
return import(spec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-sibling";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "self-mapped-prefix-tilde";
Loading