@bndynet/vue-site 0.1.10 → 0.1.11

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.
Files changed (2) hide show
  1. package/bin/vue-site.mjs +51 -36
  2. package/package.json +1 -1
package/bin/vue-site.mjs CHANGED
@@ -166,29 +166,60 @@ function resolveBootstrapUrl(path) {
166
166
  return '/' + t.replace(/^\.\//, '')
167
167
  }
168
168
 
169
- // Static import bundles bootstrap for production; dynamic import with vite-ignore is not emitted.
170
- function buildEntryCode(siteConfig) {
169
+ /**
170
+ * Bootstrap script shared by dev (virtual entry) and build (inlined in html).
171
+ * `siteConfigSpecifier` differs because dev serves from Vite root (`/foo`)
172
+ * while the build temp html lives next to the config (`./foo`).
173
+ *
174
+ * Static import bundles `bootstrap` for production; dynamic import with
175
+ * vite-ignore is not emitted.
176
+ */
177
+ function buildBootstrapScript({ siteConfig, siteConfigSpecifier }) {
171
178
  const bs = siteConfig?.bootstrap
172
179
  const bootstrapImport =
173
180
  bs != null && String(bs).trim() !== ''
174
181
  ? `import '${resolveBootstrapUrl(bs)}'\n`
175
182
  : ''
183
+ const pkgDirUrl = pkgDir.replace(/\\/g, '/')
176
184
  return [
177
185
  bootstrapImport,
178
186
  `import 'element-plus/dist/index.css'`,
179
187
  `import 'element-plus/theme-chalk/dark/css-vars.css'`,
180
- `import { createSiteApp } from '${pkgDir.replace(/\\/g, '/')}/dist/index.es.js'`,
181
- `import '${pkgDir.replace(/\\/g, '/')}/dist/style.css'`,
182
- `import siteConfig from '/${foundConfig}'`,
188
+ `import { createSiteApp } from '${pkgDirUrl}/dist/index.es.js'`,
189
+ `import '${pkgDirUrl}/dist/style.css'`,
190
+ `import siteConfig from '${siteConfigSpecifier}'`,
183
191
  `import { repositoryUrl } from '${VIRTUAL_PACKAGE}'`,
184
192
  `;(async () => {`,
185
- ` const app = await createSiteApp({ ...siteConfig, packageRepository: repositoryUrl })`,
193
+ ` const searchParams = new URLSearchParams(window.location.search)`,
194
+ ` const hasThemeQuery = searchParams.has('theme')`,
195
+ ` const queryTheme = searchParams.get('theme') || ''`,
196
+ ` const resolvedTheme = String(queryTheme).toLowerCase().includes('dark') ? 'dark' : 'light'`,
197
+ ` if (hasThemeQuery) {`,
198
+ ` try {`,
199
+ ` localStorage.setItem('vue-site-theme', resolvedTheme)`,
200
+ ` } catch {`,
201
+ ` // localStorage may be unavailable`,
202
+ ` }`,
203
+ ` }`,
204
+ ` const app = await createSiteApp({`,
205
+ ` ...siteConfig,`,
206
+ ` ...(hasThemeQuery ? { theme: { ...(siteConfig.theme || {}), default: resolvedTheme } } : {}),`,
207
+ ` packageRepository: repositoryUrl,`,
208
+ ` })`,
186
209
  ` app.mount('#app')`,
187
210
  `})()`,
188
211
  ].join('\n')
189
212
  }
190
213
 
191
- const htmlTemplate = `<!DOCTYPE html>
214
+ function buildEntryCode(siteConfig) {
215
+ return buildBootstrapScript({
216
+ siteConfig,
217
+ siteConfigSpecifier: `/${foundConfig}`,
218
+ })
219
+ }
220
+
221
+ function buildHtmlShell(scriptTag) {
222
+ return `<!DOCTYPE html>
192
223
  <html lang="en">
193
224
  <head>
194
225
  <meta charset="UTF-8" />
@@ -197,9 +228,14 @@ const htmlTemplate = `<!DOCTYPE html>
197
228
  </head>
198
229
  <body>
199
230
  <div id="app"></div>
200
- <script type="module" src="/@id/__x00__${VIRTUAL_ENTRY}"></script>
231
+ ${scriptTag}
201
232
  </body>
202
233
  </html>`
234
+ }
235
+
236
+ const htmlTemplate = buildHtmlShell(
237
+ `<script type="module" src="/@id/__x00__${VIRTUAL_ENTRY}"></script>`,
238
+ )
203
239
 
204
240
  async function loadSiteConfig() {
205
241
  const configPath = resolve(cwd, foundConfig)
@@ -469,34 +505,13 @@ async function run() {
469
505
  const hadHtml = fs.existsSync(tempHtml)
470
506
 
471
507
  if (!hadHtml) {
472
- const bs = siteConfig?.bootstrap
473
- const bootstrapImport =
474
- bs != null && String(bs).trim() !== ''
475
- ? `import '${resolveBootstrapUrl(bs)}'\n`
476
- : ''
477
- const buildHtml = `<!DOCTYPE html>
478
- <html lang="en">
479
- <head>
480
- <meta charset="UTF-8" />
481
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
482
- <title></title>
483
- </head>
484
- <body>
485
- <div id="app"></div>
486
- <script type="module">
487
- ${bootstrapImport}import 'element-plus/dist/index.css'
488
- import 'element-plus/theme-chalk/dark/css-vars.css'
489
- import { createSiteApp } from '${pkgDir.replace(/\\/g, '/')}/dist/index.es.js'
490
- import '${pkgDir.replace(/\\/g, '/')}/dist/style.css'
491
- import siteConfig from './${foundConfig}'
492
- import { repositoryUrl } from '${VIRTUAL_PACKAGE}'
493
- ;(async () => {
494
- const app = await createSiteApp({ ...siteConfig, packageRepository: repositoryUrl })
495
- app.mount('#app')
496
- })()
497
- </script>
498
- </body>
499
- </html>`
508
+ const bootstrapScript = buildBootstrapScript({
509
+ siteConfig,
510
+ siteConfigSpecifier: `./${foundConfig}`,
511
+ })
512
+ const buildHtml = buildHtmlShell(
513
+ `<script type="module">\n${bootstrapScript}\n </script>`,
514
+ )
500
515
  fs.writeFileSync(tempHtml, buildHtml)
501
516
  }
502
517
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bndynet/vue-site",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "type": "module",
5
5
  "description": "A configurable Vue 3 site framework with sidebar navigation, markdown rendering, and theme switching.",
6
6
  "repository": {