vite-plugin-import-css-sheet
Advanced tools
Comparing version
import type { Plugin } from 'vite'; | ||
export declare const viteImportCssSheet: () => Plugin; | ||
export declare const viteImportCssSheet: (transformers?: ((code: string, id: string) => string)[]) => Plugin; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,9 +0,8 @@ | ||
import { randomUUID as h } from "node:crypto"; | ||
import { readFile as r } from "node:fs/promises"; | ||
const v = () => { | ||
const n = /* @__PURE__ */ new Map(), a = [".ts", ".mts", ".js", ".mjs"], l = { | ||
import { readFile as l } from "node:fs/promises"; | ||
const g = (r = []) => { | ||
const c = /* @__PURE__ */ new Map(), d = [".ts", ".mts", ".js", ".mjs"], f = { | ||
"\\": "\\\\", | ||
"`": "\\`", | ||
$: "\\$" | ||
}, d = (t) => new RegExp(t + `['"] *(?:with|assert) *{[(?:\r? | ||
}, h = (t) => new RegExp(t + `['"] *(?:with|assert) *{[(?:\r? | ||
) ]*type: *['"]css['"][(?:\r? | ||
@@ -13,3 +12,3 @@ ) ]*};`), i = (t) => { | ||
for (const s of t) | ||
e += l[s] || s; | ||
e += f[s] || s; | ||
return `\`${e}\``; | ||
@@ -21,12 +20,10 @@ }; | ||
async resolveId(t, e) { | ||
if (t.endsWith(".css")) { | ||
if (!e) | ||
return; | ||
const s = await this.resolve(t, e); | ||
if (e = e == null ? void 0 : e.split("?")[0], s && a.some((o) => e == null ? void 0 : e.endsWith(o))) { | ||
const o = await r(e, { encoding: "utf8" }); | ||
if (d(t).test(o)) { | ||
const c = "\0virtual:" + h().replaceAll("-", ""); | ||
return n.set(c, s.id), c; | ||
} | ||
if (!t.endsWith(".css") || !e) | ||
return; | ||
const s = await this.resolve(t, e); | ||
if (e = e == null ? void 0 : e.split("?")[0], s && d.some((n) => e == null ? void 0 : e.endsWith(n))) { | ||
const n = await l(e, { encoding: "utf8" }); | ||
if (h(t).test(n)) { | ||
const o = "\0virtual:" + t.replace(".css", ".stylesheet"); | ||
return c.set(o, s.id), o; | ||
} | ||
@@ -36,21 +33,12 @@ } | ||
async load(t) { | ||
if (n.has(t)) { | ||
const e = n.get(t); | ||
try { | ||
const s = await r(e, { encoding: "utf8" }); | ||
return this.addWatchFile(e), ` | ||
let sheet; | ||
try { | ||
sheet = new CSSStyleSheet() | ||
sheet.replaceSync(${i(s)}); | ||
} catch(err) { | ||
console.error('Constructable Stylesheets are not supported in your environment.') | ||
} | ||
export default sheet; | ||
`; | ||
} catch { | ||
console.error("Unable to load asserted css file:" + e); | ||
} | ||
} | ||
if (!c.has(t)) | ||
return; | ||
const e = c.get(t); | ||
let s = await l(e, { encoding: "utf8" }); | ||
this.addWatchFile(e); | ||
for (const a of r) | ||
s = a(s, e); | ||
return `const sheet = new CSSStyleSheet(); | ||
sheet.replaceSync(${i(s)}); | ||
export default sheet;`; | ||
} | ||
@@ -60,4 +48,4 @@ }; | ||
export { | ||
v as viteImportCssSheet | ||
g as viteImportCssSheet | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "vite-plugin-import-css-sheet", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Small plugin that enables the use of tc39/proposal-import-attributes for css files in vite.", | ||
@@ -17,4 +17,4 @@ "license": "MIT", | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
@@ -24,18 +24,14 @@ "build": "vite build && tsc --project ./src/tsconfig.json", | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.6.2", | ||
"vite": "^5.0.10" | ||
"peerDependencies": { | ||
"vite": ">=5.1.6" | ||
}, | ||
"devDependencies": { | ||
"@roenlie/eslint-config": "^1.3.0", | ||
"@roenlie/package-toolbox": "^0.0.22", | ||
"@roenlie/tsconfig": "^1.0.2", | ||
"@types/node": "^20.10.5", | ||
"@typescript-eslint/eslint-plugin": "^6.15.0", | ||
"@typescript-eslint/parser": "^6.15.0", | ||
"eslint": "^8.56.0", | ||
"eslint-plugin-import": "^2.29.1", | ||
"eslint-plugin-simple-import-sort": "^10.0.0", | ||
"typescript": "^5.3.3" | ||
"tslib": "^2.6.2", | ||
"vite": "^5.1.6", | ||
"@roenlie/eslint-config": "^1.3.2", | ||
"@roenlie/package-toolbox": "^1.0.7", | ||
"@roenlie/tsconfig": "^1.0.4", | ||
"@types/node": "^20.11.28", | ||
"typescript": "^5.4.2" | ||
} | ||
} |
@@ -1,2 +0,1 @@ | ||
import { randomUUID } from 'node:crypto'; | ||
import { readFile } from 'node:fs/promises'; | ||
@@ -7,3 +6,8 @@ | ||
export const viteImportCssSheet = (): Plugin => { | ||
export const viteImportCssSheet = (options?: Partial<{ | ||
additionalCode: string[], | ||
transformers: ((code: string, id: string) => string)[]; | ||
}>): Plugin => { | ||
const { transformers = [], additionalCode = [] } = options ?? {}; | ||
const virtualModules = new Map<string, string>(); | ||
@@ -32,19 +36,19 @@ const filetypes = [ '.ts', '.mts', '.js', '.mjs' ] as const; | ||
async resolveId(source, importer) { | ||
if (source.endsWith('.css')) { | ||
if (!importer) | ||
return; | ||
if (!source.endsWith('.css')) | ||
return; | ||
if (!importer) | ||
return; | ||
const resolvedId = await this.resolve(source, importer); | ||
importer = importer?.split('?')[0]; | ||
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 (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:' + randomUUID().replaceAll('-', ''); | ||
virtualModules.set(modId, resolvedId.id); | ||
if (regxp.test(importerContent)) { | ||
const modId = '\0virtual:' + source.replace('.css', '.stylesheet'); | ||
virtualModules.set(modId, resolvedId.id); | ||
return modId; | ||
} | ||
return modId; | ||
} | ||
@@ -54,27 +58,23 @@ } | ||
async load(id) { | ||
if (virtualModules.has(id)) { | ||
const realId = virtualModules.get(id)!; | ||
if (!virtualModules.has(id)) | ||
return; | ||
try { | ||
const fileContent = await readFile(realId, { encoding: 'utf8' }); | ||
this.addWatchFile(realId); | ||
const realId = virtualModules.get(id)!; | ||
return ` | ||
let sheet; | ||
try { | ||
sheet = new CSSStyleSheet() | ||
sheet.replaceSync(${ convert(fileContent) }); | ||
} catch(err) { | ||
console.error('Constructable Stylesheets are not supported in your environment.') | ||
} | ||
let fileContent = await readFile(realId, { encoding: 'utf8' }); | ||
this.addWatchFile(realId); | ||
export default sheet; | ||
`; | ||
} | ||
catch (err) { | ||
console.error('Unable to load asserted css file:' + 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; | ||
}, | ||
}; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
-50%7
-30%9856
-5.43%128
-9.86%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed