@marko/run 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,8 +33,14 @@ __export(adapter_exports, {
33
33
  default: () => adapter
34
34
  });
35
35
  module.exports = __toCommonJS(adapter_exports);
36
- var import_path = __toESM(require("path"), 1);
36
+
37
+ // scripts/importMetaURL.js
37
38
  var import_url = require("url");
39
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
40
+
41
+ // src/adapter/index.ts
42
+ var import_path = __toESM(require("path"), 1);
43
+ var import_url2 = require("url");
38
44
 
39
45
  // src/adapter/dev-server.ts
40
46
  var import_vite = require("vite");
@@ -66,7 +72,7 @@ async function createViteDevServer(config2) {
66
72
  const devServer = await (0, import_vite.createServer)({
67
73
  ...config2,
68
74
  appType: "custom",
69
- server: { middlewareMode: true }
75
+ server: { ...config2 == null ? void 0 : config2.server, middlewareMode: true }
70
76
  });
71
77
  const originalClose = devServer.close;
72
78
  devServer.close = () => {
@@ -105,7 +111,7 @@ async function parseEnv(envFile) {
105
111
  function loadEnv(envFile) {
106
112
  (0, import_dotenv.config)({ path: envFile });
107
113
  }
108
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
114
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
109
115
  if (port <= 0) {
110
116
  port = await getAvailablePort();
111
117
  }
@@ -115,7 +121,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
115
121
  const proc = import_child_process.default.spawn(cmd, args, {
116
122
  cwd,
117
123
  shell: true,
118
- stdio: "inherit",
124
+ stdio,
119
125
  windowsHide: true,
120
126
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
121
127
  });
@@ -124,7 +130,10 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
124
130
  proc.kill();
125
131
  };
126
132
  try {
127
- await waitForServer(port, wait);
133
+ await Promise.race([
134
+ waitForError(proc, port),
135
+ waitForServer(port, wait)
136
+ ]);
128
137
  } catch (err) {
129
138
  close();
130
139
  throw err;
@@ -161,6 +170,14 @@ async function spawnServerWorker(module2, args = [], port = 0, env) {
161
170
  import_cluster.default.settings.execArgv = originalArgs;
162
171
  }
163
172
  }
173
+ async function waitForError(proc, port) {
174
+ return new Promise((_, reject) => {
175
+ proc.once("error", reject);
176
+ proc.once("exit", (code) => {
177
+ reject(new Error(`Process exited with code ${code} while waiting for server to start on port "${port}".`));
178
+ });
179
+ });
180
+ }
164
181
  async function waitForServer(port, wait = 0) {
165
182
  let remaining = wait > 0 ? wait : Infinity;
166
183
  let connection;
@@ -170,12 +187,29 @@ async function waitForServer(port, wait = 0) {
170
187
  await sleep(100);
171
188
  } else {
172
189
  throw new Error(
173
- `site-write: timeout while wating for server to start on port "${port}".`
190
+ `Timeout while wating for server to start on port "${port}".`
174
191
  );
175
192
  }
176
193
  }
177
194
  return connection;
178
195
  }
196
+ async function waitForWorker(worker, port) {
197
+ return new Promise((resolve, reject) => {
198
+ function listening(address) {
199
+ if (address.port === port) {
200
+ worker.off("listening", listening);
201
+ resolve();
202
+ }
203
+ }
204
+ worker.on("listening", listening).once("error", reject).once("exit", (code) => {
205
+ reject(
206
+ new Error(
207
+ `Worker exited with code ${code} while waiting for dev server to start on port "${port}".`
208
+ )
209
+ );
210
+ });
211
+ });
212
+ }
179
213
  async function getConnection(port) {
180
214
  return new Promise((resolve) => {
181
215
  const connection = import_net.default.connect(port).setNoDelay(true).setKeepAlive(false).on("error", () => {
@@ -200,8 +234,7 @@ function sleep(ms) {
200
234
 
201
235
  // src/adapter/index.ts
202
236
  var import_parse_node_args = __toESM(require("parse-node-args"), 1);
203
- var import_meta = {};
204
- var __dirname = (0, import_url.fileURLToPath)(new URL(".", import_meta.url));
237
+ var __dirname = import_path.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
205
238
  var defaultEntry = import_path.default.join(__dirname, "default-entry");
206
239
  var loadDevWorker = import_path.default.join(__dirname, "load-dev-worker.mjs");
207
240
  function adapter() {
@@ -275,17 +308,6 @@ function adapter() {
275
308
  }
276
309
  };
277
310
  }
278
- async function waitForWorker(worker, port) {
279
- return new Promise((resolve) => {
280
- function listening(address) {
281
- if (address.port === port) {
282
- worker.off("listening", listening);
283
- resolve();
284
- }
285
- }
286
- worker.on("listening", listening);
287
- });
288
- }
289
311
  // Annotate the CommonJS export names for ESM import in node:
290
312
  0 && (module.exports = {
291
313
  activeDevServers,
@@ -32,7 +32,7 @@ async function createViteDevServer(config2) {
32
32
  const devServer = await createServer({
33
33
  ...config2,
34
34
  appType: "custom",
35
- server: { middlewareMode: true }
35
+ server: { ...config2 == null ? void 0 : config2.server, middlewareMode: true }
36
36
  });
37
37
  const originalClose = devServer.close;
38
38
  devServer.close = () => {
@@ -71,7 +71,7 @@ async function parseEnv(envFile) {
71
71
  function loadEnv(envFile) {
72
72
  config({ path: envFile });
73
73
  }
74
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
74
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
75
75
  if (port <= 0) {
76
76
  port = await getAvailablePort();
77
77
  }
@@ -81,7 +81,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
81
81
  const proc = cp.spawn(cmd, args, {
82
82
  cwd,
83
83
  shell: true,
84
- stdio: "inherit",
84
+ stdio,
85
85
  windowsHide: true,
86
86
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
87
87
  });
@@ -90,7 +90,10 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
90
90
  proc.kill();
91
91
  };
92
92
  try {
93
- await waitForServer(port, wait);
93
+ await Promise.race([
94
+ waitForError(proc, port),
95
+ waitForServer(port, wait)
96
+ ]);
94
97
  } catch (err) {
95
98
  close();
96
99
  throw err;
@@ -127,6 +130,14 @@ async function spawnServerWorker(module, args = [], port = 0, env) {
127
130
  cluster.settings.execArgv = originalArgs;
128
131
  }
129
132
  }
133
+ async function waitForError(proc, port) {
134
+ return new Promise((_, reject) => {
135
+ proc.once("error", reject);
136
+ proc.once("exit", (code) => {
137
+ reject(new Error(`Process exited with code ${code} while waiting for server to start on port "${port}".`));
138
+ });
139
+ });
140
+ }
130
141
  async function waitForServer(port, wait = 0) {
131
142
  let remaining = wait > 0 ? wait : Infinity;
132
143
  let connection;
@@ -136,12 +147,29 @@ async function waitForServer(port, wait = 0) {
136
147
  await sleep(100);
137
148
  } else {
138
149
  throw new Error(
139
- `site-write: timeout while wating for server to start on port "${port}".`
150
+ `Timeout while wating for server to start on port "${port}".`
140
151
  );
141
152
  }
142
153
  }
143
154
  return connection;
144
155
  }
156
+ async function waitForWorker(worker, port) {
157
+ return new Promise((resolve, reject) => {
158
+ function listening(address) {
159
+ if (address.port === port) {
160
+ worker.off("listening", listening);
161
+ resolve();
162
+ }
163
+ }
164
+ worker.on("listening", listening).once("error", reject).once("exit", (code) => {
165
+ reject(
166
+ new Error(
167
+ `Worker exited with code ${code} while waiting for dev server to start on port "${port}".`
168
+ )
169
+ );
170
+ });
171
+ });
172
+ }
145
173
  async function getConnection(port) {
146
174
  return new Promise((resolve) => {
147
175
  const connection = net.connect(port).setNoDelay(true).setKeepAlive(false).on("error", () => {
@@ -166,7 +194,7 @@ function sleep(ms) {
166
194
 
167
195
  // src/adapter/index.ts
168
196
  import parseNodeArgs from "parse-node-args";
169
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
197
+ var __dirname = path.dirname(fileURLToPath(import.meta.url));
170
198
  var defaultEntry = path.join(__dirname, "default-entry");
171
199
  var loadDevWorker = path.join(__dirname, "load-dev-worker.mjs");
172
200
  function adapter() {
@@ -240,17 +268,6 @@ function adapter() {
240
268
  }
241
269
  };
242
270
  }
243
- async function waitForWorker(worker, port) {
244
- return new Promise((resolve) => {
245
- function listening(address) {
246
- if (address.port === port) {
247
- worker.off("listening", listening);
248
- resolve();
249
- }
250
- }
251
- worker.on("listening", listening);
252
- });
253
- }
254
271
  export {
255
272
  activeDevServers,
256
273
  createDevServer,
@@ -14,9 +14,10 @@ process
14
14
  .send("ready");
15
15
 
16
16
  async function start(entry, config) {
17
+ globalThis.__marko_run_vite_config__ = config;
17
18
  let changed = false;
18
- const loader = await createServer(config);
19
- ({ activeDevServers } = await loader.ssrLoadModule("@marko/run/adapter"));
19
+ const loader = await createServer({ ...config, ssr: { external: ['@marko/run/router'] }});
20
+ ({ activeDevServers } = await import("@marko/run/adapter"));
20
21
  await loader.ssrLoadModule(entry);
21
22
 
22
23
  loader.watcher.on("change", (path) => {
@@ -21,10 +21,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var middleware_exports = {};
22
22
  __export(middleware_exports, {
23
23
  createMiddleware: () => createMiddleware,
24
- getOrigin: () => getOrigin
24
+ getOrigin: () => getOrigin,
25
+ setResponseHeaders: () => setResponseHeaders
25
26
  });
26
27
  module.exports = __toCommonJS(middleware_exports);
27
28
 
29
+ // scripts/importMetaURL.js
30
+ var import_url = require("url");
31
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
32
+
28
33
  // src/adapter/polyfill.ts
29
34
  var import_web = require("stream/web");
30
35
  var import_crypto = require("crypto");
@@ -110,6 +115,30 @@ function getOrigin(req, protocol, host, trustProxy) {
110
115
  }
111
116
  return `${protocol}://${host}`;
112
117
  }
118
+ var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
119
+ function setResponseHeaders(response, res) {
120
+ for (const [key, value] of response.headers) {
121
+ if (key === "set-cookie") {
122
+ let sepIndex = value.indexOf(",") + 1;
123
+ if (!sepIndex) {
124
+ res.setHeader(key, value);
125
+ } else {
126
+ let index = 0;
127
+ do {
128
+ const valuePart = value.slice(index, sepIndex - 1);
129
+ if (!inExpiresDateRgs.test(valuePart)) {
130
+ res.appendHeader(key, valuePart);
131
+ index = sepIndex;
132
+ }
133
+ sepIndex = value.indexOf(",", sepIndex) + 1;
134
+ } while (sepIndex);
135
+ res.appendHeader(key, value.slice(index));
136
+ }
137
+ } else {
138
+ res.setHeader(key, value);
139
+ }
140
+ }
141
+ }
113
142
  function createMiddleware(fetch2, options = {}) {
114
143
  const { trustProxy = process.env.TRUST_PROXY === "1", devServer } = options;
115
144
  let { origin = process.env.ORIGIN } = options;
@@ -157,24 +186,7 @@ function createMiddleware(fetch2, options = {}) {
157
186
  return;
158
187
  }
159
188
  res.statusCode = response.status;
160
- for (const [key, value] of response.headers) {
161
- if (key === "set-cookie") {
162
- let sepIndex = value.indexOf(",") + 1;
163
- if (!sepIndex) {
164
- res.setHeader(key, value);
165
- } else {
166
- let index = 0;
167
- do {
168
- res.appendHeader(key, value.slice(index, sepIndex - 1));
169
- index = sepIndex;
170
- sepIndex = value.indexOf(",", sepIndex) + 1;
171
- } while (sepIndex);
172
- res.appendHeader(key, value.slice(index));
173
- }
174
- } else {
175
- res.setHeader(key, value);
176
- }
177
- }
189
+ setResponseHeaders(response, res);
178
190
  if (!response.body) {
179
191
  if (!response.headers.has("content-length")) {
180
192
  res.setHeader("content-length", "0");
@@ -233,5 +245,6 @@ function createMiddleware(fetch2, options = {}) {
233
245
  // Annotate the CommonJS export names for ESM import in node:
234
246
  0 && (module.exports = {
235
247
  createMiddleware,
236
- getOrigin
248
+ getOrigin,
249
+ setResponseHeaders
237
250
  });
@@ -2,6 +2,7 @@
2
2
  import type { Fetch } from "../runtime";
3
3
  import type { IncomingMessage, ServerResponse } from "http";
4
4
  import type { ViteDevServer } from "vite";
5
+ import { OutgoingMessage } from "http";
5
6
  declare module "net" {
6
7
  interface Socket {
7
8
  encrypted?: boolean;
@@ -48,6 +49,7 @@ export interface NodeAdapterOptions {
48
49
  devServer?: ViteDevServer;
49
50
  }
50
51
  export declare function getOrigin(req: IncomingMessage, protocol?: string, host?: string, trustProxy?: boolean): string;
52
+ export declare function setResponseHeaders(response: Response, res: OutgoingMessage): void;
51
53
  /**
52
54
  * Creates a request handler to be passed to http.createServer() or used as a
53
55
  * middleware in Connect-style frameworks like Express.
@@ -83,6 +83,30 @@ function getOrigin(req, protocol, host, trustProxy) {
83
83
  }
84
84
  return `${protocol}://${host}`;
85
85
  }
86
+ var inExpiresDateRgs = /Expires\s*=\s*(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s*$/i;
87
+ function setResponseHeaders(response, res) {
88
+ for (const [key, value] of response.headers) {
89
+ if (key === "set-cookie") {
90
+ let sepIndex = value.indexOf(",") + 1;
91
+ if (!sepIndex) {
92
+ res.setHeader(key, value);
93
+ } else {
94
+ let index = 0;
95
+ do {
96
+ const valuePart = value.slice(index, sepIndex - 1);
97
+ if (!inExpiresDateRgs.test(valuePart)) {
98
+ res.appendHeader(key, valuePart);
99
+ index = sepIndex;
100
+ }
101
+ sepIndex = value.indexOf(",", sepIndex) + 1;
102
+ } while (sepIndex);
103
+ res.appendHeader(key, value.slice(index));
104
+ }
105
+ } else {
106
+ res.setHeader(key, value);
107
+ }
108
+ }
109
+ }
86
110
  function createMiddleware(fetch2, options = {}) {
87
111
  const { trustProxy = process.env.TRUST_PROXY === "1", devServer } = options;
88
112
  let { origin = process.env.ORIGIN } = options;
@@ -130,24 +154,7 @@ function createMiddleware(fetch2, options = {}) {
130
154
  return;
131
155
  }
132
156
  res.statusCode = response.status;
133
- for (const [key, value] of response.headers) {
134
- if (key === "set-cookie") {
135
- let sepIndex = value.indexOf(",") + 1;
136
- if (!sepIndex) {
137
- res.setHeader(key, value);
138
- } else {
139
- let index = 0;
140
- do {
141
- res.appendHeader(key, value.slice(index, sepIndex - 1));
142
- index = sepIndex;
143
- sepIndex = value.indexOf(",", sepIndex) + 1;
144
- } while (sepIndex);
145
- res.appendHeader(key, value.slice(index));
146
- }
147
- } else {
148
- res.setHeader(key, value);
149
- }
150
- }
157
+ setResponseHeaders(response, res);
151
158
  if (!response.body) {
152
159
  if (!response.headers.has("content-length")) {
153
160
  res.setHeader("content-length", "0");
@@ -205,5 +212,6 @@ function createMiddleware(fetch2, options = {}) {
205
212
  }
206
213
  export {
207
214
  createMiddleware,
208
- getOrigin
215
+ getOrigin,
216
+ setResponseHeaders
209
217
  };
@@ -78,23 +78,31 @@ var HttpVerbColors = {
78
78
 
79
79
  // src/vite/plugin.ts
80
80
  import { fileURLToPath } from "url";
81
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
81
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
82
82
  var POSIX_SEP = "/";
83
83
  var WINDOWS_SEP = "\\";
84
84
  var normalizePath = path2.sep === WINDOWS_SEP ? (id) => id.replace(/\\/g, POSIX_SEP) : (id) => id;
85
+ async function getPackageData(dir) {
86
+ do {
87
+ const pkgPath = path2.join(dir, "package.json");
88
+ if (fs2.existsSync(pkgPath)) {
89
+ return JSON.parse(await fs2.promises.readFile(pkgPath, "utf-8"));
90
+ }
91
+ } while (dir !== (dir = path2.dirname(dir)));
92
+ return null;
93
+ }
85
94
  async function resolveAdapter(root, options, log) {
86
95
  const { adapter } = options;
87
96
  if (adapter !== void 0) {
88
97
  return adapter;
89
98
  }
90
- const { resolvePackageData } = await import("vite");
91
- const pkg = resolvePackageData(".", root);
99
+ const pkg = await getPackageData(root);
92
100
  if (pkg) {
93
- const dependecies = {
94
- ...pkg.data.dependecies,
95
- ...pkg.data.devDependencies
96
- };
97
- for (const name of Object.keys(dependecies)) {
101
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
102
+ if (pkg.devDependencies) {
103
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
104
+ }
105
+ for (const name of dependecies) {
98
106
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
99
107
  try {
100
108
  const module2 = await import(name);
@@ -115,7 +123,7 @@ async function resolveAdapter(root, options, log) {
115
123
  }
116
124
 
117
125
  // src/cli/commands.ts
118
- var __dirname2 = fileURLToPath2(new URL(".", import.meta.url));
126
+ var __dirname2 = path3.dirname(fileURLToPath2(import.meta.url));
119
127
  var defaultPort = +process.env.PORT || 3e3;
120
128
  var defaultConfigFileBases = ["serve.config", "vite.config"];
121
129
  var defaultConfigFileExts = [".js", ".cjs", ".mjs", ".ts", ".mts"];
@@ -16,3 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  // src/runtime/index.ts
17
17
  var runtime_exports = {};
18
18
  module.exports = __toCommonJS(runtime_exports);
19
+
20
+ // scripts/importMetaURL.js
21
+ var import_url = require("url");
22
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
@@ -32,6 +32,12 @@ __export(internal_exports, {
32
32
  pageResponse: () => pageResponse
33
33
  });
34
34
  module.exports = __toCommonJS(internal_exports);
35
+
36
+ // scripts/importMetaURL.js
37
+ var import_url = require("url");
38
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
39
+
40
+ // src/runtime/internal.ts
35
41
  var pageResponseInit = {
36
42
  status: 200,
37
43
  headers: { "content-type": "text/html;charset=UTF-8" }
@@ -25,6 +25,12 @@ __export(router_exports, {
25
25
  match: () => match
26
26
  });
27
27
  module.exports = __toCommonJS(router_exports);
28
+
29
+ // scripts/importMetaURL.js
30
+ var import_url = require("url");
31
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
32
+
33
+ // src/runtime/router.ts
28
34
  function fromRuntime(name) {
29
35
  return (...args) => {
30
36
  const runtime = globalThis.__marko_run__;
@@ -28,6 +28,7 @@ var vite_exports = {};
28
28
  __export(vite_exports, {
29
29
  default: () => markoRun,
30
30
  getAvailablePort: () => getAvailablePort,
31
+ getPackageData: () => getPackageData,
31
32
  isPortInUse: () => isPortInUse,
32
33
  loadEnv: () => loadEnv,
33
34
  parseEnv: () => parseEnv,
@@ -35,6 +36,10 @@ __export(vite_exports, {
35
36
  });
36
37
  module.exports = __toCommonJS(vite_exports);
37
38
 
39
+ // scripts/importMetaURL.js
40
+ var import_url = require("url");
41
+ var __importMetaURL = (0, import_url.pathToFileURL)(__filename);
42
+
38
43
  // src/vite/plugin.ts
39
44
  var import_path2 = __toESM(require("path"), 1);
40
45
  var import_crypto = __toESM(require("crypto"), 1);
@@ -180,6 +185,7 @@ async function buildRoutes(walk, basePath) {
180
185
  meta: (_e = files.get(RoutableFileTypes.Meta)) == null ? void 0 : _e[0],
181
186
  page,
182
187
  handler,
188
+ entryName: `${markoRunFilePrefix}route__${key}`,
183
189
  score: scorePath(path3, index)
184
190
  });
185
191
  for (const mw of middlewareStack) {
@@ -197,6 +203,7 @@ async function buildRoutes(walk, basePath) {
197
203
  middleware: [],
198
204
  layouts: [...layoutsStack],
199
205
  page: entries[0],
206
+ entryName: `${markoRunFilePrefix}special__${type}`,
200
207
  score: 0
201
208
  };
202
209
  }
@@ -492,7 +499,7 @@ function renderRouteTemplate(route) {
492
499
  }
493
500
  const writer = createStringWriter();
494
501
  writer.writeLines(
495
- `// ${virtualFilePrefix}/${markoRunFilePrefix}route__${route.key}.marko`
502
+ `// ${virtualFilePrefix}/${route.entryName}.marko`
496
503
  );
497
504
  writer.branch("imports");
498
505
  writer.writeLines("");
@@ -515,7 +522,7 @@ function writeRouteTemplateTag(writer, [file, ...rest], index = 1) {
515
522
  }
516
523
  function renderRouteEntry(route) {
517
524
  var _a;
518
- const { key, index, handler, page, middleware, meta } = route;
525
+ const { key, index, handler, page, middleware, meta, entryName } = route;
519
526
  const verbs = getVerbs(route);
520
527
  if (!verbs) {
521
528
  throw new Error(
@@ -524,7 +531,7 @@ function renderRouteEntry(route) {
524
531
  }
525
532
  const writer = createStringWriter();
526
533
  writer.writeLines(
527
- `// ${virtualFilePrefix}/${markoRunFilePrefix}route__${key}.js`
534
+ `// ${virtualFilePrefix}/${entryName}.js`
528
535
  );
529
536
  const imports = writer.branch("imports");
530
537
  const runtimeImports = [];
@@ -567,7 +574,7 @@ function renderRouteEntry(route) {
567
574
  }
568
575
  if (page) {
569
576
  imports.writeLines(
570
- `import page from '${virtualFilePrefix}/${markoRunFilePrefix}route__${key}.marko${serverEntryQuery}';`
577
+ `import page from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
571
578
  );
572
579
  }
573
580
  if (meta) {
@@ -672,12 +679,12 @@ function renderRouter(routes, options = {
672
679
  imports.writeLines(
673
680
  `import { ${names.join(
674
681
  ", "
675
- )} } from '${virtualFilePrefix}/${markoRunFilePrefix}route__${route.key}.js';`
682
+ )} } from '${virtualFilePrefix}/${route.entryName}.js';`
676
683
  );
677
684
  }
678
- for (const { key } of Object.values(routes.special)) {
685
+ for (const { key, entryName } of Object.values(routes.special)) {
679
686
  imports.writeLines(
680
- `import page${key} from '${virtualFilePrefix}/${markoRunFilePrefix}special__${key}.marko${serverEntryQuery}';`
687
+ `import page${key} from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
681
688
  );
682
689
  }
683
690
  writer.writeLines(
@@ -1264,13 +1271,10 @@ function logRoutesTable(routes, bundle) {
1264
1271
  }
1265
1272
  function computeRouteSize(route, bundle) {
1266
1273
  if (route.page) {
1274
+ const entryName = `${route.entryName}.marko`;
1267
1275
  for (const chunk of Object.values(bundle)) {
1268
- if (chunk.type === "chunk") {
1269
- for (const key of Object.keys(chunk.modules)) {
1270
- if (key.startsWith(route.page.filePath)) {
1271
- return computeChunkSize(chunk, bundle);
1272
- }
1273
- }
1276
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.name === entryName) {
1277
+ return computeChunkSize(chunk, bundle);
1274
1278
  }
1275
1279
  }
1276
1280
  }
@@ -1334,9 +1338,8 @@ var setExternalPluginOptions = (viteConfig, value) => setConfig(viteConfig, Plug
1334
1338
  var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterConfigKey);
1335
1339
 
1336
1340
  // src/vite/plugin.ts
1337
- var import_url = require("url");
1338
- var import_meta = {};
1339
- var __dirname = (0, import_url.fileURLToPath)(new URL(".", import_meta.url));
1341
+ var import_url2 = require("url");
1342
+ var __dirname = import_path2.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
1340
1343
  var markoExt = ".marko";
1341
1344
  var POSIX_SEP = "/";
1342
1345
  var WINDOWS_SEP = "\\";
@@ -1345,7 +1348,7 @@ function isMarkoFile(id) {
1345
1348
  return id.endsWith(markoExt);
1346
1349
  }
1347
1350
  function markoRun(opts = {}) {
1348
- let { routesDir = "src/routes", adapter, ...markoOptions } = opts;
1351
+ let { routesDir, adapter, ...markoVitePluginOptions } = opts;
1349
1352
  let compiler;
1350
1353
  let store;
1351
1354
  let root;
@@ -1379,7 +1382,7 @@ function markoRun(opts = {}) {
1379
1382
  const filepath = import_path2.default.join(typesDir, "routes.d.ts");
1380
1383
  const data = await renderRouteTypeInfo(
1381
1384
  routes,
1382
- import_path2.default.relative(typesDir, resolvedRoutesDir),
1385
+ normalizePath(import_path2.default.relative(typesDir, resolvedRoutesDir)),
1383
1386
  adapter
1384
1387
  );
1385
1388
  if (data !== typesFile || !import_fs2.default.existsSync(filepath)) {
@@ -1452,7 +1455,7 @@ function markoRun(opts = {}) {
1452
1455
  name: "marko-run-vite:pre",
1453
1456
  enforce: "pre",
1454
1457
  async config(config2, env) {
1455
- var _a, _b, _c, _d, _e, _f;
1458
+ var _a, _b, _c, _d, _e, _f, _g;
1456
1459
  const externalPluginOptions = getExternalPluginOptions(config2);
1457
1460
  if (externalPluginOptions) {
1458
1461
  opts = (0, import_vite.mergeConfig)(opts, externalPluginOptions);
@@ -1466,11 +1469,11 @@ function markoRun(opts = {}) {
1466
1469
  config2.logLevel !== "silent" && !isBuild || isSSRBuild
1467
1470
  );
1468
1471
  if (adapter) {
1469
- const externalAdapterConfig = getExternalAdapterOptions(config2);
1470
- if (externalAdapterConfig && adapter.configure) {
1471
- adapter.configure(externalAdapterConfig);
1472
- }
1473
- const adapterOptions = await ((_b = adapter.pluginOptions) == null ? void 0 : _b.call(adapter, opts));
1472
+ (_b = adapter.configure) == null ? void 0 : _b.call(adapter, {
1473
+ ...getExternalAdapterOptions(config2),
1474
+ root
1475
+ });
1476
+ const adapterOptions = await ((_c = adapter.pluginOptions) == null ? void 0 : _c.call(adapter, opts));
1474
1477
  if (adapterOptions) {
1475
1478
  opts = (0, import_vite.mergeConfig)(opts, adapterOptions);
1476
1479
  }
@@ -1484,16 +1487,19 @@ function markoRun(opts = {}) {
1484
1487
  )
1485
1488
  }
1486
1489
  });
1487
- store = opts.store || new import_vite2.FileStore(
1490
+ routesDir = opts.routesDir || "src/routes";
1491
+ markoVitePluginOptions.store = store = opts.store || new import_vite2.FileStore(
1488
1492
  `marko-serve-vite-${import_crypto.default.createHash("SHA1").update(root).digest("hex")}`
1489
1493
  );
1494
+ markoVitePluginOptions.runtimeId = opts.runtimeId;
1495
+ markoVitePluginOptions.basePathVar = opts.basePathVar;
1490
1496
  resolvedRoutesDir = import_path2.default.resolve(root, routesDir);
1491
1497
  typesDir = import_path2.default.join(root, ".marko-run");
1492
1498
  devEntryFile = import_path2.default.join(root, "index.html");
1493
1499
  devEntryFilePosix = normalizePath(devEntryFile);
1494
- const assetsDir = ((_c = config2.build) == null ? void 0 : _c.assetsDir) || "assets";
1495
- let rollupOutputOptions = (_e = (_d = config2.build) == null ? void 0 : _d.rollupOptions) == null ? void 0 : _e.output;
1496
- if (isBuild && !isSSRBuild) {
1500
+ const assetsDir = ((_d = config2.build) == null ? void 0 : _d.assetsDir) || "assets";
1501
+ let rollupOutputOptions = (_f = (_e = config2.build) == null ? void 0 : _e.rollupOptions) == null ? void 0 : _f.output;
1502
+ if (isBuild) {
1497
1503
  const defaultRollupOutputOptions = {
1498
1504
  assetFileNames({ name }) {
1499
1505
  if (name && name.indexOf("_marko-virtual_id_") < 0) {
@@ -1513,7 +1519,7 @@ function markoRun(opts = {}) {
1513
1519
  }
1514
1520
  return `${assetsDir}/${name || "[name]"}-[hash].js`;
1515
1521
  },
1516
- chunkFileNames: `${assetsDir}/_[hash].js`
1522
+ chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
1517
1523
  };
1518
1524
  if (!rollupOutputOptions) {
1519
1525
  rollupOutputOptions = defaultRollupOutputOptions;
@@ -1544,7 +1550,7 @@ function markoRun(opts = {}) {
1544
1550
  }
1545
1551
  }
1546
1552
  };
1547
- const adapterConfig = await ((_f = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _f.call(adapter, config2));
1553
+ const adapterConfig = await ((_g = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _g.call(adapter, config2));
1548
1554
  if (adapterConfig) {
1549
1555
  pluginConfig = (0, import_vite.mergeConfig)(pluginConfig, adapterConfig);
1550
1556
  }
@@ -1655,12 +1661,7 @@ function markoRun(opts = {}) {
1655
1661
  }
1656
1662
  }
1657
1663
  },
1658
- ...(0, import_vite2.default)({
1659
- ...markoOptions,
1660
- get store() {
1661
- return store;
1662
- }
1663
- }),
1664
+ ...(0, import_vite2.default)(markoVitePluginOptions),
1664
1665
  {
1665
1666
  name: "marko-run-vite:post",
1666
1667
  enforce: "post",
@@ -1781,19 +1782,27 @@ async function ensureDir(dir) {
1781
1782
  await import_fs2.default.promises.mkdir(dir, { recursive: true });
1782
1783
  }
1783
1784
  }
1785
+ async function getPackageData(dir) {
1786
+ do {
1787
+ const pkgPath = import_path2.default.join(dir, "package.json");
1788
+ if (import_fs2.default.existsSync(pkgPath)) {
1789
+ return JSON.parse(await import_fs2.default.promises.readFile(pkgPath, "utf-8"));
1790
+ }
1791
+ } while (dir !== (dir = import_path2.default.dirname(dir)));
1792
+ return null;
1793
+ }
1784
1794
  async function resolveAdapter(root, options, log) {
1785
1795
  const { adapter } = options;
1786
1796
  if (adapter !== void 0) {
1787
1797
  return adapter;
1788
1798
  }
1789
- const { resolvePackageData } = await import("vite");
1790
- const pkg = resolvePackageData(".", root);
1799
+ const pkg = await getPackageData(root);
1791
1800
  if (pkg) {
1792
- const dependecies = {
1793
- ...pkg.data.dependecies,
1794
- ...pkg.data.devDependencies
1795
- };
1796
- for (const name of Object.keys(dependecies)) {
1801
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
1802
+ if (pkg.devDependencies) {
1803
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
1804
+ }
1805
+ for (const name of dependecies) {
1797
1806
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
1798
1807
  try {
1799
1808
  const module3 = await import(name);
@@ -1833,7 +1842,7 @@ async function parseEnv(envFile) {
1833
1842
  function loadEnv(envFile) {
1834
1843
  (0, import_dotenv.config)({ path: envFile });
1835
1844
  }
1836
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
1845
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
1837
1846
  if (port <= 0) {
1838
1847
  port = await getAvailablePort();
1839
1848
  }
@@ -1843,7 +1852,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1843
1852
  const proc = import_child_process.default.spawn(cmd, args, {
1844
1853
  cwd,
1845
1854
  shell: true,
1846
- stdio: "inherit",
1855
+ stdio,
1847
1856
  windowsHide: true,
1848
1857
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
1849
1858
  });
@@ -1852,7 +1861,10 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1852
1861
  proc.kill();
1853
1862
  };
1854
1863
  try {
1855
- await waitForServer(port, wait);
1864
+ await Promise.race([
1865
+ waitForError(proc, port),
1866
+ waitForServer(port, wait)
1867
+ ]);
1856
1868
  } catch (err) {
1857
1869
  close();
1858
1870
  throw err;
@@ -1862,6 +1874,14 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1862
1874
  close
1863
1875
  };
1864
1876
  }
1877
+ async function waitForError(proc, port) {
1878
+ return new Promise((_, reject) => {
1879
+ proc.once("error", reject);
1880
+ proc.once("exit", (code) => {
1881
+ reject(new Error(`Process exited with code ${code} while waiting for server to start on port "${port}".`));
1882
+ });
1883
+ });
1884
+ }
1865
1885
  async function waitForServer(port, wait = 0) {
1866
1886
  let remaining = wait > 0 ? wait : Infinity;
1867
1887
  let connection;
@@ -1871,7 +1891,7 @@ async function waitForServer(port, wait = 0) {
1871
1891
  await sleep(100);
1872
1892
  } else {
1873
1893
  throw new Error(
1874
- `site-write: timeout while wating for server to start on port "${port}".`
1894
+ `Timeout while wating for server to start on port "${port}".`
1875
1895
  );
1876
1896
  }
1877
1897
  }
@@ -1904,6 +1924,7 @@ function sleep(ms) {
1904
1924
  // Annotate the CommonJS export names for ESM import in node:
1905
1925
  0 && (module.exports = {
1906
1926
  getAvailablePort,
1927
+ getPackageData,
1907
1928
  isPortInUse,
1908
1929
  loadEnv,
1909
1930
  parseEnv,
@@ -1,4 +1,4 @@
1
- export { default } from "./plugin";
1
+ export { default, getPackageData } from "./plugin";
2
2
  export { getAvailablePort, isPortInUse, loadEnv, parseEnv, spawnServer, } from "./utils/server";
3
3
  export type { SpawnedServer } from "./utils/server";
4
- export type { Adapter, AdapterConfig, Options, BuiltRoutes, HttpVerb, ParamInfo, Route, RoutableFile, RoutableFileType, } from "./types";
4
+ export type { Adapter, AdapterConfig, Options, BuiltRoutes, HttpVerb, PackageData, ParamInfo, Route, RoutableFile, RoutableFileType, } from "./types";
@@ -143,6 +143,7 @@ async function buildRoutes(walk, basePath) {
143
143
  meta: (_e = files.get(RoutableFileTypes.Meta)) == null ? void 0 : _e[0],
144
144
  page,
145
145
  handler,
146
+ entryName: `${markoRunFilePrefix}route__${key}`,
146
147
  score: scorePath(path3, index)
147
148
  });
148
149
  for (const mw of middlewareStack) {
@@ -160,6 +161,7 @@ async function buildRoutes(walk, basePath) {
160
161
  middleware: [],
161
162
  layouts: [...layoutsStack],
162
163
  page: entries[0],
164
+ entryName: `${markoRunFilePrefix}special__${type}`,
163
165
  score: 0
164
166
  };
165
167
  }
@@ -455,7 +457,7 @@ function renderRouteTemplate(route) {
455
457
  }
456
458
  const writer = createStringWriter();
457
459
  writer.writeLines(
458
- `// ${virtualFilePrefix}/${markoRunFilePrefix}route__${route.key}.marko`
460
+ `// ${virtualFilePrefix}/${route.entryName}.marko`
459
461
  );
460
462
  writer.branch("imports");
461
463
  writer.writeLines("");
@@ -478,7 +480,7 @@ function writeRouteTemplateTag(writer, [file, ...rest], index = 1) {
478
480
  }
479
481
  function renderRouteEntry(route) {
480
482
  var _a;
481
- const { key, index, handler, page, middleware, meta } = route;
483
+ const { key, index, handler, page, middleware, meta, entryName } = route;
482
484
  const verbs = getVerbs(route);
483
485
  if (!verbs) {
484
486
  throw new Error(
@@ -487,7 +489,7 @@ function renderRouteEntry(route) {
487
489
  }
488
490
  const writer = createStringWriter();
489
491
  writer.writeLines(
490
- `// ${virtualFilePrefix}/${markoRunFilePrefix}route__${key}.js`
492
+ `// ${virtualFilePrefix}/${entryName}.js`
491
493
  );
492
494
  const imports = writer.branch("imports");
493
495
  const runtimeImports = [];
@@ -530,7 +532,7 @@ function renderRouteEntry(route) {
530
532
  }
531
533
  if (page) {
532
534
  imports.writeLines(
533
- `import page from '${virtualFilePrefix}/${markoRunFilePrefix}route__${key}.marko${serverEntryQuery}';`
535
+ `import page from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
534
536
  );
535
537
  }
536
538
  if (meta) {
@@ -635,12 +637,12 @@ function renderRouter(routes, options = {
635
637
  imports.writeLines(
636
638
  `import { ${names.join(
637
639
  ", "
638
- )} } from '${virtualFilePrefix}/${markoRunFilePrefix}route__${route.key}.js';`
640
+ )} } from '${virtualFilePrefix}/${route.entryName}.js';`
639
641
  );
640
642
  }
641
- for (const { key } of Object.values(routes.special)) {
643
+ for (const { key, entryName } of Object.values(routes.special)) {
642
644
  imports.writeLines(
643
- `import page${key} from '${virtualFilePrefix}/${markoRunFilePrefix}special__${key}.marko${serverEntryQuery}';`
645
+ `import page${key} from '${virtualFilePrefix}/${entryName}.marko${serverEntryQuery}';`
644
646
  );
645
647
  }
646
648
  writer.writeLines(
@@ -1227,13 +1229,10 @@ function logRoutesTable(routes, bundle) {
1227
1229
  }
1228
1230
  function computeRouteSize(route, bundle) {
1229
1231
  if (route.page) {
1232
+ const entryName = `${route.entryName}.marko`;
1230
1233
  for (const chunk of Object.values(bundle)) {
1231
- if (chunk.type === "chunk") {
1232
- for (const key of Object.keys(chunk.modules)) {
1233
- if (key.startsWith(route.page.filePath)) {
1234
- return computeChunkSize(chunk, bundle);
1235
- }
1236
- }
1234
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.name === entryName) {
1235
+ return computeChunkSize(chunk, bundle);
1237
1236
  }
1238
1237
  }
1239
1238
  }
@@ -1298,7 +1297,7 @@ var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterCon
1298
1297
 
1299
1298
  // src/vite/plugin.ts
1300
1299
  import { fileURLToPath } from "url";
1301
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
1300
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
1302
1301
  var markoExt = ".marko";
1303
1302
  var POSIX_SEP = "/";
1304
1303
  var WINDOWS_SEP = "\\";
@@ -1307,7 +1306,7 @@ function isMarkoFile(id) {
1307
1306
  return id.endsWith(markoExt);
1308
1307
  }
1309
1308
  function markoRun(opts = {}) {
1310
- let { routesDir = "src/routes", adapter, ...markoOptions } = opts;
1309
+ let { routesDir, adapter, ...markoVitePluginOptions } = opts;
1311
1310
  let compiler;
1312
1311
  let store;
1313
1312
  let root;
@@ -1341,7 +1340,7 @@ function markoRun(opts = {}) {
1341
1340
  const filepath = path2.join(typesDir, "routes.d.ts");
1342
1341
  const data = await renderRouteTypeInfo(
1343
1342
  routes,
1344
- path2.relative(typesDir, resolvedRoutesDir),
1343
+ normalizePath(path2.relative(typesDir, resolvedRoutesDir)),
1345
1344
  adapter
1346
1345
  );
1347
1346
  if (data !== typesFile || !fs2.existsSync(filepath)) {
@@ -1414,7 +1413,7 @@ function markoRun(opts = {}) {
1414
1413
  name: "marko-run-vite:pre",
1415
1414
  enforce: "pre",
1416
1415
  async config(config2, env) {
1417
- var _a, _b, _c, _d, _e, _f;
1416
+ var _a, _b, _c, _d, _e, _f, _g;
1418
1417
  const externalPluginOptions = getExternalPluginOptions(config2);
1419
1418
  if (externalPluginOptions) {
1420
1419
  opts = mergeConfig(opts, externalPluginOptions);
@@ -1428,11 +1427,11 @@ function markoRun(opts = {}) {
1428
1427
  config2.logLevel !== "silent" && !isBuild || isSSRBuild
1429
1428
  );
1430
1429
  if (adapter) {
1431
- const externalAdapterConfig = getExternalAdapterOptions(config2);
1432
- if (externalAdapterConfig && adapter.configure) {
1433
- adapter.configure(externalAdapterConfig);
1434
- }
1435
- const adapterOptions = await ((_b = adapter.pluginOptions) == null ? void 0 : _b.call(adapter, opts));
1430
+ (_b = adapter.configure) == null ? void 0 : _b.call(adapter, {
1431
+ ...getExternalAdapterOptions(config2),
1432
+ root
1433
+ });
1434
+ const adapterOptions = await ((_c = adapter.pluginOptions) == null ? void 0 : _c.call(adapter, opts));
1436
1435
  if (adapterOptions) {
1437
1436
  opts = mergeConfig(opts, adapterOptions);
1438
1437
  }
@@ -1446,16 +1445,19 @@ function markoRun(opts = {}) {
1446
1445
  )
1447
1446
  }
1448
1447
  });
1449
- store = opts.store || new FileStore(
1448
+ routesDir = opts.routesDir || "src/routes";
1449
+ markoVitePluginOptions.store = store = opts.store || new FileStore(
1450
1450
  `marko-serve-vite-${crypto.createHash("SHA1").update(root).digest("hex")}`
1451
1451
  );
1452
+ markoVitePluginOptions.runtimeId = opts.runtimeId;
1453
+ markoVitePluginOptions.basePathVar = opts.basePathVar;
1452
1454
  resolvedRoutesDir = path2.resolve(root, routesDir);
1453
1455
  typesDir = path2.join(root, ".marko-run");
1454
1456
  devEntryFile = path2.join(root, "index.html");
1455
1457
  devEntryFilePosix = normalizePath(devEntryFile);
1456
- const assetsDir = ((_c = config2.build) == null ? void 0 : _c.assetsDir) || "assets";
1457
- let rollupOutputOptions = (_e = (_d = config2.build) == null ? void 0 : _d.rollupOptions) == null ? void 0 : _e.output;
1458
- if (isBuild && !isSSRBuild) {
1458
+ const assetsDir = ((_d = config2.build) == null ? void 0 : _d.assetsDir) || "assets";
1459
+ let rollupOutputOptions = (_f = (_e = config2.build) == null ? void 0 : _e.rollupOptions) == null ? void 0 : _f.output;
1460
+ if (isBuild) {
1459
1461
  const defaultRollupOutputOptions = {
1460
1462
  assetFileNames({ name }) {
1461
1463
  if (name && name.indexOf("_marko-virtual_id_") < 0) {
@@ -1475,7 +1477,7 @@ function markoRun(opts = {}) {
1475
1477
  }
1476
1478
  return `${assetsDir}/${name || "[name]"}-[hash].js`;
1477
1479
  },
1478
- chunkFileNames: `${assetsDir}/_[hash].js`
1480
+ chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
1479
1481
  };
1480
1482
  if (!rollupOutputOptions) {
1481
1483
  rollupOutputOptions = defaultRollupOutputOptions;
@@ -1506,7 +1508,7 @@ function markoRun(opts = {}) {
1506
1508
  }
1507
1509
  }
1508
1510
  };
1509
- const adapterConfig = await ((_f = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _f.call(adapter, config2));
1511
+ const adapterConfig = await ((_g = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _g.call(adapter, config2));
1510
1512
  if (adapterConfig) {
1511
1513
  pluginConfig = mergeConfig(pluginConfig, adapterConfig);
1512
1514
  }
@@ -1617,12 +1619,7 @@ function markoRun(opts = {}) {
1617
1619
  }
1618
1620
  }
1619
1621
  },
1620
- ...markoVitePlugin({
1621
- ...markoOptions,
1622
- get store() {
1623
- return store;
1624
- }
1625
- }),
1622
+ ...markoVitePlugin(markoVitePluginOptions),
1626
1623
  {
1627
1624
  name: "marko-run-vite:post",
1628
1625
  enforce: "post",
@@ -1743,19 +1740,27 @@ async function ensureDir(dir) {
1743
1740
  await fs2.promises.mkdir(dir, { recursive: true });
1744
1741
  }
1745
1742
  }
1743
+ async function getPackageData(dir) {
1744
+ do {
1745
+ const pkgPath = path2.join(dir, "package.json");
1746
+ if (fs2.existsSync(pkgPath)) {
1747
+ return JSON.parse(await fs2.promises.readFile(pkgPath, "utf-8"));
1748
+ }
1749
+ } while (dir !== (dir = path2.dirname(dir)));
1750
+ return null;
1751
+ }
1746
1752
  async function resolveAdapter(root, options, log) {
1747
1753
  const { adapter } = options;
1748
1754
  if (adapter !== void 0) {
1749
1755
  return adapter;
1750
1756
  }
1751
- const { resolvePackageData } = await import("vite");
1752
- const pkg = resolvePackageData(".", root);
1757
+ const pkg = await getPackageData(root);
1753
1758
  if (pkg) {
1754
- const dependecies = {
1755
- ...pkg.data.dependecies,
1756
- ...pkg.data.devDependencies
1757
- };
1758
- for (const name of Object.keys(dependecies)) {
1759
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
1760
+ if (pkg.devDependencies) {
1761
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
1762
+ }
1763
+ for (const name of dependecies) {
1759
1764
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
1760
1765
  try {
1761
1766
  const module2 = await import(name);
@@ -1795,7 +1800,7 @@ async function parseEnv(envFile) {
1795
1800
  function loadEnv(envFile) {
1796
1801
  config({ path: envFile });
1797
1802
  }
1798
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
1803
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
1799
1804
  if (port <= 0) {
1800
1805
  port = await getAvailablePort();
1801
1806
  }
@@ -1805,7 +1810,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1805
1810
  const proc = cp.spawn(cmd, args, {
1806
1811
  cwd,
1807
1812
  shell: true,
1808
- stdio: "inherit",
1813
+ stdio,
1809
1814
  windowsHide: true,
1810
1815
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
1811
1816
  });
@@ -1814,7 +1819,10 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1814
1819
  proc.kill();
1815
1820
  };
1816
1821
  try {
1817
- await waitForServer(port, wait);
1822
+ await Promise.race([
1823
+ waitForError(proc, port),
1824
+ waitForServer(port, wait)
1825
+ ]);
1818
1826
  } catch (err) {
1819
1827
  close();
1820
1828
  throw err;
@@ -1824,6 +1832,14 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1824
1832
  close
1825
1833
  };
1826
1834
  }
1835
+ async function waitForError(proc, port) {
1836
+ return new Promise((_, reject) => {
1837
+ proc.once("error", reject);
1838
+ proc.once("exit", (code) => {
1839
+ reject(new Error(`Process exited with code ${code} while waiting for server to start on port "${port}".`));
1840
+ });
1841
+ });
1842
+ }
1827
1843
  async function waitForServer(port, wait = 0) {
1828
1844
  let remaining = wait > 0 ? wait : Infinity;
1829
1845
  let connection;
@@ -1833,7 +1849,7 @@ async function waitForServer(port, wait = 0) {
1833
1849
  await sleep(100);
1834
1850
  } else {
1835
1851
  throw new Error(
1836
- `site-write: timeout while wating for server to start on port "${port}".`
1852
+ `Timeout while wating for server to start on port "${port}".`
1837
1853
  );
1838
1854
  }
1839
1855
  }
@@ -1866,6 +1882,7 @@ function sleep(ms) {
1866
1882
  export {
1867
1883
  markoRun as default,
1868
1884
  getAvailablePort,
1885
+ getPackageData,
1869
1886
  isPortInUse,
1870
1887
  loadEnv,
1871
1888
  parseEnv,
@@ -1,4 +1,5 @@
1
1
  import type { Plugin } from "vite";
2
- import type { Options, Adapter } from "./types";
2
+ import type { Options, Adapter, PackageData } from "./types";
3
3
  export default function markoRun(opts?: Options): Plugin[];
4
+ export declare function getPackageData(dir: string): Promise<PackageData | null>;
4
5
  export declare function resolveAdapter(root: string, options: Options, log?: boolean): Promise<Adapter | null>;
@@ -48,6 +48,7 @@ export interface Route {
48
48
  meta?: RoutableFile;
49
49
  handler?: RoutableFile;
50
50
  page?: RoutableFile;
51
+ entryName: string;
51
52
  score: number;
52
53
  }
53
54
  export interface ParamInfo {
@@ -79,3 +80,9 @@ export interface BuiltRoutes {
79
80
  special: SpecialRoutes;
80
81
  middleware: RoutableFile[];
81
82
  }
83
+ export interface PackageData {
84
+ name?: string;
85
+ version?: string;
86
+ dependencies?: Record<string, string>;
87
+ devDependencies?: Record<string, string>;
88
+ }
@@ -1,5 +1,7 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { type Socket } from "net";
4
+ import { type ChildProcess, type StdioOptions } from "child_process";
3
5
  import { type Worker } from "cluster";
4
6
  export interface SpawnedServer {
5
7
  port: number;
@@ -7,9 +9,11 @@ export interface SpawnedServer {
7
9
  }
8
10
  export declare function parseEnv(envFile: string): Promise<import("dotenv").DotenvParseOutput | undefined>;
9
11
  export declare function loadEnv(envFile: string): void;
10
- export declare function spawnServer(cmd: string, args?: string[], port?: number, env?: string | Record<string, string>, cwd?: string, wait?: number): Promise<SpawnedServer>;
12
+ export declare function spawnServer(cmd: string, args?: string[], port?: number, env?: string | Record<string, string>, cwd?: string, wait?: number, stdio?: StdioOptions): Promise<SpawnedServer>;
11
13
  export declare function spawnServerWorker(module: string, args?: string[], port?: number, env?: string | Record<string, string>): Promise<Worker>;
14
+ export declare function waitForError(proc: ChildProcess, port: number): Promise<void>;
12
15
  export declare function waitForServer(port: number, wait?: number): Promise<Socket>;
16
+ export declare function waitForWorker(worker: Worker, port: number): Promise<void>;
13
17
  export declare function getConnection(port: number): Promise<Socket | null>;
14
18
  export declare function isPortInUse(port: number): Promise<boolean>;
15
19
  export declare function getAvailablePort(): Promise<number>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "The Marko application framework.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/marko-js/run/tree/main/packages/run",
@@ -112,7 +112,7 @@
112
112
  "typescript": "^4.7.4"
113
113
  },
114
114
  "dependencies": {
115
- "@marko/vite": "^2.3.13",
115
+ "@marko/vite": "^2.4.4",
116
116
  "cli-table3": "^0.6.3",
117
117
  "compression": "^1.7.4",
118
118
  "dotenv": "^16.0.3",