Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resolution of imported css files #61

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/esbuild-plugin-lit-css/esbuild-plugin-lit-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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';
import * as path from 'node:path';

export interface LitCSSOptions extends Omit<Options, 'css'> {
filter: RegExp;
Expand All @@ -13,12 +14,27 @@ export function litCssPlugin(options?: LitCSSOptions): Plugin {
name: 'lit-css',
setup(build) {
const loader = 'js';
build.onLoad({ filter }, async args => {

build.onResolve({ filter }, args => {
return {
path: path.resolve(args.resolveDir, args.path),
namespace: 'env-lit-css',
pluginData: {
// Needed when using the "js" loader to properly resolve "lit"
resolveDir: args.resolveDir,
},
};
});

// Load paths tagged with the 'env-ns' namespace and behave as if
// they point to a JSON file containing the environment variables.
build.onLoad({ filter: /.*/, namespace: 'env-lit-css' }, 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 };
return { contents, loader, resolveDir: args.pluginData.resolveDir };
} catch (error) {
return {
errors: [{
Expand Down
Loading