signalwerk / gatsby-remark-table-of-contents

gatsby remark plugin to generate table of contents
21 stars 10 forks source link

Stopped working with Gatsby v4 upgrade when using `gatsby-plugin-mdx` #28

Closed aliceHendicott closed 2 years ago

aliceHendicott commented 2 years ago

After upgrading Gatsby to v4, using gatsby-remark-table-of-contents no longer works when used with gatsby-plugin-mdx (the same may be true for other ways of using this package but I have not tried it with gatsby-transformer-remark).

Basically what's happening is, the gatsby-plugin-mdx tries to import gatsby-remark-table-of-contents but because it uses export default instead of module.exports, it thinks the export is an object rather than a function and therefore doesn't run the transform.

After playing around with the config, a simple solution to this problem is to alter the rollup output config, specifically the output.exports option from named to default - https://github.com/aliceHendicott/gatsby-remark-table-of-contents/commit/b5b6b20444acb322ba17e1ea30b93c0468505514.

Do you see any issues with adding this change? Happy to do up a PR if it makes sense to include this change.

christopher-besch commented 2 years ago

I ran into the same problem. But I tried your proposed changes and got this:

Error: Cannot find module '/home/chris/files/web/homepage/node_modules/gatsby-remark-table-of-contents/di
st/gatsby-remark-table-of-contents.cjs.js'. Please verify that the package.json has a valid "main" entry
    at tryPackage (node:internal/modules/cjs/loader:353:19)
    at Function.Module._findPath (node:internal/modules/cjs/loader:566:18)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.resolve
(/home/chris/files/web/homepage/node_modules/v8-compile-cache/v8-compile-cache.js:164:23)
    at map
(/home/chris/files/web/homepage/node_modules/gatsby/src/bootstrap/load-plugins/validate.ts:232:48)
    at Array.map (<anonymous>)
    at /home/chris/files/web/homepage/node_modules/gatsby/src/bootstrap/load-plugins/validate.ts:225:35
    at Object.validate (/home/chris/files/web/homepage/node_modules/joi/lib/types/any.js:47:28)
    at Object.internals.rules (/home/chris/files/web/homepage/node_modules/joi/lib/validator.js:397:33)
    at Object.exports.validate (/home/chris/files/web/homepage/node_modules/joi/lib/validator.js:353:22)
    at internals.Base.$_validate (/home/chris/files/web/homepage/node_modules/joi/lib/base.js:773:26)
    at Object.validate (/home/chris/files/web/homepage/node_modules/joi/lib/types/keys.js:108:45)
    at Object.exports.validate (/home/chris/files/web/homepage/node_modules/joi/lib/validator.js:328:26)
    at Object.internals.entry (/home/chris/files/web/homepage/node_modules/joi/lib/validator.js:145:28)
    at Object.exports.entryAsync (/home/chris/files/web/homepage/node_modules/joi/lib/validator.js:58:30)
    at internals.Base.validateAsync (/home/chris/files/web/homepage/node_modules/joi/lib/base.js:553:26)
{
  code: 'MODULE_NOT_FOUND',
  path: '/home/chris/files/web/homepage/node_modules/gatsby-remark-table-of-contents/package.json',
  requestPath: '/home/chris/files/web/homepage/node_modules/gatsby-remark-table-of-contents'
}

I'm very inexperienced with the JavaScript world, so my way of installing your version yarn remove git+https://github.com/aliceHendicott/gatsby-remark-table-of-contents might be flawed.

Anyway, I think it makes sense to move this to a PR.

aliceHendicott commented 2 years ago

@christopher-besch I've done up a PR with this change so hopefully the maintainers get to this soon.

You won't be able to install my fork as I haven't published it anywhere. If you are looking for a workaround for the time being, to fix the issue in my Gatsby project, I have used patch-package to change the export in the gatsby-remark-table-of-contents main .cjs file from default export to module.exports and it has resolved the issue for now.

signalwerk commented 2 years ago

Thank you @aliceHendicott for the investigation and your work! I merged your PR to not let you wait any longer and published a new major release in npm. With Version 2.0.0 you should get your suggested export. Since I didn't find the time to test it properly, I'm super happy if you can test it in your project.

@christopher-besch also thank you for your report. Please try version 2.0.0 and let us know if you were successful. If you need help how to update your package to the new version, let us know.

aliceHendicott commented 2 years ago

Thanks @signalwerk for getting to this so quickly!

I've just tested version 2.0.0 in my gatsby project and it is working perfectly!

christopher-besch commented 2 years ago

I've tested it as well and everything works as it should. This issue can be closed. Thanks a lot, you two!

signalwerk commented 2 years ago

@aliceHendicott @christopher-besch thank you for testing and the feedback!