@marko/run 0.1.11 → 0.1.13

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");
@@ -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
  });
@@ -228,8 +234,7 @@ function sleep(ms) {
228
234
 
229
235
  // src/adapter/index.ts
230
236
  var import_parse_node_args = __toESM(require("parse-node-args"), 1);
231
- var import_meta = {};
232
- var __dirname = (0, import_url.fileURLToPath)(new URL(".", import_meta.url));
237
+ var __dirname = import_path.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
233
238
  var defaultEntry = import_path.default.join(__dirname, "default-entry");
234
239
  var loadDevWorker = import_path.default.join(__dirname, "load-dev-worker.mjs");
235
240
  function adapter() {
@@ -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
  });
@@ -194,7 +194,7 @@ function sleep(ms) {
194
194
 
195
195
  // src/adapter/index.ts
196
196
  import parseNodeArgs from "parse-node-args";
197
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
197
+ var __dirname = path.dirname(fileURLToPath(import.meta.url));
198
198
  var defaultEntry = path.join(__dirname, "default-entry");
199
199
  var loadDevWorker = path.join(__dirname, "load-dev-worker.mjs");
200
200
  function adapter() {
@@ -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
  };
@@ -68,6 +68,7 @@ import zlib from "node:zlib";
68
68
  import Table from "cli-table3";
69
69
  import kleur from "kleur";
70
70
  import format from "human-format";
71
+ import { Blob } from "buffer";
71
72
  var HttpVerbColors = {
72
73
  get: kleur.green,
73
74
  post: kleur.magenta,
@@ -78,23 +79,31 @@ var HttpVerbColors = {
78
79
 
79
80
  // src/vite/plugin.ts
80
81
  import { fileURLToPath } from "url";
81
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
82
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
82
83
  var POSIX_SEP = "/";
83
84
  var WINDOWS_SEP = "\\";
84
85
  var normalizePath = path2.sep === WINDOWS_SEP ? (id) => id.replace(/\\/g, POSIX_SEP) : (id) => id;
86
+ async function getPackageData(dir) {
87
+ do {
88
+ const pkgPath = path2.join(dir, "package.json");
89
+ if (fs2.existsSync(pkgPath)) {
90
+ return JSON.parse(await fs2.promises.readFile(pkgPath, "utf-8"));
91
+ }
92
+ } while (dir !== (dir = path2.dirname(dir)));
93
+ return null;
94
+ }
85
95
  async function resolveAdapter(root, options, log) {
86
96
  const { adapter } = options;
87
97
  if (adapter !== void 0) {
88
98
  return adapter;
89
99
  }
90
- const { resolvePackageData } = await import("vite");
91
- const pkg = resolvePackageData(".", root);
100
+ const pkg = await getPackageData(root);
92
101
  if (pkg) {
93
- const dependecies = {
94
- ...pkg.data.dependecies,
95
- ...pkg.data.devDependencies
96
- };
97
- for (const name of Object.keys(dependecies)) {
102
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
103
+ if (pkg.devDependencies) {
104
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
105
+ }
106
+ for (const name of dependecies) {
98
107
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
99
108
  try {
100
109
  const module2 = await import(name);
@@ -115,7 +124,7 @@ async function resolveAdapter(root, options, log) {
115
124
  }
116
125
 
117
126
  // src/cli/commands.ts
118
- var __dirname2 = fileURLToPath2(new URL(".", import.meta.url));
127
+ var __dirname2 = path3.dirname(fileURLToPath2(import.meta.url));
119
128
  var defaultPort = +process.env.PORT || 3e3;
120
129
  var defaultConfigFileBases = ["serve.config", "vite.config"];
121
130
  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(
@@ -1192,6 +1199,7 @@ var import_node_zlib = __toESM(require("node:zlib"), 1);
1192
1199
  var import_cli_table3 = __toESM(require("cli-table3"), 1);
1193
1200
  var import_kleur = __toESM(require("kleur"), 1);
1194
1201
  var import_human_format = __toESM(require("human-format"), 1);
1202
+ var import_buffer = require("buffer");
1195
1203
  var HttpVerbColors = {
1196
1204
  get: import_kleur.default.green,
1197
1205
  post: import_kleur.default.magenta,
@@ -1264,13 +1272,10 @@ function logRoutesTable(routes, bundle) {
1264
1272
  }
1265
1273
  function computeRouteSize(route, bundle) {
1266
1274
  if (route.page) {
1275
+ const entryName = `${route.entryName}.marko`;
1267
1276
  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
- }
1277
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.name === entryName) {
1278
+ return computeChunkSize(chunk, bundle);
1274
1279
  }
1275
1280
  }
1276
1281
  }
@@ -1280,7 +1285,7 @@ function gzipSize(source) {
1280
1285
  return import_node_zlib.default.gzipSync(source, { level: 9 }).length;
1281
1286
  }
1282
1287
  function byteSize(source) {
1283
- return new Blob([source]).size;
1288
+ return new import_buffer.Blob([source]).size;
1284
1289
  }
1285
1290
  function computeChunkSize(chunk, bundle, seen = /* @__PURE__ */ new Set()) {
1286
1291
  if (chunk.type === "asset") {
@@ -1334,9 +1339,8 @@ var setExternalPluginOptions = (viteConfig, value) => setConfig(viteConfig, Plug
1334
1339
  var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterConfigKey);
1335
1340
 
1336
1341
  // 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));
1342
+ var import_url2 = require("url");
1343
+ var __dirname = import_path2.default.dirname((0, import_url2.fileURLToPath)(__importMetaURL));
1340
1344
  var markoExt = ".marko";
1341
1345
  var POSIX_SEP = "/";
1342
1346
  var WINDOWS_SEP = "\\";
@@ -1345,7 +1349,7 @@ function isMarkoFile(id) {
1345
1349
  return id.endsWith(markoExt);
1346
1350
  }
1347
1351
  function markoRun(opts = {}) {
1348
- let { routesDir = "src/routes", adapter, ...markoOptions } = opts;
1352
+ let { routesDir, adapter, ...markoVitePluginOptions } = opts;
1349
1353
  let compiler;
1350
1354
  let store;
1351
1355
  let root;
@@ -1452,7 +1456,7 @@ function markoRun(opts = {}) {
1452
1456
  name: "marko-run-vite:pre",
1453
1457
  enforce: "pre",
1454
1458
  async config(config2, env) {
1455
- var _a, _b, _c, _d, _e, _f;
1459
+ var _a, _b, _c, _d, _e, _f, _g;
1456
1460
  const externalPluginOptions = getExternalPluginOptions(config2);
1457
1461
  if (externalPluginOptions) {
1458
1462
  opts = (0, import_vite.mergeConfig)(opts, externalPluginOptions);
@@ -1466,11 +1470,11 @@ function markoRun(opts = {}) {
1466
1470
  config2.logLevel !== "silent" && !isBuild || isSSRBuild
1467
1471
  );
1468
1472
  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));
1473
+ (_b = adapter.configure) == null ? void 0 : _b.call(adapter, {
1474
+ ...getExternalAdapterOptions(config2),
1475
+ root
1476
+ });
1477
+ const adapterOptions = await ((_c = adapter.pluginOptions) == null ? void 0 : _c.call(adapter, opts));
1474
1478
  if (adapterOptions) {
1475
1479
  opts = (0, import_vite.mergeConfig)(opts, adapterOptions);
1476
1480
  }
@@ -1484,16 +1488,19 @@ function markoRun(opts = {}) {
1484
1488
  )
1485
1489
  }
1486
1490
  });
1487
- store = opts.store || new import_vite2.FileStore(
1491
+ routesDir = opts.routesDir || "src/routes";
1492
+ markoVitePluginOptions.store = store = opts.store || new import_vite2.FileStore(
1488
1493
  `marko-serve-vite-${import_crypto.default.createHash("SHA1").update(root).digest("hex")}`
1489
1494
  );
1495
+ markoVitePluginOptions.runtimeId = opts.runtimeId;
1496
+ markoVitePluginOptions.basePathVar = opts.basePathVar;
1490
1497
  resolvedRoutesDir = import_path2.default.resolve(root, routesDir);
1491
1498
  typesDir = import_path2.default.join(root, ".marko-run");
1492
1499
  devEntryFile = import_path2.default.join(root, "index.html");
1493
1500
  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) {
1501
+ const assetsDir = ((_d = config2.build) == null ? void 0 : _d.assetsDir) || "assets";
1502
+ let rollupOutputOptions = (_f = (_e = config2.build) == null ? void 0 : _e.rollupOptions) == null ? void 0 : _f.output;
1503
+ if (isBuild) {
1497
1504
  const defaultRollupOutputOptions = {
1498
1505
  assetFileNames({ name }) {
1499
1506
  if (name && name.indexOf("_marko-virtual_id_") < 0) {
@@ -1513,7 +1520,7 @@ function markoRun(opts = {}) {
1513
1520
  }
1514
1521
  return `${assetsDir}/${name || "[name]"}-[hash].js`;
1515
1522
  },
1516
- chunkFileNames: `${assetsDir}/_[hash].js`
1523
+ chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
1517
1524
  };
1518
1525
  if (!rollupOutputOptions) {
1519
1526
  rollupOutputOptions = defaultRollupOutputOptions;
@@ -1544,7 +1551,7 @@ function markoRun(opts = {}) {
1544
1551
  }
1545
1552
  }
1546
1553
  };
1547
- const adapterConfig = await ((_f = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _f.call(adapter, config2));
1554
+ const adapterConfig = await ((_g = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _g.call(adapter, config2));
1548
1555
  if (adapterConfig) {
1549
1556
  pluginConfig = (0, import_vite.mergeConfig)(pluginConfig, adapterConfig);
1550
1557
  }
@@ -1655,12 +1662,7 @@ function markoRun(opts = {}) {
1655
1662
  }
1656
1663
  }
1657
1664
  },
1658
- ...(0, import_vite2.default)({
1659
- ...markoOptions,
1660
- get store() {
1661
- return store;
1662
- }
1663
- }),
1665
+ ...(0, import_vite2.default)(markoVitePluginOptions),
1664
1666
  {
1665
1667
  name: "marko-run-vite:post",
1666
1668
  enforce: "post",
@@ -1781,19 +1783,27 @@ async function ensureDir(dir) {
1781
1783
  await import_fs2.default.promises.mkdir(dir, { recursive: true });
1782
1784
  }
1783
1785
  }
1786
+ async function getPackageData(dir) {
1787
+ do {
1788
+ const pkgPath = import_path2.default.join(dir, "package.json");
1789
+ if (import_fs2.default.existsSync(pkgPath)) {
1790
+ return JSON.parse(await import_fs2.default.promises.readFile(pkgPath, "utf-8"));
1791
+ }
1792
+ } while (dir !== (dir = import_path2.default.dirname(dir)));
1793
+ return null;
1794
+ }
1784
1795
  async function resolveAdapter(root, options, log) {
1785
1796
  const { adapter } = options;
1786
1797
  if (adapter !== void 0) {
1787
1798
  return adapter;
1788
1799
  }
1789
- const { resolvePackageData } = await import("vite");
1790
- const pkg = resolvePackageData(".", root);
1800
+ const pkg = await getPackageData(root);
1791
1801
  if (pkg) {
1792
- const dependecies = {
1793
- ...pkg.data.dependecies,
1794
- ...pkg.data.devDependencies
1795
- };
1796
- for (const name of Object.keys(dependecies)) {
1802
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
1803
+ if (pkg.devDependencies) {
1804
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
1805
+ }
1806
+ for (const name of dependecies) {
1797
1807
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
1798
1808
  try {
1799
1809
  const module3 = await import(name);
@@ -1833,7 +1843,7 @@ async function parseEnv(envFile) {
1833
1843
  function loadEnv(envFile) {
1834
1844
  (0, import_dotenv.config)({ path: envFile });
1835
1845
  }
1836
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
1846
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
1837
1847
  if (port <= 0) {
1838
1848
  port = await getAvailablePort();
1839
1849
  }
@@ -1843,7 +1853,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1843
1853
  const proc = import_child_process.default.spawn(cmd, args, {
1844
1854
  cwd,
1845
1855
  shell: true,
1846
- stdio: "inherit",
1856
+ stdio,
1847
1857
  windowsHide: true,
1848
1858
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
1849
1859
  });
@@ -1915,6 +1925,7 @@ function sleep(ms) {
1915
1925
  // Annotate the CommonJS export names for ESM import in node:
1916
1926
  0 && (module.exports = {
1917
1927
  getAvailablePort,
1928
+ getPackageData,
1918
1929
  isPortInUse,
1919
1930
  loadEnv,
1920
1931
  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(
@@ -1155,6 +1157,7 @@ import zlib from "node:zlib";
1155
1157
  import Table from "cli-table3";
1156
1158
  import kleur from "kleur";
1157
1159
  import format from "human-format";
1160
+ import { Blob } from "buffer";
1158
1161
  var HttpVerbColors = {
1159
1162
  get: kleur.green,
1160
1163
  post: kleur.magenta,
@@ -1227,13 +1230,10 @@ function logRoutesTable(routes, bundle) {
1227
1230
  }
1228
1231
  function computeRouteSize(route, bundle) {
1229
1232
  if (route.page) {
1233
+ const entryName = `${route.entryName}.marko`;
1230
1234
  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
- }
1235
+ if (chunk.type === "chunk" && chunk.isEntry && chunk.name === entryName) {
1236
+ return computeChunkSize(chunk, bundle);
1237
1237
  }
1238
1238
  }
1239
1239
  }
@@ -1298,7 +1298,7 @@ var getExternalAdapterOptions = (viteConfig) => getConfig(viteConfig, AdapterCon
1298
1298
 
1299
1299
  // src/vite/plugin.ts
1300
1300
  import { fileURLToPath } from "url";
1301
- var __dirname = fileURLToPath(new URL(".", import.meta.url));
1301
+ var __dirname = path2.dirname(fileURLToPath(import.meta.url));
1302
1302
  var markoExt = ".marko";
1303
1303
  var POSIX_SEP = "/";
1304
1304
  var WINDOWS_SEP = "\\";
@@ -1307,7 +1307,7 @@ function isMarkoFile(id) {
1307
1307
  return id.endsWith(markoExt);
1308
1308
  }
1309
1309
  function markoRun(opts = {}) {
1310
- let { routesDir = "src/routes", adapter, ...markoOptions } = opts;
1310
+ let { routesDir, adapter, ...markoVitePluginOptions } = opts;
1311
1311
  let compiler;
1312
1312
  let store;
1313
1313
  let root;
@@ -1414,7 +1414,7 @@ function markoRun(opts = {}) {
1414
1414
  name: "marko-run-vite:pre",
1415
1415
  enforce: "pre",
1416
1416
  async config(config2, env) {
1417
- var _a, _b, _c, _d, _e, _f;
1417
+ var _a, _b, _c, _d, _e, _f, _g;
1418
1418
  const externalPluginOptions = getExternalPluginOptions(config2);
1419
1419
  if (externalPluginOptions) {
1420
1420
  opts = mergeConfig(opts, externalPluginOptions);
@@ -1428,11 +1428,11 @@ function markoRun(opts = {}) {
1428
1428
  config2.logLevel !== "silent" && !isBuild || isSSRBuild
1429
1429
  );
1430
1430
  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));
1431
+ (_b = adapter.configure) == null ? void 0 : _b.call(adapter, {
1432
+ ...getExternalAdapterOptions(config2),
1433
+ root
1434
+ });
1435
+ const adapterOptions = await ((_c = adapter.pluginOptions) == null ? void 0 : _c.call(adapter, opts));
1436
1436
  if (adapterOptions) {
1437
1437
  opts = mergeConfig(opts, adapterOptions);
1438
1438
  }
@@ -1446,16 +1446,19 @@ function markoRun(opts = {}) {
1446
1446
  )
1447
1447
  }
1448
1448
  });
1449
- store = opts.store || new FileStore(
1449
+ routesDir = opts.routesDir || "src/routes";
1450
+ markoVitePluginOptions.store = store = opts.store || new FileStore(
1450
1451
  `marko-serve-vite-${crypto.createHash("SHA1").update(root).digest("hex")}`
1451
1452
  );
1453
+ markoVitePluginOptions.runtimeId = opts.runtimeId;
1454
+ markoVitePluginOptions.basePathVar = opts.basePathVar;
1452
1455
  resolvedRoutesDir = path2.resolve(root, routesDir);
1453
1456
  typesDir = path2.join(root, ".marko-run");
1454
1457
  devEntryFile = path2.join(root, "index.html");
1455
1458
  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) {
1459
+ const assetsDir = ((_d = config2.build) == null ? void 0 : _d.assetsDir) || "assets";
1460
+ let rollupOutputOptions = (_f = (_e = config2.build) == null ? void 0 : _e.rollupOptions) == null ? void 0 : _f.output;
1461
+ if (isBuild) {
1459
1462
  const defaultRollupOutputOptions = {
1460
1463
  assetFileNames({ name }) {
1461
1464
  if (name && name.indexOf("_marko-virtual_id_") < 0) {
@@ -1475,7 +1478,7 @@ function markoRun(opts = {}) {
1475
1478
  }
1476
1479
  return `${assetsDir}/${name || "[name]"}-[hash].js`;
1477
1480
  },
1478
- chunkFileNames: `${assetsDir}/_[hash].js`
1481
+ chunkFileNames: isSSRBuild ? `_[hash].js` : `${assetsDir}/_[hash].js`
1479
1482
  };
1480
1483
  if (!rollupOutputOptions) {
1481
1484
  rollupOutputOptions = defaultRollupOutputOptions;
@@ -1506,7 +1509,7 @@ function markoRun(opts = {}) {
1506
1509
  }
1507
1510
  }
1508
1511
  };
1509
- const adapterConfig = await ((_f = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _f.call(adapter, config2));
1512
+ const adapterConfig = await ((_g = adapter == null ? void 0 : adapter.viteConfig) == null ? void 0 : _g.call(adapter, config2));
1510
1513
  if (adapterConfig) {
1511
1514
  pluginConfig = mergeConfig(pluginConfig, adapterConfig);
1512
1515
  }
@@ -1617,12 +1620,7 @@ function markoRun(opts = {}) {
1617
1620
  }
1618
1621
  }
1619
1622
  },
1620
- ...markoVitePlugin({
1621
- ...markoOptions,
1622
- get store() {
1623
- return store;
1624
- }
1625
- }),
1623
+ ...markoVitePlugin(markoVitePluginOptions),
1626
1624
  {
1627
1625
  name: "marko-run-vite:post",
1628
1626
  enforce: "post",
@@ -1743,19 +1741,27 @@ async function ensureDir(dir) {
1743
1741
  await fs2.promises.mkdir(dir, { recursive: true });
1744
1742
  }
1745
1743
  }
1744
+ async function getPackageData(dir) {
1745
+ do {
1746
+ const pkgPath = path2.join(dir, "package.json");
1747
+ if (fs2.existsSync(pkgPath)) {
1748
+ return JSON.parse(await fs2.promises.readFile(pkgPath, "utf-8"));
1749
+ }
1750
+ } while (dir !== (dir = path2.dirname(dir)));
1751
+ return null;
1752
+ }
1746
1753
  async function resolveAdapter(root, options, log) {
1747
1754
  const { adapter } = options;
1748
1755
  if (adapter !== void 0) {
1749
1756
  return adapter;
1750
1757
  }
1751
- const { resolvePackageData } = await import("vite");
1752
- const pkg = resolvePackageData(".", root);
1758
+ const pkg = await getPackageData(root);
1753
1759
  if (pkg) {
1754
- const dependecies = {
1755
- ...pkg.data.dependecies,
1756
- ...pkg.data.devDependencies
1757
- };
1758
- for (const name of Object.keys(dependecies)) {
1760
+ let dependecies = pkg.dependencies ? Object.keys(pkg.dependencies) : [];
1761
+ if (pkg.devDependencies) {
1762
+ dependecies = dependecies.concat(Object.keys(pkg.devDependencies));
1763
+ }
1764
+ for (const name of dependecies) {
1759
1765
  if (name.startsWith("@marko/run-adapter") || name.indexOf("marko-run-adapter") !== -1) {
1760
1766
  try {
1761
1767
  const module2 = await import(name);
@@ -1795,7 +1801,7 @@ async function parseEnv(envFile) {
1795
1801
  function loadEnv(envFile) {
1796
1802
  config({ path: envFile });
1797
1803
  }
1798
- async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4) {
1804
+ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), wait = 3e4, stdio = ["ignore", "inherit", "inherit"]) {
1799
1805
  if (port <= 0) {
1800
1806
  port = await getAvailablePort();
1801
1807
  }
@@ -1805,7 +1811,7 @@ async function spawnServer(cmd, args = [], port = 0, env, cwd = process.cwd(), w
1805
1811
  const proc = cp.spawn(cmd, args, {
1806
1812
  cwd,
1807
1813
  shell: true,
1808
- stdio: "inherit",
1814
+ stdio,
1809
1815
  windowsHide: true,
1810
1816
  env: { ...env, NODE_ENV: "development", ...process.env, PORT: `${port}` }
1811
1817
  });
@@ -1877,6 +1883,7 @@ function sleep(ms) {
1877
1883
  export {
1878
1884
  markoRun as default,
1879
1885
  getAvailablePort,
1886
+ getPackageData,
1880
1887
  isPortInUse,
1881
1888
  loadEnv,
1882
1889
  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,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
3
  import { type Socket } from "net";
4
- import { ChildProcess } from "child_process";
4
+ import { type ChildProcess, type StdioOptions } from "child_process";
5
5
  import { type Worker } from "cluster";
6
6
  export interface SpawnedServer {
7
7
  port: number;
@@ -9,7 +9,7 @@ export interface SpawnedServer {
9
9
  }
10
10
  export declare function parseEnv(envFile: string): Promise<import("dotenv").DotenvParseOutput | undefined>;
11
11
  export declare function loadEnv(envFile: string): void;
12
- 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>;
13
13
  export declare function spawnServerWorker(module: string, args?: string[], port?: number, env?: string | Record<string, string>): Promise<Worker>;
14
14
  export declare function waitForError(proc: ChildProcess, port: number): Promise<void>;
15
15
  export declare function waitForServer(port: number, wait?: number): Promise<Socket>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marko/run",
3
- "version": "0.1.11",
3
+ "version": "0.1.13",
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",