Skip to content

Commit

Permalink
Fix extension resolution with globally installed gatsby (#102)
Browse files Browse the repository at this point in the history
* Try multiple contexts when resolving extensions

* Fix GraphQL resolver bug

* Fix SSR build of exmample site

* Prettier

* Prettier with the updated version
  • Loading branch information
andrewbranch authored Jun 7, 2020
1 parent 3faceac commit 5328de6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 0 additions & 1 deletion examples/example-site/src/templates/react-blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class BlogPostTemplate extends React.Component {
}).Compiler;

render() {
window.props = this.props
const post = this.props.data.markdownRemark
const siteTitle = this.props.data.site.siteMetadata.title
const { previous, next } = this.props.pageContext
Expand Down
2 changes: 1 addition & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async function getFromCache(type, cache, source, context, stop) {
type: 'MarkdownRemark',
firstOnly: true,
});
return getFromCache(cache, source, context, true);
return getFromCache(type, cache, source, context, true);
}
if (!childNodes) {
logger.error(
Expand Down
31 changes: 30 additions & 1 deletion src/processExtension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @ts-check
const path = require('path');
const process = require('process');
const logger = require('loglevel');
const {
getLanguageNames,
Expand All @@ -13,6 +14,7 @@ const {
const { getHighestBuiltinLanguageId } = require('./storeUtils');
const unzipDir = path.resolve(__dirname, '../lib/extensions');
const requireMain = createRequire(require.main.filename);
const requireCwd = createRequire(path.join(process.cwd(), 'index.js'));

/**
* @param {string} packageJsonPath
Expand Down Expand Up @@ -105,7 +107,7 @@ async function mergeCache(cache, key, value) {
* @param {Host} host
*/
async function getExtensionPackageJsonPath(specifier, host) {
const absolute = path.isAbsolute(specifier) ? specifier : requireMain.resolve(path.join(specifier, 'package.json'));
const absolute = path.isAbsolute(specifier) ? specifier : requireResolveExtension(specifier);
const ext = path.extname(absolute);
if (ext.toLowerCase() === '.vsix' || ext.toLowerCase() === '.zip') {
const outDir = path.join(unzipDir, path.basename(absolute, ext));
Expand Down Expand Up @@ -137,6 +139,33 @@ async function getExtensionPackageJsonPath(specifier, host) {
}
}

/**
* We want to resolve the extension from the context of the user’s gatsby-config,
* but this turns out to be difficult. Ideally, gatsby and this plugin are installed
* in the same node_modules directory as the extension, but gatsby could be invoked
* globally, and this plugin could be npm linked. If both of those things happen, we
* can also try resolving from the current working directory. One of these will
* probably always work.
* @param {string} specifier
*/
function requireResolveExtension(specifier) {
return (
tryResolve(require) ||
tryResolve(requireMain) ||
tryResolve(requireCwd) ||
require.resolve(path.join(specifier, 'package.json'))
); // If none work, throw the best error stack

/** @param {NodeRequire} req */
function tryResolve(req) {
try {
return req.resolve(path.join(specifier, 'package.json'));
} catch (_) {
return undefined;
}
}
}

/**
* @param {string[]} extensions
* @param {Host} host
Expand Down

0 comments on commit 5328de6

Please sign in to comment.