unplugin-inline-css-modules 0.1.1 → 0.1.3
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 +5 -13
- package/dist/loader.cjs +0 -6
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { UnpluginFactory } from 'unplugin'\nimport { createUnplugin } from 'unplugin'\nimport { createHash } from 'crypto'\nimport { mkdirSync,
|
|
1
|
+
{"version":3,"sources":["../src/index.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"],"mappings":";AACA,SAAS,sBAAsB;AAC/B,SAAS,kBAAkB;AAC3B,SAAS,WAAW,eAAe,kBAAkB;AACrD,SAAS,YAAY;AACrB,OAAO,iBAAiB;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,MAClB,KAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,SAAiB,KAAK,YAAY,GAAG,GAAG,IAAI,aAAa;AAE/E,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,aAAa,MAAcA,MAAmB;AACrD,iBAAe;AACf,gBAAc,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;AACnD,QAAM,aAAa,gBACf,0BACA;AAEJ,SA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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,WAAW,eAAe,eAAe;AAEtD,IAAO,cAAQ;","names":["css"]}
|
package/dist/next.d.mts
CHANGED
|
@@ -2,8 +2,6 @@ import { PluginConfig } from './index.mjs';
|
|
|
2
2
|
export { css } from './index.mjs';
|
|
3
3
|
import 'unplugin';
|
|
4
4
|
|
|
5
|
-
declare function export_default(options?: PluginConfig):
|
|
6
|
-
apply: (compiler: unknown) => void;
|
|
7
|
-
};
|
|
5
|
+
declare function export_default(options?: PluginConfig): WebpackPluginInstance;
|
|
8
6
|
|
|
9
7
|
export { PluginConfig, export_default as default };
|
package/dist/next.d.ts
CHANGED
|
@@ -2,8 +2,6 @@ import { PluginConfig } from './index.js';
|
|
|
2
2
|
export { css } from './index.js';
|
|
3
3
|
import 'unplugin';
|
|
4
4
|
|
|
5
|
-
declare function export_default(options?: PluginConfig):
|
|
6
|
-
apply: (compiler: unknown) => void;
|
|
7
|
-
};
|
|
5
|
+
declare function export_default(options?: PluginConfig): WebpackPluginInstance;
|
|
8
6
|
|
|
9
7
|
export { PluginConfig, export_default as default };
|
package/dist/next.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/next.ts
|
|
@@ -25,14 +35,13 @@ __export(next_exports, {
|
|
|
25
35
|
});
|
|
26
36
|
module.exports = __toCommonJS(next_exports);
|
|
27
37
|
var import_unplugin2 = require("unplugin");
|
|
28
|
-
var import_path2 = require("path");
|
|
29
|
-
var import_url = require("url");
|
|
30
38
|
|
|
31
39
|
// src/index.ts
|
|
32
40
|
var import_unplugin = require("unplugin");
|
|
33
41
|
var import_crypto = require("crypto");
|
|
34
42
|
var import_fs = require("fs");
|
|
35
43
|
var import_path = require("path");
|
|
44
|
+
var import_magic_string = __toESM(require("magic-string"));
|
|
36
45
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
37
46
|
var css = (_) => ({});
|
|
38
47
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -41,7 +50,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
41
50
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
42
51
|
var cssModules = {};
|
|
43
52
|
var getCacheDir = () => (0, import_path.join)(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
44
|
-
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.css`);
|
|
53
|
+
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.module.css`);
|
|
45
54
|
function ensureCacheDir() {
|
|
46
55
|
const dir = getCacheDir();
|
|
47
56
|
if (!(0, import_fs.existsSync)(dir)) {
|
|
@@ -51,13 +60,6 @@ function ensureCacheDir() {
|
|
|
51
60
|
function hashCss(css2) {
|
|
52
61
|
return (0, import_crypto.createHash)("md5").update(css2).digest("hex");
|
|
53
62
|
}
|
|
54
|
-
function readFromCache(hash) {
|
|
55
|
-
const path = getCachePath(hash);
|
|
56
|
-
if ((0, import_fs.existsSync)(path)) {
|
|
57
|
-
return (0, import_fs.readFileSync)(path, "utf-8");
|
|
58
|
-
}
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
63
|
function writeToCache(hash, css2) {
|
|
62
64
|
ensureCacheDir();
|
|
63
65
|
(0, import_fs.writeFileSync)(getCachePath(hash), css2);
|
|
@@ -66,9 +68,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
66
68
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
67
69
|
const tagName = config.tagName ?? "css";
|
|
68
70
|
const preprocessor = config.extension ?? "css";
|
|
69
|
-
const
|
|
70
|
-
const moduleId =
|
|
71
|
-
const resolvedId =
|
|
71
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
72
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
73
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
72
74
|
return {
|
|
73
75
|
name: "inline-css-modules",
|
|
74
76
|
enforce: "pre",
|
|
@@ -76,36 +78,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
76
78
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
77
79
|
return resolvedId + id.slice(moduleId.length);
|
|
78
80
|
}
|
|
79
|
-
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
80
|
-
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
81
|
-
}
|
|
82
|
-
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
83
|
-
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
84
|
-
}
|
|
85
81
|
return void 0;
|
|
86
82
|
},
|
|
87
83
|
loadInclude(id) {
|
|
88
|
-
return id.startsWith(
|
|
84
|
+
return id.startsWith(resolvedId);
|
|
89
85
|
},
|
|
90
86
|
load(id) {
|
|
91
|
-
if (!id.startsWith(
|
|
92
|
-
|
|
93
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
94
|
-
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
87
|
+
if (!id.startsWith(resolvedId + "/")) return void 0;
|
|
88
|
+
const file = id.slice(resolvedId.length + 1).replace(/\?used$/, "");
|
|
95
89
|
const css2 = cssModules[file];
|
|
96
|
-
if (!css2)
|
|
97
|
-
if (isWebpack) {
|
|
98
|
-
const hash = file.replace(/\.module\.\w+$/, "");
|
|
99
|
-
const cached = readFromCache(hash);
|
|
100
|
-
if (cached) {
|
|
101
|
-
return {
|
|
102
|
-
code: cached,
|
|
103
|
-
map: null
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return void 0;
|
|
108
|
-
}
|
|
90
|
+
if (!css2) return void 0;
|
|
109
91
|
return {
|
|
110
92
|
code: css2,
|
|
111
93
|
map: null
|
|
@@ -116,43 +98,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
116
98
|
id: fileMatch
|
|
117
99
|
},
|
|
118
100
|
handler(src, id) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
135
|
-
}
|
|
101
|
+
const s = new import_magic_string.default(src);
|
|
102
|
+
const imports = [];
|
|
103
|
+
let hasChanges = false;
|
|
104
|
+
let match;
|
|
105
|
+
const importRegex = /import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm;
|
|
106
|
+
while ((match = importRegex.exec(src)) !== null) {
|
|
107
|
+
s.remove(match.index, match.index + match[0].length);
|
|
108
|
+
hasChanges = true;
|
|
109
|
+
}
|
|
110
|
+
matchInlineCssModules.lastIndex = 0;
|
|
111
|
+
while ((match = matchInlineCssModules.exec(src)) !== null) {
|
|
112
|
+
const [fullMatch, variableName, tag, css2] = match;
|
|
113
|
+
if (tag !== tagName) continue;
|
|
114
|
+
const hash = hashCss(css2);
|
|
115
|
+
const filename = `${hash}.module.${preprocessor}`;
|
|
136
116
|
cssModules[filename] = css2;
|
|
137
117
|
let importStatement;
|
|
138
|
-
if (
|
|
139
|
-
const hash = hashCss(css2);
|
|
118
|
+
if (useFilesystem) {
|
|
140
119
|
writeToCache(hash, css2);
|
|
141
|
-
importStatement = `import ${variableName} from "${
|
|
120
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
121
|
+
} else {
|
|
122
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
142
123
|
}
|
|
143
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
144
124
|
if (config.inlineImport === false) {
|
|
145
125
|
imports.push(importStatement);
|
|
146
|
-
|
|
126
|
+
s.overwrite(match.index, match.index + fullMatch.length, "");
|
|
127
|
+
} else {
|
|
128
|
+
s.overwrite(
|
|
129
|
+
match.index,
|
|
130
|
+
match.index + fullMatch.length,
|
|
131
|
+
importStatement
|
|
132
|
+
);
|
|
147
133
|
}
|
|
148
|
-
|
|
149
|
-
}
|
|
134
|
+
hasChanges = true;
|
|
135
|
+
}
|
|
150
136
|
if (imports.length) {
|
|
151
|
-
|
|
137
|
+
s.prepend(imports.join("\n") + "\n");
|
|
138
|
+
}
|
|
139
|
+
if (!hasChanges) {
|
|
140
|
+
return null;
|
|
152
141
|
}
|
|
153
142
|
return {
|
|
154
|
-
code:
|
|
155
|
-
map:
|
|
143
|
+
code: s.toString(),
|
|
144
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
156
145
|
};
|
|
157
146
|
}
|
|
158
147
|
}
|
|
@@ -161,30 +150,11 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
161
150
|
var unplugin = (0, import_unplugin.createUnplugin)(unpluginFactory);
|
|
162
151
|
|
|
163
152
|
// src/next.ts
|
|
164
|
-
var import_meta = {};
|
|
165
|
-
var _dirname = typeof __dirname !== "undefined" ? __dirname : (0, import_url.fileURLToPath)(new URL(".", import_meta.url));
|
|
166
|
-
var LOADER_PATH = (0, import_path2.resolve)(_dirname, "loader.cjs");
|
|
167
|
-
var PLUGIN_NAME = "inline-css-modules";
|
|
168
153
|
function next_default(options = {}) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const wp = compiler;
|
|
174
|
-
wp.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
175
|
-
const c = compilation;
|
|
176
|
-
const NormalModule = c.compiler?.webpack?.NormalModule;
|
|
177
|
-
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
|
|
178
|
-
PLUGIN_NAME,
|
|
179
|
-
(loaders, module2) => {
|
|
180
|
-
loaders.push({
|
|
181
|
-
loader: LOADER_PATH
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
);
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
return plugin;
|
|
154
|
+
return (0, import_unplugin2.createWebpackPlugin)(unpluginFactory)({
|
|
155
|
+
...options,
|
|
156
|
+
moduleStrategy: options.moduleStrategy || "filesystem"
|
|
157
|
+
});
|
|
188
158
|
}
|
|
189
159
|
// Annotate the CommonJS export names for ESM import in node:
|
|
190
160
|
0 && (module.exports = {
|
package/dist/next.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/next.ts","../src/index.ts"],"sourcesContent":["// WebPack / swc opportunistically caches resolutions so we have to\n// register a custom loader that gives back the source instead.\n\nimport { createWebpackPlugin } from 'unplugin'\nimport { resolve } from 'path'\nimport { fileURLToPath } from 'url'\nimport type { PluginConfig } from './index'\nimport { unpluginFactory } from './index'\n\nconst _dirname =\n typeof __dirname !== 'undefined'\n ? __dirname\n : fileURLToPath(new URL('.', import.meta.url))\nconst LOADER_PATH = resolve(_dirname, 'loader.cjs')\n\nconst PLUGIN_NAME = 'inline-css-modules'\n\n// const virtualModulePath = 'inline-css-modules/virtual'\n// const isVirtualModule = (module: { resource?: string }) =>\n// module.resource?.includes?.(virtualModulePath) ?? false\n\nexport default function (options: PluginConfig = {}) {\n const plugin = createWebpackPlugin(unpluginFactory)(options) as {\n apply: (compiler: unknown) => void\n }\n const originalApply = plugin.apply.bind(plugin)\n plugin.apply = (compiler: unknown) => {\n originalApply(compiler)\n const wp = compiler as {\n hooks: {\n compilation: {\n tap: (name: string, fn: (compilation: unknown) => void) => void\n }\n }\n }\n\n wp.hooks.compilation.tap(PLUGIN_NAME, (compilation: unknown) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const c = compilation as any\n const NormalModule = c.compiler?.webpack?.NormalModule\n\n NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(\n PLUGIN_NAME,\n (loaders: { loader: string }[], module: { resource?: string }) => {\n loaders.push({\n loader: LOADER_PATH,\n })\n }\n )\n })\n }\n return plugin\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, 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;AAGA,IAAAA,mBAAoC;AACpC,IAAAC,eAAwB;AACxB,iBAA8B;;;ACJ9B,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;A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AAQ;AAAA,UACV,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO;AACT;","names":["import_unplugin","import_path","css","module"]}
|
|
1
|
+
{"version":3,"sources":["../src/next.ts","../src/index.ts"],"sourcesContent":["import { createWebpackPlugin } from 'unplugin'\nimport { PluginConfig, unpluginFactory } from './index'\n\nexport default function (options: PluginConfig = {}) {\n return createWebpackPlugin(unpluginFactory)({\n ...options,\n moduleStrategy: options.moduleStrategy || 'filesystem',\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;AAAA,IAAAA,mBAAoC;;;ACCpC,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,QAAQC,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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IAAI,WAAW,YAAY;AAC/C,qBAAW,QAAQ,IAAIA;AAEvB,cAAI;AACJ,cAAI,eAAe;AACjB,yBAAa,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;;;ADpJvC,SAAR,aAAkB,UAAwB,CAAC,GAAG;AACnD,aAAO,sCAAoB,eAAe,EAAE;AAAA,IAC1C,GAAG;AAAA,IACH,gBAAgB,QAAQ,kBAAkB;AAAA,EAC5C,CAAC;AACH;","names":["import_unplugin","css","MagicString"]}
|
package/dist/next.mjs
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
// src/next.ts
|
|
2
2
|
import { createWebpackPlugin } from "unplugin";
|
|
3
|
-
import { resolve } from "path";
|
|
4
|
-
import { fileURLToPath } from "url";
|
|
5
3
|
|
|
6
4
|
// src/index.ts
|
|
7
5
|
import { createUnplugin } from "unplugin";
|
|
8
6
|
import { createHash } from "crypto";
|
|
9
|
-
import { mkdirSync,
|
|
7
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
10
8
|
import { join } from "path";
|
|
9
|
+
import MagicString from "magic-string";
|
|
11
10
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
12
11
|
var css = (_) => ({});
|
|
13
12
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -16,7 +15,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
16
15
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
17
16
|
var cssModules = {};
|
|
18
17
|
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
19
|
-
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
18
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.module.css`);
|
|
20
19
|
function ensureCacheDir() {
|
|
21
20
|
const dir = getCacheDir();
|
|
22
21
|
if (!existsSync(dir)) {
|
|
@@ -26,13 +25,6 @@ function ensureCacheDir() {
|
|
|
26
25
|
function hashCss(css2) {
|
|
27
26
|
return createHash("md5").update(css2).digest("hex");
|
|
28
27
|
}
|
|
29
|
-
function readFromCache(hash) {
|
|
30
|
-
const path = getCachePath(hash);
|
|
31
|
-
if (existsSync(path)) {
|
|
32
|
-
return readFileSync(path, "utf-8");
|
|
33
|
-
}
|
|
34
|
-
return null;
|
|
35
|
-
}
|
|
36
28
|
function writeToCache(hash, css2) {
|
|
37
29
|
ensureCacheDir();
|
|
38
30
|
writeFileSync(getCachePath(hash), css2);
|
|
@@ -41,9 +33,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
41
33
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
42
34
|
const tagName = config.tagName ?? "css";
|
|
43
35
|
const preprocessor = config.extension ?? "css";
|
|
44
|
-
const
|
|
45
|
-
const moduleId =
|
|
46
|
-
const resolvedId =
|
|
36
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
37
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
38
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
47
39
|
return {
|
|
48
40
|
name: "inline-css-modules",
|
|
49
41
|
enforce: "pre",
|
|
@@ -51,36 +43,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
51
43
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
52
44
|
return resolvedId + id.slice(moduleId.length);
|
|
53
45
|
}
|
|
54
|
-
if (id === virtualModuleId || id.startsWith(virtualModuleId + "/")) {
|
|
55
|
-
return resolvedVirtualModuleId + id.slice(virtualModuleId.length);
|
|
56
|
-
}
|
|
57
|
-
if (id === webpackModuleId || id.startsWith(webpackModuleId + "/")) {
|
|
58
|
-
return resolvedWebpackModuleId + id.slice(webpackModuleId.length);
|
|
59
|
-
}
|
|
60
46
|
return void 0;
|
|
61
47
|
},
|
|
62
48
|
loadInclude(id) {
|
|
63
|
-
return id.startsWith(
|
|
49
|
+
return id.startsWith(resolvedId);
|
|
64
50
|
},
|
|
65
51
|
load(id) {
|
|
66
|
-
if (!id.startsWith(
|
|
67
|
-
|
|
68
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
69
|
-
const file = id.slice(prefix.length).replace(/\?used$/, "");
|
|
52
|
+
if (!id.startsWith(resolvedId + "/")) return void 0;
|
|
53
|
+
const file = id.slice(resolvedId.length + 1).replace(/\?used$/, "");
|
|
70
54
|
const css2 = cssModules[file];
|
|
71
|
-
if (!css2)
|
|
72
|
-
if (isWebpack) {
|
|
73
|
-
const hash = file.replace(/\.module\.\w+$/, "");
|
|
74
|
-
const cached = readFromCache(hash);
|
|
75
|
-
if (cached) {
|
|
76
|
-
return {
|
|
77
|
-
code: cached,
|
|
78
|
-
map: null
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return void 0;
|
|
83
|
-
}
|
|
55
|
+
if (!css2) return void 0;
|
|
84
56
|
return {
|
|
85
57
|
code: css2,
|
|
86
58
|
map: null
|
|
@@ -91,43 +63,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
91
63
|
id: fileMatch
|
|
92
64
|
},
|
|
93
65
|
handler(src, id) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
110
|
-
}
|
|
66
|
+
const s = new MagicString(src);
|
|
67
|
+
const imports = [];
|
|
68
|
+
let hasChanges = false;
|
|
69
|
+
let match;
|
|
70
|
+
const importRegex = /import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm;
|
|
71
|
+
while ((match = importRegex.exec(src)) !== null) {
|
|
72
|
+
s.remove(match.index, match.index + match[0].length);
|
|
73
|
+
hasChanges = true;
|
|
74
|
+
}
|
|
75
|
+
matchInlineCssModules.lastIndex = 0;
|
|
76
|
+
while ((match = matchInlineCssModules.exec(src)) !== null) {
|
|
77
|
+
const [fullMatch, variableName, tag, css2] = match;
|
|
78
|
+
if (tag !== tagName) continue;
|
|
79
|
+
const hash = hashCss(css2);
|
|
80
|
+
const filename = `${hash}.module.${preprocessor}`;
|
|
111
81
|
cssModules[filename] = css2;
|
|
112
82
|
let importStatement;
|
|
113
|
-
if (
|
|
114
|
-
const hash = hashCss(css2);
|
|
83
|
+
if (useFilesystem) {
|
|
115
84
|
writeToCache(hash, css2);
|
|
116
|
-
importStatement = `import ${variableName} from "${
|
|
85
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
86
|
+
} else {
|
|
87
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
117
88
|
}
|
|
118
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
119
89
|
if (config.inlineImport === false) {
|
|
120
90
|
imports.push(importStatement);
|
|
121
|
-
|
|
91
|
+
s.overwrite(match.index, match.index + fullMatch.length, "");
|
|
92
|
+
} else {
|
|
93
|
+
s.overwrite(
|
|
94
|
+
match.index,
|
|
95
|
+
match.index + fullMatch.length,
|
|
96
|
+
importStatement
|
|
97
|
+
);
|
|
122
98
|
}
|
|
123
|
-
|
|
124
|
-
}
|
|
99
|
+
hasChanges = true;
|
|
100
|
+
}
|
|
125
101
|
if (imports.length) {
|
|
126
|
-
|
|
102
|
+
s.prepend(imports.join("\n") + "\n");
|
|
103
|
+
}
|
|
104
|
+
if (!hasChanges) {
|
|
105
|
+
return null;
|
|
127
106
|
}
|
|
128
107
|
return {
|
|
129
|
-
code:
|
|
130
|
-
map:
|
|
108
|
+
code: s.toString(),
|
|
109
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
131
110
|
};
|
|
132
111
|
}
|
|
133
112
|
}
|
|
@@ -136,29 +115,11 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
136
115
|
var unplugin = createUnplugin(unpluginFactory);
|
|
137
116
|
|
|
138
117
|
// src/next.ts
|
|
139
|
-
var _dirname = typeof __dirname !== "undefined" ? __dirname : fileURLToPath(new URL(".", import.meta.url));
|
|
140
|
-
var LOADER_PATH = resolve(_dirname, "loader.cjs");
|
|
141
|
-
var PLUGIN_NAME = "inline-css-modules";
|
|
142
118
|
function next_default(options = {}) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const wp = compiler;
|
|
148
|
-
wp.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
|
|
149
|
-
const c = compilation;
|
|
150
|
-
const NormalModule = c.compiler?.webpack?.NormalModule;
|
|
151
|
-
NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(
|
|
152
|
-
PLUGIN_NAME,
|
|
153
|
-
(loaders, module) => {
|
|
154
|
-
loaders.push({
|
|
155
|
-
loader: LOADER_PATH
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
);
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
return plugin;
|
|
119
|
+
return createWebpackPlugin(unpluginFactory)({
|
|
120
|
+
...options,
|
|
121
|
+
moduleStrategy: options.moduleStrategy || "filesystem"
|
|
122
|
+
});
|
|
162
123
|
}
|
|
163
124
|
export {
|
|
164
125
|
css,
|
package/dist/next.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/next.ts","../src/index.ts"],"sourcesContent":["// WebPack / swc opportunistically caches resolutions so we have to\n// register a custom loader that gives back the source instead.\n\nimport { createWebpackPlugin } from 'unplugin'\nimport { resolve } from 'path'\nimport { fileURLToPath } from 'url'\nimport type { PluginConfig } from './index'\nimport { unpluginFactory } from './index'\n\nconst _dirname =\n typeof __dirname !== 'undefined'\n ? __dirname\n : fileURLToPath(new URL('.', import.meta.url))\nconst LOADER_PATH = resolve(_dirname, 'loader.cjs')\n\nconst PLUGIN_NAME = 'inline-css-modules'\n\n// const virtualModulePath = 'inline-css-modules/virtual'\n// const isVirtualModule = (module: { resource?: string }) =>\n// module.resource?.includes?.(virtualModulePath) ?? false\n\nexport default function (options: PluginConfig = {}) {\n const plugin = createWebpackPlugin(unpluginFactory)(options) as {\n apply: (compiler: unknown) => void\n }\n const originalApply = plugin.apply.bind(plugin)\n plugin.apply = (compiler: unknown) => {\n originalApply(compiler)\n const wp = compiler as {\n hooks: {\n compilation: {\n tap: (name: string, fn: (compilation: unknown) => void) => void\n }\n }\n }\n\n wp.hooks.compilation.tap(PLUGIN_NAME, (compilation: unknown) => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const c = compilation as any\n const NormalModule = c.compiler?.webpack?.NormalModule\n\n NormalModule.getCompilationHooks(compilation).beforeLoaders.tap(\n PLUGIN_NAME,\n (loaders: { loader: string }[], module: { resource?: string }) => {\n loaders.push({\n loader: LOADER_PATH,\n })\n }\n )\n })\n }\n return plugin\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, 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":";AAGA,SAAS,2BAA2B;AACpC,SAAS,eAAe;AACxB,SAAS,qBAAqB;;;ACJ9B,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;AAC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} from 'unplugin'\nimport { PluginConfig, unpluginFactory } from './index'\n\nexport default function (options: PluginConfig = {}) {\n return createWebpackPlugin(unpluginFactory)({\n ...options,\n moduleStrategy: options.moduleStrategy || 'filesystem',\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,SAAS,2BAA2B;;;ACCpC,SAAS,sBAAsB;AAC/B,SAAS,kBAAkB;AAC3B,SAAS,WAAW,eAAe,kBAAkB;AACrD,SAAS,YAAY;AACrB,OAAO,iBAAiB;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,MAClB,KAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,SAAiB,KAAK,YAAY,GAAG,GAAG,IAAI,aAAa;AAE/E,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,aAAa,MAAcA,MAAmB;AACrD,iBAAe;AACf,gBAAc,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;AACnD,QAAM,aAAa,gBACf,0BACA;AAEJ,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA,IACT,UAAU,IAAI;AACZ,UAAI,OAAO,YAAY,GAAG,WAA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