Skip to content

Commit 5328de6

Browse files
authored
Fix extension resolution with globally installed gatsby (#102)
* Try multiple contexts when resolving extensions * Fix GraphQL resolver bug * Fix SSR build of exmample site * Prettier * Prettier with the updated version
1 parent 3faceac commit 5328de6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

examples/example-site/src/templates/react-blog-post.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class BlogPostTemplate extends React.Component {
7474
}).Compiler;
7575

7676
render() {
77-
window.props = this.props
7877
const post = this.props.data.markdownRemark
7978
const siteTitle = this.props.data.site.siteMetadata.title
8079
const { previous, next } = this.props.pageContext

gatsby-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function getFromCache(type, cache, source, context, stop) {
7777
type: 'MarkdownRemark',
7878
firstOnly: true,
7979
});
80-
return getFromCache(cache, source, context, true);
80+
return getFromCache(type, cache, source, context, true);
8181
}
8282
if (!childNodes) {
8383
logger.error(

src/processExtension.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-check
22
const path = require('path');
3+
const process = require('process');
34
const logger = require('loglevel');
45
const {
56
getLanguageNames,
@@ -13,6 +14,7 @@ const {
1314
const { getHighestBuiltinLanguageId } = require('./storeUtils');
1415
const unzipDir = path.resolve(__dirname, '../lib/extensions');
1516
const requireMain = createRequire(require.main.filename);
17+
const requireCwd = createRequire(path.join(process.cwd(), 'index.js'));
1618

1719
/**
1820
* @param {string} packageJsonPath
@@ -105,7 +107,7 @@ async function mergeCache(cache, key, value) {
105107
* @param {Host} host
106108
*/
107109
async function getExtensionPackageJsonPath(specifier, host) {
108-
const absolute = path.isAbsolute(specifier) ? specifier : requireMain.resolve(path.join(specifier, 'package.json'));
110+
const absolute = path.isAbsolute(specifier) ? specifier : requireResolveExtension(specifier);
109111
const ext = path.extname(absolute);
110112
if (ext.toLowerCase() === '.vsix' || ext.toLowerCase() === '.zip') {
111113
const outDir = path.join(unzipDir, path.basename(absolute, ext));
@@ -137,6 +139,33 @@ async function getExtensionPackageJsonPath(specifier, host) {
137139
}
138140
}
139141

142+
/**
143+
* We want to resolve the extension from the context of the user’s gatsby-config,
144+
* but this turns out to be difficult. Ideally, gatsby and this plugin are installed
145+
* in the same node_modules directory as the extension, but gatsby could be invoked
146+
* globally, and this plugin could be npm linked. If both of those things happen, we
147+
* can also try resolving from the current working directory. One of these will
148+
* probably always work.
149+
* @param {string} specifier
150+
*/
151+
function requireResolveExtension(specifier) {
152+
return (
153+
tryResolve(require) ||
154+
tryResolve(requireMain) ||
155+
tryResolve(requireCwd) ||
156+
require.resolve(path.join(specifier, 'package.json'))
157+
); // If none work, throw the best error stack
158+
159+
/** @param {NodeRequire} req */
160+
function tryResolve(req) {
161+
try {
162+
return req.resolve(path.join(specifier, 'package.json'));
163+
} catch (_) {
164+
return undefined;
165+
}
166+
}
167+
}
168+
140169
/**
141170
* @param {string[]} extensions
142171
* @param {Host} host

0 commit comments

Comments
 (0)