diff --git a/CHANGELOG.md b/CHANGELOG.md index 8654a1ee5..c0848215b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ 4chan XT uses a different user script namespace, so to migrate you need to export settings from 4chan X, and import them in XT. +### Unreleased + +- Also minify css in the minified build. + ### XT v2.1.4 (2023-09-02) - Fix DataBoard class, should solve [#7](https://github.com/TuxedoTako/4chan-xt/issues/7) diff --git a/tools/rollup-plugin-base64.js b/tools/rollup-plugin-base64.js index 06175bfe7..26a169215 100644 --- a/tools/rollup-plugin-base64.js +++ b/tools/rollup-plugin-base64.js @@ -21,7 +21,7 @@ export default function importBase64(opts) { if (!filter(id)) return; const file = await readFile(id); - return `export default '${file.toString('base64')}';`; + return { code: `export default '${file.toString('base64')}';`, map: { mappings: '' } }; } }; }; diff --git a/tools/rollup-plugin-inline-file.js b/tools/rollup-plugin-inline-file.js index 932073686..2e852d79a 100644 --- a/tools/rollup-plugin-inline-file.js +++ b/tools/rollup-plugin-inline-file.js @@ -1,20 +1,16 @@ import { createFilter } from "@rollup/pluginutils"; -import { dirname } from "path"; -import { fileURLToPath } from "url"; - -const __dirname = dirname(fileURLToPath(import.meta.url)); export default async function setupFileInliner(packageJson) { /** @param {string} string */ const escape = (string) => string.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\\${'); /** - * @param {{ - * include: import("@rollup/pluginutils").FilterPattern, - * exclude?: import("@rollup/pluginutils").FilterPattern, - * transformer?: (input: string) => string - * wrap?: boolean - * }} opts + * @param {Object} opts + * @param {import("@rollup/pluginutils").FilterPattern} opts.include + * @param {import("@rollup/pluginutils").FilterPattern} [opts.exclude] + * @param {(input: string) => string} [opts.transformer] Optional function to transform the string. + * @param {boolean} [opts.wrap] Wether to look for <%= meta.stuff %> and replace it, and wrap in a string, + * defaults to true. * @returns {import("rollup").Plugin} */ return function inlineFile(opts) { @@ -38,13 +34,15 @@ export default async function setupFileInliner(packageJson) { if (opts.transformer) { code = opts.transformer(code); } - if (!wrap) return code; + if (wrap) { + code = escape(code); + code = code.replace(/<%= meta\.(\w+) %>/g, (match, $1) => { + return escape(packageJson.meta[$1]); + }); + code = `export default \`${code}\`;`; + } - code = escape(code); - code = code.replace(/<%= meta\.(\w+) %>/g, (match, $1) => { - return escape(packageJson.meta[$1]); - }); - return `export default \`${code}\`;`; + return { code, map: { mappings: '' } }; } } }; diff --git a/tools/rollup.js b/tools/rollup.js index 50355758e..44560cc87 100644 --- a/tools/rollup.js +++ b/tools/rollup.js @@ -39,7 +39,27 @@ const minify = process.argv.includes('-min'); plugins: [ typescript(), inlineFile({ - include: ["**/*.html", "**/*.css"], + include: ["**/*.html"], + }), + inlineFile({ + include: ["**/*.css"], + transformer(css) { + if (!minify) return css; + + return css + // Remove whitespace after colon in css rules. + .replace(/^ {2,}([a-z\-]+:) +/gm, '$1') + // Remove newlines and trailing whitespace. + .replace(/\r?\n[ \t+]*/g, '') + // Remove last semicolon before the }. + .replace(/;\}/g, '}') + // Remove space between rule set and {. + .replace(/ \{/g, '{') + // Remove comments. + .replace(/\/\*[^\*]*\*\//g, '') + // Remove space before and after these characters in selectors. + .replace(/ ([>+~]) /g, '$1'); + } }), importBase64({ include: ["**/*.png", "**/*.gif", "**/*.wav", "**/*.woff", "**/*.woff2"] }), inlineFile({ diff --git a/tools/tsconfig.json b/tools/tsconfig.json index b8183e413..024702bc2 100644 --- a/tools/tsconfig.json +++ b/tools/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "moduleResolution": "node16", + "target": "ES2021", "types": [ "@violentmonkey/types", "@types/chrome",