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

Address todos for major release #29413

Merged
merged 11 commits into from
Feb 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export function loadCachedConfig() {
export default function preset(_, options = {}) {
let { targets = null } = options

// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`
const stage = options.stage || `test`
const pluginBabelConfig = loadCachedConfig()
let isBrowser
// unused because of cloud builds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ exports[`Test plugin feed custom properties work properly 1`] = `"<?xml version=

exports[`Test plugin feed custom query runs 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[my feed]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/a-custom-path</guid></item><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/another-custom-path</link><guid isPermaLink=\\"true\\">http://dummy.url/another-custom-path</guid></item></channel></rss>"`;

exports[`Test plugin feed default settings work properly 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><rss xmlns:dc=\\"http://purl.org/dc/elements/1.1/\\" xmlns:content=\\"http://purl.org/rss/1.0/modules/content/\\" xmlns:atom=\\"http://www.w3.org/2005/Atom\\" version=\\"2.0\\"><channel><title><![CDATA[a sample title]]></title><description><![CDATA[a description]]></description><link>http://github.com/dylang/node-rss</link><generator>GatsbyJS</generator><lastBuildDate>Mon, 01 Jan 2018 00:00:00 GMT</lastBuildDate><item><title><![CDATA[No title]]></title><description><![CDATA[post description]]></description><link>http://dummy.url/a-slug</link><guid isPermaLink=\\"false\\">http://dummy.url/a-slug</guid><content:encoded></content:encoded></item></channel></rss>"`;
39 changes: 6 additions & 33 deletions packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,6 @@ describe(`Test plugin feed`, () => {
fs.mkdirp = jest.fn().mockResolvedValue()
})

it(`default settings work properly`, async () => {
fs.writeFile = jest.fn()
fs.writeFile.mockResolvedValue(true)
const graphql = jest.fn()
graphql.mockResolvedValue({
data: {
site: {
siteMetadata: {
title: `a sample title`,
description: `a description`,
siteUrl: `http://dummy.url/`,
},
},
allMarkdownRemark: {
edges: [
{
node: {
fields: {
slug: `a-slug`,
},
excerpt: `post description`,
},
},
],
},
},
})
await onPostBuild({ graphql }, {})
const [filePath, contents] = fs.writeFile.mock.calls[0]
expect(filePath).toEqual(path.join(`public`, `rss.xml`))
expect(contents).toMatchSnapshot()
})

it(`custom properties work properly`, async () => {
fs.writeFile = jest.fn()
fs.writeFile.mockResolvedValue(true)
Expand Down Expand Up @@ -243,6 +210,12 @@ describe(`Test plugin feed`, () => {
{
output: `rss.xml`,
query: `{ firstMarkdownQuery }`,
serialize: ({ query: { allMarkdownRemark } }) =>
allMarkdownRemark.edges.map(edge => {
return {
...edge.node.frontmatter,
}
}),
},
],
}
Expand Down
46 changes: 17 additions & 29 deletions packages/gatsby-plugin-feed/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,7 @@ const publicPath = `./public`

exports.pluginOptionsSchema = pluginOptionsSchema

// TODO: remove in the next major release
// A default function to transform query data into feed entries.
const serialize = ({ query: { site, allMarkdownRemark } }) =>
allMarkdownRemark.edges.map(edge => {
return {
...edge.node.frontmatter,
description: edge.node.excerpt,
url: site.siteMetadata.siteUrl + edge.node.fields.slug,
guid: site.siteMetadata.siteUrl + edge.node.fields.slug,
custom_elements: [{ "content:encoded": edge.node.html }],
}
})

exports.onPostBuild = async ({ graphql }, pluginOptions) => {
exports.onPostBuild = async ({ graphql, reporter }, pluginOptions) => {
/*
* Run the site settings query to gather context, then
* then run the corresponding feed for each query.
Expand All @@ -47,21 +34,22 @@ exports.onPostBuild = async ({ graphql }, pluginOptions) => {
...feed,
}

const serializer =
feed.serialize && typeof feed.serialize === `function`
? feed.serialize
: serialize

const rssFeed = (await serializer(locals)).reduce((merged, item) => {
merged.item(item)
return merged
}, new RSS(setup(locals)))

const outputPath = path.join(publicPath, feed.output)
const outputDir = path.dirname(outputPath)
if (!(await fs.exists(outputDir))) {
await fs.mkdirp(outputDir)
if (!feed.serialize || typeof feed.serialize !== `function`) {
reporter.warn(
`You did not pass in a valid serialize function. Your feed will not be generated.`
)
} else {
const rssFeed = (await feed.serialize(locals)).reduce((merged, item) => {
merged.item(item)
return merged
}, new RSS(setup(locals)))

const outputPath = path.join(publicPath, feed.output)
const outputDir = path.dirname(outputPath)
if (!(await fs.exists(outputDir))) {
await fs.mkdirp(outputDir)
}
await fs.writeFile(outputPath, rssFeed.xml())
}
await fs.writeFile(outputPath, rssFeed.xml())
}
}
8 changes: 5 additions & 3 deletions packages/gatsby-plugin-google-analytics/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
exports.pluginOptionsSchema = ({ Joi }) =>
// TODO: make sure that trackingId gets required() when releasing a major version
Joi.object({
trackingId: Joi.string().description(
`The property ID; the tracking code won't be generated without it`
),
trackingId: Joi.string()
.description(
`The property ID; the tracking code won't be generated without it`
)
.required(),
head: Joi.boolean()
.default(false)
.description(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ describe(`Load plugins`, () => {
"configDir": null,
"pluginName": "gatsby-plugin-google-analytics",
"validationErrors": Array [
Object {
"context": Object {
"key": "trackingId",
"label": "trackingId",
},
"message": "\\"trackingId\\" is required",
"path": Array [
"trackingId",
],
"type": "any.required",
},
Object {
"context": Object {
"key": "anonymize",
Expand Down