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-core-utils,gatsby-cli): Allow write to gatsby-config.ts #35074

Merged
merged 16 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions packages/gatsby-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@babel/core": "^7.15.5",
"@babel/generator": "^7.16.8",
"@babel/helper-plugin-utils": "^7.16.7",
"@babel/preset-typescript": "^7.16.7",
"@babel/runtime": "^7.15.4",
"@babel/template": "^7.16.7",
"@babel/types": "^7.16.8",
Expand Down
60 changes: 43 additions & 17 deletions packages/gatsby-cli/src/handlers/plugin-add-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
getConfigPath,
getConfigStore,
} from "gatsby-core-utils"
import { transform } from "@babel/core"
import { BabelPluginAddPluginsToGatsbyConfig } from "./plugin-babel-utils"
import { transform, TransformOptions } from "@babel/core"
import BabelPluginAddPluginsToGatsbyConfig from "./plugin-babel-utils"

const addPluginToConfig = (
src: string,
srcPath: string,
{
name,
options,
Expand All @@ -22,19 +23,41 @@ const addPluginToConfig = (
key: string
}
): string => {
const addPlugins = new BabelPluginAddPluginsToGatsbyConfig({
pluginOrThemeName: name,
options,
shouldAdd: true,
key,
})
let code

// @ts-ignore - fix me
const { code } = transform(src, {
// @ts-ignore - fix me
plugins: [addPlugins.plugin],
configFile: false,
})
try {
const transformOptions: TransformOptions = {
plugins: [
[
BabelPluginAddPluginsToGatsbyConfig,
{
pluginOrThemeName: name,
options,
key,
},
],
],
filename: srcPath,
configFile: false,
}

// Use the Babel TS preset if we're operating on `gatsby-config.ts`
if (srcPath.endsWith(`ts`)) {
transformOptions.presets = [`@babel/preset-typescript`]
}

code = transform(src, transformOptions)?.code

// Add back stripped type import, do light formatting, remove added empty module export
if (srcPath.endsWith(`ts`)) {
code = `import type { GatsbyConfig } from 'gatsby'\n\n${code}`
code = code.replace(/;/g, ``)
code = code.replace(`export {}`, ``)
code = code.replace(`export default config`, `\nexport default config`)
}
} catch (error) {
console.error(`Failed to transform gatsby config`, error)
}

return code
}
Expand All @@ -53,10 +76,13 @@ export const GatsbyPluginCreate = async ({
key,
}: IGatsbyPluginCreateInput): Promise<void> => {
const release = await lock(`gatsby-config.js`)
const configSrcPath = getConfigPath(root)
const configSrc = await readConfigFile(root)

const code = addPluginToConfig(configSrc, { name, options, key })

const code = addPluginToConfig(configSrc, configSrcPath, {
name,
options,
key,
})
await fs.writeFile(getConfigPath(root), code)
release()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-cli/src/handlers/plugin-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function installPluginConfig(
options,
key: pluginKey,
})
reporter.info(`Installed ${pluginName || pluginKey} in gatsby-config.js`)
reporter.info(`Installed ${pluginName || pluginKey} in gatsby-config`)
} catch (err) {
reporter.error(JSON.parse(err)?.message)
installTimer.setStatus(`FAILED`)
Expand Down
Loading