Skip to content

Commit 3c120b4

Browse files
committed
v2.5.0
1 parent 39e8286 commit 3c120b4

File tree

5 files changed

+119
-16
lines changed

5 files changed

+119
-16
lines changed

changelog.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
1+
## V2.5.0
2+
3+
- upgrade `@parcel/css` to `1.12.0`
4+
- validate class name, js keywords are considered to be invalid, e.g. `.default { } .const { }` will throw error during building
5+
6+
## V2.4.0
7+
8+
- Add filter option: Regular expression used to match CSS module files by @christianvuerings in #40
9+
110
## V2.3.1
211

312
- Fix [issue#33](https://github.com/indooorsman/esbuild-css-modules-plugin/issues/33)
413

514
## V2.3.0
615

716
- **V2**: upgrade `@parcel/css` to `1.9.0`
8-
- **V2**: add a new option `v2CssModulesOption`, refer to <https://github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0>
17+
- **V2**: add a new option `v2CssModulesOption`, refer to <https://github.com/parcel-bundler/parcel-css/releases/tag/v1.9.0>
918

1019
## V2.2.16
1120

1221
> commit: [6d0cc68](https://github.com/indooorsman/esbuild-css-modules-plugin/commit/6d0cc68ba51ed0f31d37894c4e3afec203b44d3d)
1322
1423
- **V2**: pass relative path to `@parcel/css` as filename to keep hash stable in different machines
1524

16-
1725
## V2.2.13
26+
1827
- **[v2]** [bugfix] exports of entry js are lost with auto inject
28+
1929
## V2.2.12
30+
2031
- **[v2]** only use cache in watch mode
2132
- **[v2]** refine inject logic
2233
- **[v2]** add example of custom inject to tests
2334

2435
## V2.2.11
36+
2537
- replace `process.memoryUsage.rss()` to `process.memoryUsage().rss` to support Nodejs<15.6.0
2638

2739
## V2.2.10
40+
2841
- **[v2]** refine cache logic
2942
- **[v2]** replace fs sync methods with promises
3043

3144
## V2.2.8
45+
3246
- **[v2]** refine some logs
3347
- **[v2]** make hash of outputs stable
3448
- **[v2]** support `entryPoints` as object
3549

3650
## V2.2.6
51+
3752
- **[v2]** refine some logs
3853
- **[v2]** handle `onResolve` for `.modules.css` files to add `sideEffects: true` & `namespace` to the resolve result
3954
- **[v2]** better support `watch` mode
4055

4156
## V2.2.5
57+
4258
- refactor a lot, **v2** will not generate temporary folders/files anymore
4359
- **v2** now support auto inject generated css into page
4460
- inject for and only for **v2** can be set to a css selector of the element which you want to inject css to, if the element can't be found then inject to document.head
@@ -50,4 +66,4 @@
5066
## V2.1.3:
5167

5268
- support `@import url(./xxx/xxx.css)` in v2 (path can't be remote url)
53-
- upgrade `@parcel/css` to `1.3.1`
69+
- upgrade `@parcel/css` to `1.3.1`

lib/plugin.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const {
1212
getModulesCssRegExp,
1313
getBuiltModulesCssRegExp,
1414
getRelativePath,
15-
getBuildId
15+
getBuildId,
16+
validateNamedExport
1617
} = require('./utils.js');
1718
const cssHandler = require('@parcel/css');
1819
const camelCase = require('lodash/camelCase');
@@ -87,9 +88,16 @@ export default new Proxy(${classNamesMapString}, {
8788
`
8889
: `export default ${classNamesMapString};`;
8990

90-
const namedExportStatements = Object.entries(cssModulesJSON).map(
91-
([camelCaseClassName, className]) => `export const ${camelCaseClassName} = "${className}";`
92-
).join('\n');
91+
const namedExportStatements = Object.entries(cssModulesJSON)
92+
.map(([camelCaseClassName, className]) => {
93+
if (!validateNamedExport(camelCaseClassName)) {
94+
throw new Error(
95+
`the class name "${camelCaseClassName}" in file ${fullPath} is a reserved keyword in javascript, please change it to someother word to avoid potential errors`
96+
);
97+
}
98+
return `export const ${camelCaseClassName} = "${className}";`;
99+
})
100+
.join('\n');
93101

94102
const js = `${importStatement}\n${exportStatement};\n${namedExportStatements}`;
95103

lib/utils.js

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const { createHash } = require('crypto');
33
const { readFile } = require('fs/promises');
4+
const fs = require('fs');
45
const pluginName = require('../package.json').name.toLowerCase();
56
const pluginNamespace = `${pluginName}-namespace`;
67
const buildingCssSuffix = `?${pluginName}-building`;
@@ -13,8 +14,8 @@ const builtCssSuffixRegExp = builtCssSuffix.replace('?', '\\?').replace(/\-/g, '
1314
* @returns {RegExp}
1415
*/
1516
const getModulesCssRegExp = (options) => {
16-
return options.filter ?? /\.modules?\.css$/i
17-
}
17+
return options.filter ?? /\.modules?\.css$/i;
18+
};
1819

1920
/**
2021
* getBuiltModulesCssRegExp
@@ -23,9 +24,11 @@ const getModulesCssRegExp = (options) => {
2324
*/
2425
const getBuiltModulesCssRegExp = (options) => {
2526
const baseRegExp = getModulesCssRegExp(options);
26-
const baseRegExpSource = baseRegExp.source.endsWith('$') ? baseRegExp.source.slice(0, -1) : baseRegExp.source;
27+
const baseRegExpSource = baseRegExp.source.endsWith('$')
28+
? baseRegExp.source.slice(0, -1)
29+
: baseRegExp.source;
2730
return new RegExp(`${baseRegExpSource}${builtCssSuffixRegExp}$`, 'i');
28-
}
31+
};
2932

3033
/**
3134
* getLogger
@@ -103,6 +106,22 @@ const getRootDir = (build) => {
103106
return rootDir;
104107
};
105108

109+
/**
110+
* getPackageVersion
111+
* @param {import('..').Build} build
112+
* @returns {string}
113+
*/
114+
const getPackageVersion = (build) => {
115+
const rootDir = getRootDir(build);
116+
const packageJsonFile = path.resolve(rootDir, './package.json');
117+
try {
118+
fs.accessSync(packageJsonFile, fs.constants.R_OK);
119+
return require(packageJsonFile).version ?? 'unknown';
120+
} catch (error) {
121+
return 'unknown';
122+
}
123+
};
124+
106125
/**
107126
* getRelativePath
108127
* @description get relative path from build root
@@ -127,6 +146,7 @@ const getRelativePath = (build, to) => {
127146
const getBuildId = async (build) => {
128147
const { entryPoints } = build.initialOptions;
129148
const buildRoot = getRootDir(build);
149+
const packageVersion = getPackageVersion(build);
130150
let entries = [];
131151
if (Array.isArray(entryPoints)) {
132152
entries = [...entryPoints];
@@ -137,7 +157,7 @@ const getBuildId = async (build) => {
137157
entries.push(entryPoints[k]);
138158
});
139159
}
140-
const entryContents = (
160+
const entryContents = packageVersion + (
141161
await Promise.all(
142162
entries.map(async (p) => {
143163
const absPath = path.isAbsolute(p) ? p : path.resolve(buildRoot, p);
@@ -148,6 +168,63 @@ const getBuildId = async (build) => {
148168
return createHash('sha256').update(entryContents).digest('hex');
149169
};
150170

171+
const jsKeywords = [
172+
'await',
173+
'break',
174+
'case',
175+
'catch',
176+
'class',
177+
'const',
178+
'continue',
179+
'debugger',
180+
'default',
181+
'delete',
182+
'do',
183+
'else',
184+
'enum',
185+
'export',
186+
'extends',
187+
'false',
188+
'finally',
189+
'for',
190+
'function',
191+
'if',
192+
'implements',
193+
'import',
194+
'in',
195+
'instanceof',
196+
'interface',
197+
'let',
198+
'new',
199+
'null',
200+
'package',
201+
'private',
202+
'protected',
203+
'public',
204+
'return',
205+
'super',
206+
'switch',
207+
'static',
208+
'this',
209+
'throw',
210+
'try',
211+
'true',
212+
'typeof',
213+
'var',
214+
'void',
215+
'while',
216+
'with',
217+
'yield'
218+
];
219+
220+
/**
221+
* @param {string} name
222+
* @returns {boolean}
223+
*/
224+
const validateNamedExport = (name) => {
225+
return !jsKeywords.includes(name);
226+
};
227+
151228
module.exports = {
152229
pluginName,
153230
pluginNamespace,
@@ -159,5 +236,7 @@ module.exports = {
159236
getBuiltModulesCssRegExp,
160237
buildingCssSuffix,
161238
getRelativePath,
162-
getBuildId
239+
getBuildId,
240+
validateNamedExport,
241+
getPackageVersion
163242
};

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.4.0",
3+
"version": "2.5.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.9.0",
31+
"@parcel/css": "^1.12.0",
3232
"fs-extra": "^10.1.0",
3333
"lodash": "^4.17.21",
3434
"postcss": "^8.4.12",

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# esbuild-css-modules-plugin
22

3-
[![npm version](https://img.shields.io/npm/v/esbuild-css-modules-plugin.svg?style=flat)](https://www.npmjs.com/package/esbuild-css-modules-plugin)
3+
[![npm version](https://img.shields.io/npm/v/esbuild-css-modules-plugin.svg?v=2.5.0)](https://www.npmjs.com/package/esbuild-css-modules-plugin)
44
[![Test](https://github.com/indooorsman/esbuild-css-modules-plugin/actions/workflows/test.yml/badge.svg)](https://github.com/indooorsman/esbuild-css-modules-plugin/actions/workflows/test.yml)
55

66
A esbuild plugin to bundle css modules into js(x)/ts(x).

0 commit comments

Comments
 (0)