Skip to content

Commit c628d1a

Browse files
committed
v2.3.0
1 parent 650e921 commit c628d1a

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## V2.3.0
2+
3+
- **V2**: upgrade `@parcel/css` to `1.9.0`
4+
- **V2**: add a new option `v2CssModulesOption`, refer to <https://github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0>
5+
16
## V2.2.16
27

38
> commit: [6d0cc68](https://github.com/indooorsman/esbuild-css-modules-plugin/commit/6d0cc68ba51ed0f31d37894c4e3afec203b44d3d)

index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ declare interface PluginOptions {
4747
generateScopedName?: CssModulesOptions['generateScopedName'];
4848
cssModulesOption?: CssModulesOptions;
4949
v2?: boolean;
50+
v2CssModulesOption?: {
51+
/**
52+
* refer to: https://github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0
53+
*/
54+
dashedIndents?: boolean;
55+
/**
56+
* The currently supported segments are:
57+
* [name] - the base name of the CSS file, without the extension
58+
* [hash] - a hash of the full file path
59+
* [local] - the original class name
60+
*/
61+
pattern?: string;
62+
};
5063
root?: string;
5164
package?: {
5265
name: string;

lib/plugin.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ const buildCssModulesJs = async ({ fullPath, options, build }) => {
2727
const cssFileName = path.basename(fullPath); // e.g. xxx.module.css?esbuild-css-modules-plugin-building
2828
const { buildId, relative } = build.context;
2929
const resolveDir = path.dirname(fullPath);
30-
const classPrefix = path.basename(fullPath, path.extname(fullPath)).replace(/\./g, '-') + '__';
30+
const classPrefix =
31+
path.basename(fullPath, path.extname(fullPath)).replace(/[^a-zA-Z0-9]/g, '-') + '__';
3132
const originCss = await readFile(fullPath);
33+
const cssModulesOption = options.v2CssModulesOption || {};
3234

3335
/**
3436
* @type {import('@parcel/css').BundleOptions}
@@ -38,7 +40,10 @@ const buildCssModulesJs = async ({ fullPath, options, build }) => {
3840
code: originCss,
3941
minify: false,
4042
sourceMap: true,
41-
cssModules: true,
43+
cssModules: {
44+
pattern: `${classPrefix}[local]_[hash]`,
45+
...cssModulesOption
46+
},
4247
analyzeDependencies: false
4348
};
4449
const { code, exports = {}, map } = cssHandler.transform(bundleConfig);
@@ -50,11 +55,7 @@ const buildCssModulesJs = async ({ fullPath, options, build }) => {
5055
.sort() // to keep order consistent in different builds
5156
.forEach((originClass) => {
5257
const patchedClass = exports[originClass].name;
53-
cssModulesJSON[camelCase(originClass)] = classPrefix + patchedClass;
54-
cssModulesContent = cssModulesContent.replace(
55-
new RegExp(`\\.${patchedClass}`, 'g'),
56-
'.' + classPrefix + patchedClass
57-
);
58+
cssModulesJSON[camelCase(originClass)] = patchedClass;
5859
});
5960
const classNamesMapString = JSON.stringify(cssModulesJSON);
6061

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "2.2.16",
3+
"version": "2.3.0",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
55
"main": "./index.js",
66
"types": "./index.d.ts",
@@ -28,7 +28,7 @@
2828
"esbuild": "^0.14.38"
2929
},
3030
"dependencies": {
31-
"@parcel/css": "1.7.3",
31+
"@parcel/css": "1.9.0",
3232
"fs-extra": "^10.1.0",
3333
"lodash": "^4.17.21",
3434
"postcss": "^8.4.12",

readme.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ yarn add -D esbuild-css-modules-plugin
2525

2626
## Usage
2727

28-
```js
28+
````js
2929
const esbuild = require('esbuild');
3030
const cssModulesPlugin = require('esbuild-css-modules-plugin');
3131

@@ -57,7 +57,17 @@ esbuild.build({
5757
},
5858

5959
v2: true // experimental. v2 can bundle images in css, note if set `v2` to true, other options except `inject` will be ignored. and v2 only works with `bundle: true`.
60+
v2CssModulesOption: { // Optional.
61+
dashedIndents: boolean; // Optional. refer to: https://github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0
62+
/**
63+
* Optional. The currently supported segments are:
64+
* [name] - the base name of the CSS file, without the extension
65+
* [hash] - a hash of the full file path
66+
* [local] - the original class name
67+
*/
68+
pattern: string;
69+
}
6070
})
6171
]
6272
});
63-
```
73+
````

0 commit comments

Comments
 (0)