@@ -3,6 +3,7 @@ const postcss = require("postcss");
33const util = require ( "util" ) ;
44const tmp = require ( "tmp" ) ;
55const path = require ( "path" ) ;
6+ const { sync : resolve } = require ( "resolve" ) ;
67
78const readFile = util . promisify ( fs . readFile ) ;
89const writeFile = util . promisify ( fs . writeFile ) ;
@@ -13,10 +14,19 @@ module.exports = (options = { plugins: [] }) => ({
1314 setup : function ( build ) {
1415 const { rootDir = options . rootDir || process . cwd ( ) } = options ;
1516 const tmpDirPath = tmp . dirSync ( ) . name ;
17+
1618 build . onResolve (
1719 { filter : / .\. ( c s s ) $ / , namespace : "file" } ,
1820 async ( args ) => {
19- const sourceFullPath = path . resolve ( args . resolveDir , args . path ) ;
21+ let sourceFullPath ;
22+
23+ // Use Node's module resolution algorithm for modules from node_modules
24+ if ( args . path . startsWith ( '.' ) || path . isAbsolute ( args . path ) ) {
25+ sourceFullPath = path . resolve ( args . resolveDir , args . path ) ;
26+ } else {
27+ sourceFullPath = resolve ( args . path , { basedir : args . resolveDir } ) ;
28+ }
29+
2030 const sourceExt = path . extname ( sourceFullPath ) ;
2131 const sourceBaseName = path . basename ( sourceFullPath , sourceExt ) ;
2232 const sourceDir = path . dirname ( sourceFullPath ) ;
@@ -27,13 +37,12 @@ module.exports = (options = { plugins: [] }) => ({
2737 await ensureDir ( tmpDir ) ;
2838
2939 const css = await readFile ( sourceFullPath ) ;
30-
3140 const result = await postcss ( options . plugins ) . process ( css , {
3241 from : sourceFullPath ,
3342 to : tmpFilePath ,
3443 } ) ;
3544
36- // Write result file
45+ // Write the result file
3746 await writeFile ( tmpFilePath , result . css ) ;
3847
3948 return {
0 commit comments