unplugin-inline-css-modules 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +408 -0
  3. package/dist/astro.d.mts +8 -0
  4. package/dist/astro.d.ts +8 -0
  5. package/dist/astro.js +185 -0
  6. package/dist/astro.js.map +1 -0
  7. package/dist/astro.mjs +158 -0
  8. package/dist/astro.mjs.map +1 -0
  9. package/dist/bun.d.mts +7 -0
  10. package/dist/bun.d.ts +7 -0
  11. package/dist/bun.js +167 -0
  12. package/dist/bun.js.map +1 -0
  13. package/dist/bun.mjs +142 -0
  14. package/dist/bun.mjs.map +1 -0
  15. package/dist/esbuild.d.mts +7 -0
  16. package/dist/esbuild.d.ts +7 -0
  17. package/dist/esbuild.js +167 -0
  18. package/dist/esbuild.js.map +1 -0
  19. package/dist/esbuild.mjs +142 -0
  20. package/dist/esbuild.mjs.map +1 -0
  21. package/dist/farm.d.mts +6 -0
  22. package/dist/farm.d.ts +6 -0
  23. package/dist/farm.js +167 -0
  24. package/dist/farm.js.map +1 -0
  25. package/dist/farm.mjs +142 -0
  26. package/dist/farm.mjs.map +1 -0
  27. package/dist/index.d.mts +15 -0
  28. package/dist/index.d.ts +15 -0
  29. package/dist/index.js +166 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/index.mjs +139 -0
  32. package/dist/index.mjs.map +1 -0
  33. package/dist/loader.cjs +6 -0
  34. package/dist/next.d.mts +9 -0
  35. package/dist/next.d.ts +9 -0
  36. package/dist/next.js +193 -0
  37. package/dist/next.js.map +1 -0
  38. package/dist/next.mjs +167 -0
  39. package/dist/next.mjs.map +1 -0
  40. package/dist/nuxt.d.mts +7 -0
  41. package/dist/nuxt.d.ts +7 -0
  42. package/dist/nuxt.js +169 -0
  43. package/dist/nuxt.js.map +1 -0
  44. package/dist/nuxt.mjs +144 -0
  45. package/dist/nuxt.mjs.map +1 -0
  46. package/dist/rolldown.d.mts +8 -0
  47. package/dist/rolldown.d.ts +8 -0
  48. package/dist/rolldown.js +167 -0
  49. package/dist/rolldown.js.map +1 -0
  50. package/dist/rolldown.mjs +142 -0
  51. package/dist/rolldown.mjs.map +1 -0
  52. package/dist/rollup.d.mts +8 -0
  53. package/dist/rollup.d.ts +8 -0
  54. package/dist/rollup.js +167 -0
  55. package/dist/rollup.js.map +1 -0
  56. package/dist/rollup.mjs +142 -0
  57. package/dist/rollup.mjs.map +1 -0
  58. package/dist/rspack.d.mts +7 -0
  59. package/dist/rspack.d.ts +7 -0
  60. package/dist/rspack.js +167 -0
  61. package/dist/rspack.js.map +1 -0
  62. package/dist/rspack.mjs +142 -0
  63. package/dist/rspack.mjs.map +1 -0
  64. package/dist/vite.d.mts +8 -0
  65. package/dist/vite.d.ts +8 -0
  66. package/dist/vite.js +167 -0
  67. package/dist/vite.js.map +1 -0
  68. package/dist/vite.mjs +142 -0
  69. package/dist/vite.mjs.map +1 -0
  70. package/dist/webpack.d.mts +7 -0
  71. package/dist/webpack.d.ts +7 -0
  72. package/dist/webpack.js +167 -0
  73. package/dist/webpack.js.map +1 -0
  74. package/dist/webpack.mjs +142 -0
  75. package/dist/webpack.mjs.map +1 -0
  76. package/package.json +197 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Mia Korennykh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,408 @@
1
+ # unplugin-inline-css-modules
2
+
3
+ [![npm](https://img.shields.io/npm/v/unplugin-inline-css-modules.svg)](https://www.npmjs.com/package/unplugin-inline-css-modules)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ > Zero-runtime scoped CSS, targeting any framework on any bundler.
7
+
8
+ A simple example:
9
+
10
+ ```tsx
11
+ import { css } from 'unplugin-inline-css-modules'
12
+
13
+ const classes = css`
14
+ .button {
15
+ background-color: #1f1e33;
16
+ color: white;
17
+ padding: 1rem 2rem;
18
+ border-radius: 4px;
19
+ }
20
+ `
21
+
22
+ export const Button = () => <button className={classes.button}>Click me</button>
23
+ ```
24
+
25
+ At build time, the CSS is extracted into a real CSS module. Existing PostCSS syntax, Tailwind `@apply` directives, and CSS tooling all work out of the box however they were configured in the bundler.
26
+
27
+ ## Why
28
+
29
+ Frameworks like Vue have `<style scoped>`. The rest of us have been stuck choosing between separate `.module.css` files (context-switching) and CSS-in-JS libraries (runtime overhead, incompatible with PostCSS tooling).
30
+
31
+ This plugin gives you the best of both worlds: **co-located styles with zero runtime cost**. Under the hood it just generates CSS modules, so your entire CSS toolchain works without any changes.
32
+
33
+ ## Install
34
+
35
+ ```bash
36
+ npm install unplugin-inline-css-modules
37
+ ```
38
+
39
+ ## Setup
40
+
41
+ Add the plugin for your bundler:
42
+
43
+ <details>
44
+ <summary><b>Vite</b></summary>
45
+
46
+ ```ts
47
+ // vite.config.ts
48
+ import { defineConfig } from 'vite'
49
+ import inlineCSSModules from 'unplugin-inline-css-modules/vite'
50
+
51
+ export default defineConfig({
52
+ plugins: [inlineCSSModules()],
53
+ })
54
+ ```
55
+
56
+ </details>
57
+
58
+ <details>
59
+ <summary><b>Rollup</b></summary>
60
+
61
+ ```ts
62
+ // rollup.config.js
63
+ import inlineCSSModules from 'unplugin-inline-css-modules/rollup'
64
+
65
+ export default {
66
+ plugins: [inlineCSSModules()],
67
+ }
68
+ ```
69
+
70
+ </details>
71
+
72
+ <details>
73
+ <summary><b>Rolldown</b></summary>
74
+
75
+ ```ts
76
+ // rolldown.config.js
77
+ import inlineCSSModules from 'unplugin-inline-css-modules/rolldown'
78
+
79
+ export default {
80
+ plugins: [inlineCSSModules()],
81
+ }
82
+ ```
83
+
84
+ </details>
85
+
86
+ <details>
87
+ <summary><b>Webpack</b></summary>
88
+
89
+ ```ts
90
+ // webpack.config.js
91
+ import inlineCSSModules from 'unplugin-inline-css-modules/webpack'
92
+
93
+ export default {
94
+ plugins: [inlineCSSModules()],
95
+ }
96
+ ```
97
+
98
+ </details>
99
+
100
+ <details>
101
+ <summary><b>Rspack / Rsbuild</b></summary>
102
+
103
+ ```ts
104
+ // rsbuild.config.ts
105
+ import { defineConfig } from '@rsbuild/core'
106
+ import inlineCSSModules from 'unplugin-inline-css-modules/rspack'
107
+
108
+ export default defineConfig({
109
+ tools: {
110
+ rspack: {
111
+ plugins: [inlineCSSModules()],
112
+ },
113
+ },
114
+ })
115
+ ```
116
+
117
+ </details>
118
+
119
+ <details>
120
+ <summary><b>esbuild</b></summary>
121
+
122
+ ```ts
123
+ import { build } from 'esbuild'
124
+ import inlineCSSModules from 'unplugin-inline-css-modules/esbuild'
125
+
126
+ build({
127
+ plugins: [inlineCSSModules()],
128
+ })
129
+ ```
130
+
131
+ </details>
132
+
133
+ <details>
134
+ <summary><b>Farm</b></summary>
135
+
136
+ Farm is an experimental target. There has not been thorough testing for functionality.
137
+
138
+ ```ts
139
+ // farm.config.ts
140
+ import inlineCSSModules from 'unplugin-inline-css-modules/farm'
141
+
142
+ export default {
143
+ vitePlugins: [inlineCSSModules()],
144
+ }
145
+ ```
146
+
147
+ </details>
148
+
149
+ <details>
150
+ <summary><b>Bun</b></summary>
151
+
152
+ ```ts
153
+ import inlineCSSModules from 'unplugin-inline-css-modules/bun'
154
+
155
+ Bun.build({
156
+ plugins: [inlineCSSModules()],
157
+ })
158
+ ```
159
+
160
+ </details>
161
+
162
+ ### Framework Integrations
163
+
164
+ <details>
165
+ <summary><b>Next.js</b></summary>
166
+
167
+ ```ts
168
+ // next.config.ts
169
+ import type { NextConfig } from 'next'
170
+ import inlineCSSModules from 'unplugin-inline-css-modules/next'
171
+
172
+ const nextConfig: NextConfig = {
173
+ webpack: config => {
174
+ config.plugins = config.plugins || []
175
+ config.plugins.push(inlineCSSModules())
176
+ return config
177
+ },
178
+ }
179
+
180
+ export default nextConfig
181
+ ```
182
+
183
+ > **Note:** SWC breaks some assumptions for virtual module resolution. As a workaround, CSS modules are cached in `node_modules/.cache/inline-css-modules/`. If you reinstall `node_modules`, remove the `.next` folder to clear stale references.
184
+
185
+ </details>
186
+
187
+ <details>
188
+ <summary><b>Nuxt</b></summary>
189
+
190
+ ```ts
191
+ // nuxt.config.ts
192
+ import inlineCSSModules from 'unplugin-inline-css-modules/vite'
193
+
194
+ export default defineNuxtConfig({
195
+ vite: {
196
+ plugins: [inlineCSSModules()],
197
+ },
198
+ })
199
+ ```
200
+
201
+ </details>
202
+
203
+ <details>
204
+ <summary><b>Astro</b></summary>
205
+
206
+ ```js
207
+ // astro.config.mjs
208
+ import { defineConfig } from 'astro/config'
209
+ import inlineCSSModules from 'unplugin-inline-css-modules/astro'
210
+
211
+ export default defineConfig({
212
+ integrations: [inlineCSSModules()],
213
+ })
214
+ ```
215
+
216
+ </details>
217
+
218
+ <details>
219
+ <summary><b>SvelteKit</b></summary>
220
+
221
+ ```ts
222
+ // vite.config.ts
223
+ import { sveltekit } from '@sveltejs/kit/vite'
224
+ import { defineConfig } from 'vite'
225
+ import inlineCSSModules from 'unplugin-inline-css-modules/vite'
226
+
227
+ export default defineConfig({
228
+ plugins: [inlineCSSModules(), sveltekit()],
229
+ })
230
+ ```
231
+
232
+ </details>
233
+
234
+ ## Usage Examples
235
+
236
+ ### React
237
+
238
+ ```tsx
239
+ import { css } from 'unplugin-inline-css-modules'
240
+
241
+ const classes = css`
242
+ .root {
243
+ display: flex;
244
+ align-items: center;
245
+ justify-content: center;
246
+ min-height: 100vh;
247
+ }
248
+
249
+ .button {
250
+ background-color: #1f1e33;
251
+ color: white;
252
+ padding: 1rem 2rem;
253
+ border: none;
254
+ border-radius: 4px;
255
+ cursor: pointer;
256
+ }
257
+ `
258
+
259
+ export const Root = () => (
260
+ <div className={classes.root}>
261
+ <button className={classes.button}>Click me</button>
262
+ </div>
263
+ )
264
+ ```
265
+
266
+ ### Vue
267
+
268
+ ```vue
269
+ <script setup lang="ts">
270
+ import { css } from 'unplugin-inline-css-modules'
271
+
272
+ const classes = css`
273
+ .container {
274
+ display: flex;
275
+ align-items: center;
276
+ justify-content: center;
277
+ min-height: 100vh;
278
+ }
279
+
280
+ .button {
281
+ background-color: #1f1e33;
282
+ color: white;
283
+ padding: 1rem 2rem;
284
+ border: none;
285
+ border-radius: 4px;
286
+ cursor: pointer;
287
+ }
288
+ `
289
+ </script>
290
+
291
+ <template>
292
+ <div :class="classes.container">
293
+ <button :class="classes.button">Click me</button>
294
+ </div>
295
+ </template>
296
+ ```
297
+
298
+ ### Solid
299
+
300
+ ```tsx
301
+ import type { Component } from 'solid-js'
302
+ import { css } from 'unplugin-inline-css-modules'
303
+
304
+ const styles = css`
305
+ .container {
306
+ display: flex;
307
+ align-items: center;
308
+ justify-content: center;
309
+ min-height: 100vh;
310
+ }
311
+
312
+ .button {
313
+ background-color: #1f1e33;
314
+ color: white;
315
+ padding: 1rem 2rem;
316
+ border: none;
317
+ border-radius: 4px;
318
+ cursor: pointer;
319
+ }
320
+ `
321
+
322
+ const App: Component = () => (
323
+ <div class={styles.container}>
324
+ <button class={styles.button}>Click me</button>
325
+ </div>
326
+ )
327
+
328
+ export default App
329
+ ```
330
+
331
+ ### Svelte
332
+
333
+ ```svelte
334
+ <script lang="ts">
335
+ import { css } from 'unplugin-inline-css-modules'
336
+
337
+ const classes = css`
338
+ .button {
339
+ background-color: #1f1e33;
340
+ color: white;
341
+ padding: 1rem 2rem;
342
+ border-radius: 4px;
343
+ }
344
+ `
345
+ </script>
346
+
347
+ <button class={classes.button}>Click me</button>
348
+ ```
349
+
350
+ ### Astro
351
+
352
+ ```astro
353
+ ---
354
+ import { css } from 'unplugin-inline-css-modules'
355
+
356
+ const classes = css`
357
+ .button {
358
+ background-color: #1f1e33;
359
+ color: white;
360
+ padding: 1rem 2rem;
361
+ border-radius: 4px;
362
+ }
363
+ `
364
+ ---
365
+
366
+ <button class={classes.button}>Click me</button>
367
+ ```
368
+
369
+ ## How It Works
370
+
371
+ At build time, the plugin transforms your `css` tagged template literals into real CSS module imports:
372
+
373
+ ```ts
374
+ // What you write:
375
+ const classes = css`
376
+ .root {
377
+ color: red;
378
+ }
379
+ `
380
+
381
+ // What gets compiled:
382
+ import classes from 'virtual:inline-css-modules/App-0.module.css'
383
+ ```
384
+
385
+ The CSS gets extracted into a virtual module and then processed through your bundler's normal CSS pipeline. This means PostCSS plugins, Tailwind `@apply`, preprocessors, and any other CSS tooling work exactly as they would with a regular `.module.css` file.
386
+
387
+ ## Options
388
+
389
+ | Option | Type | Default | Description |
390
+ | -------------- | ------------------------------------------------------------------------------------------ | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- |
391
+ | `tagName` | `string` | `'css'` | Template tag name to match. Useful for avoiding conflicts with other CSS-in-JS libraries. |
392
+ | `fileMatch` | `RegExp` | `/\.(tsx\|jsx\|js\|vue\|svelte)$/` | Pattern for files to transform. |
393
+ | `extension` | `'css' \| 'scss' \| 'sass' \| 'styl' \| 'less' \| (filename: string) => SupportedExtension` | `'css'` | CSS preprocessor to use. Can be a string or a function that returns the extension based on the filename. |
394
+ | `inlineImport` | `boolean` | `true` | When `false`, generated imports are hoisted to the top of the file instead of replacing the declaration inline. |
395
+
396
+ ## Caveats
397
+
398
+ **No string interpolation.** The `css` tag looks like a template literal, but it's a compile-time transform. The contents are moved into a real CSS module, so dynamic values can't work.
399
+
400
+ **Class variables are replaced at compile time.** The `const classes = css\`...\`` declaration is replaced with an import statement, so you can't reassign or manipulate the variable at runtime.
401
+
402
+ ## Troubleshooting
403
+
404
+ **`css is not defined` or similar errors** -- Make sure the `tagName` option matches the tag you're using in your code. The plugin removes the import from `unplugin-inline-css-modules` and replaces the tagged template with a CSS module import. If the tag names don't match, the import gets removed but the template isn't transformed.
405
+
406
+ ## License
407
+
408
+ [MIT](./LICENSE)
@@ -0,0 +1,8 @@
1
+ import { PluginConfig } from './index.mjs';
2
+ export { css } from './index.mjs';
3
+ import { AstroIntegration } from 'astro';
4
+ import 'unplugin';
5
+
6
+ declare function export_default(options?: PluginConfig): AstroIntegration;
7
+
8
+ export { PluginConfig, export_default as default };
@@ -0,0 +1,8 @@
1
+ import { PluginConfig } from './index.js';
2
+ export { css } from './index.js';
3
+ import { AstroIntegration } from 'astro';
4
+ import 'unplugin';
5
+
6
+ declare function export_default(options?: PluginConfig): AstroIntegration;
7
+
8
+ export { PluginConfig, export_default as default };
package/dist/astro.js ADDED
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/astro.ts
21
+ var astro_exports = {};
22
+ __export(astro_exports, {
23
+ css: () => css,
24
+ default: () => astro_default
25
+ });
26
+ module.exports = __toCommonJS(astro_exports);
27
+
28
+ // src/index.ts
29
+ var import_unplugin = require("unplugin");
30
+ var import_crypto = require("crypto");
31
+ var import_fs = require("fs");
32
+ var import_path = require("path");
33
+ var matchInlineCssModules = /(?:const|var|let)\s*(\w+)(?:\s*:.*)?\s*=\s*(\w+)\s*`([\s\S]*?)`/gm;
34
+ var css = (_) => ({});
35
+ var virtualModuleId = "virtual:inline-css-modules";
36
+ var webpackModuleId = "inline-css-modules/virtual";
37
+ var resolvedVirtualModuleId = "\0" + virtualModuleId;
38
+ var resolvedWebpackModuleId = "\0inline-css-modules/virtual";
39
+ var cssModules = {};
40
+ 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`);
42
+ function ensureCacheDir() {
43
+ const dir = getCacheDir();
44
+ if (!(0, import_fs.existsSync)(dir)) {
45
+ (0, import_fs.mkdirSync)(dir, { recursive: true });
46
+ }
47
+ }
48
+ function hashCss(css2) {
49
+ return (0, import_crypto.createHash)("md5").update(css2).digest("hex");
50
+ }
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
+ function writeToCache(hash, css2) {
59
+ ensureCacheDir();
60
+ (0, import_fs.writeFileSync)(getCachePath(hash), css2);
61
+ }
62
+ var unpluginFactory = (config = {}, meta) => {
63
+ const fileMatch = config.fileMatch ?? /\.(tsx|jsx|js|vue|svelte)$/;
64
+ const tagName = config.tagName ?? "css";
65
+ const preprocessor = config.extension ?? "css";
66
+ const isWebpack = meta?.framework === "webpack" || meta?.framework === "rspack";
67
+ const moduleId = isWebpack ? webpackModuleId : virtualModuleId;
68
+ const resolvedId = isWebpack ? resolvedWebpackModuleId : resolvedVirtualModuleId;
69
+ return {
70
+ name: "inline-css-modules",
71
+ enforce: "pre",
72
+ resolveId(id) {
73
+ if (id === moduleId || id.startsWith(moduleId + "/")) {
74
+ return resolvedId + id.slice(moduleId.length);
75
+ }
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
+ return void 0;
83
+ },
84
+ loadInclude(id) {
85
+ return id.startsWith(resolvedVirtualModuleId) || id.startsWith(resolvedWebpackModuleId);
86
+ },
87
+ load(id) {
88
+ if (!id.startsWith(resolvedVirtualModuleId + "/") && !id.startsWith(resolvedWebpackModuleId + "/"))
89
+ return void 0;
90
+ const prefix = id.startsWith(resolvedVirtualModuleId + "/") ? resolvedVirtualModuleId + "/" : resolvedWebpackModuleId + "/";
91
+ const file = id.slice(prefix.length).replace(/\?used$/, "");
92
+ 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
+ }
106
+ return {
107
+ code: css2,
108
+ map: null
109
+ };
110
+ },
111
+ transform: {
112
+ filter: {
113
+ id: fileMatch
114
+ },
115
+ handler(src, id) {
116
+ let imports = [];
117
+ src = src.replace(
118
+ /import\s*{\s*(?:css|inlineCss)\s*(?:as\s*\w+\s*)?}\s*from\s*('|"|`)unplugin-inline-css-modules\1;?/gm,
119
+ ""
120
+ );
121
+ src = src.replaceAll(matchInlineCssModules, (substring, ...args) => {
122
+ const [variableName, tag, css2] = args;
123
+ if (tag !== tagName) return substring;
124
+ let baseFilename = id.slice(id.lastIndexOf("/") + 1);
125
+ baseFilename = baseFilename.slice(0, baseFilename.lastIndexOf("."));
126
+ let cnt = 0;
127
+ const ext = typeof preprocessor == "function" ? preprocessor(baseFilename) : preprocessor;
128
+ let filename = `${baseFilename}-${cnt}.module.${ext}`;
129
+ while (cssModules[filename]) {
130
+ cnt++;
131
+ filename = `${baseFilename}-${cnt}.module.${ext}`;
132
+ }
133
+ cssModules[filename] = css2;
134
+ let importStatement;
135
+ if (isWebpack) {
136
+ const hash = hashCss(css2);
137
+ writeToCache(hash, css2);
138
+ importStatement = `import ${variableName} from "${moduleId}/${hash}.module.${ext}"`;
139
+ }
140
+ importStatement = `import ${variableName} from "${moduleId}/${filename}"`;
141
+ if (config.inlineImport === false) {
142
+ imports.push(importStatement);
143
+ return "";
144
+ }
145
+ return importStatement;
146
+ });
147
+ if (imports.length) {
148
+ return imports.join("\n") + "\n" + src;
149
+ }
150
+ return {
151
+ code: src,
152
+ map: null
153
+ };
154
+ }
155
+ }
156
+ };
157
+ };
158
+ var unplugin = (0, import_unplugin.createUnplugin)(unpluginFactory);
159
+ var src_default = unplugin;
160
+
161
+ // src/astro.ts
162
+ function astro_default(options = {}) {
163
+ return {
164
+ name: "unplugin-inline-css-modules",
165
+ hooks: {
166
+ "astro:config:setup": async (astro) => {
167
+ astro.updateConfig({
168
+ vite: {
169
+ plugins: [
170
+ src_default.vite({
171
+ ...options,
172
+ fileMatch: options.fileMatch ?? /\.(tsx|jsx|js|vue|svelte|astro)$/
173
+ })
174
+ ]
175
+ }
176
+ });
177
+ }
178
+ }
179
+ };
180
+ }
181
+ // Annotate the CommonJS export names for ESM import in node:
182
+ 0 && (module.exports = {
183
+ css
184
+ });
185
+ //# sourceMappingURL=astro.js.map
@@ -0,0 +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, 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;;;ACCA,sBAA+B;AAC/B,oBAA2B;AAC3B,gBAAmE;AACnE,kBAAqB;AAWrB,IAAM,wBACJ;AAEK,IAAM,MAAM,CAAC,OAAqD,CAAC;AAE1E,IAAM,kBAAkB;AACxB,IAAM,kBAAkB;AACxB,IAAM,0BAA0B,OAAO;AACvC,IAAM,0BAA0B;AAEhC,IAAI,aAAqC,CAAC;AAE1C,IAAM,cAAc,UAClB,kBAAK,QAAQ,IAAI,GAAG,gBAAgB,UAAU,oBAAoB;AACpE,IAAM,eAAe,CAAC,aAAiB,kBAAK,YAAY,GAA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