diff --git a/packages/postcss-config/README.md b/packages/postcss-config/README.md index a25de09..484747a 100644 --- a/packages/postcss-config/README.md +++ b/packages/postcss-config/README.md @@ -19,8 +19,11 @@ const postcssSettings = require('@yardinternet/postcss-config'); module.exports = postcssSettings(getPathToTheme(), __dirname); ``` -The default path to the styles is `web/app/themes/sage/resources/styles`. -You can change this values by passing the paths as an parameter. +The `@sage` import alias points at the default theme's +`resources/styles`, resolved from the [project +layout](../shared-utils/README.md#project-layout-detection). It falls back to +`web/app/themes/sage/resources/styles` when the layout cannot be resolved. +You can change this value by passing the path as a parameter. ```js const postcssSettings = require('@yardinternet/postcss-config'); diff --git a/packages/postcss-config/package.json b/packages/postcss-config/package.json index f8cacb9..7a1a05e 100644 --- a/packages/postcss-config/package.json +++ b/packages/postcss-config/package.json @@ -18,6 +18,7 @@ "author": "", "license": "ISC", "dependencies": { + "@yardinternet/shared-utils": "workspace:*", "autoprefixer": "^10.6.1", "cssnano": "^8.0.10", "postcss": "^8.5.28", diff --git a/packages/postcss-config/src/index.js b/packages/postcss-config/src/index.js index bcf94ec..2650ebc 100644 --- a/packages/postcss-config/src/index.js +++ b/packages/postcss-config/src/index.js @@ -1,10 +1,27 @@ const path = require( 'path' ); +const { tryResolveThemeContext } = require( '@yardinternet/shared-utils' ); + +/** + * Styles of the parent theme the `@sage` import alias points at. Falls back to + * the historical sage path when the project layout cannot be resolved. + */ +const defaultStylesPath = ( basePath ) => { + const context = tryResolveThemeContext( { cwd: basePath } ); + + if ( ! context ) { + return 'web/app/themes/sage/resources/styles'; + } + + return path.join( + context.themeRelDir( context.defaultTheme ), + 'resources', + 'styles' + ); +}; + +module.exports = ( pathToTheme, basePath, pathToStyles = null ) => { + const parentStylesPath = pathToStyles ?? defaultStylesPath( basePath ); -module.exports = ( - pathToTheme, - basePath, - pathToStyles = 'web/app/themes/sage/resources/styles' -) => { // Add alias paths for PostCSS imports const resolveAliasPaths = ( id ) => { if ( id.startsWith( '@sage' ) ) { @@ -12,7 +29,7 @@ module.exports = ( const sageParentThemeStylesPath = path.join( basePath, - pathToStyles + parentStylesPath ); return path.join( sageParentThemeStylesPath, relativePath ); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index af99b9f..58e0194 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ importers: packages/postcss-config: dependencies: + '@yardinternet/shared-utils': + specifier: workspace:* + version: link:../shared-utils autoprefixer: specifier: ^10.6.1 version: 10.6.1(postcss@8.5.28)