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

feat(gatsby-transformer-remark): Allow for multiple different remark sources #7512

Merged
merged 4 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update tests
  • Loading branch information
wardpeet committed Mar 13, 2019
commit f68f05cb42a1ab6fd809532fef48776085fae3e4
1 change: 0 additions & 1 deletion packages/gatsby-transformer-remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"bluebird": "^3.5.0",
"gray-matter": "^4.0.0",
"hast-util-raw": "^4.0.0",
"hast-util-to-html": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
GraphQLList,
GraphQLSchema,
} = require(`gatsby/graphql`)
const { Promise } = require(`bluebird`)
const { onCreateNode, setFieldsOnGraphQLNodeType } = require(`../gatsby-node`)
const {
inferObjectStructureFromNodes,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Promise = require(`bluebird`)
const _ = require(`lodash`)

const { onCreateNode } = require(`../gatsby-node`)
Expand Down
22 changes: 17 additions & 5 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const visit = require(`unist-util-visit`)
const toHAST = require(`mdast-util-to-hast`)
const hastToHTML = require(`hast-util-to-html`)
const mdastToToc = require(`mdast-util-toc`)
const Promise = require(`bluebird`)
const unified = require(`unified`)
const parse = require(`remark-parse`)
const stringify = require(`remark-stringify`)
Expand Down Expand Up @@ -68,6 +67,19 @@ const safeGetCache = ({ getCache, cache }) => id => {
return getCache(id)
}

/**
* @template T
* @param {Array<T>} input
* @param {(input: T) => Promise<void>} iterator
* @return Promise<void>
*/
const eachPromise = (input, iterator) =>
input.reduce(
(accumulatorPromise, nextValue) =>
accumulatorPromise.then(() => void iterator(nextValue)),
Copy link
Contributor

Choose a reason for hiding this comment

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

this void here is problematic? after removing it, it seems to work - why is it here? some context is needed

Copy link
Contributor

Choose a reason for hiding this comment

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

Also generally - where do we stand on this? We use bluebird pretty heavily--so I'm not sure it was worth refactoring this.

But a bit tangential, if we went that way--just need to ensure we have a working alternative for sequential promises!

Copy link
Contributor

@pieh pieh Mar 14, 2019

Choose a reason for hiding this comment

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

I personally don't agree with removing dependency for the sake of removing it (at least for stuff that is only used during the builds and don't end up in production js bundles produced by webpack). If it fixes bugs or improve performance, then that's fine.

I will open PR fixing that and add some tests for this helper.

Copy link
Contributor

Choose a reason for hiding this comment

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

When utils like bluebird have maintainers with domain knowledge and it was battle-tested by thousands of developers - I will trust that more than hand rolled and not very tested approaches.

Copy link
Contributor

Choose a reason for hiding this comment

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

In any case - here's hot-fix for issue #12578 (first commit on purpose just added failing test, and second commit "fixes" the problem.

@alexkirsz I would appreciate you taking a look on this, because I imagine there was some reason for that void to be there, but I have no clue what that would be (and it did break that utility)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pieh I'm not the author of this particular piece of code, @wardpeet is (I only authored the first commit of this PR).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, ok - sorry @alexkirsz. I should pay more attention to individual commits in the PR

Copy link
Contributor

Choose a reason for hiding this comment

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

my bady, sorry about this

Promise.resolve()
)

const HeadingType = new GraphQLObjectType({
name: `MarkdownHeading`,
fields: {
Expand Down Expand Up @@ -221,8 +233,8 @@ module.exports = (
if (process.env.NODE_ENV !== `production` || !fileNodes) {
fileNodes = getNodesByType(`File`)
}
// Use Bluebird's Promise function "each" to run remark plugins serially.
await Promise.each(plugins, plugin => {

await eachPromise(plugins, plugin => {
Copy link
Contributor

Choose a reason for hiding this comment

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

this doesn't seem to work

const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin.mutateSource)) {
return requiredPlugin.mutateSource(
Expand Down Expand Up @@ -289,8 +301,8 @@ module.exports = (
if (process.env.NODE_ENV !== `production` || !fileNodes) {
fileNodes = getNodesByType(`File`)
}
// Use Bluebird's Promise function "each" to run remark plugins serially.
await Promise.each(plugins, plugin => {

await eachPromise(plugins, plugin => {
const requiredPlugin = require(plugin.resolve)
if (_.isFunction(requiredPlugin)) {
return requiredPlugin(
Expand Down