Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 3 additions & 11 deletions packages/prettier-config/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
const fs = require( 'fs' );
const path = require( 'path' );
const findTailwindStylesheet = require( './utils/find-tailwind-stylesheet' );

const tailwindPath = path.resolve(
process.cwd(),
'web/app/themes/sage/resources/styles/base/config.css'
);

const hasTailwindConfig = fs.existsSync( tailwindPath );
const tailwindStylesheet = findTailwindStylesheet();

module.exports = {
...require( '@wordpress/prettier-config' ),
plugins: [
require.resolve( '@shufo/prettier-plugin-blade' ),
require.resolve( 'prettier-plugin-tailwindcss' ),
],
...( hasTailwindConfig && {
tailwindStylesheet: tailwindPath,
} ),
...( tailwindStylesheet && { tailwindStylesheet } ),
overrides: [
{
files: [ '*.css', '*.js', '*.jsx', '*.ts', '*.tsx' ],
Expand Down
31 changes: 31 additions & 0 deletions packages/prettier-config/src/utils/find-tailwind-stylesheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require( 'fs' );
const path = require( 'path' );

function findTailwindStylesheet() {
const relative = 'web/app/themes/sage/resources/styles/base/config.css';

const roots = [];

// 1. PWD / VSCODE_CWD env vars — set to the project root by the shell and
// VSCode, and unlike process.cwd() they survive being reset to '/'.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Werkt trouwens ook voor PHPStorm users

if ( process.env.VSCODE_CWD ) roots.push( process.env.VSCODE_CWD );
if ( process.env.PWD ) roots.push( process.env.PWD );

// 2. Installed normally (no symlink): strip at the first node_modules boundary.
const nmIndex = __dirname.indexOf( 'node_modules' );
if ( nmIndex > 0 ) {
roots.push( __dirname.substring( 0, nmIndex - 1 ) );
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __dirname.substring( 0, nmIndex - 1 ) calculation can drop the root path separator on Windows when the project lives at the drive root (e.g. C:\node_modules\...), resulting in C: (not C:\) and causing path.resolve() to base the path on the current drive CWD instead of the drive root. Use __dirname.slice( 0, nmIndex ) (optionally followed by a trim of trailing separators that preserves C:\) to reliably get the directory before node_modules across platforms.

Suggested change
roots.push( __dirname.substring( 0, nmIndex - 1 ) );
roots.push( __dirname.slice( 0, nmIndex ) );

Copilot uses AI. Check for mistakes.
}

// 3. process.cwd() — correct for standard CLI runs.
roots.push( process.cwd() );

for ( const root of roots ) {
const candidate = path.resolve( root, relative );
if ( fs.existsSync( candidate ) ) return candidate;
}

return null;
}

module.exports = findTailwindStylesheet;