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
63 changes: 53 additions & 10 deletions docs/ns-builtin-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,11 @@ const stale = getLoadedModuleUrls().filter((url) => url.includes("/src/"));
invalidateModules(stale);
```

`getLoadedModuleUrls()` reports the **URL-like** keys only: registry entries
that are `blob:`-prefixed or contain `://`. A module keyed by a plain
filesystem path is not in the result, so the eviction set a dev client computes
from it covers served modules rather than the app's own bundled files.
`getLoadedModuleUrls()` reports the **URL-like** keys only: `blob:` URLs and
http(s) URLs. A file module, keyed by its filesystem path and any query it was
imported with, is never in the result, even when that query holds `://`, so the
eviction set a dev client computes from it covers served modules rather than
the app's own bundled files.

`createRequire` gives a module-relative `require` from anywhere, including an
ES module that has no `__filename`:
Expand Down Expand Up @@ -565,7 +566,8 @@ ending in `/` must have a target ending in `/`.

A **scope key is matched as a plain prefix of the importing module's canonical
registry key** — an absolute `http(s)` URL for a served module, or a canonical
absolute path for a file on disk. That key is this runtime's analogue of the
absolute path for a file on disk, without the query a file module may have been
imported with. That key is this runtime's analogue of the
web's resolved referrer URL, which is what scope prefixes match in a browser.
An `http(s)` scope key is spelled the way registry keys are (see
[Registry canonicalization](#registry-canonicalization)) when the map is
Expand Down Expand Up @@ -811,16 +813,57 @@ 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
scheme (`data:`, `file:/x`) is never looked up on disk. 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()`.
From a file module, `import 'pkg?x'` and `import('pkg?x')` both look `pkg?x`
up in the import map, exactly as written: an entry for `pkg` does not match it,
while a `pkg/` prefix entry matches `pkg/x.js?v=1`. A query or fragment is
dropped only when a file path is derived, so `./x.js?v=1` and `./x.js#f` load
`x.js`.

#### File module identity

As in browsers and Node, the query is part of a file module's identity:
`./x.js?v=1`, `./x.js?v=2` and `./x.js` are three instances of one file, each
evaluated once, whether imported statically or with `import()`; an import-map
target's query counts the same way. `./x.js?` is a fourth: the URL standard
keeps an empty query distinct from none. The fragment is not part of the
identity — `./x.js#a` and `./x.js#b` are the `./x.js` instance — matching how
this loader keys http(s) modules. The query is kept as the URL parser
serializes it.

Inside such a module, `import.meta.url` carries the query
(`file://<app root>/x.js?v=1`) while `import.meta.dirname` does not, and
relative imports, `createRequire(import.meta.url)`, `new Worker('./w.js')` and
import-map scopes resolve exactly as for the module without a query. Anything
that names a file without a specifier — the entry module, a worker's script,
`require()` of an ES module — loads the instance without a query. The code
cache is per file, shared by every instance. File modules are not listed by
`getLoadedModuleUrls()`, with or without a query, and `invalidateModules()`
given a `file://` URL with a query drops only that instance.

A module's `file://` URL spells its path verbatim after `file://`, so a `#` in
a directory or file name is part of the path there, not a fragment: a relative
`import()`, `createRequire(import.meta.url)` and `new Worker('./w.js')` from a
script under `a#b/` resolve inside `a#b/`. A `?` in a file name is not
supported: wherever a module's key or URL is read as a path, a `?` starts its
query.

`import()` resolves its specifier once, by the same rules and against the same
importer as a static import from the module that calls it, so the import map is
applied once: an entry's target is never looked up in the map again, and a
`./x` or `../x` that names no file is reported as written
(`Cannot find module './x' (tried <path>)`), as a static import reports it.

`import()` of a `blob:` URL loads from the blob store, from any importer. The
store is looked up the way the File API resolves a blob URL: without the
fragment and with the query, so `blobUrl + '#f'` loads the blob and
`blobUrl + '?x'` rejects with `Blob not found`. A blob URL with and without a
fragment is one module. A static import of a `blob:` URL does not resolve.

### The `require()` specifier

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.__importPinConcurrentEvaluations = (globalThis.__importPinConcurrentEvaluations || 0) + 1;
export const name = "concurrent";
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/esm/import-pin/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "name": "json" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "dir-index";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.load = function (spec) {
return import(spec);
};
7 changes: 7 additions & 0 deletions test-app/app/src/main/assets/app/esm/import-pin/importer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function load(spec, options) {
return options === undefined ? import(spec) : import(spec, options);
}

export function evalLoad(spec) {
return eval("import(spec)");
}
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/esm/import-pin/leaf.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "leaf";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { missingExport } from "./leaf.mjs";
export const name = "link-export" + missingExport;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import "./missing-dependency.mjs";
export const name = "link-missing";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = ;
2 changes: 2 additions & 0 deletions test-app/app/src/main/assets/app/esm/import-pin/throws.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const name = "throws";
throw new TypeError("import-pin evaluation error");
2 changes: 2 additions & 0 deletions test-app/app/src/main/assets/app/esm/import-pin/tla.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await Promise.resolve();
export const name = "tla";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const counts = (globalThis.__queryIdentityEvaluations ??= {});
counts[import.meta.url] = (counts[import.meta.url] || 0) + 1;

export const url = import.meta.url;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load(spec) {
return import(spec);
}
20 changes: 20 additions & 0 deletions test-app/app/src/main/assets/app/esm/query-identity/probe.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createRequire } from "ns:module";
import { name as siblingName } from "./sibling.mjs";

export const url = import.meta.url;
export const search = new URL(import.meta.url).search;
export const dirname = import.meta.dirname;
export const staticSibling = siblingName;
export const required = createRequire(import.meta.url)("./sibling-cjs.js").name;

export function dynamicSibling() {
return import("./sibling.mjs").then((m) => m.name);
}

export function scopedImport() {
return import("ns-query-identity-scoped").then((m) => m.name);
}

export function startWorker() {
return new Worker("./query-worker.js");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postMessage("query-worker-ready");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "scoped-target";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.name = "sibling-cjs";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "sibling";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-identity-mapped";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "./counted.mjs?v=1";
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/esm/query-map/dir/sub.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "sub";
1 change: 1 addition & 0 deletions test-app/app/src/main/assets/app/esm/query-map/file.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const name = "file";
3 changes: 3 additions & 0 deletions test-app/app/src/main/assets/app/esm/query-map/importer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function load(spec) {
return import(spec);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as query from "./file.mjs?v=1";
export * as fragment from "./file.mjs#frag";
export * as tilde from "~/esm/query-map/file.mjs?v=1";
export * as dotsInQuery from "./file.mjs?from=/../../nope";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-http/query.mjs?k=pfx";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-dir/sub.mjs?x";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-scoped?x";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as dep from "ns-query-map-unmapped?x";
2 changes: 2 additions & 0 deletions test-app/app/src/main/assets/app/mainpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ require('./tests/testRuntimeBoundDispatch');
require('./tests/testConcurrentProxyGeneration');
// Fetches from the in-app loopback fixture server, so it goes last
require('./tests/testEsmHttpLoader');
// What import() settles with, per importer kind and specifier shape
require('./tests/testDynamicImport');
// Node-API addon surface
require('./tests/NapiTests');
require('./tests/NapiCoverageTests');
Expand Down
6 changes: 6 additions & 0 deletions test-app/app/src/main/assets/app/tests/testCreateRequire.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe("createRequire", function () {
expect(req("./target.js").tag).toBe("createrequire-target");
});

it("ignores a URL object's fragment", function () {
var req = nsModule.createRequire(
new URL("file://" + fixtureDir + "/anything.js#fragment/child"));
expect(req("./target.js").tag).toBe("createrequire-target");
});

it("still resolves ~ specifiers against the app root", function () {
var req = nsModule.createRequire(fixtureDir + "/anything.js");
expect(req("~/esm/createrequire/target.js").tag).toBe("createrequire-target");
Expand Down
Loading