'use strict'; const fsp = require('node:fs/promises'); const node_url = require('node:url'); const kit = require('@nuxt/kit'); const chalk = require('chalk'); const globby = require('globby'); const pathe = require('pathe'); const ufo = require('ufo'); const wbn = require('wbn'); const mime = require('mime'); var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null; function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } const fsp__default = /*#__PURE__*/_interopDefaultCompat(fsp); const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk); const mime__default = /*#__PURE__*/_interopDefaultCompat(mime); const module$1 = kit.defineNuxtModule({ meta: { configKey: "webBundle", name: "nuxt-web-bundle" }, defaults: (nuxt) => ({ baseURL: ufo.joinURL("http://localhost:3000", nuxt.options.app.baseURL), formatVersion: "b2", filename: "bundle.wbn" }), async setup(options, nuxt) { if (nuxt.options._prepare) return; nuxt.options._generate = true; nuxt.hook("nitro:config", (config) => { config.prerender ||= {}; config.prerender.crawlLinks = true; config.prerender.routes = [ ...config.prerender.routes || [], nuxt.options.ssr ? "/" : "/index.html" ].filter((i) => i !== "/200.html"); }); const themeDir = node_url.fileURLToPath(new URL("./runtime/extends", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('module.cjs', document.baseURI).href)))); nuxt.options._layers.push({ cwd: themeDir, config: { rootDir: themeDir, srcDir: themeDir } // eslint-disable-next-line @typescript-eslint/no-explicit-any }); const logger = kit.useLogger("nuxt-web-bundle"); nuxt.hook("ready", () => { nuxt.hook("build:done", async () => { logger.info("Generating web bundle."); const builder = new wbn.BundleBuilder("b2"); builder.setPrimaryURL(options.primaryURL || options.baseURL); const files = await globby.globby("**/*", { cwd: pathe.resolve(nuxt.options.rootDir, "dist") }); for (const [index, file] of files.sort().reverse().entries()) { const content = await fsp__default.readFile(pathe.resolve(nuxt.options.rootDir, "dist", file)); const headers = { "Content-Type": mime__default.getType(pathe.basename(file)) || "application/octet-stream", "Access-Control-Allow-Origin": "*" }; const url2 = new URL(file, options.baseURL).toString().replace(/\/index.html$/, "/").replace(/\.html$/, ""); builder.addExchange(url2, 200, headers, content); pathe.dirname(url2); const { pathname } = ufo.parseURL(url2); const treeChar = index === files.length - 1 ? "\u2514\u2500" : "\u251C\u2500"; process.stdout.write(chalk__default.gray(` ${treeChar} ${pathname} `)); await fsp__default.rm(pathe.resolve(nuxt.options.rootDir, "dist", file), { force: true }); } const buf = builder.createBundle(); await fsp__default.mkdir(pathe.resolve(nuxt.options.rootDir, "dist"), { recursive: true }); await fsp__default.writeFile( pathe.resolve(nuxt.options.rootDir, "dist", options.filename), Buffer.from(buf, buf.byteOffset, buf.byteLength) ); const url = node_url.pathToFileURL(pathe.resolve(nuxt.options.rootDir, "dist", options.filename)); logger.success(`Bundle generated at \`${url}\`.`); }); }); } }); module.exports = module$1;