Skip to content

RFC 12: Imports for their effect #13

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: none beyond upstream PR solidjs#69.

Summary

Allow import "./styles.css": an import that binds nothing and is kept for what loading the module does. It needs no declaration file, and the compiled module imports it as written. An .rx module cannot be imported this way.

Motivation

UI kits ship a stylesheet the app has to load, and fonts and polyfills work the same way. Kobalte's examples import their CSS, and @fontsource/inter is used only as import "@fontsource/inter". In TypeScript or JSX this is one line. In .rx on upstream, import must be followed by { … } or a default name (07 §1), so a bare string is a parse error. The workaround is a JS entry file that imports the CSS and then the .rx app. That splits the module's own dependencies across two files.

Proposal

07 §1a, An import for its effect.

import "./app.css"
import "@fontsource/inter/index.css"
import { Dialog } from "./kobalte.js"

export default component App() {
  <Dialog>…</Dialog>
}
  • import "<specifier>" binds no name. It has no .d.rx (the declaration file that describes a JS module to .rx), and none is looked for. The checker adds nothing for it.
  • The compiled module contains import "<specifier>"; unchanged, in the same place as the module's other imports, so the bundler (Vite) handles it as it would in JS: it injects a stylesheet, runs a polyfill, and so on.
  • import "./other.rx" is PARSE, and the message points to import { … } from "./other.rx". An .rx module is imported only for what it exports. Its module state is per app (MO-1b), and nothing would observe "loading" it.

Drawbacks and alternatives

  • An effect import can run arbitrary JS at load time, which the checker does not see. That is already true of any JS module .rx imports. Load order follows the ES module order the bundler gives. Order-sensitive polyfills behave as they do in JS.
  • Alternative: keep such imports in a JS entry file. That works, but it is the kind of split .rx otherwise avoids, and a component library's CSS belongs with the module that uses the library.
  • Alternative: a dedicated form for stylesheets. .rx already has scoped styles (07 §6b). This is for files the program does not own.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript, commit 4b3f957, which also adds MO-11q (RFC 21). For this rule it changed:

  • ref/src/parse.ts, compiler/src/parse.rs: parse the form and refuse .rx.
  • ref/compile/emit.ts, compiler/src/emit.rs: emit it as written.

Spec: 07 §1a. Scenario: S-module-24, which accepts import "./ui.css" and import "@fontsource/inter/index.css" and refuses import "./other.rx" as PARSE. examples/kobalte imports its stylesheet this way and is built and driven in Chromium by compiler/test/kobalte.mjs. Both implementations (ref/ and the Rust crate) agree.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions