Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit 6a3b5da

Browse files
authored
Use require.resolve() to check if Prism plugin files exist (#236)
The old approach implied that the filesystem layout of the `node_modules` folder is flat, but this does not necessarily have to be the case. For example, when using pnpm as a package manager the `prism` package will be resolvable from `ember-prism`, but it will not be at the location that the old approach expects. This commit should fix the problem by relying on the node module resolution algorithm to determine if certain files of the Prism package exist or not.
1 parent 5c3798e commit 6a3b5da

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
var fs = require('fs');
4-
53
module.exports = {
64
name: require('./package').name,
75
included() {
@@ -49,10 +47,8 @@ module.exports = {
4947

5048
fileExtensions.forEach((fileExtension) => {
5149
const nodeAssetsPath = `plugins/${plugin}/prism-${plugin}.${fileExtension}`;
52-
const file = `node_modules/prismjs/${nodeAssetsPath}`;
53-
5450

55-
if (fs.existsSync(file)) {
51+
if (maybeResolve(`prismjs/${nodeAssetsPath}`)) {
5652
this.plugins.push(nodeAssetsPath);
5753
}
5854
});
@@ -79,6 +75,17 @@ module.exports = {
7975
}
8076
};
8177

78+
function maybeResolve(path) {
79+
try {
80+
return require.resolve(path);
81+
} catch (error) {
82+
if (error.code === 'MODULE_NOT_FOUND') {
83+
return null;
84+
} else {
85+
throw error;
86+
}
87+
}
88+
}
8289

8390
// Polyfill [Addon._findHost](https://ember-cli.com/api/classes/Addon.html#method__findHost) for older versions of ember-cli
8491
function findHost(addon) {

0 commit comments

Comments
 (0)