UNPKG

tiddlywiki-plugin-dev

Version:

[![](https://img.shields.io/badge/Join-TiddlyWiki_CN-blue)](https://github.com/tiddly-gittly)

148 lines (147 loc) 4.43 kB
import { basename, resolve } from "path"; import { writeFileSync, readFileSync } from "fs"; import chalk from "chalk"; import { minify as htmlMinify } from "html-minifier-terser"; import { rebuild } from "./packup.js"; import { tiddlywiki, mkdirsForFileSync, waitForFile } from "./utils.js"; const printPlugins = (plugins) => { console.log(chalk.bgCyan.black.bold(" Minimized plugins ")); plugins.forEach((plugin, title) => { let size = plugin.length; let unit = "B "; if (size > 1024) { size /= 1024; unit = "KiB"; } if (size > 1024) { size /= 1024; unit = "MiB"; } const sizeFormatted = `${size.toFixed(2)} ${unit}`.padStart(11); console.log(chalk.cyan(`${sizeFormatted} ${title}`)); }); console.log(""); }; const build = async (output, excludeFilter, srcPath = "src") => { const $tw = tiddlywiki(); const plugins = await rebuild($tw, srcPath, void 0, false, excludeFilter); const pluginJsons = /* @__PURE__ */ new Map(); plugins.forEach((plugin) => { const pluginTiddlerName = `${basename( $tw.utils.generateTiddlerFilepath(plugin.title, {}) )}.json`; const jsonStr = JSON.stringify(plugin); pluginJsons.set(plugin.title, jsonStr); if (output) { const path = resolve(output, pluginTiddlerName); mkdirsForFileSync(path); writeFileSync(path, jsonStr); } }); printPlugins(pluginJsons); return plugins; }; const buildLibrary = async (output, excludeFilter, srcPath = "src", wikiPath = "wiki") => { const $tw = tiddlywiki(); const plugins = {}; const pluginJsons = /* @__PURE__ */ new Map(); (await rebuild($tw, srcPath, void 0, false, excludeFilter)).forEach( (plugin) => { const jsonStr = JSON.stringify(plugin); pluginJsons.set(plugin.title, jsonStr); plugins[plugin.title] = plugin; } ); printPlugins(pluginJsons); const pluginPaths = $tw.getLibraryItemSearchPaths($tw.config.pluginsPath); console.log(chalk.green.bold("Generating plugin library...")); tiddlywiki( [ /* 收集所有已安装插件 */ { title: "$:/UpgradeLibrary", type: "application/json", "plugin-type": "library", text: JSON.stringify({ tiddlers: plugins }) }, ...[ "tiddlywiki/filesystem", "tiddlywiki/tiddlyweb", "tiddlywiki/pluginlibrary" ].map( (pluginName) => $tw.loadPluginFolder( $tw.findLibraryItem(pluginName, pluginPaths) ) ) ], wikiPath, [ ...["--output", resolve(output)], ...[ "--savelibrarytiddlers", "$:/UpgradeLibrary", "", "recipes/library/tiddlers/", "$:/UpgradeLibrary/List" ], ...[ "--savetiddler", "$:/UpgradeLibrary/List", "recipes/library/tiddlers.json" ], ...[ "--rendertiddler", "$:/plugins/tiddlywiki/pluginlibrary/library.template.html", "index.html", "text/plain" ], ...[ "--deletetiddlers", "[[$:/UpgradeLibrary]] [[$:/UpgradeLibrary/List]]" ] ] ); const HTMLPath = resolve(output, "index.html"); await waitForFile(HTMLPath); const result = await htmlMinify(readFileSync(HTMLPath, "utf-8"), { caseSensitive: true, collapseBooleanAttributes: false, collapseInlineTagWhitespace: false, collapseWhitespace: true, conservativeCollapse: true, continueOnParseError: true, customAttrCollapse: /.*/, decodeEntities: true, html5: true, ignoreCustomFragments: [/<#[\s\S]*?#>/, /<%[\s\S]*?%>/, /<\?[\s\S]*?\?>/], includeAutoGeneratedTags: false, keepClosingSlash: false, maxLineLength: 0, minifyCSS: true, minifyJS: true, minifyURLs: true, preserveLineBreaks: false, preventAttributesEscaping: false, processConditionalComments: true, processScripts: ["text/html"], removeAttributeQuotes: true, removeComments: true, removeEmptyAttributes: true, removeEmptyElements: false, removeOptionalTags: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, removeTagWhitespace: true, sortAttributes: true, sortClassName: true, trimCustomFragments: true, useShortDoctype: true }); writeFileSync(HTMLPath, result); return plugins; }; export { build, buildLibrary };