@openlaboratory/open-doc 0.1.2 → 0.1.3

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.
@@ -1,16 +1,64 @@
1
1
  import { fileURLToPath } from 'node:url'
2
+ import { createRequire } from 'node:module'
2
3
 
3
4
  // The bundled Astro app this integration ships inside, e.g.
4
5
  // `…/node_modules/@openlaboratory/open-doc/app`. Its `src/` holds the React
5
6
  // island components Vite must be allowed to serve in dev.
6
7
  const APP_ROOT = fileURLToPath(new URL('../../', import.meta.url))
7
8
 
9
+ // Resolve specifiers from open-doc's own install (not the consumer's).
10
+ const require = createRequire(import.meta.url)
11
+
12
+ /**
13
+ * Astro's static build emits an SSR server bundle into the consumer's `dist/`
14
+ * and then runs it with Node to prerender the pages. That bundle keeps open-doc's
15
+ * runtime deps (Astro, the React renderer, and their transitive deps like
16
+ * `clsx`/`piccolore`) as bare `import` specifiers — but those live in open-doc's
17
+ * own `node_modules`, not the consumer's, so under a strict (non-hoisted) install
18
+ * Node can't resolve them from `dist/` and prerendering fails.
19
+ *
20
+ * This plugin rewrites those bare specifiers to absolute paths resolved from
21
+ * open-doc's install, making the build self-contained under any package manager
22
+ * or workspace layout — without consumer-side hoisting. It runs in `renderChunk`
23
+ * (after Astro's module graph is fully resolved) so Astro's own resolution and
24
+ * virtual modules (`astro:*`) are left untouched. Client chunks bundle their
25
+ * deps, so they have nothing bare to rewrite.
26
+ *
27
+ * @returns {import('vite').Plugin}
28
+ */
29
+ function absolutizeSsrExternals() {
30
+ const rewrite = (_match, pre, quote, spec) => {
31
+ if (/^[./]/.test(spec) || spec.startsWith('\0') || spec.includes(':')) return _match
32
+ try {
33
+ return pre + quote + require.resolve(spec) + quote
34
+ } catch {
35
+ return _match
36
+ }
37
+ }
38
+ return {
39
+ name: 'open-doc:absolutize-ssr-externals',
40
+ renderChunk(code) {
41
+ // Only the SSR build emits bare external imports; skip the client build.
42
+ if (this.environment && this.environment.name !== 'ssr') return null
43
+ const out = code
44
+ // `import … from '<spec>'` / `export … from '<spec>'`
45
+ .replace(/^(\s*(?:import|export)\b[^\n'"]*?\bfrom\s*)(['"])([^'"]+)\2/gm, rewrite)
46
+ // bare side-effect `import '<spec>'`
47
+ .replace(/^(\s*import\s+)(['"])([^'"]+)\2/gm, rewrite)
48
+ // dynamic `import('<spec>')`
49
+ .replace(/(\bimport\s*\(\s*)(['"])([^'"]+)\2/g, rewrite)
50
+ return out === code ? null : { code: out }
51
+ },
52
+ }
53
+ }
54
+
8
55
  /**
9
56
  * Internal Astro integration that wires the consumer's project into the bundled
10
57
  * app:
11
58
  * • exposes the resolved config as the `virtual:open-doc-config` module
12
59
  * • aliases `@docs` to the consumer's content directory
13
- * • allows Vite to read files from the (external) content + public dirs
60
+ * • allows Vite to read the app's island sources + the consumer's content/public dirs
61
+ * • makes the production build self-contained (see absolutizeSsrExternals)
14
62
  *
15
63
  * The resolved config is passed from the CLI via OPEN_DOC_CONFIG_JSON.
16
64
  *
@@ -28,24 +76,15 @@ export function openDocConfig({ contentDir, publicDir } = {}) {
28
76
  name: 'open-doc:config',
29
77
  hooks: {
30
78
  'astro:config:setup': ({ command, updateConfig }) => {
31
- // For `build`, bundle the runtime deps our chunks reference into the SSR
32
- // output so the build is self-contained under every package manager
33
- // (they live in open-doc's node_modules, not the consumer's). Bundling
34
- // *everything* (`noExternal: true`) breaks `astro sync` on a CommonJS
35
- // dep, so we target the specific packages. In `dev` they must stay
36
- // external — bundling CommonJS React breaks the dev SSR runtime.
37
- const noExternal =
38
- command === 'build' ? ['fuse.js', 'react', 'react-dom', '@astrojs/react'] : []
39
-
79
+ const buildPlugins = command === 'build' ? [absolutizeSsrExternals()] : []
40
80
  updateConfig({
41
81
  vite: {
42
- plugins: [virtualConfigPlugin(configJson)],
82
+ plugins: [virtualConfigPlugin(configJson), ...buildPlugins],
43
83
  resolve: {
44
84
  alias: contentDir ? { '@docs': contentDir } : {},
45
85
  // One React instance, even if a consumer installs their own for MDX.
46
86
  dedupe: ['react', 'react-dom'],
47
87
  },
48
- ssr: { noExternal },
49
88
  server: { fs: { allow } },
50
89
  },
51
90
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openlaboratory/open-doc",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "A polished, GitBook-style documentation-site framework. Write Markdown, run one command, get a beautiful static site.",
5
5
  "keywords": [
6
6
  "documentation",