Skip to content

Commit 8f2eace

Browse files
authored
v3.1.0 (#64)
1 parent c39b4c8 commit 8f2eace

File tree

9 files changed

+107
-15
lines changed

9 files changed

+107
-15
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## V3.1.0
2+
- fix [issue#61](https://github.com/indooorsman/esbuild-css-modules-plugin/issues/61)
3+
- fix [issue#59](https://github.com/indooorsman/esbuild-css-modules-plugin/issues/59)
4+
- do not modify user's configuration, throw warning if configuration is no valid for css modules plugin
5+
- support more options of `lightningcss`, see [index.d.ts](https://github.com/indooorsman/esbuild-css-modules-plugin/blob/main/index.d.ts) for details
6+
17
## V3.0.3
28
- Fix sourcemap
39

index.d.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import type { Plugin, PluginBuild } from 'esbuild';
2+
import type { BundleOptions, CustomAtRules, TransformOptions } from 'lightningcss';
23

34
declare type EmitDtsConfig = Partial<Record<'.d.css.ts' | '.css.d.ts' | '*', string>>;
45

5-
declare interface BuildOptions {
6+
declare interface BuildOptions
7+
extends Partial<
8+
Pick<
9+
BundleOptions<CustomAtRules>,
10+
| 'targets'
11+
| 'drafts'
12+
| 'nonStandard'
13+
| 'pseudoClasses'
14+
| 'errorRecovery'
15+
| 'visitor'
16+
| 'customAtRules'
17+
>
18+
> {
619
/**
720
* force to build css modules files even if `bundle` is disabled in esbuild
821
* @default false
@@ -46,7 +59,7 @@ declare interface BuildOptions {
4659
* ```
4760
* the plugin will try to get `shadowRoot` of the found element, and append css to the
4861
* `shadowRoot`, if no shadowRoot then append to the found element, if no element found then append to `document.head`.
49-
*
62+
*
5063
* could be a function with params content & digest (return a string of js code to inject css into page),
5164
* e.g.
5265
*
@@ -84,6 +97,16 @@ declare interface BuildOptions {
8497
* @default "camelCaseOnly"
8598
*/
8699
localsConvention?: 'camelCase' | 'pascalCase' | 'camelCaseOnly' | 'pascalCaseOnly';
100+
/**
101+
* Features that should always be compiled, even when supported by targets.
102+
* @see https://lightningcss.dev/transpilation.html#feature-flags
103+
*/
104+
featuresInclude?: BundleOptions<CustomAtRules>['include'];
105+
/**
106+
* Features that should never be compiled, even when unsupported by targets.
107+
* @see https://lightningcss.dev/transpilation.html#feature-flags
108+
*/
109+
featuresExclude?: BundleOptions<CustomAtRules>['exclude'];
87110
/**
88111
* namedExports
89112
* e.g.:

index.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { patchContext } from './lib/context.js';
2222
* @param {import('./index.js').Options} _options
2323
*/
2424
export const setup = (build, _options) => {
25-
build.initialOptions.metafile = true;
2625
const options = _options || {};
2726
validateOptions(options);
2827

@@ -41,6 +40,25 @@ export const setup = (build, _options) => {
4140
const forceInlineImages = !!options.forceInlineImages;
4241
const emitDts = options.emitDeclarationFile;
4342

43+
const warnMetafile = () => {
44+
if (patchedBuild.initialOptions.metafile) {
45+
return;
46+
}
47+
const warnings = patchedBuild.esbuild.formatMessagesSync(
48+
[
49+
{
50+
text: '`metafile` is not enabled, it may not work properly, please consider to set `metafile` to true is your esbuild configuration.',
51+
pluginName: pluginName
52+
}
53+
],
54+
{
55+
kind: 'warning',
56+
color: true
57+
}
58+
);
59+
console.log(warnings.join('\n'));
60+
}
61+
4462
patchedBuild.onLoad({ filter: /.+/, namespace: pluginCssNamespace }, (args) => {
4563
const { path } = args;
4664
log(`[${pluginCssNamespace}] on load:`, args);
@@ -245,6 +263,7 @@ export const setup = (build, _options) => {
245263
/** @type {[string, string][]} */
246264
const moduleJsFiles = [];
247265

266+
warnMetafile();
248267
Object.entries(r.metafile?.outputs ?? {}).forEach(([js, meta]) => {
249268
if (meta.entryPoint && modulesCssRegExp.test(meta.entryPoint)) {
250269
moduleJsFiles.push([meta.entryPoint, js]);
@@ -300,6 +319,7 @@ export const setup = (build, _options) => {
300319

301320
/** @type {[string, string][]} */
302321
const filesToBuild = [];
322+
warnMetafile();
303323
Object.entries(r.metafile?.outputs ?? {}).forEach(([outfile, meta]) => {
304324
if (meta.cssBundle) {
305325
filesToBuild.push([outfile, meta.cssBundle]);

lib/context.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const patchContext = (_build, options = {}) => {
1010
/** @type {import('../index.js').Build} */
1111
// @ts-ignore
1212
const build = _build;
13-
build.initialOptions.metafile = true;
1413
build.initialOptions.outbase ??= '.';
1514

1615
const buildId = getBuildId(build);

lib/css.helper.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ import { dirname, relative, resolve } from 'node:path';
33
import {
44
contentPlaceholder,
55
digestPlaceholder,
6+
fixImportPath,
67
pluginCssNamespace,
78
validateNamedExport
89
} from './utils.js';
9-
import { camelCase, sortBy, uniqBy, upperFirst } from 'lodash-es';
10+
import { camelCase, sortBy, uniqBy, upperFirst, pick } from 'lodash-es';
1011
import { injectorVirtualPath, pluginJsNamespace } from './utils.js';
1112
import { readFileSync } from 'node:fs';
1213

14+
const lightningcssOptions = [
15+
'targets',
16+
'drafts',
17+
'nonStandard',
18+
'pseudoClasses',
19+
'errorRecovery',
20+
'visitor',
21+
'customAtRules'
22+
];
23+
1324
export class CSSInjector {
1425
/** @type {Map<import('../index.js').Build, CSSInjector>} */
1526
static __instances__ = new Map();
@@ -106,7 +117,9 @@ ${isEsbuildBundleMode ? 'export { inject };' : ''}
106117
genImportCode(cssPath = '') {
107118
const isEsbuildBundleMode = this.build.initialOptions.bundle ?? false;
108119
return isEsbuildBundleMode
109-
? `import { inject } from "${pluginJsNamespace}:${cssPath}:${injectorVirtualPath}";`
120+
? `import { inject } from "${pluginJsNamespace}:${fixImportPath(
121+
cssPath
122+
)}:${injectorVirtualPath}";`
110123
: this.libCode;
111124
}
112125

@@ -154,7 +167,9 @@ export class CSSTransformer {
154167
const isEsbuildBundleMode = this.build.initialOptions.bundle ?? false;
155168

156169
/** @type {string[]} */
157-
const jsLines = isEsbuildBundleMode ? [`import "${pluginCssNamespace}:${relativePath}";`] : [];
170+
const jsLines = isEsbuildBundleMode
171+
? [`import "${pluginCssNamespace}:${fixImportPath(relativePath)}";`]
172+
: [];
158173

159174
/** @type {string[]} */
160175
const dtsLines = [];
@@ -268,7 +283,8 @@ ${uniqNames.map(([o, l]) => ` "${o}": ${l}`).join(',\n')}
268283
projectRoot: this.build.context.buildRoot,
269284
targets: {
270285
chrome: 112 << 16
271-
}
286+
},
287+
...pick(options, lightningcssOptions)
272288
};
273289
/** @type {{code: Buffer, exports: import('lightningcss').CSSModuleExports}} */
274290
// @ts-ignore

lib/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isAbsolute, resolve, sep, relative, basename, dirname } from 'node:path';
2+
import { sep as posixSep } from 'node:path/posix'
23
import { createHash } from 'node:crypto';
34
import { accessSync, constants } from 'node:fs';
45
import { createRequire } from 'node:module';
@@ -241,3 +242,11 @@ export {
241242
simpleMinifyCss,
242243
ensureFile
243244
};
245+
246+
/**
247+
* @param {string} p import path
248+
* @return {string}
249+
*/
250+
export const fixImportPath = (p) => {
251+
return p.split(sep).join(posixSep);
252+
}

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "3.0.3",
3+
"version": "3.1.0",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x), based on extremely fast [Lightning CSS](https://lightningcss.dev/)",
55
"main": "./index.cjs",
66
"module": "./index.js",
@@ -29,22 +29,25 @@
2929
},
3030
"author": "indooorsman@gmail.com",
3131
"license": "MIT",
32-
"repository": "https://github.com/indooorsman/esbuild-css-modules-plugin.git",
32+
"repository": {
33+
"type": "git",
34+
"url": "git+https://github.com/indooorsman/esbuild-css-modules-plugin.git"
35+
},
3336
"scripts": {
3437
"test": "cd ./test && rm -rf ./dist && node test.js",
3538
"test:cjs": "cd ./test && rm -rf ./dist && node test.cjs",
3639
"pub": "npm_config_registry=https://registry.npmjs.org/ npm publish --userconfig ~/.pubnpmrc --access public --tag latest"
3740
},
3841
"devDependencies": {
39-
"@types/lodash-es": "^4.17.7",
40-
"@types/node": "^17.0.23",
41-
"esbuild": "^0.19.4"
42+
"@types/lodash-es": "^4.17.12",
43+
"@types/node": "^20.10.0",
44+
"esbuild": "^0.19.8"
4245
},
4346
"peerDependencies": {
4447
"esbuild": "*"
4548
},
4649
"dependencies": {
47-
"lightningcss": "^1.22.0",
50+
"lightningcss": "^1.22.1",
4851
"lodash-es": "^4.17.21"
4952
},
5053
"publishConfig": {

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,19 @@ import cssModulesPlugin from '../index.js';
148148

149149
await esbuild.build(buildOptions);
150150
console.log('[test][esbuild:no:bundle] done, please check `test/dist/no-bundle`', '\n');
151+
152+
// testing no metafile & write false
153+
const r = await esbuild.build({
154+
...buildOptions,
155+
entryPoints: ['./app.jsx'],
156+
bundle: true,
157+
packages: 'external',
158+
metafile: false,
159+
write: false,
160+
loader: {
161+
'.jpg': 'file'
162+
},
163+
outdir: '__virtual_path__'
164+
});
165+
console.log('\nbuild result with metafile: false & write: false', r);
151166
})();

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"include": [
2222
"index.js",
23-
"lib/**/*"
23+
"lib/**/*",
24+
"index.d.ts"
2425
]
2526
}

0 commit comments

Comments
 (0)