Skip to content

Commit 041864f

Browse files
authored
Fix path resolution for css files
1 parent af3e9a2 commit 041864f

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const postcss = require("postcss");
33
const util = require("util");
44
const tmp = require("tmp");
55
const path = require("path");
6+
const { sync: resolve } = require("resolve");
67

78
const readFile = util.promisify(fs.readFile);
89
const 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: /.\.(css)$/, 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

Comments
 (0)