vite-plugin-import-css-sheet
Advanced tools
Comparing version
import type { Plugin } from 'vite'; | ||
export declare const viteImportCssSheet: (options?: Partial<{ | ||
export declare const importCSSSheet: (options?: Partial<{ | ||
transformers: ((code: string, id: string) => string)[]; | ||
additionalCode: string[]; | ||
transformers: ((code: string, id: string) => string)[]; | ||
}> | undefined) => Plugin; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,44 +0,12 @@ | ||
import { readFile as l } from "node:fs/promises"; | ||
const v = (r) => { | ||
const { transformers: d = [], additionalCode: f = [] } = r ?? {}, o = /* @__PURE__ */ new Map(), i = [".ts", ".mts", ".js", ".mjs"], h = { | ||
"\\": "\\\\", | ||
"`": "\\`", | ||
$: "\\$" | ||
}, u = (t) => new RegExp(t + `['"] *(?:with|assert) *{[(?:\r? | ||
) ]*type: *['"]css['"][(?:\r? | ||
) ]*};`), y = (t) => { | ||
let e = ""; | ||
for (const s of t) | ||
e += h[s] || s; | ||
return `\`${e}\``; | ||
}; | ||
import { ImportCSSSheet as i } from "./import-css-sheet.js"; | ||
const m = (r) => { | ||
const { transformers: o = [], additionalCode: n = [] } = r ?? {}, t = new i(o, n); | ||
return { | ||
enforce: "pre", | ||
name: "vite-import-css-sheet", | ||
async resolveId(t, e) { | ||
if (!t.endsWith(".css") || !e) | ||
return; | ||
const s = await this.resolve(t, e); | ||
if (e = e == null ? void 0 : e.split("?")[0], s && i.some((n) => e == null ? void 0 : e.endsWith(n))) { | ||
const n = await l(e, { encoding: "utf8" }); | ||
if (u(t).test(n)) { | ||
const a = "\0virtual:" + t.replace(".css", ".stylesheet"); | ||
return o.set(a, s.id), a; | ||
} | ||
} | ||
name: "vite-plugin-import-css-sheet", | ||
async resolveId(e, s) { | ||
return t.resolveId(this, e, s); | ||
}, | ||
async load(t) { | ||
if (!o.has(t)) | ||
return; | ||
const e = o.get(t); | ||
let s = await l(e, { encoding: "utf8" }); | ||
this.addWatchFile(e); | ||
for (const c of d) | ||
s = c(s, e); | ||
return `const styles = ${y(s)} | ||
${f.join(` | ||
`)} | ||
const sheet = new CSSStyleSheet(); | ||
sheet.replaceSync(styles); | ||
export default sheet;`; | ||
async load(e) { | ||
return t.load(this, e); | ||
} | ||
@@ -48,4 +16,4 @@ }; | ||
export { | ||
v as viteImportCssSheet | ||
m as importCSSSheet | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vite-plugin-import-css-sheet", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Small plugin that enables the use of tc39/proposal-import-attributes for css files in vite.", | ||
@@ -34,3 +34,7 @@ "license": "MIT", | ||
"typescript": "^5.4.2" | ||
} | ||
}, | ||
"engines": { | ||
"node": ">=20.10" | ||
}, | ||
"packageManager": "pnpm@8.15.4" | ||
} |
@@ -1,77 +0,23 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import type { Plugin } from 'vite'; | ||
import { ImportCSSSheet } from './import-css-sheet.js'; | ||
export const viteImportCssSheet = (options?: Partial<{ | ||
export const importCSSSheet = (options?: Partial<{ | ||
transformers: ((code: string, id: string) => string)[]; | ||
additionalCode: string[], | ||
transformers: ((code: string, id: string) => string)[]; | ||
}>): Plugin => { | ||
const { transformers = [], additionalCode = [] } = options ?? {}; | ||
const importSheet = new ImportCSSSheet(transformers, additionalCode); | ||
const virtualModules = new Map<string, string>(); | ||
const filetypes = [ '.ts', '.mts', '.js', '.mjs' ] as const; | ||
const illegalChars: Record<string, string> = { | ||
'\\': '\\\\', | ||
'`': '\\`', | ||
'$': '\\$', | ||
}; | ||
const cssImportAssertRegex = (str: string) => | ||
new RegExp(str + `['"] *(?:with|assert) *{[(?:\r?\n) \t]*type: *['"]css['"][(?:\r?\n) ]*};`); | ||
const convert = (str: string) => { | ||
let res = ''; | ||
for (const c of str) | ||
res += illegalChars[c] || c; | ||
return `\`${ res }\``; | ||
}; | ||
return { | ||
enforce: 'pre', | ||
name: 'vite-import-css-sheet', | ||
name: 'vite-plugin-import-css-sheet', | ||
async resolveId(source, importer) { | ||
if (!source.endsWith('.css')) | ||
return; | ||
if (!importer) | ||
return; | ||
const resolvedId = await this.resolve(source, importer); | ||
importer = importer?.split('?')[0]; | ||
if (resolvedId && filetypes.some(str => importer?.endsWith(str))) { | ||
const importerContent = await readFile(importer!, { encoding: 'utf8' }); | ||
const regxp = cssImportAssertRegex(source); | ||
if (regxp.test(importerContent)) { | ||
const modId = '\0virtual:' + source.replace('.css', '.stylesheet'); | ||
virtualModules.set(modId, resolvedId.id); | ||
return modId; | ||
} | ||
} | ||
return importSheet.resolveId(this, source, importer); | ||
}, | ||
async load(id) { | ||
if (!virtualModules.has(id)) | ||
return; | ||
const realId = virtualModules.get(id)!; | ||
let fileContent = await readFile(realId, { encoding: 'utf8' }); | ||
this.addWatchFile(realId); | ||
for (const transform of transformers) | ||
fileContent = transform(fileContent, realId); | ||
const createCode = | ||
`const styles = ${ convert(fileContent) }` | ||
+ `\n${ additionalCode.join('\n') }` | ||
+ '\nconst sheet = new CSSStyleSheet();' | ||
+ '\nsheet.replaceSync(styles);' | ||
+ '\nexport default sheet;'; | ||
return createCode; | ||
return importSheet.load(this, id); | ||
}, | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15065
45.19%14
55.56%193
44.03%1
Infinity%