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

fix(gatsby): Support ESM-only gatsby-remark-* plugins #37440

Merged
merged 9 commits into from
Jan 11, 2023
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
4 changes: 3 additions & 1 deletion integration-tests/esm-in-gatsby-files/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ yarn.lock
# These are removed and created during the test lifecycle
./gatsby-config.js
./gatsby-config.mjs
./gatsby-config.ts
./gatsby-config.ts
./gatsby-node.js
./gatsby-node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ helloDefaultCJS()
helloNamedCJS()

const config = {
plugins: [],
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-cjs`,
}
]
},
},
],
}

module.exports = config
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ const config = {
slugify,
},
},
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-esm`,
}
]
},
},
],
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.info(`gatsby-remark-cjs-gatsby-config-js`)

module.exports = ({ markdownAST }, pluginOptions) => {
return markdownAST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "gatsby-remark-cjs",
"version": "1.0.0",
"main": "index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.info(`gatsby-remark-esm-gatsby-config-mjs`)

export default ({ markdownAST }, pluginOptions) => {
return markdownAST
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "gatsby-remark-esm",
"version": "1.0.0",
"type": "module",
"main": "index.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ async function build() {
// Tests include multiple assertions since running multiple builds is time consuming

describe(`gatsby-config.js`, () => {
afterEach(() => {
fs.rmSync(configPath.cjs)
afterEach(async () => {
await fs.rm(configPath.cjs)
await fs.rm(localPluginTargetDir, { recursive: true })
})

it(`works with required CJS modules`, async () => {
await fs.copyFile(fixturePath.cjs, configPath.cjs)

await fs.ensureDir(localPluginTargetDir)
await fs.copy(localPluginFixtureDir, localPluginTargetDir)

const stdout = await build()

// Build succeeded
Expand All @@ -51,6 +55,9 @@ describe(`gatsby-config.js`, () => {
// Requires work
expect(stdout).toContain(`hello-default-cjs`)
expect(stdout).toContain(`hello-named-cjs`)

// Local gatsby-remark-plugin works
expect(stdout).toContain(`gatsby-remark-cjs-gatsby-config-js`)
})
})

Expand Down Expand Up @@ -78,6 +85,9 @@ describe(`gatsby-config.mjs`, () => {
// Local plugin gatsby-config.mjs works
expect(stdout).toContain(`a-local-plugin-gatsby-config-mjs`)

// Local ESM gatsby-remark-plugin works
expect(stdout).toContain(`gatsby-remark-esm-gatsby-config-mjs`)

// Local plugin with an esm module passed via options works, this implicitly tests gatsby-node.mjs too
expect(stdout).toContain(`a-local-plugin-using-passed-esm-module`)
})
Expand Down
1 change: 1 addition & 0 deletions integration-tests/esm-in-gatsby-files/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"dependencies": {
"gatsby": "next",
"gatsby-transformer-remark": "next",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ module.exports = {
keepDynamicImports: [
`./src/utils/feedback.ts`,

// These files use dynamic imports to load gatsby-config and gatsby-node so esm works
// These files use dynamic imports to load gatsby-config, gatsby-node, and subPlugins so esm works
`./src/bootstrap/get-config-file.ts`,
`./src/bootstrap/resolve-module-exports.ts`,
`./src/bootstrap/load-plugins/validate.ts`,
`./src/utils/import-gatsby-plugin.ts`
]
}]],
Expand Down
26 changes: 24 additions & 2 deletions packages/gatsby/src/bootstrap/load-plugins/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ISiteConfig,
} from "./types"
import { resolvePlugin } from "./resolve-plugin"
import { preferDefault } from "../prefer-default"

interface IApi {
version?: string
Expand Down Expand Up @@ -178,6 +179,27 @@ export async function handleBadExports({
}
}

interface ISubPluginCustomReturn {
resolve: string
modulePath: string
options: {
[key: string]: unknown
}
module: any
}

const addModuleImport = async (
value: Array<ISubPluginCustomReturn>
): Promise<Array<ISubPluginCustomReturn>> => {
for (const plugin of value) {
const importedModule = await import(plugin.modulePath)
const pluginModule = preferDefault(importedModule)
plugin.module = pluginModule
}

return value
}

async function validatePluginsOptions(
plugins: Array<IPluginRefObject>,
rootDir: string
Expand Down Expand Up @@ -233,7 +255,6 @@ async function validatePluginsOptions(
`${resolvedPlugin.resolve}${entry ? `/${entry}` : ``}`
)
value.modulePath = modulePath
value.module = require(modulePath)

const normalizedPath = helpers.state.path
.map((key, index) => {
Expand Down Expand Up @@ -266,7 +287,8 @@ async function validatePluginsOptions(
return value
})
}, `Gatsby specific subplugin validation`)
.default([]),
.default([])
.external(addModuleImport, `add module key to subplugin`),
args: (schema: any, args: any): any => {
if (
args?.entry &&
Expand Down