@stylezjs/postcss-plugin
Version:
@stylezjs/postcss-plugin is a PostCSS plugin for integrating `@stylezjs/stylez` to generate static CSS styles. Stylez → 'Z' of Zero Runtime.
129 lines (123 loc) • 4.55 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_node_fs = require("node:fs");
var import_autoprefixer = __toESM(require("autoprefixer"), 1);
var import_postcss = __toESM(require("postcss"), 1);
var import_postcss_js = require("postcss-js");
var import_postcss_nested = __toESM(require("postcss-nested"), 1);
// src/scripts/extractor.ts
var import_fast_glob = __toESM(require("fast-glob"), 1);
var import_esbuild = require("esbuild");
var import_fs = require("fs");
var import_path = __toESM(require("path"), 1);
var { glob } = import_fast_glob.default;
var tmpDir = "./.tmp";
if (!(0, import_fs.existsSync)(tmpDir)) {
(0, import_fs.mkdirSync)(tmpDir, { recursive: true });
}
var extractStylesFromFiles = async (patterns) => {
const entryFiles = await glob(patterns);
const format = "cjs";
for (const entry of entryFiles) {
const tmpFile = import_path.default.join(tmpDir, `tmp-${import_path.default.basename(entry)}.js`);
await (0, import_esbuild.build)({
bundle: true,
entryPoints: [entry],
external: ["@stylezjs/stylez"],
format,
minify: true,
outfile: tmpFile,
platform: "node"
});
if (format === "esm") {
await import(import_path.default.resolve(tmpFile));
} else {
require(import_path.default.resolve(tmpFile));
}
(0, import_fs.rmSync)(tmpDir, { force: true, recursive: true });
}
let finalCss;
if (format === "esm") {
const { getAllCss } = await import("@stylezjs/stylez");
finalCss = getAllCss();
} else {
const { getAllCss } = require("@stylezjs/stylez");
finalCss = getAllCss();
}
return finalCss;
};
// src/index.ts
var state = {
duplicatesFound: false,
processed: false
};
var plugin = (opts) => ({
async Once(root) {
if (state.duplicatesFound) return;
const atRule = root.first;
const filePath = atRule?.source?.input?.file;
if (atRule?.name === "stylez" && filePath?.includes(process.cwd())) {
if (state.processed) {
state.duplicatesFound = true;
process.stdout.write(
"\u26D4\uFE0F Multiple @stylez files detected. Remove duplicates before processing.\n\n"
);
return;
}
try {
state.processed = true;
const styles = await extractStylesFromFiles(opts?.patterns ?? []);
if (state.duplicatesFound) return;
root.removeAll();
const result = await (0, import_postcss.default)([import_postcss_nested.default, import_autoprefixer.default]).process(styles, {
from: void 0,
parser: import_postcss_js.parse
});
if (result.css.length === 0) return;
const finalCSS = `@stylez;
${result.css}`;
(0, import_node_fs.writeFileSync)(filePath, finalCSS);
process.stdout.write(`\u{1F3A8} Styles saved to: ${filePath}
`);
} catch {
process.stdout.write("Issue saving styles");
}
}
},
postcssPlugin: "@stylezjs/postcss-plugin"
});
plugin.postcss = true;
var index_default = plugin;
module.exports = plugin.default || plugin;