From 89f0047c0dfe487da6c0329e00ee52c10ccbfcb4 Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Thu, 26 Sep 2024 12:32:46 +0200 Subject: [PATCH] CLI: Use the right `base` when loading files from `stdin` (#14522) Fixes #14521 When using the CLI to read files from `stdin` like this: ```bash npx tailwindcss --input=- -o bar.css < foo.css ``` We need to set the `base` path to be the current working directory (`process.cwd()`). However, `cwd()` already _is_ a directory and calling `dirname()` on it did go to the parent directory _which might not have the `tailwindcss` dependency installed. --- CHANGELOG.md | 1 + integrations/cli/index.test.ts | 25 +++++++++++++++++++ .../src/commands/build/index.ts | 6 +++-- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e288aa2b6609..472341631f71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Use the right import base path when using the CLI to reading files from stdin ([#14522](https://github.com/tailwindlabs/tailwindcss/pull/14522)) - _Experimental_: Improve codemod output, keep CSS after last Tailwind directive unlayered ([#14512](https://github.com/tailwindlabs/tailwindcss/pull/14512)) - _Experimental_: Fix incorrect empty `layer()` at the end of `@import` at-rules when running codemods ([#14513](https://github.com/tailwindlabs/tailwindcss/pull/14513)) - _Experimental_: Migrate `@import "tailwindcss/tailwind.css"` to `@import "tailwindcss"` ([#14514](https://github.com/tailwindlabs/tailwindcss/pull/14514)) diff --git a/integrations/cli/index.test.ts b/integrations/cli/index.test.ts index 50842aea4ace..f1c4288d1912 100644 --- a/integrations/cli/index.test.ts +++ b/integrations/cli/index.test.ts @@ -182,4 +182,29 @@ describe.each([ ]) }, ) + + test( + 'production build (stdin)', + { + fs: { + 'package.json': json` + { + "dependencies": { + "tailwindcss": "workspace:^", + "@tailwindcss/cli": "workspace:^" + } + } + `, + 'index.html': html` +
+ `, + 'src/index.css': css`@import 'tailwindcss';`, + }, + }, + async ({ fs, exec }) => { + await exec(`${command} --input=- --output dist/out.css < src/index.css`) + + await fs.expectFileToContain('dist/out.css', [candidate`underline`]) + }, + ) }) diff --git a/packages/@tailwindcss-cli/src/commands/build/index.ts b/packages/@tailwindcss-cli/src/commands/build/index.ts index daf12294cf74..cf2fb90605f8 100644 --- a/packages/@tailwindcss-cli/src/commands/build/index.ts +++ b/packages/@tailwindcss-cli/src/commands/build/index.ts @@ -118,8 +118,10 @@ export async function handle(args: Result>) { } } - let inputFile = args['--input'] && args['--input'] !== '-' ? args['--input'] : process.cwd() - let inputBasePath = path.dirname(path.resolve(inputFile)) + let inputBasePath = + args['--input'] && args['--input'] !== '-' + ? path.dirname(path.resolve(args['--input'])) + : process.cwd() let fullRebuildPaths: string[] = [] function createCompiler(css: string) {