Skip to content

Commit 9b864a3

Browse files
committed
v2.2.6
1 parent 14f8ad6 commit 9b864a3

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

lib/plugin.js

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const buildCssModulesJs = async ({ fullPath, options, build }) => {
6565
const cssImportPath = './' + builtCssFileName.split(path.sep).join(path.posix.sep).trim();
6666
const injectCode = `import "${cssImportPath}";`;
6767

68-
const exportDefault = `
68+
const exportDefault = options.inject
69+
? `
6970
export default new Proxy(${classNamesMapString}, {
7071
get: function(source, key) {
7172
setTimeout(() => {
@@ -74,7 +75,8 @@ export default new Proxy(${classNamesMapString}, {
7475
return source[key];
7576
}
7677
});
77-
`;
78+
`
79+
: `export default ${classNamesMapString};`;
7880

7981
const js = `${injectCode}\n${exportDefault};`;
8082

@@ -86,6 +88,45 @@ export default new Proxy(${classNamesMapString}, {
8688
};
8789
};
8890

91+
/**
92+
* onResolve
93+
* @description mark module(s).css as sideEffects and add namespace
94+
* @param {import('esbuild').OnResolveArgs} args
95+
* @param {import('..').Build} build
96+
* @returns {Promise<import('esbuild').OnResolveResult>}
97+
*/
98+
const onResolve = async (args, build) => {
99+
const { resolve, initialOptions } = build;
100+
const log = getLogger(build);
101+
const { resolveDir, path: p, pluginData } = args;
102+
const { buildRoot } = build.context;
103+
const { path: absPath } = await resolve(p, { resolveDir });
104+
log('resolve', p, 'to', path.relative(buildRoot, absPath));
105+
106+
/**
107+
* @type {import('esbuild').OnResolveResult}
108+
*/
109+
const result = {
110+
namespace: pluginNamespace,
111+
path: absPath,
112+
external: false,
113+
pluginData: {
114+
...(pluginData ?? {}),
115+
resolveDir: pluginData?.resolveDir || resolveDir,
116+
path: absPath
117+
},
118+
sideEffects: true,
119+
pluginName
120+
};
121+
122+
if (initialOptions.watch) {
123+
log('watching', path.relative(buildRoot, absPath));
124+
result.watchFiles = [absPath];
125+
}
126+
127+
return result;
128+
};
129+
89130
/**
90131
* onLoad
91132
* @param {import('..').Build} build
@@ -100,7 +141,7 @@ const onLoad = async (build, options, args) => {
100141

101142
const cached = cache.get(fullPath);
102143
if (cached) {
103-
log('return cache for', fullPath);
144+
log('return cache for', path.relative(buildRoot, fullPath));
104145
return cached;
105146
}
106147

@@ -121,7 +162,8 @@ const onLoad = async (build, options, args) => {
121162
pluginData: {
122163
css,
123164
exports,
124-
digest
165+
digest,
166+
resolveDir
125167
},
126168
contents: js,
127169
loader: 'js'
@@ -138,9 +180,6 @@ const onLoad = async (build, options, args) => {
138180
* @param {import('esbuild').BuildResult} result
139181
*/
140182
const onEnd = async (build, options, result) => {
141-
if (build.initialOptions.watch) {
142-
return;
143-
}
144183
const { buildId, buildRoot } = build.context;
145184
const log = getLogger(build);
146185

@@ -151,6 +190,7 @@ const onEnd = async (build, options, result) => {
151190
let injectTo = null;
152191
Object.keys(result.metafile?.outputs ?? []).forEach((f) => {
153192
if (
193+
!injectTo &&
154194
result.metafile.outputs[f].entryPoint &&
155195
entries.includes(path.resolve(buildRoot, result.metafile.outputs[f].entryPoint)) &&
156196
path.extname(f) === '.js'
@@ -163,7 +203,7 @@ const onEnd = async (build, options, result) => {
163203
cssContents.push(`\n/* ${f} */\n${css}\n`);
164204
}
165205
});
166-
log('inject css to', injectTo);
206+
log('inject css to', path.relative(buildRoot, injectTo));
167207
if (injectTo && cssContents.length) {
168208
const allCss = cssContents.join('');
169209
const container = typeof options.inject === 'string' ? options.inject : 'head';
@@ -196,10 +236,14 @@ const prepareBuild = async (build, options) => {
196236
/**
197237
* onLoadBuiltCss
198238
* @param {import('esbuild').OnLoadArgs} args
199-
* @returns {import('esbuild').OnLoadResult}
239+
* @param {import('..').Build} build
240+
* @returns {Promise<import('esbuild').OnLoadResult>}
200241
*/
201-
const onLoadBuiltCss = async ({ pluginData }) => {
202-
const { css, resolveDir } = pluginData;
242+
const onLoadBuiltCss = async ({ pluginData }, build) => {
243+
const { buildRoot } = build.context;
244+
const { css, resolveDir, path: p } = pluginData;
245+
const log = getLogger(build);
246+
log('loading built css', p, 'in', path.relative(buildRoot, resolveDir));
203247

204248
/**
205249
* @type {import('esbuild').OnLoadResult}
@@ -218,10 +262,10 @@ const onLoadBuiltCss = async ({ pluginData }) => {
218262
/**
219263
* onResolveBuiltCss
220264
* @param {import('esbuild').OnResolveArgs} args
221-
* @returns {import('esbuild').OnResolveResult}
265+
* @returns {Promise<import('esbuild').OnResolveResult>}
222266
*/
223267
const onResolveBuiltCss = async (args) => {
224-
const { resolveDir, path: p, pluginData } = args;
268+
const { resolveDir, path: p, pluginData = {} } = args;
225269

226270
/**
227271
* @type {import('esbuild').OnResolveResult}
@@ -232,7 +276,7 @@ const onResolveBuiltCss = async (args) => {
232276
external: false,
233277
pluginData: {
234278
...pluginData,
235-
resolveDir,
279+
resolveDir: pluginData.resolveDir || resolveDir,
236280
path: p
237281
},
238282
sideEffects: true,
@@ -250,11 +294,15 @@ const onResolveBuiltCss = async (args) => {
250294
*/
251295
const setup = async (build, options) => {
252296
await prepareBuild(build, options);
253-
297+
const { buildRoot } = build.context;
254298
const log = getLogger(build);
255299

256-
build.onLoad({ filter: /\.modules?\.css$/i }, async (args) => {
257-
log('loading', args.path);
300+
build.onResolve({ filter: /\.modules?\.css$/i, namespace: 'file' }, async (args) => {
301+
return await onResolve(args, build);
302+
});
303+
304+
build.onLoad({ filter: /\.modules?\.css$/i, namespace: pluginNamespace }, async (args) => {
305+
log('loading', path.relative(buildRoot, args.path));
258306
return await onLoad(build, options, args);
259307
});
260308

@@ -266,8 +314,7 @@ const setup = async (build, options) => {
266314
namespace: pluginNamespace
267315
},
268316
async (args) => {
269-
log('loading built css', args.path);
270-
return await onLoadBuiltCss(args);
317+
return await onLoadBuiltCss(args, build);
271318
}
272319
);
273320

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "esbuild-css-modules-plugin",
3-
"version": "2.2.5",
3+
"version": "2.2.6",
44
"description": "A esbuild plugin to bundle css modules into js(x)/ts(x).",
55
"main": "index.js",
66
"keywords": [

0 commit comments

Comments
 (0)