Skip to content

configureLoader: a base URL for import map values, and a guarantee for attribute-less JSON imports #623

Backlog

Description

@edusperoni

@nativescript/vite dev mode (NativeScript/NativeScript#11650, with NativeScript/nativescript-cli#6210) now boots each device from a dev server origin that the CLI writes into dev-server.json next to bundle.mjs. bundle.mjs imports that file, then calls require('ns:module').configureLoader({ importMap, ... }) and imports the boot module from that origin. Both items below have working workarounds in @nativescript/vite. They are nice to have, not blocking. Android has the same behavior, so whatever is decided should land in both runtimes.

1. A base URL for import map values

The dev server serves an origin-free import map (root-relative values such as "vue": "/node_modules/.vite/ios/deps/vue.js"), because each device reaches it at a different origin (127.0.0.1, 10.0.2.2, the Mac's end of a USB tunnel, the LAN). Import map values resolve against the importer, and the importer that configures the loader is bundle.mjs, a file. So a root-relative value resolves on disk, and scope keys must be absolute. @nativescript/vite therefore rewrites every value and scope key with the device's origin before calling configureLoader, using string operations only, since module identity is the URL as written.

Suggestion: an optional base for the map, e.g. configureLoader({ importMap, importMapBaseURL: origin }), which resolves values and scope keys against that URL instead of the importer. That matches how a browser resolves a map against the document URL.

2. JSON import without with { type: 'json' }

bundle.mjs reads the file with a dynamic import whose specifier the bundler can't see:

const file = './dev-server.json';
const { default: server } = await import(/* @vite-ignore */ file);

This works on iOS and Android today without an import attribute, and a missing file is a catchable rejection. By the spec, a JSON module needs with { type: 'json' }. Could the runtimes either document that attribute-less JSON imports keep working, or confirm that import(file, { with: { type: 'json' } }) is supported so we can switch to it? Either one stops a future spec-alignment change from silently breaking dev boot.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions