unplugin-inline-css-modules 0.1.1 → 0.1.2
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/dist/astro.js +53 -62
- package/dist/astro.js.map +1 -1
- package/dist/astro.mjs +44 -63
- package/dist/astro.mjs.map +1 -1
- package/dist/bun.js +53 -62
- package/dist/bun.js.map +1 -1
- package/dist/bun.mjs +44 -63
- package/dist/bun.mjs.map +1 -1
- package/dist/esbuild.js +53 -62
- package/dist/esbuild.js.map +1 -1
- package/dist/esbuild.mjs +44 -63
- package/dist/esbuild.mjs.map +1 -1
- package/dist/farm.js +53 -62
- package/dist/farm.js.map +1 -1
- package/dist/farm.mjs +44 -63
- package/dist/farm.mjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +53 -62
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -63
- package/dist/index.mjs.map +1 -1
- package/dist/next.d.mts +1 -3
- package/dist/next.d.ts +1 -3
- package/dist/next.js +57 -87
- package/dist/next.js.map +1 -1
- package/dist/next.mjs +48 -87
- package/dist/next.mjs.map +1 -1
- package/dist/nuxt.js +53 -62
- package/dist/nuxt.js.map +1 -1
- package/dist/nuxt.mjs +44 -63
- package/dist/nuxt.mjs.map +1 -1
- package/dist/rolldown.js +53 -62
- package/dist/rolldown.js.map +1 -1
- package/dist/rolldown.mjs +44 -63
- package/dist/rolldown.mjs.map +1 -1
- package/dist/rollup.js +53 -62
- package/dist/rollup.js.map +1 -1
- package/dist/rollup.mjs +44 -63
- package/dist/rollup.mjs.map +1 -1
- package/dist/rspack.js +53 -62
- package/dist/rspack.js.map +1 -1
- package/dist/rspack.mjs +44 -63
- package/dist/rspack.mjs.map +1 -1
- package/dist/vite.js +53 -62
- package/dist/vite.js.map +1 -1
- package/dist/vite.mjs +44 -63
- package/dist/vite.mjs.map +1 -1
- package/dist/webpack.js +53 -62
- package/dist/webpack.js.map +1 -1
- package/dist/webpack.mjs +44 -63
- package/dist/webpack.mjs.map +1 -1
- package/package.json +4 -3
- package/dist/loader.cjs +0 -6
package/dist/astro.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
8
|
var __export = (target, all) => {
|
|
7
9
|
for (var name in all)
|
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
17
|
}
|
|
16
18
|
return to;
|
|
17
19
|
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
29
|
|
|
20
30
|
// src/astro.ts
|
|
@@ -30,6 +40,7 @@ var import_unplugin = require("unplugin");
|
|
|
30
40
|
var import_crypto = require("crypto");
|
|
31
41
|
var import_fs = require("fs");
|
|
32
42
|
var import_path = require("path");
|
|
43
|
+
var import_magic_string = __toESM(require("magic-string"));
|
|
33
44
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
34
45
|
var css = (_) => ({});
|
|
35
46
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -38,7 +49,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
38
49
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
39
50
|
var cssModules = {};
|
|
40
51
|
var getCacheDir = () => (0, import_path.join)(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
41
|
-
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.css`);
|
|
52
|
+
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.module.css`);
|
|
42
53
|
function ensureCacheDir() {
|
|
43
54
|
const dir = getCacheDir();
|
|
44
55
|
if (!(0, import_fs.existsSync)(dir)) {
|
|
@@ -48,13 +59,6 @@ function ensureCacheDir() {
|
|
|
48
59
|
function hashCss(css2) {
|
|
49
60
|
return (0, import_crypto.createHash)("md5").update(css2).digest("hex");
|
|
50
61
|
}
|
|
51
|
-
function readFromCache(hash) {
|
|
52
|
-
const path = getCachePath(hash);
|
|
53
|
-
if ((0, import_fs.existsSync)(path)) {
|
|
54
|
-
return (0, import_fs.readFileSync)(path, "utf-8");
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
62
|
function writeToCache(hash, css2) {
|
|
59
63
|
ensureCacheDir();
|
|
60
64
|
(0, import_fs.writeFileSync)(getCachePath(hash), css2);
|
|
@@ -63,9 +67,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
63
67
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
64
68
|
const tagName = config.tagName ?? "css";
|
|
65
69
|
const preprocessor = config.extension ?? "css";
|
|
66
|
-
const
|
|
67
|
-
const moduleId =
|
|
68
|
-
const resolvedId =
|
|
70
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
71
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
72
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
69
73
|
return {
|
|
70
74
|
name: "inline-css-modules",
|
|
71
75
|
enforce: "pre",
|
|
@@ -73,36 +77,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
73
77
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
74
78
|
return resolvedId + id.slice(moduleId.length);
|
|
75
79
|
}
|
|
76
|
-
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
77
|
-
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
78
|
-
}
|
|
79
|
-
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
80
|
-
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
81
|
-
}
|
|
82
80
|
return void 0;
|
|
83
81
|
},
|
|
84
82
|
loadInclude(id) {
|
|
85
|
-
return id.startsWith(
|
|
83
|
+
return id.startsWith(resolvedId);
|
|
86
84
|
},
|
|
87
85
|
load(id) {
|
|
88
|
-
if (!id.startsWith(
|
|
89
|
-
|
|
90
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
91
|
-
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
86
|
+
if (!id.startsWith(resolvedId + "/")) return void 0;
|
|
87
|
+
const file = id.slice(resolvedId.length + 1).replace(/\?used$/, "");
|
|
92
88
|
const css2 = cssModules[file];
|
|
93
|
-
if (!css2)
|
|
94
|
-
if (isWebpack) {
|
|
95
|
-
const hash = file.replace(/\.module\.\w+$/, "");
|
|
96
|
-
const cached = readFromCache(hash);
|
|
97
|
-
if (cached) {
|
|
98
|
-
return {
|
|
99
|
-
code: cached,
|
|
100
|
-
map: null
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return void 0;
|
|
105
|
-
}
|
|
89
|
+
if (!css2) return void 0;
|
|
106
90
|
return {
|
|
107
91
|
code: css2,
|
|
108
92
|
map: null
|
|
@@ -113,43 +97,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
113
97
|
id: fileMatch
|
|
114
98
|
},
|
|
115
99
|
handler(src, id) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
132
|
-
}
|
|
100
|
+
const s = new import_magic_string.default(src);
|
|
101
|
+
const imports = [];
|
|
102
|
+
let hasChanges = false;
|
|
103
|
+
let match;
|
|
104
|
+
const importRegex = /import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm;
|
|
105
|
+
while ((match = importRegex.exec(src)) !== null) {
|
|
106
|
+
s.remove(match.index, match.index + match[0].length);
|
|
107
|
+
hasChanges = true;
|
|
108
|
+
}
|
|
109
|
+
matchInlineCssModules.lastIndex = 0;
|
|
110
|
+
while ((match = matchInlineCssModules.exec(src)) !== null) {
|
|
111
|
+
const [fullMatch, variableName, tag, css2] = match;
|
|
112
|
+
if (tag !== tagName) continue;
|
|
113
|
+
const hash = hashCss(css2);
|
|
114
|
+
const filename = `${hash}.module.${preprocessor}`;
|
|
133
115
|
cssModules[filename] = css2;
|
|
134
116
|
let importStatement;
|
|
135
|
-
if (
|
|
136
|
-
const hash = hashCss(css2);
|
|
117
|
+
if (useFilesystem) {
|
|
137
118
|
writeToCache(hash, css2);
|
|
138
|
-
importStatement = `import ${variableName} from "${
|
|
119
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
120
|
+
} else {
|
|
121
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
139
122
|
}
|
|
140
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
141
123
|
if (config.inlineImport === false) {
|
|
142
124
|
imports.push(importStatement);
|
|
143
|
-
|
|
125
|
+
s.overwrite(match.index, match.index + fullMatch.length, "");
|
|
126
|
+
} else {
|
|
127
|
+
s.overwrite(
|
|
128
|
+
match.index,
|
|
129
|
+
match.index + fullMatch.length,
|
|
130
|
+
importStatement
|
|
131
|
+
);
|
|
144
132
|
}
|
|
145
|
-
|
|
146
|
-
}
|
|
133
|
+
hasChanges = true;
|
|
134
|
+
}
|
|
147
135
|
if (imports.length) {
|
|
148
|
-
|
|
136
|
+
s.prepend(imports.join("\n") + "\n");
|
|
137
|
+
}
|
|
138
|
+
if (!hasChanges) {
|
|
139
|
+
return null;
|
|
149
140
|
}
|
|
150
141
|
return {
|
|
151
|
-
code:
|
|
152
|
-
map:
|
|
142
|
+
code: s.toString(),
|
|
143
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
153
144
|
};
|
|
154
145
|
}
|
|
155
146
|
}
|
package/dist/astro.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/astro.ts","../src/index.ts"],"sourcesContent":["import type { PluginConfig } from './index'\nimport unplugin from './index'\nimport type { AstroIntegration } from 'astro'\n\nexport default function (options: PluginConfig = {}) {\n return <AstroIntegration>{\n name: 'unplugin-inline-css-modules',\n hooks: {\n 'astro:config:setup': async astro => {\n astro.updateConfig({\n vite: {\n plugins: [\n unplugin.vite({\n ...options,\n fileMatch:\n options.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte|astro)$/,\n }) as Plugin,\n ],\n },\n })\n },\n },\n }\n}\n\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,
|
|
1
|
+
{"version":3,"sources":["../src/astro.ts","../src/index.ts"],"sourcesContent":["import type { PluginConfig } from './index'\nimport unplugin from './index'\nimport type { AstroIntegration } from 'astro'\n\nexport default function (options: PluginConfig = {}) {\n return <AstroIntegration>{\n name: 'unplugin-inline-css-modules',\n hooks: {\n 'astro:config:setup': async astro => {\n astro.updateConfig({\n vite: {\n plugins: [\n unplugin.vite({\n ...options,\n fileMatch:\n options.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte|astro)$/,\n }) as Plugin,\n ],\n },\n })\n },\n },\n }\n}\n\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, writeFileSync, existsSync } from 'fs'\nimport { join } from 'path'\nimport MagicString from 'magic-string'\n\ntype SupportedExtension = 'css' | 'scss' | 'sass' | 'styl' | 'less'\n\nexport type PluginConfig = {\n fileMatch?: RegExp\n tagName?: string\n extension?: SupportedExtension\n inlineImport?: boolean\n moduleStrategy?: 'virtual' | 'filesystem'\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}.module.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 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 useFilesystem = config.moduleStrategy === 'filesystem'\n const moduleId = useFilesystem ? webpackModuleId : virtualModuleId\n const resolvedId = useFilesystem\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 return undefined\n },\n loadInclude(id) {\n return id.startsWith(resolvedId)\n },\n load(id) {\n if (!id.startsWith(resolvedId + '/')) return undefined\n\n const file = id.slice(resolvedId.length + 1).replace(/\\?used$/, '')\n const css = cssModules[file]\n\n if (!css) 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 const s = new MagicString(src)\n const imports: string[] = []\n let hasChanges = false\n\n let match: RegExpExecArray | null\n const importRegex =\n /import\\s*{\\s*(?:css|inlineCss)\\s*(?:as\\s*\\w+\\s*)?}\\s*from\\s*('|\"|`)unplugin-inline-css-modules\\1;?/gm\n while ((match = importRegex.exec(src)) !== null) {\n s.remove(match.index, match.index + match[0].length)\n hasChanges = true\n }\n\n matchInlineCssModules.lastIndex = 0\n while ((match = matchInlineCssModules.exec(src)) !== null) {\n const [fullMatch, variableName, tag, css] = match\n if (tag !== tagName) continue\n\n const hash = hashCss(css)\n const filename = `${hash}.module.${preprocessor}`\n cssModules[filename] = css\n\n let importStatement: string\n if (useFilesystem) {\n writeToCache(hash, css)\n importStatement = `import ${variableName} from \"${getCachePath(hash)}\"`\n } else {\n importStatement = `import ${variableName} from \"${moduleId}/${filename}\"`\n }\n\n if (config.inlineImport === false) {\n imports.push(importStatement)\n s.overwrite(match.index, match.index + fullMatch.length, '')\n } else {\n s.overwrite(\n match.index,\n match.index + fullMatch.length,\n importStatement\n )\n }\n hasChanges = true\n }\n\n if (imports.length) {\n s.prepend(imports.join('\\n') + '\\n')\n }\n\n if (!hasChanges) {\n return null\n }\n\n return {\n code: s.toString(),\n map: s.generateMap({ source: id, includeContent: true }),\n }\n },\n },\n }\n}\n\nexport const unplugin = createUnplugin(unpluginFactory)\n\nexport default unplugin\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,sBAA+B;AAC/B,oBAA2B;AAC3B,gBAAqD;AACrD,kBAAqB;AACrB,0BAAwB;AAYxB,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,aAAa;AAE/E,SAAS,iBAAiB;AACxB,QAAM,MAAM,YAAY;AACxB,MAAI,KAAC,sBAAW,GAAG,GAAG;AACpB,6BAAU,KAAK,EAAE,WAAW,KAAK,CAAC;AAAA,EACpC;AACF;AAEA,SAAS,QAAQA,MAAqB;AACpC,aAAO,0BAAW,KAAK,EAAE,OAAOA,IAAG,EAAE,OAAO,KAAK;AACnD;AAEA,SAAS,aAAa,MAAcA,MAAmB;AACrD,iBAAe;AACf,+BAAc,aAAa,IAAI,GAAGA,IAAG;AACvC;AAEO,IAAM,kBAA6D,CACxE,SAAS,CAAC,GACV,SACG;AACH,QAAM,YAAY,OAAO,aAAa;AACtC,QAAM,UAAU,OAAO,WAAW;AAClC,QAAM,eAAe,OAAO,aAAa;AACzC,QAAM,gBAAgB,OAAO,mBAAmB;AAChD,QAAM,WAAW,gBAAgB,kBAAkB;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a,MAAMA,IAAG;AACtB,8BAAkB,UAAU,YAAY,UAAU,aAAa,IAAI,CAAC;AAAA,UACtE,OAAO;AACL,8BAAkB,UAAU,YAAY,UAAU,QAAQ,IAAI,QAAQ;AAAA,UACxE;AAEA,cAAI,OAAO,iBAAiB,OAAO;AACjC,oBAAQ,KAAK,eAAe;AAC5B,cAAE,UAAU,MAAM,OAAO,MAAM,QAAQ,UAAU,QAAQ,EAAE;AAAA,UAC7D,OAAO;AACL,cAAE;AAAA,cACA,MAAM;AAAA,cACN,MAAM,QAAQ,UAAU;AAAA,cACxB;AAAA,YACF;AAAA,UACF;AACA,uBAAa;AAAA,QACf;AAEA,YAAI,QAAQ,QAAQ;AAClB,YAAE,QAAQ,QAAQ,KAAK,IAAI,IAAI,IAAI;AAAA,QACrC;AAEA,YAAI,CAAC,YAAY;AACf,iBAAO;AAAA,QACT;AAEA,eAAO;AAAA,UACL,MAAM,EAAE,SAAS;AAAA,UACjB,KAAK,EAAE,YAAY,EAAE,QAAQ,IAAI,gBAAgB,KAAK,CAAC;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAEO,IAAM,eAAW,gCAAe,eAAe;AAEtD,IAAO,cAAQ;;;ADrJA,SAAR,cAAkB,UAAwB,CAAC,GAAG;AACnD,SAAyB;AAAA,IACvB,MAAM;AAAA,IACN,OAAO;AAAA,MACL,sBAAsB,OAAM,UAAS;AACnC,cAAM,aAAa;AAAA,UACjB,MAAM;AAAA,YACJ,SAAS;AAAA,cACP,YAAS,KAAK;AAAA,gBACZ,GAAG;AAAA,gBACH,WACE,QAAQ,aAAa;AAAA,cACzB,CAAC;AAAA,YACH;AAAA,UACF;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;","names":["css","MagicString"]}
|
package/dist/astro.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createUnplugin } from "unplugin";
|
|
3
3
|
import { createHash } from "crypto";
|
|
4
|
-
import { mkdirSync,
|
|
4
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
5
5
|
import { join } from "path";
|
|
6
|
+
import MagicString from "magic-string";
|
|
6
7
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
7
8
|
var css = (_) => ({});
|
|
8
9
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -11,7 +12,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
11
12
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
12
13
|
var cssModules = {};
|
|
13
14
|
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
14
|
-
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
15
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.module.css`);
|
|
15
16
|
function ensureCacheDir() {
|
|
16
17
|
const dir = getCacheDir();
|
|
17
18
|
if (!existsSync(dir)) {
|
|
@@ -21,13 +22,6 @@ function ensureCacheDir() {
|
|
|
21
22
|
function hashCss(css2) {
|
|
22
23
|
return createHash("md5").update(css2).digest("hex");
|
|
23
24
|
}
|
|
24
|
-
function readFromCache(hash) {
|
|
25
|
-
const path = getCachePath(hash);
|
|
26
|
-
if (existsSync(path)) {
|
|
27
|
-
return readFileSync(path, "utf-8");
|
|
28
|
-
}
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
25
|
function writeToCache(hash, css2) {
|
|
32
26
|
ensureCacheDir();
|
|
33
27
|
writeFileSync(getCachePath(hash), css2);
|
|
@@ -36,9 +30,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
36
30
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
37
31
|
const tagName = config.tagName ?? "css";
|
|
38
32
|
const preprocessor = config.extension ?? "css";
|
|
39
|
-
const
|
|
40
|
-
const moduleId =
|
|
41
|
-
const resolvedId =
|
|
33
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
34
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
35
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
42
36
|
return {
|
|
43
37
|
name: "inline-css-modules",
|
|
44
38
|
enforce: "pre",
|
|
@@ -46,36 +40,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
46
40
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
47
41
|
return resolvedId + id.slice(moduleId.length);
|
|
48
42
|
}
|
|
49
|
-
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
50
|
-
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
51
|
-
}
|
|
52
|
-
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
53
|
-
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
54
|
-
}
|
|
55
43
|
return void 0;
|
|
56
44
|
},
|
|
57
45
|
loadInclude(id) {
|
|
58
|
-
return id.startsWith(
|
|
46
|
+
return id.startsWith(resolvedId);
|
|
59
47
|
},
|
|
60
48
|
load(id) {
|
|
61
|
-
if (!id.startsWith(
|
|
62
|
-
|
|
63
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
64
|
-
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
49
|
+
if (!id.startsWith(resolvedId + "/")) return void 0;
|
|
50
|
+
const file = id.slice(resolvedId.length + 1).replace(/\?used$/, "");
|
|
65
51
|
const css2 = cssModules[file];
|
|
66
|
-
if (!css2)
|
|
67
|
-
if (isWebpack) {
|
|
68
|
-
const hash = file.replace(/\.module\.\w+$/, "");
|
|
69
|
-
const cached = readFromCache(hash);
|
|
70
|
-
if (cached) {
|
|
71
|
-
return {
|
|
72
|
-
code: cached,
|
|
73
|
-
map: null
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return void 0;
|
|
78
|
-
}
|
|
52
|
+
if (!css2) return void 0;
|
|
79
53
|
return {
|
|
80
54
|
code: css2,
|
|
81
55
|
map: null
|
|
@@ -86,43 +60,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
86
60
|
id: fileMatch
|
|
87
61
|
},
|
|
88
62
|
handler(src, id) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
105
|
-
}
|
|
63
|
+
const s = new MagicString(src);
|
|
64
|
+
const imports = [];
|
|
65
|
+
let hasChanges = false;
|
|
66
|
+
let match;
|
|
67
|
+
const importRegex = /import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm;
|
|
68
|
+
while ((match = importRegex.exec(src)) !== null) {
|
|
69
|
+
s.remove(match.index, match.index + match[0].length);
|
|
70
|
+
hasChanges = true;
|
|
71
|
+
}
|
|
72
|
+
matchInlineCssModules.lastIndex = 0;
|
|
73
|
+
while ((match = matchInlineCssModules.exec(src)) !== null) {
|
|
74
|
+
const [fullMatch, variableName, tag, css2] = match;
|
|
75
|
+
if (tag !== tagName) continue;
|
|
76
|
+
const hash = hashCss(css2);
|
|
77
|
+
const filename = `${hash}.module.${preprocessor}`;
|
|
106
78
|
cssModules[filename] = css2;
|
|
107
79
|
let importStatement;
|
|
108
|
-
if (
|
|
109
|
-
const hash = hashCss(css2);
|
|
80
|
+
if (useFilesystem) {
|
|
110
81
|
writeToCache(hash, css2);
|
|
111
|
-
importStatement = `import ${variableName} from "${
|
|
82
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
83
|
+
} else {
|
|
84
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
112
85
|
}
|
|
113
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
114
86
|
if (config.inlineImport === false) {
|
|
115
87
|
imports.push(importStatement);
|
|
116
|
-
|
|
88
|
+
s.overwrite(match.index, match.index + fullMatch.length, "");
|
|
89
|
+
} else {
|
|
90
|
+
s.overwrite(
|
|
91
|
+
match.index,
|
|
92
|
+
match.index + fullMatch.length,
|
|
93
|
+
importStatement
|
|
94
|
+
);
|
|
117
95
|
}
|
|
118
|
-
|
|
119
|
-
}
|
|
96
|
+
hasChanges = true;
|
|
97
|
+
}
|
|
120
98
|
if (imports.length) {
|
|
121
|
-
|
|
99
|
+
s.prepend(imports.join("\n") + "\n");
|
|
100
|
+
}
|
|
101
|
+
if (!hasChanges) {
|
|
102
|
+
return null;
|
|
122
103
|
}
|
|
123
104
|
return {
|
|
124
|
-
code:
|
|
125
|
-
map:
|
|
105
|
+
code: s.toString(),
|
|
106
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
126
107
|
};
|
|
127
108
|
}
|
|
128
109
|
}
|
package/dist/astro.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/astro.ts"],"sourcesContent":["import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/astro.ts"],"sourcesContent":["import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync, writeFileSync, existsSync } from 'fs'\nimport { join } from 'path'\nimport MagicString from 'magic-string'\n\ntype SupportedExtension = 'css' | 'scss' | 'sass' | 'styl' | 'less'\n\nexport type PluginConfig = {\n fileMatch?: RegExp\n tagName?: string\n extension?: SupportedExtension\n inlineImport?: boolean\n moduleStrategy?: 'virtual' | 'filesystem'\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}.module.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 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 useFilesystem = config.moduleStrategy === 'filesystem'\n const moduleId = useFilesystem ? webpackModuleId : virtualModuleId\n const resolvedId = useFilesystem\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 return undefined\n },\n loadInclude(id) {\n return id.startsWith(resolvedId)\n },\n load(id) {\n if (!id.startsWith(resolvedId + '/')) return undefined\n\n const file = id.slice(resolvedId.length + 1).replace(/\\?used$/, '')\n const css = cssModules[file]\n\n if (!css) 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 const s = new MagicString(src)\n const imports: string[] = []\n let hasChanges = false\n\n let match: RegExpExecArray | null\n const importRegex =\n /import\\s*{\\s*(?:css|inlineCss)\\s*(?:as\\s*\\w+\\s*)?}\\s*from\\s*('|\"|`)unplugin-inline-css-modules\\1;?/gm\n while ((match = importRegex.exec(src)) !== null) {\n s.remove(match.index, match.index + match[0].length)\n hasChanges = true\n }\n\n matchInlineCssModules.lastIndex = 0\n while ((match = matchInlineCssModules.exec(src)) !== null) {\n const [fullMatch, variableName, tag, css] = match\n if (tag !== tagName) continue\n\n const hash = hashCss(css)\n const filename = `${hash}.module.${preprocessor}`\n cssModules[filename] = css\n\n let importStatement: string\n if (useFilesystem) {\n writeToCache(hash, css)\n importStatement = `import ${variableName} from \"${getCachePath(hash)}\"`\n } else {\n importStatement = `import ${variableName} from \"${moduleId}/${filename}\"`\n }\n\n if (config.inlineImport === false) {\n imports.push(importStatement)\n s.overwrite(match.index, match.index + fullMatch.length, '')\n } else {\n s.overwrite(\n match.index,\n match.index + fullMatch.length,\n importStatement\n )\n }\n hasChanges = true\n }\n\n if (imports.length) {\n s.prepend(imports.join('\\n') + '\\n')\n }\n\n if (!hasChanges) {\n return null\n }\n\n return {\n code: s.toString(),\n map: s.generateMap({ source: id, includeContent: true }),\n }\n },\n },\n }\n}\n\nexport const unplugin = createUnplugin(unpluginFactory)\n\nexport default unplugin\n","import type { PluginConfig } from './index'\nimport unplugin from './index'\nimport type { AstroIntegration } from 'astro'\n\nexport default function (options: PluginConfig = {}) {\n return <AstroIntegration>{\n name: 'unplugin-inline-css-modules',\n hooks: {\n 'astro:config:setup': async astro => {\n astro.updateConfig({\n vite: {\n plugins: [\n unplugin.vite({\n ...options,\n fileMatch:\n options.fileMatch ?? /\\.(tsx|jsx|js|vue|svelte|astro)$/,\n }) as Plugin,\n ],\n },\n })\n },\n },\n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