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

Add ability for ESBuild to watch a directory for CSS changes #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jhirn
Copy link

@jhirn jhirn commented Oct 26, 2021

Hello. I needed this make ES Build recognize changes to my stylesheets which were in a different location. The approach is copied form esbuild-scss-plugin and works well for me locally.

I can make some changes if you desire to merge it. If not this serves as an example of how to do this.

Cheers!

@jhirn
Copy link
Author

jhirn commented Oct 26, 2021

Usage:

 plugins: [
    svgrPlugin(svgrOptions),
    esbuildPostCssPlugin({
      cssPath:'./app/assets/stylesheets/',
      plugins: [
        postCssImportPlugin,
        postCssTailwindNestingPlugin,
        postCssTailwindPlugin('./tailwind.config.js'),
        postCssAutoPrefixer
      ]
    })
  ],

Happy to update the readme as well.

@hanayashiki
Copy link

I think you might just watch those are bundled!

@hanayashiki
Copy link

hanayashiki commented Nov 5, 2021

I just wonder why this plugin uses temp files. I wrote a custom one, which is quite easy, handling file watching with no extra efforts. Just use postcss as the loader:

import { Plugin } from 'esbuild'
import postcss, { AcceptedPlugin } from 'postcss'
import fs from 'fs/promises'

export const pluginPostcss = (plugins?: AcceptedPlugin[]): Plugin => {
  return {
    name: 'plugin-postcss',
    setup(build) {
      build.onLoad({ filter: /\.css$/ }, async ({ path }) => {
        const processor = postcss(plugins);
        const content = await fs.readFile(path);
        const result = await processor.process(content, { from: path });
        return {
          contents: result.toString(),
          loader: 'css',
        }
      });
    },
  }
};

@jhirn
Copy link
Author

jhirn commented Nov 18, 2021

I just wonder why this plugin uses temp files. I wrote a custom one, which is quite easy, handling file watching with no extra efforts. Just use postcss as the loader:

import { Plugin } from 'esbuild'

import postcss, { AcceptedPlugin } from 'postcss'

import fs from 'fs/promises'



export const pluginPostcss = (plugins?: AcceptedPlugin[]): Plugin => {

  return {

    name: 'plugin-postcss',

    setup(build) {

      build.onLoad({ filter: /\.css$/ }, async ({ path }) => {

        const processor = postcss(plugins);

        const content = await fs.readFile(path);

        const result = await processor.process(content, { from: path });

        return {

          contents: result.toString(),

          loader: 'css',

        }

      });

    },

  }

};

This is real nice @hanayashiki i will try and if it meets my needs, would you be interested publishing this as a new esbuild plug in. Seems the maintainer doesn't need or has moved on. Understandable but the esbuild needs better plugins in its ecosystem.

@hanayashiki
Copy link

I just wonder why this plugin uses temp files. I wrote a custom one, which is quite easy, handling file watching with no extra efforts. Just use postcss as the loader:

import { Plugin } from 'esbuild'

import postcss, { AcceptedPlugin } from 'postcss'

import fs from 'fs/promises'



export const pluginPostcss = (plugins?: AcceptedPlugin[]): Plugin => {

  return {

    name: 'plugin-postcss',

    setup(build) {

      build.onLoad({ filter: /\.css$/ }, async ({ path }) => {

        const processor = postcss(plugins);

        const content = await fs.readFile(path);

        const result = await processor.process(content, { from: path });

        return {

          contents: result.toString(),

          loader: 'css',

        }

      });

    },

  }

};

This is real nice @hanayashiki i will try and if it meets my needs, would you be interested publishing this as a new esbuild plug in. Seems the maintainer doesn't need or has moved on. Understandable but the esbuild needs better plugins in its ecosystem.

https://github.com/karolis-sh/esbuild-postcss/blob/main/src/index.ts

Hi, this library is using the same way as I do. Hopefully it works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants