import { minify as csso } from "csso"
import stylelint from "stylelint"
import type { ConfigRuleSettings } from "stylelint"
import plugin from "stylelint-order"
import recommended from "stylelint-config-recommended"
import ordering from "stylelint-config-idiomatic-order"
import { SEPARATOR } from "@std/path/constants"
export async function bundle(input: URL | string, { minify = false, banner = "", rules = {} as Rules } = {}): Promise<string> {
const code = input instanceof URL ? await fetch(input).then((response) => response.text()) : input
const { results: [{ warnings }], ...result } = await stylelint.lint({
config: {
plugins: [plugin],
rules: {
...recommended.rules,
...ordering.rules,
...rules,
},
fix: true,
},
code,
cacheLocation: `/dev/null/${SEPARATOR}`,
})
result.code ??= ""
if (warnings.length) {
throw new TypeError(`Failed to bundle css:\n${warnings.map(({ severity, rule, text }) => `[${severity.toUpperCase()}] ${rule}: ${text}`).join("\n")}`)
}
if (minify) {
result.code = csso(result.code!, { comments: false }).css
}
if (banner) {
banner = `/**\n${banner.split("\n").map((line) => ` * ${line}`).join("\n")}\n */`
result.code = `${banner}\n${result.code}`
}
return result.code!
}
type Rules = ConfigRuleSettings<any, Object>
|