@@ -14,12 +14,11 @@ import {
1414 normalizeSourceMap ,
1515 normalizeSourceMapAfterPostcss ,
1616 parsePackageJson ,
17- isFileExists ,
17+ findPackageJsonDir ,
1818} from "./utils" ;
1919
2020let hasExplicitDependencyOnPostCSS = false ;
21-
22- const PACKAGE_JSON_PATH = path . resolve ( process . cwd ( ) , "package.json" ) ;
21+ let packageJsonDir ;
2322
2423/**
2524 * **PostCSS Loader**
@@ -109,15 +108,22 @@ export default async function loader(content, sourceMap, meta) {
109108 processOptions
110109 ) ;
111110 } catch ( error ) {
111+ // The `findPackageJsonDir` function returns `string` or `null`.
112+ // This is used to do for caching, that is, an explicit comparison with `undefined`
113+ // is used to make the condition body run once.
114+ if ( packageJsonDir === undefined ) {
115+ packageJsonDir = findPackageJsonDir ( process . cwd ( ) , this . fs . statSync ) ;
116+ }
112117 // Check postcss versions to avoid using PostCSS 7.
113118 // For caching reasons, we use the readFileSync and existsSync functions from the context,
114119 // not the functions from the `fs` module.
115120 if (
116121 ! hasExplicitDependencyOnPostCSS &&
117122 postcssFactory ( ) . version . startsWith ( "7." ) &&
118- isFileExists ( PACKAGE_JSON_PATH , this . fs . statSync )
123+ packageJsonDir
119124 ) {
120- const pkg = parsePackageJson ( PACKAGE_JSON_PATH , this . fs . readFileSync ) ;
125+ const filePath = path . resolve ( packageJsonDir , "package.json" ) ;
126+ const pkg = parsePackageJson ( filePath , this . fs . readFileSync ) ;
121127 if ( ! pkg . dependencies . postcss && ! pkg . devDependencies . postcss ) {
122128 this . emitWarning (
123129 "Add postcss as project dependency. postcss is not a peer dependency for postcss-loader. " +
0 commit comments