Skip to content

Remote functions: Build fails with shared variables from <script module> #14806

@Blackwidow-sudo

Description

@Blackwidow-sudo

Describe the bug

As i've mentioned in #14682 (comment), when you share a validation schema (or something else) from a <script module> in routes/+page.svelte with your routes/login.remote.js the build will fail because the order of declarations is wrong in the output files of .svelte-kit/.

For example in my case the output looked like this:

import { z as attributes } from "./index.js";
import "@sveltejs/kit/internal/server";
import "./utils.js";
import "@sveltejs/kit";
import { f as form } from "./query.js";
import * as z from "zod";
import { init_remote_functions } from "@sveltejs/kit/internal";
const m = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  get login() {
    return login;
  }
}, Symbol.toStringTag, { value: "Module" }));
const login = form(schema, (data) => {
  console.log(data);
});
init_remote_functions(m, "src/routes/form/login.remote.js", "dth4yy");
for (const [name, fn] of Object.entries(m)) {
  fn.__.id = "dth4yy/" + name;
  fn.__.name = name;
}
const schema = z.object({ email: z.email(), password: z.string().min(8) });
function _page($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    $$renderer2.push(`<form${attributes({ ...login.preflight(schema) })}><label>Email: <input${attributes({ ...login.fields.email.as("email") }, void 0, void 0, void 0, 4)}/></label> <label>Password: <input${attributes({ ...login.fields.password.as("password") }, void 0, void 0, void 0, 4)}/></label> <button type="submit">Login</button></form>`);
  });
}
export {
  _page as _,
  m,
  schema as s
};

As you see, the schema is used in the remote form but declared later.

Reproduction

Just run npm run build in:
https://stackblitz.com/edit/sveltejs-kit-template-default-41ubmdoj?file=src%2Froutes%2F%2Bpage.svelte

Logs

❯ npm run build

> [email protected] build
> vite build

vite v7.1.12 building SSR bundle for production...
✓ 196 modules transformed.
ReferenceError [Error]: Cannot access 'schema' before initialization
    at eval (file:///home/projects/sveltejs-kit-template-default-41ubmdoj/.svelte-kit/output/server/chunks/_page.js:33:27)
    at _0x28fbd4 (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:824619)
    at _0x534aeb.run (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:825395)
    at _0x352c9a (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:840463)
    at _0x59aa5d._evaluate (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:840815)
    at _0x24fdfe (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:840103)
    at _0x59aa5d._evaluate (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:840684)
    at _0x59aa5d.evaluate (https://sveltejskittemplatedefault41ub-ijq1.w-credentialless-staticblitz.com/blitz.1db57326.js:31:837252)
    at ModuleJob.run (node:internal/modules/esm/module_job:158:3371)

Node.js v20.19.1

System Info

System:
    OS: Linux 6.6 Ubuntu 24.04.3 LTS 24.04.3 LTS (Noble Numbat)
    CPU: (16) x64 AMD Ryzen 7 7700X 8-Core Processor
    Memory: 11.64 GB / 15.17 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 22.17.1 - /home/user/.local/share/pnpm/node
    npm: 10.9.2 - /home/user/.local/share/pnpm/npm
    pnpm: 10.14.0 - /home/user/.local/share/pnpm/pnpm
  npmPackages:
    @sveltejs/adapter-node: ^5.3.2 => 5.4.0 
    @sveltejs/kit: ^2.43.2 => 2.47.3 
    @sveltejs/vite-plugin-svelte: ^6.2.0 => 6.2.1 
    svelte: ^5.39.5 => 5.41.4 
    vite: ^7.1.7 => 7.1.12

Severity

annoyance

Additional Information

No response

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