Skip to content

Commit 25ad5b0

Browse files
committed
3.1 release
1 parent 153b785 commit 25ad5b0

File tree

5 files changed

+810
-381
lines changed

5 files changed

+810
-381
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# @note bruh stupid pnpm removes optional deps from package.json, that are deps of another dep, so i had to hoist pug packages into root modules folder, so I could fucking import this shit
2+
public-hoist-pattern[]=pug-*

README.md

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
[![npm](https://img.shields.io/npm/v/vite-plugin-vue-static-css-modules?color=pink&style=flat-square)](https://www.npmjs.com/package/vite-plugin-vue-static-css-modules)
2-
[![npm](https://img.shields.io/npm/dw/vite-plugin-vue-static-css-modules?color=pink&style=flat-square)](https://www.npmjs.com/package/vite-plugin-vue-static-css-modules)
1+
[![npm](https://img.shields.io/npm/v/vite-plugin-vue-css-modules?color=pink&style=flat-square)](https://www.npmjs.com/package/vite-plugin-vue-css-modules)
2+
[![npm](https://img.shields.io/npm/dw/vite-plugin-vue-css-modules?color=pink&style=flat-square)](https://www.npmjs.com/package/vite-plugin-vue-css-modules)
33
[![Discord](https://img.shields.io/discord/405510915845390347?color=pink&label=join%20discord&style=flat-square)](https://zeokku.com/discord)
44

5-
# ✨ vite-plugin-vue-static-css-modules
5+
# ✨ vite-plugin-vue-css-modules
66

77
**Ultimate solution** for using **CSS modules** without any hassle. Automatic replacement for Vue templates and scripts. You don't have to use `$style` object, just write the code as usual.
88

@@ -11,24 +11,24 @@
1111
## Installation
1212

1313
```
14-
pnpm add -D vite-plugin-vue-static-css-modules
14+
pnpm add -D vite-plugin-vue-css-modules
1515
# or
16-
yarn add -D vite-plugin-vue-static-css-modules
16+
yarn add -D vite-plugin-vue-css-modules
1717
# or
18-
npm i -D vite-plugin-vue-static-css-modules
18+
npm i -D vite-plugin-vue-css-modules
1919
```
2020

2121
## Usage
2222

2323
In `vite.config.ts`:
2424

2525
```javascript
26-
import staticCssModules, { removeCssModulesChunk } from "vite-plugin-vue-static-css-modules";
26+
import { cssm, removeCssModulesChunk } from "vite-plugin-vue-css-modules";
2727

2828
export default defineConfig({
2929
plugins: [
3030
//...,
31-
staticCssModules(),
31+
cssm(),
3232
vue(),
3333
// optionally
3434
removeCssModulesChunk(),
@@ -44,28 +44,28 @@ If you used `<style scoped>` before, the plugin should work out of the box witho
4444

4545
## Options
4646

47-
The `staticCssModules()` plugin accepts an object `{}` with options:
47+
The plugin accepts an object `{}` with options:
4848

4949
- `preservePrefix` - an arbitrary string to be used as a prefix for names so they would not be processed and instead would be preserved as-is without the prefix. Useful for styles unaffected by CSS modules or custom #id values **(default: `"--"`)**
5050
- `scopeBehaviour` - corresponds to `CSSModulesOptions["scopeBehaviour"]` **(default: `"local"`)**
51-
- `scriptTransform` - if it's `false` - the plugin **will** wrap variables inside of `<template>` in CSS module context variable like so `$style[var]`. If it's `true` then the plugin will transform macros in `<script>` and `<script setup>` blocks and **will not** wrap anything in `<template>` (see more below) **(default: `false`)**
51+
- `scriptTransform` - if it's `false` - the plugin **will** wrap variables inside of `<template>` in CSS module context variable like so `$style[var]`. If it's `true` then the plugin will transform `$cssModule` macros in `<script>` and `<script setup>` blocks and **will not** wrap anything in `<template>` (see more below) **(default: `false`)**
5252
- `pugLocals` - an object containing variables for Pug templates **(default: `{}`)**
53-
- `nameGenerator` - a function of type `CSSModulesOptions["generateScopedName"]` accepting (name, filename, css) arguments. This function will be called for each name in each file and it should return a result which will be used for generating a stylesheet. It is possible that the function may be called multiple times with the same pair of name and filename, so it must maintain its own state to return the same name in such case.
53+
- `nameGenerator` - a function of type `CSSModulesOptions["generateScopedName"]` accepting `(name, filename, css)` arguments. This function will be called for each name in each file and it should return a result which will be used for generating a stylesheet. It is possible that the function may be called multiple times with the same pair of name and filename, so it must maintain its own state to return the same name in such case.
5454

55-
The plugin provides two generators as **default** value. If `process.env.NODE_ENV === "production"` then the generator will minify resulting names, otherwise during development the generator returns `COMPONENT__CLASSNAME` type of string.
55+
The plugin provides two generators as **default** value. If `process.env.NODE_ENV === "production"` then the generator will minify resulting names, otherwise during development the generator returns `Component_Path__classname` type of string.
5656

5757
## Script handling
5858

59-
You can optionally use `removeCssModulesChunk()` plugin after `vue()` to strip out CSS module object for each component due to its redundancy, in this case `$style` and other CSS module context variables won't be available in `<template>`, so if you reference names in variables and use them in `<template>`, you must use `$cssModule` macro in `<script>` (see below).
59+
You can optionally use `removeCssModulesChunk()` plugin after `vue()` to strip out CSS module object for each component due to its redundancy, in this case `$style` and other CSS module context variables won't be available in `<template>`, so if you reference names in variables and then use them in `<template>`, you must use `$cssModule` macro in `<script>` (see below).
6060

6161
If you need to access CSS modules in Javascript, you have two options:
6262

63-
1. _RECOMMENDED!_ Use `$cssModule` macro to access CSS modules (and set `scriptTransform` to `true`).
63+
1. **_RECOMMENDED!_** Use `$cssModule` macro to access CSS modules (and set `scriptTransform` to `true`).
6464

65-
If you're using Typescript, place the following code in your `env.d.ts` (or any other file) to get types support
65+
If you're using Typescript, place the following code in your `env.d.ts` (or any other file) to get basic types support
6666

6767
```ts
68-
/// <reference types="vite-plugin-vue-static-css-modules/macros" />
68+
/// <reference types="vite-plugin-vue-css-modules/macros" />
6969
```
7070

7171
The macro will be statically replaced with a resulting name string, so you can reference the variable in `<template>` as usual. Since the replacement is static you're allowed to use only the following forms:
@@ -76,10 +76,93 @@ If you need to access CSS modules in Javascript, you have two options:
7676
// OR
7777
$cssModule['class2'];
7878
// OR
79+
// NOTICE! camel case will be transformed to hyphenated when using property notation
80+
// so this will be processed as 'another-class'
7981
$cssModule.anotherClass;
8082
```
8183

82-
2. `useCssModule` Vue composition function. Depending on the usage of JS variables in `<template>` you may either enable or disable `scriptTransform`. If you use the result of `useCssModules()[...]` in your `<template>` then you should enable `scriptTransform`, so the plugin doesn't wrap these variables in `$style[...]`. Otherwise set it to `false`, so any other referenced variables in `<template>` will be wrapped.
84+
2. `useCssModule` Vue composition function. Depending on the usage of JS variables in `<template>` you may either enable or disable `scriptTransform`. If you use the result of `useCssModules()[...]` in your `<template>` then you should enable `scriptTransform`, so the plugin doesn't wrap these variables in `$style[...]`. Otherwise set it to `false`, so any referenced variables in `<template>` will be wrapped.
85+
86+
## Cross component referencing
87+
88+
Default name generators maintain a record which maps particular class from a particular component file to CSS modules name. This allows us to reference class names from other components, achieving global accessability of any class name in any component. Look at the example:
89+
90+
```vue
91+
<!-- src/App.vue -->
92+
93+
<template lang="pug">
94+
.app
95+
.class-name
96+
</template>
97+
98+
<style module>
99+
.app {
100+
}
101+
102+
.class-name {
103+
}
104+
</style>
105+
```
106+
107+
We can access class names from App.vue by using scope `App__`
108+
109+
```vue
110+
<!-- src/components/Foo.vue -->
111+
112+
<template lang="pug">
113+
.foo
114+
.App__class-name
115+
</template>
116+
117+
<style module>
118+
.foo {
119+
}
120+
</style>
121+
```
122+
123+
Any class name is available from any component by using a scope prefix. Scope prefix must be specified following the rules:
124+
125+
1. Scope prefix is separated from class name by double underscore `__`
126+
`App__class-name`
127+
1. Root directory is `/src/`. Subdirectories are denoted by single underscore `_`
128+
`/src/path/sub/Bar.vue` will be `path_sub_Bar__class-name`
129+
1. If the file is in `/src/components/` folder then prefix must be `C[ComponentFileName]`
130+
`/src/components/Foo.vue` will be `CFoo__class-name`
131+
Subdirectories work the same:
132+
`/src/components/Foo/Bar.vue` will be `CFoo_Bar__class-name`
133+
1. If the file is in `/src/views/` folder then prefix must be `V[ComponentFileName]`
134+
`/src/views/About.vue` will be `VAbout__class-name`
135+
136+
## Edge cases
137+
138+
Sometimes it's needed to preserve id/class names. Here is where `preservePrefix` option is used (in the example below we assume it's the default `--` value). Individual class names in both regular attributes and as string literals in `:class` having the prefix will not be processed but the prefix being removed. You can also use `--class` or `:--class` attributes to skip processing of entire attribute value.
139+
140+
```pug
141+
.--escaped0
142+
//- you can mix escaped as you want
143+
.--escaped1.class0
144+
145+
#--escaped2
146+
147+
div(:--class="someRawVar")
148+
div(:--id="someRawVar2")
149+
150+
div(--class="class0 class1")
151+
```
152+
153+
will be turned into
154+
155+
```html
156+
<div class="escaped0"></div>
157+
<div class="escaped1 TRANSFORMED_class0"></div>
158+
159+
<div id="escaped2"></div>
160+
161+
<div :class="someRawVar"></div>
162+
<div :id="someRawVar2"></div>
163+
164+
<div class="class0 class1"></div>
165+
```
83166

84167
## Example
85168

@@ -122,7 +205,7 @@ console.log("script");
122205
</script>
123206
124207
<script lang="ts" setup>
125-
/// <reference path="vite-plugin-vue-static-css-modules/macros.d.ts" />
208+
/// <reference path="vite-plugin-vue-css-modules/macros.d.ts" />
126209
127210
const props = defineProps<{ title: string }>();
128211
@@ -279,26 +362,3 @@ Result with `scriptTransform` disabled. Notice that variables are wrapped in `$s
279362
<div :id="someRawVar2"></div>
280363
</template>
281364
```
282-
283-
## Edge cases
284-
285-
Sometimes it's needed to preserve id/class names. Here is where `preservePrefix` option is used. From the examples above:
286-
287-
```pug
288-
.--escaped0
289-
#--escaped1
290-
291-
div(:--class="someRawVar")
292-
div(:--id="someRawVar2")
293-
```
294-
295-
was turned into
296-
297-
```html
298-
<div class="escaped0"></div>
299-
<div id="escaped1"></div>
300-
<div :class="someRawVar"></div>
301-
<div :id="someRawVar2"></div>
302-
```
303-
304-
no matter the settings

notes.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ ended up just using parse and `.slice(0, -1)` to remove the semicolon
2828
# @todo css dictionary names
2929

3030
# idea to enable script transform as `<script css-module-transform>` tag attribute?
31+
32+
# !!! allow linking class names from other components
33+
34+
options:
35+
36+
```js
37+
{
38+
linkingPrefix: '-',
39+
linkingDir: '_',
40+
linkingSeparator: '--'
41+
}
42+
```
43+
44+
// link from /src/
45+
`.-components_menu_button--wrap` - link `.wrap` class from `/src/components/menu/Button.vue

package.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "vite-plugin-vue-static-css-modules",
2+
"name": "vite-plugin-vue-css-modules",
33
"description": "✨ Ultimate solution for using CSS modules without any hassle. Automatic replacement for Vue templates and scripts",
4-
"version": "3.0.4",
4+
"version": "3.1.0",
55
"author": "Lutymane <[email protected]>",
66
"funding": "https://ko-fi.com/zeokku",
77
"license": "MIT",
@@ -11,13 +11,14 @@
1111
"scripts": {
1212
"build": "tsc",
1313
"watch": "tsc --watch",
14-
"test": "node ./test"
14+
"test:static": "node ./test",
15+
"test": "vitest --watch"
1516
},
1617
"files": [
1718
"dist/*",
1819
"macros.d.ts"
1920
],
20-
"repository": "github:zeokku/vite-plugin-vue-static-css-modules",
21+
"repository": "github:zeokku/vite-plugin-vue-css-modules",
2122
"keywords": [
2223
"vite",
2324
"vite-plugin",
@@ -32,22 +33,23 @@
3233
],
3334
"devDependencies": {
3435
"@types/babel__generator": "^7.6.4",
35-
"@types/babel__traverse": "^7.18.2",
36-
"@types/node": "^16.18.3",
36+
"@types/babel__traverse": "^7.18.3",
37+
"@types/node": "^18.15.10",
3738
"@types/pug": "^2.0.6",
38-
"@vue/compiler-sfc": "^3.2.41",
39+
"@vue/compiler-sfc": "^3.2.47",
3940
"pug": "^3.0.2",
40-
"typescript": "^4.9.2-rc",
41-
"vite": "^3.2.3"
41+
"typescript": "^5.0.2",
42+
"vite": "^4.2.1",
43+
"vitest": "^0.29.7"
4244
},
4345
"dependencies": {
44-
"@babel/generator": "^7.20.3",
45-
"@babel/parser": "^7.20.3",
46-
"@babel/traverse": "^7.20.1",
47-
"@babel/types": "^7.20.2",
46+
"@babel/generator": "^7.21.3",
47+
"@babel/parser": "^7.21.3",
48+
"@babel/traverse": "^7.21.3",
49+
"@babel/types": "^7.21.3",
4850
"dom-serializer": "^2.0.0",
4951
"domhandler": "^5.0.3",
50-
"htmlparser2": "^8.0.1",
52+
"htmlparser2": "^8.0.2",
5153
"incstr": "^1.2.3"
5254
},
5355
"optionalDependencies": {
@@ -58,7 +60,7 @@
5860
"@vue/compiler-sfc": "^3.0.0"
5961
},
6062
"bugs": {
61-
"url": "https://github.com/zeokku/vite-plugin-vue-static-css-modules/issues"
63+
"url": "https://github.com/zeokku/vite-plugin-vue-css-modules/issues"
6264
},
63-
"homepage": "https://github.com/zeokku/vite-plugin-vue-static-css-modules#readme"
65+
"homepage": "https://github.com/zeokku/vite-plugin-vue-css-modules#readme"
6466
}

0 commit comments

Comments
 (0)