Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequireFallback function for requiring userland code #1800

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions packages/gatsby/cache-dir/develop-static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import { merge } from "lodash"
import apiRunner from "./api-runner-ssr"
import pages from "./pages.json"
import ReactDOMServer from "react-dom/server"
import requireWithFallback from "./require-with-fallback"

let HTML
try {
HTML = require(`../src/html`)
} catch (e) {
HTML = require(`./default-html`)
}
const HTML = requireWithFallback(`../src/html`, `./default-html`)

module.exports = (locals, callback) => {
// const apiRunner = require(`${directory}/.cache/api-runner-ssr`)
Expand Down
13 changes: 13 additions & 0 deletions packages/gatsby/cache-dir/require-with-fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function requireWithFallback (mainPath, fallbackPath) {
let module
try {
module = require(mainPath)
} catch (e) {
if (e.code === `MODULE_NOT_FOUND`) {
module = require(fallbackPath)
} else {
throw new Error(`Failed to parse ${mainPath} file.`, e)
}
}
return module
}
8 changes: 2 additions & 6 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ 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 requireWithFallback from './require-with-fallback'

let Html
try {
Html = require(`../src/html`)
} catch (e) {
Html = require(`./default-html`)
}
const Html = requireWithFallback(`../src/html`, `./default-html`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you can abstract this actually. Webpack statically analyzes requires, if the require path is completely dynamic the chances of stuff going wrong are high. like it including ALL files in the root in the bundle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't know about these webpack features. So, seems like the only way here is to add e.code !== MODULE_NOT_FOUND checking for each try/catch block?

Copy link
Contributor

Choose a reason for hiding this comment

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

for any require in cache-dir yes, you can rescope the helper to contain the check and throw if you want though. but you need to do the require with string literals


const pathChunkName = path => {
const name = path === `/` ? `index` : kebabCase(path)
Expand Down