Skip to content

Commit

Permalink
Only ignore caught require errors if the required module doesn't exist
Browse files Browse the repository at this point in the history
…fixes #1795
  • Loading branch information
KyleAMathews committed Aug 14, 2017
1 parent 7a3f692 commit a581d5e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 56 deletions.
82 changes: 41 additions & 41 deletions examples/using-contentful/src/pages/image-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ export default props => {
options
</p>
<h2>Resize</h2>
{assets.map(({ node: { title, resize } }) => (
<img
alt={title}
src={resize.src}
width={resize.width}
height={resize.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
))}
{assets.map(({ node: { title, resize } }) =>
<img
alt={title}
src={resize.src}
width={resize.width}
height={resize.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
)}
<h4>GraphQL query</h4>
<pre style={{ background: `#efeded`, padding: rhythm(3 / 4) }}>
<code
Expand Down Expand Up @@ -67,20 +67,20 @@ export default props => {
<p>
You should prefer this operator over <code>resize</code>.
</p>
{assets.map(({ node: { title, responsiveResolution } }) => (
<img
alt={title}
src={responsiveResolution.src}
srcSet={responsiveResolution.srcSet}
width={responsiveResolution.width}
height={responsiveResolution.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
))}
{assets.map(({ node: { title, responsiveResolution } }) =>
<img
alt={title}
src={responsiveResolution.src}
srcSet={responsiveResolution.srcSet}
width={responsiveResolution.width}
height={responsiveResolution.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
)}
<h4>GraphQL query</h4>
<pre style={{ background: `#efeded`, padding: rhythm(3 / 4) }}>
<code
Expand Down Expand Up @@ -122,20 +122,20 @@ export default props => {
resizing focus area
</a>
</p>
{assets.map(({ node: { title, resizing } }) => (
<img
alt={title}
src={resizing.src}
srcSet={resizing.srcSet}
width={resizing.width}
height={resizing.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
))}
{assets.map(({ node: { title, resizing } }) =>
<img
alt={title}
src={resizing.src}
srcSet={resizing.srcSet}
width={resizing.width}
height={resizing.height}
style={{
marginRight: rhythm(1 / 2),
marginBottom: rhythm(1 / 2),
border: `1px solid tomato`,
}}
/>
)}
<h4>GraphQL query</h4>
<pre style={{ background: `#efeded`, padding: rhythm(3 / 4) }}>
<code
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const packageJson = require(`./package.json`)
const path = require(`path`)
const _ = require(`lodash`)
const resolveCwd = require(`resolve-cwd`)
const testRequireError = require(`../utils/test-require-error`)

program.version(packageJson.version).usage(`[command] [options]`)

Expand All @@ -28,8 +29,13 @@ try {
) {
inGatsbySite = true
}
} catch (e) {
// ignore
} catch (err) {
if (testRequireError(`package.json`, err)) {
// ignore
} else {
report.error(`There is an error in your site's package.json`, err)
process.exit(1)
}
}

const defaultHost = `localhost`
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby/cache-dir/develop-static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import React from "react"
import { renderToStaticMarkup } from "react-dom/server"
import { merge } from "lodash"
import apiRunner from "./api-runner-ssr"
import pages from "./pages.json"
import ReactDOMServer from "react-dom/server"
import testRequireError from "./test-require-error"

let HTML
try {
HTML = require(`../src/html`)
} catch (e) {
HTML = require(`./default-html`)
} catch (err) {
if (testRequireError(`html`, err)) {
HTML = require(`./default-html`)
} else {
console.log(`There was an error requiring "src/html.js"\n\n`, err, `\n\n`)
process.exit()
}
}

module.exports = (locals, callback) => {
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { kebabCase, get, merge, isArray, isString } from "lodash"
import apiRunner from "./api-runner-ssr"
import pages from "./pages.json"
import syncRequires from "./sync-requires"
import testRequireError from "./test-require-error"

let Html
try {
Html = require(`../src/html`)
} catch (e) {
Html = require(`./default-html`)
} catch (err) {
if (testRequireError(`html`, err)) {
Html = require(`./default-html`)
} else {
console.log(`There was an error requiring "src/html.js"\n\n`, err, `\n\n`)
process.exit()
}
}

const pathChunkName = path => {
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby/src/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Promise = require(`bluebird`)
const resolveCwd = require(`resolve-cwd`)

const report = require(`../reporter`)
const testRequireError = require(`../utils/test-require-error`)

// Improve Promise error handling. Maybe... what's the best
// practice for this these days?
Expand Down Expand Up @@ -39,8 +40,13 @@ try {
if (localPackageJSON.dependencies && localPackageJSON.dependencies.gatsby) {
inGatsbySite = true
}
} catch (e) {
// ignore
} catch (err) {
if (testRequireError(`package.json`, err)) {
// ignore
} else {
report.error(`There is an error in your site's package.json`, err)
process.exit(1)
}
}

const directory = path.resolve(`.`)
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const md5File = require(`md5-file/promise`)
const crypto = require(`crypto`)

const apiRunnerNode = require(`../utils/api-runner-node`)
const testRequireError = require(`../utils/test-require-error`)
const { graphql } = require(`graphql`)
const { store, emitter } = require(`../redux`)
const loadPlugins = require(`./load-plugins`)
Expand Down Expand Up @@ -46,8 +47,7 @@ module.exports = async (program: any) => {
// $FlowFixMe
config = preferDefault(require(`${program.directory}/gatsby-config`))
} catch (err) {
const firstLine = err.toString().split(`\n`)[0]
if (!/Error: Cannot find module.*gatsby-config/.test(firstLine)) {
if (!testRequireError(`gatsby-config`, err)) {
report.error(`Could not load gatsby-config`, err)
process.exit(1)
}
Expand Down Expand Up @@ -131,8 +131,12 @@ module.exports = async (program: any) => {
activity.start()
const srcDir = `${__dirname}/../../cache-dir`
const siteDir = `${program.directory}/.cache`
const tryRequire = `${__dirname}/../utils/test-require-error.js`
try {
await fs.copy(srcDir, siteDir, { clobber: true })
await fs.copy(tryRequire, `${siteDir}/test-require-error.js`, {
clobber: true,
})
await fs.ensureDirSync(`${program.directory}/.cache/json`)
await fs.ensureDirSync(`${program.directory}/.cache/layouts`)
} catch (err) {
Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby/src/bootstrap/load-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ const slash = require(`slash`)
const fs = require(`fs`)
const path = require(`path`)
const crypto = require(`crypto`)
const glob = require(`glob`)

const { store } = require(`../redux`)
const nodeAPIs = require(`../utils/api-node-docs`)
const glob = require(`glob`)
const testRequireError = require(`../utils/test-require-error`)
const report = require(`../reporter`)

function createFileContentHash(root, globPattern) {
const hash = crypto.createHash(`md5`)
Expand Down Expand Up @@ -194,8 +197,12 @@ module.exports = async (config = {}) => {
let gatsbyNode
try {
gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
} catch (e) {
// ignore
} catch (err) {
if (testRequireError(`gatsby-node`, err)) {
// ignore
} else {
report.panic(`Error requiring ${plugin.resolve}/gatsby-node.js`, err)
}
}

if (gatsbyNode) {
Expand Down

1 comment on commit a581d5e

@arcanis
Copy link
Contributor

@arcanis arcanis commented on a581d5e Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KyleAMathews Do you remember why you didn't choose to just check for err.code === 'MODULE_NOT_FOUND'? Was there a particular case where this isn't true?

Please sign in to comment.