UNPKG

vite-plugin-indexify

Version:

Generate jsons that index all or some files output by vite.

112 lines (111 loc) 3.61 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const fs = require("node:fs"); const path = require("node:path"); const vite = require("vite"); const DEFAULT_OPTIONS = { directory: ".", includeSubdirs: false, indexFileName: "index.json", recurse: true }; function getIndexifier(indexifyOptionsParam = [], outDir) { if (outDir === void 0) { return () => { console.warn( "vite-plugin-indexify-public: No output directory identified." ); }; } if (indexifyOptionsParam.length === 0) { indexifyOptionsParam.push(DEFAULT_OPTIONS); } return () => { for (let entry of indexifyOptionsParam) { const full_entry = { ...DEFAULT_OPTIONS, ...entry }; const validator = getIndexifyValidator(full_entry); const target_path = path.join(outDir, full_entry.directory); indexify$1(target_path, full_entry, validator); } }; } function indexify$1(valid_dir_path, options, validate) { const entries = []; try { const dirents = fs.readdirSync(valid_dir_path, { withFileTypes: true }); for (let dirent of dirents) { if (validate(dirent)) { if (dirent.isFile()) { entries.push({ name: dirent.name, isDirectory: false }); } else { if (options.includeSubdirs) { entries.push({ name: dirent.name, isDirectory: true }); } if (options.recurse) { indexify$1(path.join(valid_dir_path, dirent.name), options, validate); } } } } } catch (e) { console.error("vite-plugin-indexify-public : error reading directory. ", e); } const outpath = path.join(valid_dir_path, options.indexFileName); fs.writeFileSync(outpath, JSON.stringify(entries, void 0, 4)); console.log("\n" + vite.normalizePath(path.relative(".", outpath))); } function getIndexifyValidator(directoryOptions) { let include = (name) => true; let exclude = (name) => false; if (directoryOptions.exclude) { include = (name) => directoryOptions.exclude.test(name); } if (directoryOptions.include) { include = (name) => directoryOptions.include.test(name); } return function(dirent) { return include(dirent.name) && !exclude(dirent.name); }; } function indexify(indexifyOptions) { let iWriter = getIndexifier(indexifyOptions, void 0); return { name: "indexify-assets", version: "0.0.1", writeBundle: { order: "post", handler: () => { iWriter(); } }, outputOptions(options) { iWriter = getIndexifier(indexifyOptions, options.dir); } // configureServer(server) { // console.log("\n~~~ configureServer called ~~~"); // const { config, httpServer } = server; // httpServer?.once("listening", () => { // const { root, base } = config; // console.log("\n >> root, base: ", root, base); // const inputs = config.build.rollupOptions.input; // console.log("\n >> rollupOptions.input: ", inputs); // console.log("\n >> config.cacheDir ", config.cacheDir); // console.log("\n >> server middlewares", server.middlewares); // // fs.writeFileSync( // // path.join(config.cacheDir, "index.json"), // // '{"hi":"hello"}' // // ); // console.log("\n >> config.publicDir ", config.publicDir); // console.log("\n >> config.build.outDir", config.build.outDir); // }); // it's probably going to have to work in this area // }, }; } exports.default = indexify;