-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathesbuild-plugin-lit-css.ts
38 lines (36 loc) · 1.08 KB
/
esbuild-plugin-lit-css.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Plugin } from 'esbuild';
import type { Options } from '@pwrs/lit-css/lit-css';
import { transform } from '@pwrs/lit-css';
import { readFile } from 'node:fs/promises';
export interface LitCSSOptions extends Omit<Options, 'css'> {
filter: RegExp;
}
export function litCssPlugin(options?: LitCSSOptions): Plugin {
const { filter = /\.css$/, specifier, tag, ...rest } = options ?? {};
return {
name: 'lit-css',
setup(build) {
const loader = 'js';
build.onLoad({ filter }, async args => {
const css = await readFile(args.path, 'utf8');
const filePath = args.path;
try {
const contents = await transform({ css, specifier, tag, filePath, ...rest });
return { contents, loader };
} catch (error) {
return {
errors: [{
text: error.message,
detail: error,
location: {
file: error.file,
line: error.line,
column: error.column,
},
}],
};
}
});
},
};
}