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

chore(gatsby, gatsby-transformer-remark, gatsby-plugin-sharp): Cleanup v4 compile flag conditions #36486

Merged
merged 6 commits into from Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 4 additions & 27 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Expand Up @@ -11,20 +11,6 @@ const { slash } = require(`gatsby-core-utils/path`)
const { setPluginOptions } = require(`./plugin-options`)
const path = require(`path`)

let coreSupportsOnPluginInit
try {
const { isGatsbyNodeLifecycleSupported } = require(`gatsby-plugin-utils`)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(`onPluginInit`)
} else {
coreSupportsOnPluginInit = isGatsbyNodeLifecycleSupported(
`unstable_onPluginInit`
)
}
} catch (e) {
coreSupportsOnPluginInit = false
}

function removeCachedValue(cache, key) {
if (cache?.del) {
// if cache expose ".del" method directly on public interface
Expand Down Expand Up @@ -129,19 +115,10 @@ exports.onPostBootstrap = async ({ reporter, cache, store }) => {
}
}

if (coreSupportsOnPluginInit) {
// to properly initialize plugin in worker (`onPreBootstrap` won't run in workers)
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
exports.onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
}
} else {
exports.unstable_onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
}
}
// to properly initialize plugin in worker (`onPreBootstrap` won't run in workers)
exports.onPluginInit = async ({ actions }, pluginOptions) => {
setActions(actions)
setPluginOptions(pluginOptions)
}
Copy link
Contributor

@wardpeet wardpeet Aug 29, 2022

Choose a reason for hiding this comment

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

This can be used instead of the whole try catch

exports.onPluginInit = async ({ actions }, pluginOptions) => {
    setActions(actions)
    setPluginOptions(pluginOptions)
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean removing the if (coreSupportsOnPluginInit) { check ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


exports.onPreBootstrap = async ({ actions, emitter, cache }, pluginOptions) => {
Expand Down
Expand Up @@ -40,8 +40,7 @@ module.exports = (nodeApiArgs, pluginOptions = {}) => {
// to customize the GraphQL schema. This makes it possible for subplugins to
// modify types owned by `gatsby-transformer-remark`.
plugins.forEach(plugin => {
const resolvedPlugin =
_CFLAGS_.GATSBY_MAJOR === `4` ? plugin.module : require(plugin.resolve)
const resolvedPlugin = plugin.module

if (typeof resolvedPlugin.createSchemaCustomization === `function`) {
resolvedPlugin.createSchemaCustomization(
Expand Down
15 changes: 5 additions & 10 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Expand Up @@ -135,8 +135,7 @@ module.exports = function remarkExtendNodeType(
}

for (const plugin of pluginOptions.plugins) {
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4` ? plugin.module : require(plugin.resolve)
const requiredPlugin = plugin.module
if (_.isFunction(requiredPlugin.setParserPlugins)) {
for (const parserPlugin of requiredPlugin.setParserPlugins(
plugin.pluginOptions
Expand Down Expand Up @@ -294,10 +293,8 @@ module.exports = function remarkExtendNodeType(

// Use a for loop to run remark plugins serially.
for (const plugin of pluginOptions.plugins) {
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4`
? plugin.module
: require(plugin.resolve)
const requiredPlugin = plugin.module

// Allow both exports = function(), and exports.default = function()
const defaultFunction = _.isFunction(requiredPlugin)
? requiredPlugin
Expand Down Expand Up @@ -350,10 +347,8 @@ module.exports = function remarkExtendNodeType(
//
// Use for loop to run remark plugins serially.
for (const plugin of pluginOptions.plugins) {
const requiredPlugin =
_CFLAGS_.GATSBY_MAJOR === `4`
? plugin.module
: require(plugin.resolve)
const requiredPlugin = plugin.module

if (typeof requiredPlugin.mutateSource === `function`) {
await requiredPlugin.mutateSource(
{
Expand Down
20 changes: 3 additions & 17 deletions packages/gatsby-transformer-remark/src/gatsby-node.js
Expand Up @@ -18,22 +18,8 @@ exports.pluginOptionsSchema = function ({ Joi }) {
excerpt_separator: Joi.string().description(
`If your Markdown file contains HTML, excerpt will not return a value. In that case, you can set an excerpt_separator to an HTML tag. Edit your Markdown files to include that HTML tag after the text you’d like to appear in the excerpt.`
),
plugins:
_CFLAGS_.GATSBY_MAJOR === `4`
? Joi.subPlugins().description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
)
: Joi.array()
.items(
Joi.string(),
Joi.object({
resolve: Joi.string(),
options: Joi.object({}).unknown(true),
})
)
.default([])
.description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
),
plugins: Joi.subPlugins().description(
`A list of remark plugins. See also: https://github.com/gatsbyjs/gatsby/tree/master/examples/using-remark for examples`
),
})
}
Expand Up @@ -76,7 +76,6 @@ export function loadInternalPlugins(
})

if (
_CFLAGS_.GATSBY_MAJOR === `4` &&
configuredPluginNames.has(GATSBY_CLOUD_PLUGIN_NAME) &&
incompatibleGatsbyCloudPlugin(plugins)
) {
Expand Down
10 changes: 2 additions & 8 deletions packages/gatsby/src/bootstrap/requires-writer.ts
Expand Up @@ -111,14 +111,8 @@ const getMatchPaths = (
const matchPathPages: Array<IMatchPathEntry> = []

pages.forEach((page: IGatsbyPage, index: number): void => {
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
if (page.matchPath && getPageMode(page) === `SSG`) {
matchPathPages.push(createMatchPathEntry(page, index))
}
} else {
if (page.matchPath) {
matchPathPages.push(createMatchPathEntry(page, index))
}
if (page.matchPath && getPageMode(page) === `SSG`) {
matchPathPages.push(createMatchPathEntry(page, index))
}
})

Expand Down
8 changes: 0 additions & 8 deletions packages/gatsby/src/commands/build-html.ts
Expand Up @@ -546,14 +546,6 @@ export async function buildHTMLPagesAndDeleteStaleArtifacts({
reporter.info(`There are no new or changed html files to build.`)
}

if (_CFLAGS_.GATSBY_MAJOR !== `4` && !program.keepPageRenderer) {
try {
await deleteRenderer(pageRenderer)
} catch (err) {
// pass through
}
}

if (toDelete.length > 0) {
const publicDir = path.join(program.directory, `public`)
const deletePageDataActivityTimer = reporter.activityTimer(
Expand Down
10 changes: 1 addition & 9 deletions packages/gatsby/src/commands/build-utils.ts
Expand Up @@ -162,15 +162,7 @@ export function calcDirtyHtmlFiles(state: IGatsbyState): {
// and because of that `isDeleted` might not be set ...
markActionForPage(path, `delete`)
} else {
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
if (getPageMode(page, state) === `SSG`) {
if (htmlFile.dirty || state.html.unsafeBuiltinWasUsedInSSR) {
markActionForPage(path, `regenerate`)
} else {
markActionForPage(path, `reuse`)
}
}
} else {
if (getPageMode(page, state) === `SSG`) {
if (htmlFile.dirty || state.html.unsafeBuiltinWasUsedInSSR) {
markActionForPage(path, `regenerate`)
} else {
Expand Down
15 changes: 7 additions & 8 deletions packages/gatsby/src/commands/build.ts
Expand Up @@ -180,7 +180,7 @@ module.exports = async function build(
buildActivityTimer.end()
}

if (_CFLAGS_.GATSBY_MAJOR === `4` && shouldGenerateEngines()) {
if (shouldGenerateEngines()) {
const state = store.getState()
const buildActivityTimer = report.activityTimer(
`Building Rendering Engines`,
Expand Down Expand Up @@ -246,7 +246,7 @@ module.exports = async function build(
pageConfigActivity.end()
}

if (_CFLAGS_.GATSBY_MAJOR === `4` && shouldGenerateEngines()) {
if (shouldGenerateEngines()) {
const validateEnginesActivity = report.activityTimer(
`Validating Rendering Engines`,
{
Expand Down Expand Up @@ -284,11 +284,10 @@ module.exports = async function build(
const { queryIds } = await calculateDirtyQueries({ store })

// Only run queries with mode SSG
if (_CFLAGS_.GATSBY_MAJOR === `4`) {
queryIds.pageQueryIds = queryIds.pageQueryIds.filter(
query => getPageMode(query) === `SSG`
)
}

queryIds.pageQueryIds = queryIds.pageQueryIds.filter(
query => getPageMode(query) === `SSG`
)

// Start saving page.mode in the main process (while queries run in workers in parallel)
const waitMaterializePageMode = materializePageMode()
Expand Down Expand Up @@ -405,7 +404,7 @@ module.exports = async function build(
// we need to save it again to make sure our latest state has been saved
await db.saveState()

if (_CFLAGS_.GATSBY_MAJOR === `4` && shouldGenerateEngines()) {
if (shouldGenerateEngines()) {
// well, tbf we should just generate this in `.cache` and avoid deleting it :shrug:
program.keepPageRenderer = true
}
Expand Down