Export public sorting APIs to /sorter#438
Conversation
2d9e283 to
48a4a97
Compare
48a4a97 to
996fa68
Compare
…f all paths are absolute path
7e601f9 to
82e061f
Compare
/lib/sorter
|
|
||
| /** | ||
| * Get a Tailwind CSS API instance based on the provided options. | ||
| * @internal |
There was a problem hiding this comment.
May I ask why this isn't being exported as a public API? @Dunqing
I think this is very helpful for those who want to implement other tailwind integrations.
There was a problem hiding this comment.
There is no usage in the Oxfmt. We can use createSorter directly, which has already called getTailwindConfig inside.
What is your reason for using getTailwindConfig rather than createSorter?
There was a problem hiding this comment.
To my knowledge, the design system also offers several additional methods.
Maybe we can create an access layer compatible with v3 and v4?
There was a problem hiding this comment.
I think this is out of scope for this plugin. This is not a library for those who want to build something on top of Tailwind CSS.
Speaking of which, I personally think some of the resolving logic should move into tailwindcss and export a public method from it; that would be very useful.
### Resolve issues: * close: #18072 The new version of `prettier-plugin-tailwindcss` solved all root causes mentioned in the description of the above issue, see the commit history at https://github.com/tailwindlabs/prettier-plugin-tailwindcss/commits/main/ * close: #17972 * close: #18459 * close: #18628 These three issues are related to resolving the TailwindCSS configuration of that plugin, the new version decoupled with Prettier, and provides a public `createSorter` sorting API to `/sorter`, so that we can configure `base` correctly, which is the key for the plugin to resolve `stylesheet` and `config` from `base`. ### Key change on Oxfmt side In this PR, the `stylesheets` and `config` options are normalized to absolute paths using the Oxfmt configuration directory before calling `createSorter` and delegating to `Prettier`. The reason is as follows: For formatting non-JS/TS files, Oxfmt delegates to Prettier. Therefore, we should continue using `prettier-plugin-tailwindcss` to handle this, ensuring that Prettier's configuration resolution remains intact. I made a change in [that plugin](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/blob/125a8bc77639529a5a0c7e4e8a02174d7ed2d70b/src/config.ts#L50-L54) to prevent resolution when both `stylesheet` and `config` are absolute paths. This change is valid and could enhance the plugin's performance for both Oxfmt and Prettier users. The plugin needs to resolve the Prettier configuration to obtain the directory of the configuration file and join it with `stylesheet` and `config`. If they are already absolute, there is no need to join them. ### Benchmark > Copy from tailwindlabs/prettier-plugin-tailwindcss#438 Note: `new` means the current branch, `old` means the latest package in npm, so it includes a few optimizations that have already been merged into the main branch, not all in this branch. Benchmark repo: https://github.com/dubinc/dub ```shell Benchmark 3: oxfmt-new Time (mean ± σ): 999.8 ms ± 32.1 ms [User: 8968.9 ms, System: 2246.7 ms] Range (min … max): 977.2 ms … 1022.5 ms 2 runs Benchmark 4: oxfmt-old Time (mean ± σ): 1.628 s ± 0.027 s [User: 13.239 s, System: 4.980 s] Range (min … max): 1.609 s … 1.647 s 2 runs Summary oxfmt-new ran 1.63 ± 0.06 times faster than oxfmt-old ``` Benchmark repo: https://github.com/tailwindcsslab/headlessui ```shell Benchmark 3: oxfmt-new Time (mean ± σ): 778.8 ms ± 28.9 ms [User: 7633.6 ms, System: 912.9 ms] Range (min … max): 738.3 ms … 806.9 ms 4 runs Benchmark 4: oxfmt-old Time (mean ± σ): 1.033 s ± 0.034 s [User: 10.048 s, System: 1.454 s] Range (min … max): 0.994 s … 1.073 s 4 runs Summary oxfmt-new ran 1.33 ± 0.07 times faster than oxfmt-old ```
Continue work on https://github.com/tailwindlabs/prettier-plugin-tailwindcss/tree/feat/public-api
close: #430
Some differences from #430
/sorter, which is a more descriptive entry pointformatterConfigPath, I found this is useless, it is only used to log a warning, I think logbaseis okay.Extra Optimization
843a4dc made an optimization to avoid unnecessary resolution of the Prettier configuration, which is quite expensive!
I think the following
dubbenchmark is at least half as fast due to this optimization because this repo has no Tailwind configuration set.Benchmark
Note:
newmeans the current branch,oldmeans the latest package in npm, so it includes a few optimizations that have already been merged into the main branch, not all in this branch.Benchmark repo: https://github.com/dubinc/dub
Benchmark 1: prettier-tailwindcss-new Time (mean ± σ): 8.038 s ± 0.216 s [User: 12.546 s, System: 0.836 s] Range (min … max): 7.886 s … 8.191 s 2 runs Benchmark 2: prettier-tailwindcss-old Time (mean ± σ): 11.923 s ± 0.060 s [User: 14.954 s, System: 2.520 s] Range (min … max): 11.881 s … 11.966 s 2 runs Benchmark 3: oxfmt-new Time (mean ± σ): 999.8 ms ± 32.1 ms [User: 8968.9 ms, System: 2246.7 ms] Range (min … max): 977.2 ms … 1022.5 ms 2 runs Benchmark 4: oxfmt-old Time (mean ± σ): 1.628 s ± 0.027 s [User: 13.239 s, System: 4.980 s] Range (min … max): 1.609 s … 1.647 s 2 runs Summary oxfmt-new ran 1.63 ± 0.06 times faster than oxfmt-old 8.04 ± 0.34 times faster than prettier-tailwindcss-new 11.93 ± 0.39 times faster than prettier-tailwindcss-oldBenchmark repo: https://github.com/tailwindcsslab/headlessui
Benchmark 1: prettier-tailwindcss-new Time (mean ± σ): 3.588 s ± 0.056 s [User: 6.432 s, System: 0.361 s] Range (min … max): 3.551 s … 3.670 s 4 runs Benchmark 2: prettier-tailwindcss-old Time (mean ± σ): 4.134 s ± 0.108 s [User: 6.919 s, System: 0.611 s] Range (min … max): 4.030 s … 4.246 s 4 runs Benchmark 3: oxfmt-new Time (mean ± σ): 778.8 ms ± 28.9 ms [User: 7633.6 ms, System: 912.9 ms] Range (min … max): 738.3 ms … 806.9 ms 4 runs Benchmark 4: oxfmt-old Time (mean ± σ): 1.033 s ± 0.034 s [User: 10.048 s, System: 1.454 s] Range (min … max): 0.994 s … 1.073 s 4 runs Summary oxfmt-new ran 1.33 ± 0.07 times faster than oxfmt-old 5.61 ± 0.19 times faster than prettier-tailwindcss-new 6.31 ± 0.24 times faster than prettier-tailwindcss-old