unplugin-inline-css-modules 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +408 -0
- package/dist/astro.d.mts +8 -0
- package/dist/astro.d.ts +8 -0
- package/dist/astro.js +185 -0
- package/dist/astro.js.map +1 -0
- package/dist/astro.mjs +158 -0
- package/dist/astro.mjs.map +1 -0
- package/dist/bun.d.mts +7 -0
- package/dist/bun.d.ts +7 -0
- package/dist/bun.js +167 -0
- package/dist/bun.js.map +1 -0
- package/dist/bun.mjs +142 -0
- package/dist/bun.mjs.map +1 -0
- package/dist/esbuild.d.mts +7 -0
- package/dist/esbuild.d.ts +7 -0
- package/dist/esbuild.js +167 -0
- package/dist/esbuild.js.map +1 -0
- package/dist/esbuild.mjs +142 -0
- package/dist/esbuild.mjs.map +1 -0
- package/dist/farm.d.mts +6 -0
- package/dist/farm.d.ts +6 -0
- package/dist/farm.js +167 -0
- package/dist/farm.js.map +1 -0
- package/dist/farm.mjs +142 -0
- package/dist/farm.mjs.map +1 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +166 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +139 -0
- package/dist/index.mjs.map +1 -0
- package/dist/loader.cjs +6 -0
- package/dist/next.d.mts +9 -0
- package/dist/next.d.ts +9 -0
- package/dist/next.js +193 -0
- package/dist/next.js.map +1 -0
- package/dist/next.mjs +167 -0
- package/dist/next.mjs.map +1 -0
- package/dist/nuxt.d.mts +7 -0
- package/dist/nuxt.d.ts +7 -0
- package/dist/nuxt.js +169 -0
- package/dist/nuxt.js.map +1 -0
- package/dist/nuxt.mjs +144 -0
- package/dist/nuxt.mjs.map +1 -0
- package/dist/rolldown.d.mts +8 -0
- package/dist/rolldown.d.ts +8 -0
- package/dist/rolldown.js +167 -0
- package/dist/rolldown.js.map +1 -0
- package/dist/rolldown.mjs +142 -0
- package/dist/rolldown.mjs.map +1 -0
- package/dist/rollup.d.mts +8 -0
- package/dist/rollup.d.ts +8 -0
- package/dist/rollup.js +167 -0
- package/dist/rollup.js.map +1 -0
- package/dist/rollup.mjs +142 -0
- package/dist/rollup.mjs.map +1 -0
- package/dist/rspack.d.mts +7 -0
- package/dist/rspack.d.ts +7 -0
- package/dist/rspack.js +167 -0
- package/dist/rspack.js.map +1 -0
- package/dist/rspack.mjs +142 -0
- package/dist/rspack.mjs.map +1 -0
- package/dist/vite.d.mts +8 -0
- package/dist/vite.d.ts +8 -0
- package/dist/vite.js +167 -0
- package/dist/vite.js.map +1 -0
- package/dist/vite.mjs +142 -0
- package/dist/vite.mjs.map +1 -0
- package/dist/webpack.d.mts +7 -0
- package/dist/webpack.d.ts +7 -0
- package/dist/webpack.js +167 -0
- package/dist/webpack.js.map +1 -0
- package/dist/webpack.mjs +142 -0
- package/dist/webpack.mjs.map +1 -0
- package/package.json +197 -0
package/dist/nuxt.mjs
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// src/nuxt.ts
|
|
2
|
+
import { createWebpackPlugin } from "unplugin";
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { createUnplugin } from "unplugin";
|
|
6
|
+
import { createHash } from "crypto";
|
|
7
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
10
|
+
var css = (_) => ({});
|
|
11
|
+
var virtualModuleId = "virtual:inline-css-modules";
|
|
12
|
+
var webpackModuleId = "inline-css-modules/virtual";
|
|
13
|
+
var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
14
|
+
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
15
|
+
var cssModules = {};
|
|
16
|
+
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
17
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
18
|
+
function ensureCacheDir() {
|
|
19
|
+
const dir = getCacheDir();
|
|
20
|
+
if (!existsSync(dir)) {
|
|
21
|
+
mkdirSync(dir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function hashCss(css2) {
|
|
25
|
+
return createHash("md5").update(css2).digest("hex");
|
|
26
|
+
}
|
|
27
|
+
function readFromCache(hash) {
|
|
28
|
+
const path = getCachePath(hash);
|
|
29
|
+
if (existsSync(path)) {
|
|
30
|
+
return readFileSync(path, "utf-8");
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function writeToCache(hash, css2) {
|
|
35
|
+
ensureCacheDir();
|
|
36
|
+
writeFileSync(getCachePath(hash), css2);
|
|
37
|
+
}
|
|
38
|
+
var unpluginFactory = (config = {}, meta) => {
|
|
39
|
+
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
40
|
+
const tagName = config.tagName ?? "css";
|
|
41
|
+
const preprocessor = config.extension ?? "css";
|
|
42
|
+
const isWebpack = meta?.framework === "webpack" || meta?.framework === "rspack";
|
|
43
|
+
const moduleId = isWebpack ? webpackModuleId : virtualModuleId;
|
|
44
|
+
const resolvedId = isWebpack ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
45
|
+
return {
|
|
46
|
+
name: "inline-css-modules",
|
|
47
|
+
enforce: "pre",
|
|
48
|
+
resolveId(id) {
|
|
49
|
+
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
50
|
+
return resolvedId + id.slice(moduleId.length);
|
|
51
|
+
}
|
|
52
|
+
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
53
|
+
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
54
|
+
}
|
|
55
|
+
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
56
|
+
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
57
|
+
}
|
|
58
|
+
return void 0;
|
|
59
|
+
},
|
|
60
|
+
loadInclude(id) {
|
|
61
|
+
return id.startsWith(resolvedVirtualModuleId) || id.startsWith(resolvedWebpackModuleId);
|
|
62
|
+
},
|
|
63
|
+
load(id) {
|
|
64
|
+
if (!id.startsWith(resolvedVirtualModuleId + "/") && !id.startsWith(resolvedWebpackModuleId + "/"))
|
|
65
|
+
return void 0;
|
|
66
|
+
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
67
|
+
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
68
|
+
const css2 = cssModules[file];
|
|
69
|
+
if (!css2) {
|
|
70
|
+
if (isWebpack) {
|
|
71
|
+
const hash = file.replace(/\.module\.\w+$/, "");
|
|
72
|
+
const cached = readFromCache(hash);
|
|
73
|
+
if (cached) {
|
|
74
|
+
return {
|
|
75
|
+
code: cached,
|
|
76
|
+
map: null
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
code: css2,
|
|
84
|
+
map: null
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
transform: {
|
|
88
|
+
filter: {
|
|
89
|
+
id: fileMatch
|
|
90
|
+
},
|
|
91
|
+
handler(src, id) {
|
|
92
|
+
let imports = [];
|
|
93
|
+
src = src.replace(
|
|
94
|
+
/import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm,
|
|
95
|
+
""
|
|
96
|
+
);
|
|
97
|
+
src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {
|
|
98
|
+
const [variableName, tag, css2] = args;
|
|
99
|
+
if (tag !== tagName) return substring;
|
|
100
|
+
let baseFilename = id.slice(id.lastIndexOf("/") + 1);
|
|
101
|
+
baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf("."));
|
|
102
|
+
let cnt = 0;
|
|
103
|
+
const ext = typeof preprocessor == "function" ? preprocessor(baseFilename) : preprocessor;
|
|
104
|
+
let filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
105
|
+
while (cssModules[filename]) {
|
|
106
|
+
cnt++;
|
|
107
|
+
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
108
|
+
}
|
|
109
|
+
cssModules[filename] = css2;
|
|
110
|
+
let importStatement;
|
|
111
|
+
if (isWebpack) {
|
|
112
|
+
const hash = hashCss(css2);
|
|
113
|
+
writeToCache(hash, css2);
|
|
114
|
+
importStatement = `import ${variableName} from "${moduleId}/${hash}.module.${ext}"`;
|
|
115
|
+
}
|
|
116
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
117
|
+
if (config.inlineImport === false) {
|
|
118
|
+
imports.push(importStatement);
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
return importStatement;
|
|
122
|
+
});
|
|
123
|
+
if (imports.length) {
|
|
124
|
+
return imports.join("\n") + "\n" + src;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
code: src,
|
|
128
|
+
map: null
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
var unplugin = createUnplugin(unpluginFactory);
|
|
135
|
+
|
|
136
|
+
// src/nuxt.ts
|
|
137
|
+
function nuxt_default(options = {}) {
|
|
138
|
+
return createWebpackPlugin(unpluginFactory)(options);
|
|
139
|
+
}
|
|
140
|
+
export {
|
|
141
|
+
css,
|
|
142
|
+
nuxt_default as default
|
|
143
|
+
};
|
|
144
|
+
//# sourceMappingURL=nuxt.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/nuxt.ts","../src/index.ts"],"sourcesContent":["import { createWebpackPlugin } from 'unplugin'\nimport type { PluginConfig } from './index'\n\nexport default function (options: PluginConfig = {}) {\n return createWebpackPlugin(unpluginFactory)(options)\n}\n\nexport { css } from './index'\nexport type { PluginConfig } from './index'\n\nimport { unpluginFactory } from './index'\n","import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync, readFileSync, writeFileSync, existsSync } from 'fs'\nimport { join } from 'path'\n\ntype SupportedExtension = 'css' | 'scss' | 'sass' | 'styl' | 'less'\n\nexport type PluginConfig = {\n fileMatch?: RegExp\n tagName?: string\n extension?: SupportedExtension | ((filename: string) => SupportedExtension)\n inlineImport?: boolean\n}\n\nconst matchInlineCssModules =\n /(?:const|var|let)\\s*(\\w+)(?:\\s*:.*)?\\s*=\\s*(\\w+)\\s*`([\\s\\S]*?)`/gm\n\nexport const css = (_: TemplateStringsArray): Record<string, string> => ({})\n\nconst virtualModuleId = 'virtual:inline-css-modules'\nconst webpackModuleId = 'inline-css-modules/virtual'\nconst resolvedVirtualModuleId = '\\0' + virtualModuleId\nconst resolvedWebpackModuleId = '\\0inline-css-modules/virtual'\n\nlet cssModules: Record<string, string> = {}\n\nconst getCacheDir = () =>\n join(process.cwd(), 'node_modules', '.cache', 'inline-css-modules')\nconst getCachePath = (hash: string) => join(getCacheDir(), `${hash}.css`)\n\nfunction ensureCacheDir() {\n const dir = getCacheDir()\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true })\n }\n}\n\nfunction hashCss(css: string): string {\n return createHash('md5').update(css).digest('hex')\n}\n\nfunction readFromCache(hash: string): string | null {\n const path = getCachePath(hash)\n if (existsSync(path)) {\n return readFileSync(path, 'utf-8')\n }\n return null\n}\n\nfunction writeToCache(hash: string, css: string): void {\n ensureCacheDir()\n writeFileSync(getCachePath(hash), css)\n}\n\nexport const unpluginFactory: UnpluginFactory<PluginConfig | undefined> = (\n config = {},\n meta\n) => {\n const fileMatch = config.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte)$/\n const tagName = config.tagName ?? 'css'\n const preprocessor = config.extension ?? 'css'\n const isWebpack =\n meta?.framework === 'webpack' || meta?.framework === 'rspack'\n const moduleId = isWebpack ? webpackModuleId : virtualModuleId\n const resolvedId = isWebpack\n ? resolvedWebpackModuleId\n : resolvedVirtualModuleId\n\n return {\n name: 'inline-css-modules',\n enforce: 'pre',\n resolveId(id) {\n if (id === moduleId || id.startsWith(moduleId + '/')) {\n return resolvedId + id.slice(moduleId.length)\n }\n if (id === virtualModuleId || id.startsWith(virtualModuleId + '/')) {\n return resolvedVirtualModuleId + id.slice(virtualModuleId.length)\n }\n if (id === webpackModuleId || id.startsWith(webpackModuleId + '/')) {\n return resolvedWebpackModuleId + id.slice(webpackModuleId.length)\n }\n return undefined\n },\n loadInclude(id) {\n return (\n id.startsWith(resolvedVirtualModuleId) ||\n id.startsWith(resolvedWebpackModuleId)\n )\n },\n load(id) {\n if (\n !id.startsWith(resolvedVirtualModuleId + '/') &&\n !id.startsWith(resolvedWebpackModuleId + '/')\n )\n return undefined\n\n const prefix = id.startsWith(resolvedVirtualModuleId + '/')\n ? resolvedVirtualModuleId + '/'\n : resolvedWebpackModuleId + '/'\n const file = id.slice(prefix.length).replace(/\\?used$/, '')\n const css = cssModules[file]\n\n if (!css) {\n if (isWebpack) {\n const hash = file.replace(/\\.module\\.\\w+$/, '')\n const cached = readFromCache(hash)\n if (cached) {\n return {\n code: cached,\n map: null,\n }\n }\n }\n return undefined\n }\n return {\n code: css,\n map: null,\n }\n },\n transform: {\n filter: {\n id: fileMatch,\n },\n handler(src, id) {\n // Build up a list of import statements to inject to the top of the file\n let imports: string[] = []\n\n src = src.replace(\n /import\\s*{\\s*(?:css|inlineCss)\\s*(?:as\\s*\\w+\\s*)?}\\s*from\\s*('|\"|`)unplugin-inline-css-modules\\1;?/gm,\n ''\n )\n\n src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {\n const [variableName, tag, css] = args\n\n if (tag !== tagName) return substring\n\n let baseFilename = id.slice(id.lastIndexOf('/') + 1)\n baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf('.'))\n let cnt = 0\n const ext =\n typeof preprocessor == 'function'\n ? preprocessor(baseFilename)\n : preprocessor\n let filename = `${baseFilename}-${cnt}.module.${ext}`\n while (cssModules[filename]) {\n cnt++\n filename = `${baseFilename}-${cnt}.module.${ext}`\n }\n cssModules[filename] = css\n\n let importStatement\n\n if (isWebpack) {\n const hash = hashCss(css)\n writeToCache(hash, css)\n importStatement = `import ${variableName} from \"${moduleId}/${hash}.module.${ext}\"`\n }\n importStatement = `import ${variableName} from \"${moduleId}/${filename}\"`\n\n if (config.inlineImport === false) {\n imports.push(importStatement)\n return ''\n }\n return importStatement\n })\n if (imports.length) {\n return imports.join('\\n') + '\\n' + src\n }\n return {\n code: src,\n map: null,\n }\n },\n },\n }\n}\n\nexport const unplugin = createUnplugin(unpluginFactory)\n\nexport default unplugin\n"],"mappings":";AAAA,SAAS,2BAA2B;;;ACCpC,SAAS,sBAAsB;AAC/B,SAAS,kBAAkB;AAC3B,SAAS,WAAW,cAAc,eAAe,kBAAkB;AACnE,SAAS,YAAY;AAWrB,IAAM,wBACJ;AAEK,IAAM,MAAM,CAAC,OAAqD,CAAC;AAE1E,IAAM,kBAAkB;AACxB,IAAM,kBAAkB;AACxB,IAAM,0BAA0B,OAAO;AACvC,IAAM,0BAA0B;AAEhC,IAAI,aAAqC,CAAC;AAE1C,IAAM,cAAc,MAClB,KAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,SAAiB,KAAK,YAAY,GAAG,GAAG,IAAI,MAAM;AAExE,SAAS,iBAAiB;AACxB,QAAM,MAAM,YAAY;AACxB,MAAI,CAAC,WAAW,GAAG,GAAG;AACpB,cAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAAA,EACpC;AACF;AAEA,SAAS,QAAQA,MAAqB;AACpC,SAAO,WAAW,KAAK,EAAE,OAAOA,IAAG,EAAE,OAAO,KAAK;AACnD;AAEA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AU,QAAQ,IAAI,QAAQ;AAEtE,cAAI,OAAO,iBAAiB,OAAO;AACjC,oBAAQ,KAAK,eAAe;AAC5B,mBAAO;AAAA,UACT;AACA,iBAAO;AAAA,QACT,CAAC;AACD,YAAI,QAAQ,QAAQ;AAClB,iBAAO,QAAQ,KAAK,IAAI,IAAI,OAAO;AAAA,QACrC;AACA,eAAO;AAAA,UACL,MAAM;AAAA,UACN,KAAK;AAAA,QACP;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEO,IAAM,WAAW,eAAe,eAAe;;;ADjLvC,SAAR,aAAkB,UAAwB,CAAC,GAAG;AACnD,SAAO,oBAAoB,eAAe,EAAE,OAAO;AACrD;","names":["css"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as rolldown from 'rolldown';
|
|
2
|
+
import { PluginConfig } from './index.mjs';
|
|
3
|
+
export { css } from './index.mjs';
|
|
4
|
+
import 'unplugin';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options?: PluginConfig | undefined) => rolldown.Plugin<any> | rolldown.Plugin<any>[];
|
|
7
|
+
|
|
8
|
+
export { PluginConfig, _default as default };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as rolldown from 'rolldown';
|
|
2
|
+
import { PluginConfig } from './index.js';
|
|
3
|
+
export { css } from './index.js';
|
|
4
|
+
import 'unplugin';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options?: PluginConfig | undefined) => rolldown.Plugin<any> | rolldown.Plugin<any>[];
|
|
7
|
+
|
|
8
|
+
export { PluginConfig, _default as default };
|
package/dist/rolldown.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/rolldown.ts
|
|
21
|
+
var rolldown_exports = {};
|
|
22
|
+
__export(rolldown_exports, {
|
|
23
|
+
css: () => css,
|
|
24
|
+
default: () => rolldown_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(rolldown_exports);
|
|
27
|
+
var import_unplugin2 = require("unplugin");
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var import_unplugin = require("unplugin");
|
|
31
|
+
var import_crypto = require("crypto");
|
|
32
|
+
var import_fs = require("fs");
|
|
33
|
+
var import_path = require("path");
|
|
34
|
+
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
35
|
+
var css = (_) => ({});
|
|
36
|
+
var virtualModuleId = "virtual:inline-css-modules";
|
|
37
|
+
var webpackModuleId = "inline-css-modules/virtual";
|
|
38
|
+
var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
39
|
+
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
40
|
+
var cssModules = {};
|
|
41
|
+
var getCacheDir = () => (0, import_path.join)(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
42
|
+
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.css`);
|
|
43
|
+
function ensureCacheDir() {
|
|
44
|
+
const dir = getCacheDir();
|
|
45
|
+
if (!(0, import_fs.existsSync)(dir)) {
|
|
46
|
+
(0, import_fs.mkdirSync)(dir, { recursive: true });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function hashCss(css2) {
|
|
50
|
+
return (0, import_crypto.createHash)("md5").update(css2).digest("hex");
|
|
51
|
+
}
|
|
52
|
+
function readFromCache(hash) {
|
|
53
|
+
const path = getCachePath(hash);
|
|
54
|
+
if ((0, import_fs.existsSync)(path)) {
|
|
55
|
+
return (0, import_fs.readFileSync)(path, "utf-8");
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
function writeToCache(hash, css2) {
|
|
60
|
+
ensureCacheDir();
|
|
61
|
+
(0, import_fs.writeFileSync)(getCachePath(hash), css2);
|
|
62
|
+
}
|
|
63
|
+
var unpluginFactory = (config = {}, meta) => {
|
|
64
|
+
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
65
|
+
const tagName = config.tagName ?? "css";
|
|
66
|
+
const preprocessor = config.extension ?? "css";
|
|
67
|
+
const isWebpack = meta?.framework === "webpack" || meta?.framework === "rspack";
|
|
68
|
+
const moduleId = isWebpack ? webpackModuleId : virtualModuleId;
|
|
69
|
+
const resolvedId = isWebpack ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
70
|
+
return {
|
|
71
|
+
name: "inline-css-modules",
|
|
72
|
+
enforce: "pre",
|
|
73
|
+
resolveId(id) {
|
|
74
|
+
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
75
|
+
return resolvedId + id.slice(moduleId.length);
|
|
76
|
+
}
|
|
77
|
+
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
78
|
+
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
79
|
+
}
|
|
80
|
+
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
81
|
+
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
82
|
+
}
|
|
83
|
+
return void 0;
|
|
84
|
+
},
|
|
85
|
+
loadInclude(id) {
|
|
86
|
+
return id.startsWith(resolvedVirtualModuleId) || id.startsWith(resolvedWebpackModuleId);
|
|
87
|
+
},
|
|
88
|
+
load(id) {
|
|
89
|
+
if (!id.startsWith(resolvedVirtualModuleId + "/") && !id.startsWith(resolvedWebpackModuleId + "/"))
|
|
90
|
+
return void 0;
|
|
91
|
+
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
92
|
+
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
93
|
+
const css2 = cssModules[file];
|
|
94
|
+
if (!css2) {
|
|
95
|
+
if (isWebpack) {
|
|
96
|
+
const hash = file.replace(/\.module\.\w+$/, "");
|
|
97
|
+
const cached = readFromCache(hash);
|
|
98
|
+
if (cached) {
|
|
99
|
+
return {
|
|
100
|
+
code: cached,
|
|
101
|
+
map: null
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return void 0;
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
code: css2,
|
|
109
|
+
map: null
|
|
110
|
+
};
|
|
111
|
+
},
|
|
112
|
+
transform: {
|
|
113
|
+
filter: {
|
|
114
|
+
id: fileMatch
|
|
115
|
+
},
|
|
116
|
+
handler(src, id) {
|
|
117
|
+
let imports = [];
|
|
118
|
+
src = src.replace(
|
|
119
|
+
/import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm,
|
|
120
|
+
""
|
|
121
|
+
);
|
|
122
|
+
src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {
|
|
123
|
+
const [variableName, tag, css2] = args;
|
|
124
|
+
if (tag !== tagName) return substring;
|
|
125
|
+
let baseFilename = id.slice(id.lastIndexOf("/") + 1);
|
|
126
|
+
baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf("."));
|
|
127
|
+
let cnt = 0;
|
|
128
|
+
const ext = typeof preprocessor == "function" ? preprocessor(baseFilename) : preprocessor;
|
|
129
|
+
let filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
130
|
+
while (cssModules[filename]) {
|
|
131
|
+
cnt++;
|
|
132
|
+
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
133
|
+
}
|
|
134
|
+
cssModules[filename] = css2;
|
|
135
|
+
let importStatement;
|
|
136
|
+
if (isWebpack) {
|
|
137
|
+
const hash = hashCss(css2);
|
|
138
|
+
writeToCache(hash, css2);
|
|
139
|
+
importStatement = `import ${variableName} from "${moduleId}/${hash}.module.${ext}"`;
|
|
140
|
+
}
|
|
141
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
142
|
+
if (config.inlineImport === false) {
|
|
143
|
+
imports.push(importStatement);
|
|
144
|
+
return "";
|
|
145
|
+
}
|
|
146
|
+
return importStatement;
|
|
147
|
+
});
|
|
148
|
+
if (imports.length) {
|
|
149
|
+
return imports.join("\n") + "\n" + src;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
code: src,
|
|
153
|
+
map: null
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
var unplugin = (0, import_unplugin.createUnplugin)(unpluginFactory);
|
|
160
|
+
|
|
161
|
+
// src/rolldown.ts
|
|
162
|
+
var rolldown_default = (0, import_unplugin2.createRolldownPlugin)(unpluginFactory);
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
css
|
|
166
|
+
});
|
|
167
|
+
//# sourceMappingURL=rolldown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rolldown.ts","../src/index.ts"],"sourcesContent":["import { createRolldownPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRolldownPlugin(unpluginFactory)\nexport { css } from './index'\nexport type { PluginConfig } from './index'\n","import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync, readFileSync, writeFileSync, existsSync } from 'fs'\nimport { join } from 'path'\n\ntype SupportedExtension = 'css' | 'scss' | 'sass' | 'styl' | 'less'\n\nexport type PluginConfig = {\n fileMatch?: RegExp\n tagName?: string\n extension?: SupportedExtension | ((filename: string) => SupportedExtension)\n inlineImport?: boolean\n}\n\nconst matchInlineCssModules =\n /(?:const|var|let)\\s*(\\w+)(?:\\s*:.*)?\\s*=\\s*(\\w+)\\s*`([\\s\\S]*?)`/gm\n\nexport const css = (_: TemplateStringsArray): Record<string, string> => ({})\n\nconst virtualModuleId = 'virtual:inline-css-modules'\nconst webpackModuleId = 'inline-css-modules/virtual'\nconst resolvedVirtualModuleId = '\\0' + virtualModuleId\nconst resolvedWebpackModuleId = '\\0inline-css-modules/virtual'\n\nlet cssModules: Record<string, string> = {}\n\nconst getCacheDir = () =>\n join(process.cwd(), 'node_modules', '.cache', 'inline-css-modules')\nconst getCachePath = (hash: string) => join(getCacheDir(), `${hash}.css`)\n\nfunction ensureCacheDir() {\n const dir = getCacheDir()\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true })\n }\n}\n\nfunction hashCss(css: string): string {\n return createHash('md5').update(css).digest('hex')\n}\n\nfunction readFromCache(hash: string): string | null {\n const path = getCachePath(hash)\n if (existsSync(path)) {\n return readFileSync(path, 'utf-8')\n }\n return null\n}\n\nfunction writeToCache(hash: string, css: string): void {\n ensureCacheDir()\n writeFileSync(getCachePath(hash), css)\n}\n\nexport const unpluginFactory: UnpluginFactory<PluginConfig | undefined> = (\n config = {},\n meta\n) => {\n const fileMatch = config.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte)$/\n const tagName = config.tagName ?? 'css'\n const preprocessor = config.extension ?? 'css'\n const isWebpack =\n meta?.framework === 'webpack' || meta?.framework === 'rspack'\n const moduleId = isWebpack ? webpackModuleId : virtualModuleId\n const resolvedId = isWebpack\n ? resolvedWebpackModuleId\n : resolvedVirtualModuleId\n\n return {\n name: 'inline-css-modules',\n enforce: 'pre',\n resolveId(id) {\n if (id === moduleId || id.startsWith(moduleId + '/')) {\n return resolvedId + id.slice(moduleId.length)\n }\n if (id === virtualModuleId || id.startsWith(virtualModuleId + '/')) {\n return resolvedVirtualModuleId + id.slice(virtualModuleId.length)\n }\n if (id === webpackModuleId || id.startsWith(webpackModuleId + '/')) {\n return resolvedWebpackModuleId + id.slice(webpackModuleId.length)\n }\n return undefined\n },\n loadInclude(id) {\n return (\n id.startsWith(resolvedVirtualModuleId) ||\n id.startsWith(resolvedWebpackModuleId)\n )\n },\n load(id) {\n if (\n !id.startsWith(resolvedVirtualModuleId + '/') &&\n !id.startsWith(resolvedWebpackModuleId + '/')\n )\n return undefined\n\n const prefix = id.startsWith(resolvedVirtualModuleId + '/')\n ? resolvedVirtualModuleId + '/'\n : resolvedWebpackModuleId + '/'\n const file = id.slice(prefix.length).replace(/\\?used$/, '')\n const css = cssModules[file]\n\n if (!css) {\n if (isWebpack) {\n const hash = file.replace(/\\.module\\.\\w+$/, '')\n const cached = readFromCache(hash)\n if (cached) {\n return {\n code: cached,\n map: null,\n }\n }\n }\n return undefined\n }\n return {\n code: css,\n map: null,\n }\n },\n transform: {\n filter: {\n id: fileMatch,\n },\n handler(src, id) {\n // Build up a list of import statements to inject to the top of the file\n let imports: string[] = []\n\n src = src.replace(\n /import\\s*{\\s*(?:css|inlineCss)\\s*(?:as\\s*\\w+\\s*)?}\\s*from\\s*('|\"|`)unplugin-inline-css-modules\\1;?/gm,\n ''\n )\n\n src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {\n const [variableName, tag, css] = args\n\n if (tag !== tagName) return substring\n\n let baseFilename = id.slice(id.lastIndexOf('/') + 1)\n baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf('.'))\n let cnt = 0\n const ext =\n typeof preprocessor == 'function'\n ? preprocessor(baseFilename)\n : preprocessor\n let filename = `${baseFilename}-${cnt}.module.${ext}`\n while (cssModules[filename]) {\n cnt++\n filename = `${baseFilename}-${cnt}.module.${ext}`\n }\n cssModules[filename] = css\n\n let importStatement\n\n if (isWebpack) {\n const hash = hashCss(css)\n writeToCache(hash, css)\n importStatement = `import ${variableName} from \"${moduleId}/${hash}.module.${ext}\"`\n }\n importStatement = `import ${variableName} from \"${moduleId}/${filename}\"`\n\n if (config.inlineImport === false) {\n imports.push(importStatement)\n return ''\n }\n return importStatement\n })\n if (imports.length) {\n return imports.join('\\n') + '\\n' + src\n }\n return {\n code: src,\n map: null,\n }\n },\n },\n }\n}\n\nexport const unplugin = createUnplugin(unpluginFactory)\n\nexport default unplugin\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,mBAAqC;;;ACCrC,sBAA+B;AAC/B,oBAA2B;AAC3B,gBAAmE;AACnE,kBAAqB;AAWrB,IAAM,wBACJ;AAEK,IAAM,MAAM,CAAC,OAAqD,CAAC;AAE1E,IAAM,kBAAkB;AACxB,IAAM,kBAAkB;AACxB,IAAM,0BAA0B,OAAO;AACvC,IAAM,0BAA0B;AAEhC,IAAI,aAAqC,CAAC;AAE1C,IAAM,cAAc,UAClB,kBAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,aAAiB,kBAAK,YAAY,GAAG,GAAG,IAAI,MAAM;AAExE,SAAS,iBAAiB;AACxB,QAAM,MAAM,YAAY;AACxB,MAAI,KAAC,sBAAW,GAAG,GAAG;AACpB,6BAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAAA,EACpC;AACF;AAEA,SAAS,QAAQC,MAAqB;AACpC,aAAO,0BAAW,KAAK,EAAE,OAAOA,IAAG,EAAE,OAAO,KAAK;AACnD;AAEA,SAA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,QAAQ,IAAI,QAAQ;AAEtE,cAAI,OAAO,iBAAiB,OAAO;AACjC,oBAAQ,KAAK,eAAe;AAC5B,mBAAO;AAAA,UACT;AACA,iBAAO;AAAA,QACT,CAAC;AACD,YAAI,QAAQ,QAAQ;AAClB,iBAAO,QAAQ,KAAK,IAAI,IAAI,OAAO;AAAA,QACrC;AACA,eAAO;AAAA,UACL,MAAM;AAAA,UACN,KAAK;AAAA,QACP;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEO,IAAM,eAAW,gCAAe,eAAe;;;ADjLtD,IAAO,uBAAQ,uCAAqB,eAAe;","names":["import_unplugin","css"]}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// src/rolldown.ts
|
|
2
|
+
import { createRolldownPlugin } from "unplugin";
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { createUnplugin } from "unplugin";
|
|
6
|
+
import { createHash } from "crypto";
|
|
7
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
10
|
+
var css = (_) => ({});
|
|
11
|
+
var virtualModuleId = "virtual:inline-css-modules";
|
|
12
|
+
var webpackModuleId = "inline-css-modules/virtual";
|
|
13
|
+
var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
14
|
+
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
15
|
+
var cssModules = {};
|
|
16
|
+
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
17
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
18
|
+
function ensureCacheDir() {
|
|
19
|
+
const dir = getCacheDir();
|
|
20
|
+
if (!existsSync(dir)) {
|
|
21
|
+
mkdirSync(dir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function hashCss(css2) {
|
|
25
|
+
return createHash("md5").update(css2).digest("hex");
|
|
26
|
+
}
|
|
27
|
+
function readFromCache(hash) {
|
|
28
|
+
const path = getCachePath(hash);
|
|
29
|
+
if (existsSync(path)) {
|
|
30
|
+
return readFileSync(path, "utf-8");
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
function writeToCache(hash, css2) {
|
|
35
|
+
ensureCacheDir();
|
|
36
|
+
writeFileSync(getCachePath(hash), css2);
|
|
37
|
+
}
|
|
38
|
+
var unpluginFactory = (config = {}, meta) => {
|
|
39
|
+
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
40
|
+
const tagName = config.tagName ?? "css";
|
|
41
|
+
const preprocessor = config.extension ?? "css";
|
|
42
|
+
const isWebpack = meta?.framework === "webpack" || meta?.framework === "rspack";
|
|
43
|
+
const moduleId = isWebpack ? webpackModuleId : virtualModuleId;
|
|
44
|
+
const resolvedId = isWebpack ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
45
|
+
return {
|
|
46
|
+
name: "inline-css-modules",
|
|
47
|
+
enforce: "pre",
|
|
48
|
+
resolveId(id) {
|
|
49
|
+
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
50
|
+
return resolvedId + id.slice(moduleId.length);
|
|
51
|
+
}
|
|
52
|
+
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
53
|
+
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
54
|
+
}
|
|
55
|
+
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
56
|
+
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
57
|
+
}
|
|
58
|
+
return void 0;
|
|
59
|
+
},
|
|
60
|
+
loadInclude(id) {
|
|
61
|
+
return id.startsWith(resolvedVirtualModuleId) || id.startsWith(resolvedWebpackModuleId);
|
|
62
|
+
},
|
|
63
|
+
load(id) {
|
|
64
|
+
if (!id.startsWith(resolvedVirtualModuleId + "/") && !id.startsWith(resolvedWebpackModuleId + "/"))
|
|
65
|
+
return void 0;
|
|
66
|
+
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
67
|
+
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
68
|
+
const css2 = cssModules[file];
|
|
69
|
+
if (!css2) {
|
|
70
|
+
if (isWebpack) {
|
|
71
|
+
const hash = file.replace(/\.module\.\w+$/, "");
|
|
72
|
+
const cached = readFromCache(hash);
|
|
73
|
+
if (cached) {
|
|
74
|
+
return {
|
|
75
|
+
code: cached,
|
|
76
|
+
map: null
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return void 0;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
code: css2,
|
|
84
|
+
map: null
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
transform: {
|
|
88
|
+
filter: {
|
|
89
|
+
id: fileMatch
|
|
90
|
+
},
|
|
91
|
+
handler(src, id) {
|
|
92
|
+
let imports = [];
|
|
93
|
+
src = src.replace(
|
|
94
|
+
/import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm,
|
|
95
|
+
""
|
|
96
|
+
);
|
|
97
|
+
src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {
|
|
98
|
+
const [variableName, tag, css2] = args;
|
|
99
|
+
if (tag !== tagName) return substring;
|
|
100
|
+
let baseFilename = id.slice(id.lastIndexOf("/") + 1);
|
|
101
|
+
baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf("."));
|
|
102
|
+
let cnt = 0;
|
|
103
|
+
const ext = typeof preprocessor == "function" ? preprocessor(baseFilename) : preprocessor;
|
|
104
|
+
let filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
105
|
+
while (cssModules[filename]) {
|
|
106
|
+
cnt++;
|
|
107
|
+
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
108
|
+
}
|
|
109
|
+
cssModules[filename] = css2;
|
|
110
|
+
let importStatement;
|
|
111
|
+
if (isWebpack) {
|
|
112
|
+
const hash = hashCss(css2);
|
|
113
|
+
writeToCache(hash, css2);
|
|
114
|
+
importStatement = `import ${variableName} from "${moduleId}/${hash}.module.${ext}"`;
|
|
115
|
+
}
|
|
116
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
117
|
+
if (config.inlineImport === false) {
|
|
118
|
+
imports.push(importStatement);
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
return importStatement;
|
|
122
|
+
});
|
|
123
|
+
if (imports.length) {
|
|
124
|
+
return imports.join("\n") + "\n" + src;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
code: src,
|
|
128
|
+
map: null
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
var unplugin = createUnplugin(unpluginFactory);
|
|
135
|
+
|
|
136
|
+
// src/rolldown.ts
|
|
137
|
+
var rolldown_default = createRolldownPlugin(unpluginFactory);
|
|
138
|
+
export {
|
|
139
|
+
css,
|
|
140
|
+
rolldown_default as default
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=rolldown.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/rolldown.ts","../src/index.ts"],"sourcesContent":["import { createRolldownPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRolldownPlugin(unpluginFactory)\nexport { css } from './index'\nexport type { PluginConfig } from './index'\n","import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync, readFileSync, writeFileSync, existsSync } from 'fs'\nimport { join } from 'path'\n\ntype SupportedExtension = 'css' | 'scss' | 'sass' | 'styl' | 'less'\n\nexport type PluginConfig = {\n fileMatch?: RegExp\n tagName?: string\n extension?: SupportedExtension | ((filename: string) => SupportedExtension)\n inlineImport?: boolean\n}\n\nconst matchInlineCssModules =\n /(?:const|var|let)\\s*(\\w+)(?:\\s*:.*)?\\s*=\\s*(\\w+)\\s*`([\\s\\S]*?)`/gm\n\nexport const css = (_: TemplateStringsArray): Record<string, string> => ({})\n\nconst virtualModuleId = 'virtual:inline-css-modules'\nconst webpackModuleId = 'inline-css-modules/virtual'\nconst resolvedVirtualModuleId = '\\0' + virtualModuleId\nconst resolvedWebpackModuleId = '\\0inline-css-modules/virtual'\n\nlet cssModules: Record<string, string> = {}\n\nconst getCacheDir = () =>\n join(process.cwd(), 'node_modules', '.cache', 'inline-css-modules')\nconst getCachePath = (hash: string) => join(getCacheDir(), `${hash}.css`)\n\nfunction ensureCacheDir() {\n const dir = getCacheDir()\n if (!existsSync(dir)) {\n mkdirSync(dir, { recursive: true })\n }\n}\n\nfunction hashCss(css: string): string {\n return createHash('md5').update(css).digest('hex')\n}\n\nfunction readFromCache(hash: string): string | null {\n const path = getCachePath(hash)\n if (existsSync(path)) {\n return readFileSync(path, 'utf-8')\n }\n return null\n}\n\nfunction writeToCache(hash: string, css: string): void {\n ensureCacheDir()\n writeFileSync(getCachePath(hash), css)\n}\n\nexport const unpluginFactory: UnpluginFactory<PluginConfig | undefined> = (\n config = {},\n meta\n) => {\n const fileMatch = config.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte)$/\n const tagName = config.tagName ?? 'css'\n const preprocessor = config.extension ?? 'css'\n const isWebpack =\n meta?.framework === 'webpack' || meta?.framework === 'rspack'\n const moduleId = isWebpack ? webpackModuleId : virtualModuleId\n const resolvedId = isWebpack\n ? resolvedWebpackModuleId\n : resolvedVirtualModuleId\n\n return {\n name: 'inline-css-modules',\n enforce: 'pre',\n resolveId(id) {\n if (id === moduleId || id.startsWith(moduleId + '/')) {\n return resolvedId + id.slice(moduleId.length)\n }\n if (id === virtualModuleId || id.startsWith(virtualModuleId + '/')) {\n return resolvedVirtualModuleId + id.slice(virtualModuleId.length)\n }\n if (id === webpackModuleId || id.startsWith(webpackModuleId + '/')) {\n return resolvedWebpackModuleId + id.slice(webpackModuleId.length)\n }\n return undefined\n },\n loadInclude(id) {\n return (\n id.startsWith(resolvedVirtualModuleId) ||\n id.startsWith(resolvedWebpackModuleId)\n )\n },\n load(id) {\n if (\n !id.startsWith(resolvedVirtualModuleId + '/') &&\n !id.startsWith(resolvedWebpackModuleId + '/')\n )\n return undefined\n\n const prefix = id.startsWith(resolvedVirtualModuleId + '/')\n ? resolvedVirtualModuleId + '/'\n : resolvedWebpackModuleId + '/'\n const file = id.slice(prefix.length).replace(/\\?used$/, '')\n const css = cssModules[file]\n\n if (!css) {\n if (isWebpack) {\n const hash = file.replace(/\\.module\\.\\w+$/, '')\n const cached = readFromCache(hash)\n if (cached) {\n return {\n code: cached,\n map: null,\n }\n }\n }\n return undefined\n }\n return {\n code: css,\n map: null,\n }\n },\n transform: {\n filter: {\n id: fileMatch,\n },\n handler(src, id) {\n // Build up a list of import statements to inject to the top of the file\n let imports: string[] = []\n\n src = src.replace(\n /import\\s*{\\s*(?:css|inlineCss)\\s*(?:as\\s*\\w+\\s*)?}\\s*from\\s*('|\"|`)unplugin-inline-css-modules\\1;?/gm,\n ''\n )\n\n src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {\n const [variableName, tag, css] = args\n\n if (tag !== tagName) return substring\n\n let baseFilename = id.slice(id.lastIndexOf('/') + 1)\n baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf('.'))\n let cnt = 0\n const ext =\n typeof preprocessor == 'function'\n ? preprocessor(baseFilename)\n : preprocessor\n let filename = `${baseFilename}-${cnt}.module.${ext}`\n while (cssModules[filename]) {\n cnt++\n filename = `${baseFilename}-${cnt}.module.${ext}`\n }\n cssModules[filename] = css\n\n let importStatement\n\n if (isWebpack) {\n const hash = hashCss(css)\n writeToCache(hash, css)\n importStatement = `import ${variableName} from \"${moduleId}/${hash}.module.${ext}\"`\n }\n importStatement = `import ${variableName} from \"${moduleId}/${filename}\"`\n\n if (config.inlineImport === false) {\n imports.push(importStatement)\n return ''\n }\n return importStatement\n })\n if (imports.length) {\n return imports.join('\\n') + '\\n' + src\n }\n return {\n code: src,\n map: null,\n }\n },\n },\n }\n}\n\nexport const unplugin = createUnplugin(unpluginFactory)\n\nexport default unplugin\n"],"mappings":";AAAA,SAAS,4BAA4B;;;ACCrC,SAAS,sBAAsB;AAC/B,SAAS,kBAAkB;AAC3B,SAAS,WAAW,cAAc,eAAe,kBAAkB;AACnE,SAAS,YAAY;AAWrB,IAAM,wBACJ;AAEK,IAAM,MAAM,CAAC,OAAqD,CAAC;AAE1E,IAAM,kBAAkB;AACxB,IAAM,kBAAkB;AACxB,IAAM,0BAA0B,OAAO;AACvC,IAAM,0BAA0B;AAEhC,IAAI,aAAqC,CAAC;AAE1C,IAAM,cAAc,MAClB,KAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,SAAiB,KAAK,YAAY,GAAG,GAAG,IAAI,MAAM;AAExE,SAAS,iBAAiB;AACxB,QAAM,MAAM,YAAY;AACxB,MAAI,CAAC,WAAW,GAAG,GAAG;AACpB,cAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAAA,EACpC;AACF;AAEA,SAAS,QAAQA,MAAqB;AACpC,SAAO,WAAW,KAAK,EAAE,OAAOA,IAAG,EAAE,OAAO,KAAK;AACnD;AAEA,SAAS,cAAc,MAA6B;AAClD,QAAM,OAAO,aAAa,IAAI;AAC9B,MAAI,WAAW,IAAI,GAAG;AACpB,WAAO,aAAa,MAAM,OAAO;AAAA,EACnC;AACA,SAAO;AACT;AAEA,SAAS,aAAa,MAAcA,MAAmB;AACrD,iBAAe;AACf,gBAAc,aAAa,IAAI,GAAGA,IAAG;AACvC;AAEO,IAAM,kBAA6D,CACxE,SAAS,CAAC,GACV,SACG;AACH,QAAM,YAAY,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ACF;AAAA,EACF;AACF;AAEO,IAAM,WAAW,eAAe,eAAe;;;ADjLtD,IAAO,mBAAQ,qBAAqB,eAAe;","names":["css"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as rollup from 'rollup';
|
|
2
|
+
import { PluginConfig } from './index.mjs';
|
|
3
|
+
export { css } from './index.mjs';
|
|
4
|
+
import 'unplugin';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options?: PluginConfig | undefined) => rollup.Plugin<any> | rollup.Plugin<any>[];
|
|
7
|
+
|
|
8
|
+
export { PluginConfig, _default as default };
|
package/dist/rollup.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as rollup from 'rollup';
|
|
2
|
+
import { PluginConfig } from './index.js';
|
|
3
|
+
export { css } from './index.js';
|
|
4
|
+
import 'unplugin';
|
|
5
|
+
|
|
6
|
+
declare const _default: (options?: PluginConfig | undefined) => rollup.Plugin<any> | rollup.Plugin<any>[];
|
|
7
|
+
|
|
8
|
+
export { PluginConfig, _default as default };
|