Skip to content

Commit 647926a

Browse files
author
Michael Vurchio
committed
Check if the file exist in node_modules before throwing an error
1 parent cecb4f8 commit 647926a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/processors/parseMarkup.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path';
2-
import fs from 'fs';
2+
import fs, { constants } from 'fs';
33
import matchAll from 'string.prototype.matchall';
44
import fromEntries from 'object.fromentries';
55
import { PluginOptions, CSSModuleList } from '../types';
@@ -84,8 +84,9 @@ const parseMarkup = async (
8484

8585
parsedContent = parsedContent.replace(
8686
PATTERN_IMPORT,
87-
(_match, varName, relativePath, extension) => {
87+
(match, varName, relativePath, extension) => {
8888
const absolutePath = path.resolve(path.dirname(filename), relativePath);
89+
const nodeModulesPath = path.resolve(`${path.resolve()}/node_modules`, relativePath);
8990
try {
9091
const fileContent = fs.readFileSync(absolutePath, 'utf8');
9192
importedStyleContent.push(fileContent);
@@ -143,7 +144,12 @@ const parseMarkup = async (
143144

144145
return `const ${varName} = ${JSON.stringify(fromEntries(classlist))};`;
145146
} catch (err) {
146-
throw new Error(err);
147+
fs.access(nodeModulesPath, constants.F_OK, (error) => {
148+
if (error) {
149+
throw new Error(err); // not found in node_modules packages either, throw orignal error
150+
}
151+
});
152+
return match;
147153
}
148154
}
149155
);

0 commit comments

Comments
 (0)