@@ -2,6 +2,7 @@ import type { Plugin } from 'esbuild';
2
2
import type { Options } from '@pwrs/lit-css/lit-css' ;
3
3
import { transform } from '@pwrs/lit-css' ;
4
4
import { readFile } from 'node:fs/promises' ;
5
+ import * as path from 'node:path' ;
5
6
6
7
export interface LitCSSOptions extends Omit < Options , 'css' > {
7
8
filter : RegExp ;
@@ -13,12 +14,27 @@ export function litCssPlugin(options?: LitCSSOptions): Plugin {
13
14
name : 'lit-css' ,
14
15
setup ( build ) {
15
16
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 => {
17
32
const css = await readFile ( args . path , 'utf8' ) ;
18
33
const filePath = args . path ;
34
+
19
35
try {
20
36
const contents = await transform ( { css, specifier, tag, filePath, ...rest } ) ;
21
- return { contents, loader } ;
37
+ return { contents, loader, resolveDir : args . pluginData . resolveDir } ;
22
38
} catch ( error ) {
23
39
return {
24
40
errors : [ {
0 commit comments