Also minify css.

The minifier didn't keep the newlines anyway.
Also, now I'm working on the build, return empty source mappings for a warning in the console less.
This commit is contained in:
Tuxedo Takodachi 2023-09-08 20:10:32 +02:00
parent 1973f87414
commit 0372d676e1
5 changed files with 41 additions and 18 deletions

View File

@ -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)

View File

@ -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: '' } };
}
};
};

View File

@ -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: '' } };
}
}
};

View File

@ -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({

View File

@ -1,6 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "node16",
"target": "ES2021",
"types": [
"@violentmonkey/types",
"@types/chrome",