@inglorious/ssx 2.1.12 → 2.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.
package/bin/ssx.js CHANGED
@@ -31,12 +31,13 @@ program
31
31
  .description("Start development server with hot reload")
32
32
  .option("-c, --config <file>", "config file name", "site.config.js")
33
33
  .option("-r, --root <dir>", "root directory", ".")
34
- .option("-p, --port <port>", "dev server port", 3000)
34
+ // No default here: an explicit port overrides the config's, 3000 otherwise.
35
+ .option("-p, --port <port>", "dev server port (default: 3000)")
35
36
  .action(async (options) => {
36
37
  const cwd = process.cwd()
37
38
  const rootDir = path.resolve(cwd, options.root)
38
39
  const configPath = resolveConfigFile(rootDir, options.config)
39
- const port = Number(options.port)
40
+ const port = options.port ? Number(options.port) : undefined
40
41
 
41
42
  try {
42
43
  await dev({
@@ -149,10 +150,12 @@ program
149
150
 
150
151
  program.parse()
151
152
 
153
+ // The config lives in src/, where it is loaded from: site.config.ts is used
154
+ // when there is no site.config.js.
152
155
  function resolveConfigFile(rootDir, configFile) {
153
156
  if (configFile === "site.config.js") {
154
- const jsPath = path.resolve(rootDir, "site.config.js")
155
- const tsPath = path.resolve(rootDir, "site.config.ts")
157
+ const jsPath = path.resolve(rootDir, "src", "site.config.js")
158
+ const tsPath = path.resolve(rootDir, "src", "site.config.ts")
156
159
 
157
160
  if (!existsSync(jsPath) && existsSync(tsPath)) {
158
161
  return "site.config.ts"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/ssx",
3
- "version": "2.1.12",
3
+ "version": "2.1.13",
4
4
  "description": "Server-Side-X. Xecution? Xperience? Who knows.",
5
5
  "author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -48,9 +48,9 @@
48
48
  },
49
49
  "sideEffects": false,
50
50
  "dependencies": {
51
- "@inglorious/store": "11.0.3",
51
+ "@inglorious/store": "11.0.4",
52
52
  "@inglorious/utils": "5.0.0",
53
- "@inglorious/web": "6.6.0",
53
+ "@inglorious/web": "6.6.1",
54
54
  "@lit-labs/ssr": "^4.1.0",
55
55
  "commander": "^15.0.0",
56
56
  "connect": "^3.7.0",
@@ -58,18 +58,18 @@
58
58
  "glob": "^13.0.6",
59
59
  "gray-matter": "^4.0.3",
60
60
  "highlight.js": "^11.12.0",
61
- "katex": "^0.18.6",
62
- "markdown-it": "^15.0.1",
61
+ "katex": "^0.19.0",
62
+ "markdown-it": "^15.0.3",
63
63
  "markdown-it-texmath": "^1.0.0",
64
- "mermaid": "^11.17.2",
64
+ "mermaid": "^12.2.0",
65
65
  "sharp": "^0.35.4",
66
66
  "svgo": "^4.1.0",
67
- "vite": "^8.2.2",
67
+ "vite": "^8.3.4",
68
68
  "vite-plugin-image-optimizer": "^2.0.3"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@inglorious/eslint-config": "1.1.2",
72
- "@inglorious/logo": "3.0.15",
72
+ "@inglorious/logo": "3.0.16",
73
73
  "prettier": "^3.9.6",
74
74
  "vitest": "^5.0.0"
75
75
  },
@@ -60,6 +60,9 @@ export async function build(options = {}) {
60
60
  mode: "production",
61
61
  server: { middlewareMode: true, hmr: false },
62
62
  appType: "custom",
63
+ // This server only loads modules for SSR. Discovering client deps would
64
+ // scan the client entry, which is not written yet.
65
+ optimizeDeps: { noDiscovery: true },
63
66
  })
64
67
  const loader = (p) => vite.ssrLoadModule(p)
65
68
 
@@ -11,21 +11,22 @@ import { markdownPlugin } from "../utils/markdown.js"
11
11
  * @param {Object} options - SSX configuration options.
12
12
  * @param {string} [options.rootDir="src"] - The source root directory.
13
13
  * @param {string} [options.publicDir="public"] - The public assets directory (relative to rootDir).
14
+ * @param {number} [options.port] - The port from the command line, which overrides the config's.
14
15
  * @param {Object} [options.vite={}] - Additional Vite configuration to merge.
15
16
  * @returns {Object} The merged Vite configuration.
16
17
  */
17
18
  export function createViteConfig(options = {}) {
18
- const { rootDir = ".", vite = {}, markdown = {} } = options
19
- const { port = 3000 } = vite.dev ?? {}
19
+ const { rootDir = ".", port, vite = {}, markdown = {} } = options
20
+ const { port: defaultPort = 3000 } = vite.dev ?? {}
20
21
 
21
22
  const srcDir = path.resolve(process.cwd(), rootDir, "src")
22
23
  const publicDir = path.resolve(process.cwd(), rootDir, "public")
23
24
 
24
- return mergeConfig(
25
+ const config = mergeConfig(
25
26
  {
26
27
  root: process.cwd(),
27
28
  publicDir,
28
- server: { port, middlewareMode: true },
29
+ server: { port: defaultPort, middlewareMode: true },
29
30
  appType: "custom",
30
31
  plugins: [virtualPlugin(), markdownPlugin(markdown)],
31
32
  resolve: {
@@ -36,6 +37,10 @@ export function createViteConfig(options = {}) {
36
37
  },
37
38
  vite,
38
39
  )
40
+
41
+ // A port given on the command line wins over the config's.
42
+ if (port) config.server.port = port
43
+ return config
39
44
  }
40
45
 
41
46
  /**
@@ -29,6 +29,14 @@ describe("createViteConfig", () => {
29
29
  expect(config.server.port).toBe(4000)
30
30
  expect(config.define.__TEST__).toBe(true)
31
31
  })
32
+
33
+ it("should let the command-line port override the config's", () => {
34
+ const config = createViteConfig({
35
+ port: 5000,
36
+ vite: { server: { port: 4000 } },
37
+ })
38
+ expect(config.server.port).toBe(5000)
39
+ })
32
40
  })
33
41
 
34
42
  describe("virtualPlugin", () => {
@@ -6,6 +6,7 @@
6
6
  * @param {string} [options.lang="en"] - Language attribute.
7
7
  * @param {string} [options.charset="UTF-8"] - Character set.
8
8
  * @param {string} [options.title=""] - Page title.
9
+ * @param {string} [options.favicon=""] - Favicon URL; without one, no icon link is rendered.
9
10
  * @param {Object} [options.meta={}] - Meta tags.
10
11
  * @param {string[]} [options.styles=[]] - Stylesheets.
11
12
  * @param {string} [options.head=""] - Additional head content.
@@ -35,7 +36,7 @@ export function layout(body, options) {
35
36
  .join("\n")}
36
37
  <meta charset="${charset}" />
37
38
  <title>${title}</title>
38
- <link rel="icon" type="image/x-icon" href="${favicon}">
39
+ ${favicon ? `<link rel="icon" href="${favicon}">` : ""}
39
40
  ${Object.entries(meta)
40
41
  .map(
41
42
  ([key, content]) =>
@@ -1,17 +1,31 @@
1
+ import { existsSync } from "node:fs"
1
2
  import path from "node:path"
2
3
  import { pathToFileURL } from "node:url"
3
4
 
5
+ import { runnerImport } from "vite"
6
+
7
+ /**
8
+ * Loads the site config from `src/`. A TypeScript config is loaded through
9
+ * Vite, as the pages are, so it can import other TypeScript modules.
10
+ * Without a config file, the defaults apply.
11
+ */
4
12
  export async function loadConfig(options) {
5
13
  const { rootDir = ".", configPath = "site.config.js" } = options
6
- const srcDir = path.join(rootDir, "src")
14
+ const configFile = path.resolve(rootDir, "src", configPath)
7
15
 
8
- try {
9
- const config = await import(pathToFileURL(path.join(srcDir, configPath)))
10
- return config.default || config
11
- } catch (error) {
12
- if (error.code === "MODULE_NOT_FOUND") {
13
- return {} // Default config
14
- }
15
- throw error
16
+ if (!existsSync(configFile)) {
17
+ return {} // Default config
16
18
  }
19
+
20
+ const config = /\.[cm]?ts$/.test(configFile)
21
+ ? (
22
+ await runnerImport(configFile, {
23
+ root: path.resolve(rootDir),
24
+ configFile: false,
25
+ logLevel: "error",
26
+ })
27
+ ).module
28
+ : await import(pathToFileURL(configFile).href)
29
+
30
+ return config.default || config
17
31
  }
@@ -0,0 +1,57 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
2
+ import path from "node:path"
3
+
4
+ import { afterEach, beforeEach, describe, expect, it } from "vitest"
5
+
6
+ import { loadConfig } from "./config.js"
7
+
8
+ describe("loadConfig", () => {
9
+ let rootDir
10
+
11
+ beforeEach(() => {
12
+ // Inside the package: Vitest's module loader only imports files under
13
+ // the project root.
14
+ rootDir = mkdtempSync(path.join(import.meta.dirname, ".tmp-config-"))
15
+ mkdirSync(path.join(rootDir, "src"))
16
+ })
17
+
18
+ afterEach(() => {
19
+ rmSync(rootDir, { recursive: true, force: true })
20
+ })
21
+
22
+ function writeSource(file, code) {
23
+ writeFileSync(path.join(rootDir, "src", file), code)
24
+ }
25
+
26
+ it("loads a JavaScript config from src", async () => {
27
+ writeSource("site.config.js", `export default { title: "JS" }`)
28
+
29
+ await expect(loadConfig({ rootDir })).resolves.toEqual({ title: "JS" })
30
+ })
31
+
32
+ it("loads a TypeScript config, and the TypeScript it imports", async () => {
33
+ writeSource("title.ts", `export const title: string = "TS"`)
34
+ writeSource(
35
+ "site.config.ts",
36
+ `import type { SiteConfig } from "@inglorious/ssx"
37
+ import { title } from "./title"
38
+
39
+ const config: SiteConfig = { title }
40
+ export default config`,
41
+ )
42
+
43
+ await expect(
44
+ loadConfig({ rootDir, configPath: "site.config.ts" }),
45
+ ).resolves.toEqual({ title: "TS" })
46
+ })
47
+
48
+ it("falls back to the defaults without a config file", async () => {
49
+ await expect(loadConfig({ rootDir })).resolves.toEqual({})
50
+ })
51
+
52
+ it("reports an error inside the config instead of ignoring it", async () => {
53
+ writeSource("site.config.js", `import "./missing.js"\nexport default {}`)
54
+
55
+ await expect(loadConfig({ rootDir })).rejects.toThrow()
56
+ })
57
+ })