postcss / postcss-load-config

Autoload Config for PostCSS
MIT License
638 stars 71 forks source link

feat: support loading ts config files in esm contexts #249

Closed brc-dd closed 8 months ago

brc-dd commented 11 months ago

Notable Changes

Commit Message Summary (CHANGELOG)

support loading ts config files in esm contexts and add support for .cts and .mts files

Type

SemVer

Issues

Checklist

ai commented 11 months ago

Can you fix test coverage?

brc-dd commented 11 months ago

Updated!

yunsii commented 9 months ago

What a great work, any progress here?

ai commented 9 months ago

@yunsii can you test this branch on big project? If it works, I can try to merge and release it.

yunsii commented 9 months ago

@yunsii can you test this branch on big project? If it works, I can try to merge and release it.

Sure, I can test it, but I use postcss config with simple config, only use plugins option for now. Test coverage is not good enouph to prove it?

ai commented 9 months ago

@yunsii

Sure, I can test it, but I use postcss config with simple config, only use plugins option for now. Test coverage is not good enouph to prove it?

Yes, we do not need more (we just need to be sure that config works in general, anyway different keys parsing is the same for all config types).

yunsii commented 9 months ago

Since it is so, I will try to test in this weekend.

neoReuters commented 9 months ago

So just to confirm,

a postcss.config.ts file with the following configuration:

import type { Config } from 'postcss-load-config';
import tailwindcss from 'tailwindcss';
import autoprefixer from 'autoprefixer';

export default {
  plugins: [
    tailwindcss(),    
    autoprefixer,
  ],
} satisfies Config;

Should work with this fix?

ai commented 9 months ago

@neoReuters as I understand, yes

brc-dd commented 8 months ago

I should also test this once with Deno. I don't think jiti supports that. With Node and Bun it should work fine, but Deno is different story. We can probably bypass the loader in Deno (or probably for everything where native import doesn't throw) ๐Ÿ‘€

ai commented 8 months ago

@brc-dd can you also rebase PR?

We can merge it when somebody will test it in big production project.

brc-dd commented 8 months ago

Updated the code to lazy load jiti -- use native import and only fallback to jiti if that fails. This enables better support for .postcssrc.ts files in Deno and Bun. But yeah, there can be some hidden issues (like with interop default). I'd suggest to publish a pre-release version like 5.0.0-beta.0, and ask people to use that version (preferably using something like npm/pnpm overrides so we can see how it plays with other tools).

ai commented 8 months ago

I'd suggest to publish a pre-release version like 5.0.0-beta.0, and ask people to use that version (preferably using something like npm/pnpm overrides so we can see how it plays with other tools).

We donโ€™t have enough active followers for that :(

kingyue737 commented 8 months ago

Test this PR in my template project https://github.com/kingyue737/vitify-admin/pull/25 and it works (CI failed because I install postcss-load-config from github:brc-dd/postcss-load-config, I don't know how to make it pass in github CI). But I need to rename postcss.config.ts to postcss.config.mts, even though type: module has been set in package.json.

brc-dd commented 8 months ago

Ah, with vite 5 it won't work properly. In vite, we are currently bundling this module with a custom patch.

ai commented 8 months ago

In vite, we are currently bundling this module with a custom patch.

What patches do you use? We can move them to the core?

ai commented 8 months ago

@brc-dd will we be able to use JSON .postcssrc but import there TS ESM plugins like:

{
  "plugins": {
    "./postcss/my-custom-plugin.ts": {}
  }
}
brc-dd commented 8 months ago

What patches do you use? We can move them to the core?

Ah, that's same as fa058dd0c856391fd8c9ab0b416eef75a4060807. But during vite 5 release, that wasn't published (mts / ts esm is still not supported). Also there are some patches for ts-node (need to tell rollup that it's not required at top and don't bundle that) -- those won't make sense in the core. That's why normal pnpm overrides for this module won't work for vite. Similar case for Next.js too, they also patch/bundle this module I guess.

brc-dd commented 8 months ago

will we be able to use JSON .postcssrc but import there TS ESM plugins like:

Not sure, I'll need to test that. I'm currently heading out of country, will fix that in 1-2 days ๐Ÿ‘

This will require changes in req.js

brc-dd commented 8 months ago
{
  "plugins": {
    "./postcss/my-custom-plugin.ts": {}
  }
}

This syntax currently (both with and before this PR) doesn't support anything other than common JS. Supporting ESM (even without TS) will need big refactor - will have to make everything async to use dynamic imports, etc. Do we need that? I don't have any issue with that, I can implement it too if you guys need.

ai commented 8 months ago

Supporting ESM (even without TS) will need big refactor - will have to make everything async to use dynamic imports, etc. Do we need that?

I need it in my project :D. But I can move to JS config if it too hard to add.

OK, letโ€™s finish the error handling and we are ready to merge and release.

ai commented 8 months ago

Why we have Draft state on PR? Do we need to add some changes?

brc-dd commented 8 months ago

Do we need to add some changes?

Yeah, had to move that ts extension test above importing jiti. Updated that now.

brc-dd commented 8 months ago

Supporting ESM (even without TS) will need big refactor - will have to make everything async to use dynamic imports, etc. Do we need that?

I need it in my project :D.

I'll try to create a PR separately. It won't be a breaking change as our exported function is async anyway.

brc-dd commented 8 months ago

@ai Any specific reason for making postcss required peer dep in 75364b4521af04fd1755ee83cc1971a567c9b935? Initially, it was discussed here I guess - https://github.com/vitejs/vite/pull/7495#issuecomment-1080800820

I'm fine with the change because mostly we already have postcss installed. But curious if there are people using this just to load config and pass it to some other tool and they don't have a direct dependency on postcss, this might gonna start giving warnings on things like yarn berry or pnpm with autoInstallPeers off / or on pnpm@7 or lower

GitHub search results - https://github.com/search?q=/%22postcss-load-config%22/++path:/package%5C.json/++NOT+/%22postcss%22/+NOT+is:fork+NOT+is:archived&type=code

ai commented 8 months ago

Any specific reason for making postcss required peer dep

I removed it because I didnโ€™t find a reason of loading config without having postcss.

If we will have real use case, I will revert change.

brc-dd commented 8 months ago

If we will have real use case

People doing something like this - https://github.com/didi/mand-mobile/blob/4611c9bf5dda15b244a0da768fde20b80cb997b2/build/rollup/rollup-plugin-example-config.js#L37-L56 (passing plugins/options to rollup-plugin-css)

ai commented 8 months ago

People doing something like this

Sure. Reverted.

ai commented 8 months ago

I'll try to create a PR separately. It won't be a breaking change as our exported function is async anyway.

Is it OK if I will wait this PR before the release?

I updated code style. The project is ready for forking again.

brc-dd commented 8 months ago

Is it OK if I will wait this PR before the release?

I'll try to create one by this week ๐Ÿ‘

ai commented 8 months ago

Released in 5.0.

privatenumber commented 6 months ago

Would yall be open to supporting tsx as a fallback loader too? Would be less dependencies in projects that use esbuild (e.g. Vite).

brc-dd commented 6 months ago

@privatenumber Does tsx provide a way to require specific files? Or register/unregister loaders in node 18?

privatenumber commented 6 months ago

I'll look into supporting both of those!

brc-dd commented 6 months ago

Also, the way it's currently implemented, it already supports tsx. Like if you run tsx file_that_loads_config.js or node --import tsx file_that_loads_config.js it will work fine even if you don't have jiti installed. Same with anything like tsimp or tsm. But to make it work for node.js without any loader, it'll need something that can require TS on demand.

privatenumber commented 6 months ago

I noticed, but

  1. I would prefer not to need to use tsx for running future versions of Vite only for the PostCSS config

  2. This is an interesting but valid use-case, and I'd like tsx to be a more versatile utility to handle something like this as well

brc-dd commented 6 months ago

Also, if you're supporting something like importUsingTsx then can you also add dependency tracking? It's not needed for postcss, but many of the projects in vite ecosystem are using hacky way of using vite's loadConfigFromFile for this. There vite is returning esbuild's metafile that has list of deps. For example, in vitepress we use that for loading config, path and data loader files, and need to reload if they or any of their dependent files are changed. In contrast, Nuxt is using jiti, but that doesn't support this and hence if you change any dependent file of config it doesn't trigger reload. You need to manually restart the process.

karlhorky commented 5 months ago

Thanks for the release postcss-load-config@5.0.0 with full ESM support in postcss.config.ts with "type": "module" @brc-dd and @ai ๐Ÿ™Œ

brc-dd commented 5 months ago

Thanks for the release postcss-load-config@5.0.0 with full ESM support in postcss.config.ts with "type": "module" brc-dd and ai ๐Ÿ™Œ

Hey man, we understand that you're happy about the added support. But please avoid pinging and posting the same message on each thread. We already have to go through a bunch of notifications on daily basis and these just add noise. Prefer using emoji reactions instead.

karlhorky commented 5 months ago

Understood, I'll try to reduce the pinging and notifications. Sorry.

For background, what I'm aiming to do is make it easier to easily find the exact version that support was added in when visiting this issue from a search engine eg. Google - because this relates to a bug that will probably affect people for some time.

I'll try to find a middle path that still does this but reduces the noise.