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/rollup.mjs
CHANGED
|
@@ -4,8 +4,9 @@ import { createRollupPlugin } from "unplugin";
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
import { createUnplugin } from "unplugin";
|
|
6
6
|
import { createHash } from "crypto";
|
|
7
|
-
import { mkdirSync,
|
|
7
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
8
8
|
import { join } from "path";
|
|
9
|
+
import MagicString from "magic-string";
|
|
9
10
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
10
11
|
var css = (_) => ({});
|
|
11
12
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -14,7 +15,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
14
15
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
15
16
|
var cssModules = {};
|
|
16
17
|
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
17
|
-
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
18
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.module.css`);
|
|
18
19
|
function ensureCacheDir() {
|
|
19
20
|
const dir = getCacheDir();
|
|
20
21
|
if (!existsSync(dir)) {
|
|
@@ -24,13 +25,6 @@ function ensureCacheDir() {
|
|
|
24
25
|
function hashCss(css2) {
|
|
25
26
|
return createHash("md5").update(css2).digest("hex");
|
|
26
27
|
}
|
|
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
28
|
function writeToCache(hash, css2) {
|
|
35
29
|
ensureCacheDir();
|
|
36
30
|
writeFileSync(getCachePath(hash), css2);
|
|
@@ -39,9 +33,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
39
33
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
40
34
|
const tagName = config.tagName ?? "css";
|
|
41
35
|
const preprocessor = config.extension ?? "css";
|
|
42
|
-
const
|
|
43
|
-
const moduleId =
|
|
44
|
-
const resolvedId =
|
|
36
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
37
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
38
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
45
39
|
return {
|
|
46
40
|
name: "inline-css-modules",
|
|
47
41
|
enforce: "pre",
|
|
@@ -49,36 +43,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
49
43
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
50
44
|
return resolvedId + id.slice(moduleId.length);
|
|
51
45
|
}
|
|
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
46
|
return void 0;
|
|
59
47
|
},
|
|
60
48
|
loadInclude(id) {
|
|
61
|
-
return id.startsWith(
|
|
49
|
+
return id.startsWith(resolvedId);
|
|
62
50
|
},
|
|
63
51
|
load(id) {
|
|
64
|
-
if (!id.startsWith(
|
|
65
|
-
|
|
66
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
67
|
-
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$/, "");
|
|
68
54
|
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
|
-
}
|
|
55
|
+
if (!css2) return void 0;
|
|
82
56
|
return {
|
|
83
57
|
code: css2,
|
|
84
58
|
map: null
|
|
@@ -89,43 +63,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
89
63
|
id: fileMatch
|
|
90
64
|
},
|
|
91
65
|
handler(src, id) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
108
|
-
}
|
|
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}`;
|
|
109
81
|
cssModules[filename] = css2;
|
|
110
82
|
let importStatement;
|
|
111
|
-
if (
|
|
112
|
-
const hash = hashCss(css2);
|
|
83
|
+
if (useFilesystem) {
|
|
113
84
|
writeToCache(hash, css2);
|
|
114
|
-
importStatement = `import ${variableName} from "${
|
|
85
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
86
|
+
} else {
|
|
87
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
115
88
|
}
|
|
116
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
117
89
|
if (config.inlineImport === false) {
|
|
118
90
|
imports.push(importStatement);
|
|
119
|
-
|
|
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
|
+
);
|
|
120
98
|
}
|
|
121
|
-
|
|
122
|
-
}
|
|
99
|
+
hasChanges = true;
|
|
100
|
+
}
|
|
123
101
|
if (imports.length) {
|
|
124
|
-
|
|
102
|
+
s.prepend(imports.join("\n") + "\n");
|
|
103
|
+
}
|
|
104
|
+
if (!hasChanges) {
|
|
105
|
+
return null;
|
|
125
106
|
}
|
|
126
107
|
return {
|
|
127
|
-
code:
|
|
128
|
-
map:
|
|
108
|
+
code: s.toString(),
|
|
109
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
129
110
|
};
|
|
130
111
|
}
|
|
131
112
|
}
|
package/dist/rollup.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/rollup.ts","../src/index.ts"],"sourcesContent":["import { createRollupPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRollupPlugin(unpluginFactory)\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/rollup.ts","../src/index.ts"],"sourcesContent":["import { createRollupPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRollupPlugin(unpluginFactory)\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":"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","names":["css"]}
|
package/dist/rspack.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/rspack.ts
|
|
@@ -31,6 +41,7 @@ var import_unplugin = require("unplugin");
|
|
|
31
41
|
var import_crypto = require("crypto");
|
|
32
42
|
var import_fs = require("fs");
|
|
33
43
|
var import_path = require("path");
|
|
44
|
+
var import_magic_string = __toESM(require("magic-string"));
|
|
34
45
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
35
46
|
var css = (_) => ({});
|
|
36
47
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -39,7 +50,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
39
50
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
40
51
|
var cssModules = {};
|
|
41
52
|
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`);
|
|
53
|
+
var getCachePath = (hash) => (0, import_path.join)(getCacheDir(), `${hash}.module.css`);
|
|
43
54
|
function ensureCacheDir() {
|
|
44
55
|
const dir = getCacheDir();
|
|
45
56
|
if (!(0, import_fs.existsSync)(dir)) {
|
|
@@ -49,13 +60,6 @@ function ensureCacheDir() {
|
|
|
49
60
|
function hashCss(css2) {
|
|
50
61
|
return (0, import_crypto.createHash)("md5").update(css2).digest("hex");
|
|
51
62
|
}
|
|
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
63
|
function writeToCache(hash, css2) {
|
|
60
64
|
ensureCacheDir();
|
|
61
65
|
(0, import_fs.writeFileSync)(getCachePath(hash), css2);
|
|
@@ -64,9 +68,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
64
68
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
65
69
|
const tagName = config.tagName ?? "css";
|
|
66
70
|
const preprocessor = config.extension ?? "css";
|
|
67
|
-
const
|
|
68
|
-
const moduleId =
|
|
69
|
-
const resolvedId =
|
|
71
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
72
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
73
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
70
74
|
return {
|
|
71
75
|
name: "inline-css-modules",
|
|
72
76
|
enforce: "pre",
|
|
@@ -74,36 +78,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
74
78
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
75
79
|
return resolvedId + id.slice(moduleId.length);
|
|
76
80
|
}
|
|
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
81
|
return void 0;
|
|
84
82
|
},
|
|
85
83
|
loadInclude(id) {
|
|
86
|
-
return id.startsWith(
|
|
84
|
+
return id.startsWith(resolvedId);
|
|
87
85
|
},
|
|
88
86
|
load(id) {
|
|
89
|
-
if (!id.startsWith(
|
|
90
|
-
|
|
91
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
92
|
-
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$/, "");
|
|
93
89
|
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
|
-
}
|
|
90
|
+
if (!css2) return void 0;
|
|
107
91
|
return {
|
|
108
92
|
code: css2,
|
|
109
93
|
map: null
|
|
@@ -114,43 +98,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
114
98
|
id: fileMatch
|
|
115
99
|
},
|
|
116
100
|
handler(src, id) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
133
|
-
}
|
|
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}`;
|
|
134
116
|
cssModules[filename] = css2;
|
|
135
117
|
let importStatement;
|
|
136
|
-
if (
|
|
137
|
-
const hash = hashCss(css2);
|
|
118
|
+
if (useFilesystem) {
|
|
138
119
|
writeToCache(hash, css2);
|
|
139
|
-
importStatement = `import ${variableName} from "${
|
|
120
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
121
|
+
} else {
|
|
122
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
140
123
|
}
|
|
141
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
142
124
|
if (config.inlineImport === false) {
|
|
143
125
|
imports.push(importStatement);
|
|
144
|
-
|
|
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
|
+
);
|
|
145
133
|
}
|
|
146
|
-
|
|
147
|
-
}
|
|
134
|
+
hasChanges = true;
|
|
135
|
+
}
|
|
148
136
|
if (imports.length) {
|
|
149
|
-
|
|
137
|
+
s.prepend(imports.join("\n") + "\n");
|
|
138
|
+
}
|
|
139
|
+
if (!hasChanges) {
|
|
140
|
+
return null;
|
|
150
141
|
}
|
|
151
142
|
return {
|
|
152
|
-
code:
|
|
153
|
-
map:
|
|
143
|
+
code: s.toString(),
|
|
144
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
154
145
|
};
|
|
155
146
|
}
|
|
156
147
|
}
|
package/dist/rspack.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/rspack.ts","../src/index.ts"],"sourcesContent":["import { createRspackPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRspackPlugin(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,
|
|
1
|
+
{"version":3,"sources":["../src/rspack.ts","../src/index.ts"],"sourcesContent":["import { createRspackPlugin } from 'unplugin'\nimport { unpluginFactory } from './index'\n\nexport default createRspackPlugin(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, 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":"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","names":["import_unplugin","css","MagicString"]}
|
package/dist/rspack.mjs
CHANGED
|
@@ -4,8 +4,9 @@ import { createRspackPlugin } from "unplugin";
|
|
|
4
4
|
// src/index.ts
|
|
5
5
|
import { createUnplugin } from "unplugin";
|
|
6
6
|
import { createHash } from "crypto";
|
|
7
|
-
import { mkdirSync,
|
|
7
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
8
8
|
import { join } from "path";
|
|
9
|
+
import MagicString from "magic-string";
|
|
9
10
|
var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
|
|
10
11
|
var css = (_) => ({});
|
|
11
12
|
var virtualModuleId = "virtual:inline-css-modules";
|
|
@@ -14,7 +15,7 @@ var resolvedVirtualModuleId = "\0" + virtualModuleId;
|
|
|
14
15
|
var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
|
|
15
16
|
var cssModules = {};
|
|
16
17
|
var getCacheDir = () => join(process.cwd(), "node_modules", ".cache", "inline-css-modules");
|
|
17
|
-
var getCachePath = (hash) => join(getCacheDir(), `${hash}.css`);
|
|
18
|
+
var getCachePath = (hash) => join(getCacheDir(), `${hash}.module.css`);
|
|
18
19
|
function ensureCacheDir() {
|
|
19
20
|
const dir = getCacheDir();
|
|
20
21
|
if (!existsSync(dir)) {
|
|
@@ -24,13 +25,6 @@ function ensureCacheDir() {
|
|
|
24
25
|
function hashCss(css2) {
|
|
25
26
|
return createHash("md5").update(css2).digest("hex");
|
|
26
27
|
}
|
|
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
28
|
function writeToCache(hash, css2) {
|
|
35
29
|
ensureCacheDir();
|
|
36
30
|
writeFileSync(getCachePath(hash), css2);
|
|
@@ -39,9 +33,9 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
39
33
|
const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
|
|
40
34
|
const tagName = config.tagName ?? "css";
|
|
41
35
|
const preprocessor = config.extension ?? "css";
|
|
42
|
-
const
|
|
43
|
-
const moduleId =
|
|
44
|
-
const resolvedId =
|
|
36
|
+
const useFilesystem = config.moduleStrategy === "filesystem";
|
|
37
|
+
const moduleId = useFilesystem ? webpackModuleId : virtualModuleId;
|
|
38
|
+
const resolvedId = useFilesystem ? resolvedWebpackModuleId : resolvedVirtualModuleId;
|
|
45
39
|
return {
|
|
46
40
|
name: "inline-css-modules",
|
|
47
41
|
enforce: "pre",
|
|
@@ -49,36 +43,16 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
49
43
|
if (id === moduleId || id.startsWith(moduleId + "/")) {
|
|
50
44
|
return resolvedId + id.slice(moduleId.length);
|
|
51
45
|
}
|
|
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
46
|
return void 0;
|
|
59
47
|
},
|
|
60
48
|
loadInclude(id) {
|
|
61
|
-
return id.startsWith(
|
|
49
|
+
return id.startsWith(resolvedId);
|
|
62
50
|
},
|
|
63
51
|
load(id) {
|
|
64
|
-
if (!id.startsWith(
|
|
65
|
-
|
|
66
|
-
const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
|
|
67
|
-
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$/, "");
|
|
68
54
|
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
|
-
}
|
|
55
|
+
if (!css2) return void 0;
|
|
82
56
|
return {
|
|
83
57
|
code: css2,
|
|
84
58
|
map: null
|
|
@@ -89,43 +63,50 @@ var unpluginFactory = (config = {}, meta) => {
|
|
|
89
63
|
id: fileMatch
|
|
90
64
|
},
|
|
91
65
|
handler(src, id) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
filename = `${baseFilename}-${cnt}.module.${ext}`;
|
|
108
|
-
}
|
|
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}`;
|
|
109
81
|
cssModules[filename] = css2;
|
|
110
82
|
let importStatement;
|
|
111
|
-
if (
|
|
112
|
-
const hash = hashCss(css2);
|
|
83
|
+
if (useFilesystem) {
|
|
113
84
|
writeToCache(hash, css2);
|
|
114
|
-
importStatement = `import ${variableName} from "${
|
|
85
|
+
importStatement = `import ${variableName} from "${getCachePath(hash)}"`;
|
|
86
|
+
} else {
|
|
87
|
+
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
115
88
|
}
|
|
116
|
-
importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
|
|
117
89
|
if (config.inlineImport === false) {
|
|
118
90
|
imports.push(importStatement);
|
|
119
|
-
|
|
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
|
+
);
|
|
120
98
|
}
|
|
121
|
-
|
|
122
|
-
}
|
|
99
|
+
hasChanges = true;
|
|
100
|
+
}
|
|
123
101
|
if (imports.length) {
|
|
124
|
-
|
|
102
|
+
s.prepend(imports.join("\n") + "\n");
|
|
103
|
+
}
|
|
104
|
+
if (!hasChanges) {
|
|
105
|
+
return null;
|
|
125
106
|
}
|
|
126
107
|
return {
|
|
127
|
-
code:
|
|
128
|
-
map:
|
|
108
|
+
code: s.toString(),
|
|
109
|
+
map: s.generateMap({ source: id, includeContent: true })
|
|
129
110
|
};
|
|
130
111
|
}
|
|
131
112
|
}
|