Skip to content

Commit 57da45d

Browse files
authored
Merge pull request #16 from FrancoRivera/main
Fix path resolution for css files
2 parents af3e9a2 + f0219f5 commit 57da45d

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

index.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,36 @@ module.exports = (options = { plugins: [] }) => ({
1313
setup: function (build) {
1414
const { rootDir = options.rootDir || process.cwd() } = options;
1515
const tmpDirPath = tmp.dirSync().name;
16+
1617
build.onResolve(
1718
{ filter: /.\.(css)$/, namespace: "file" },
1819
async (args) => {
19-
const sourceFullPath = path.resolve(args.resolveDir, args.path);
20+
let sourceFullPath;
21+
22+
// Manual attempt at resolving from node_modules and other typical directories
23+
if (args.path.startsWith('.') || path.isAbsolute(args.path)) {
24+
sourceFullPath = path.resolve(args.resolveDir, args.path);
25+
} else {
26+
const modulePaths = [
27+
// possible locations for node modules, maybe this is not strictly necessary
28+
path.resolve(args.resolveDir, 'node_modules', args.path),
29+
path.resolve(rootDir, 'node_modules', args.path),
30+
path.resolve(rootDir, '../node_modules', args.path)
31+
];
32+
33+
for (const modulePath of modulePaths) {
34+
if (fs.existsSync(modulePath)) {
35+
// if we find the path we need, use it as the sourceFullPath
36+
sourceFullPath = modulePath;
37+
break;
38+
}
39+
}
40+
41+
if (!sourceFullPath) {
42+
throw new Error(`Cannot resolve module: ${args.path}`);
43+
}
44+
}
45+
2046
const sourceExt = path.extname(sourceFullPath);
2147
const sourceBaseName = path.basename(sourceFullPath, sourceExt);
2248
const sourceDir = path.dirname(sourceFullPath);
@@ -27,13 +53,12 @@ module.exports = (options = { plugins: [] }) => ({
2753
await ensureDir(tmpDir);
2854

2955
const css = await readFile(sourceFullPath);
30-
3156
const result = await postcss(options.plugins).process(css, {
3257
from: sourceFullPath,
3358
to: tmpFilePath,
3459
});
3560

36-
// Write result file
61+
// Write the result file
3762
await writeFile(tmpFilePath, result.css);
3863

3964
return {

0 commit comments

Comments
 (0)