Skip to content

Commit 9830e05

Browse files
committed
fix resolution of imported css files
1 parent ddd01df commit 9830e05

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/esbuild-plugin-lit-css/esbuild-plugin-lit-css.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Plugin } from 'esbuild';
22
import type { Options } from '@pwrs/lit-css/lit-css';
33
import { transform } from '@pwrs/lit-css';
44
import { readFile } from 'node:fs/promises';
5+
import * as path from 'node:path';
56

67
export interface LitCSSOptions extends Omit<Options, 'css'> {
78
filter: RegExp;
@@ -13,12 +14,27 @@ export function litCssPlugin(options?: LitCSSOptions): Plugin {
1314
name: 'lit-css',
1415
setup(build) {
1516
const loader = 'js';
16-
build.onLoad({ filter }, async args => {
17+
18+
build.onResolve({ filter }, args => {
19+
return {
20+
path: path.resolve(args.resolveDir, args.path),
21+
namespace: 'env-lit-css',
22+
pluginData: {
23+
// Needed when using the "js" loader to properly resolve "lit"
24+
resolveDir: args.resolveDir,
25+
},
26+
};
27+
});
28+
29+
// Load paths tagged with the 'env-ns' namespace and behave as if
30+
// they point to a JSON file containing the environment variables.
31+
build.onLoad({ filter: /.*/, namespace: 'env-lit-css' }, async args => {
1732
const css = await readFile(args.path, 'utf8');
1833
const filePath = args.path;
34+
1935
try {
2036
const contents = await transform({ css, specifier, tag, filePath, ...rest });
21-
return { contents, loader };
37+
return { contents, loader, resolveDir: args.pluginData.resolveDir };
2238
} catch (error) {
2339
return {
2440
errors: [{

0 commit comments

Comments
 (0)