-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9a60b3e
Make getConfigPath in gatsby-core-utils check for TS and JS gatsby-co…
tyhopp c9b705a
Update minimal TS starter to use gatsby-config.ts
tyhopp f457430
Extract common babel plugin logic to func
tyhopp 277916c
Add comments for context
tyhopp ceaa938
Make it work
tyhopp 62c406e
DRY get plugin nodes
tyhopp a26e0bd
Fix unit test
tyhopp d04a146
Test coverage for existing functionality
tyhopp 2fbde84
Test coverage for TS config write
tyhopp 7ba5dfe
Use __dirname to make CI happy
tyhopp 222444a
Ensure fixtures dir exists
tyhopp 812c341
Fix type import string literal
tyhopp 7a6b6ef
Update snapshot
tyhopp d85952d
Use fs-extra, not fs/promises
tyhopp 1f8321a
Extract out starter gatsby-config.ts to #35128
tyhopp 084471a
Actually make it a JS file
tyhopp 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
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/gatsby-cli/src/__tests__/handlers/__snapshots__/plugin-add.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`addPlugins gatsby-config.js should not write with no plugins 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\`, | ||
}, | ||
plugins: [], | ||
} | ||
" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.js should write a plugin with options 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [{ | ||
resolve: 'gatsby-plugin-hello', | ||
options: { | ||
\\"greet\\": true | ||
} | ||
}] | ||
};" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.js should write a single plugin 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\"] | ||
};" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.js should write multiple plugins 1`] = ` | ||
"module.exports = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\", \\"gatsby-plugin-world\\"] | ||
};" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.ts should not write with no plugins 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\" | ||
|
||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\`, | ||
}, | ||
plugins: [], | ||
} | ||
|
||
export default config | ||
" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.ts should write a plugin with options 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
|
||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [{ | ||
resolve: 'gatsby-plugin-hello', | ||
options: { | ||
\\"greet\\": true | ||
} | ||
}] | ||
}; | ||
|
||
export default config; | ||
" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.ts should write a single plugin 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
|
||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\"] | ||
}; | ||
|
||
export default config; | ||
" | ||
`; | ||
|
||
exports[`addPlugins gatsby-config.ts should write multiple plugins 1`] = ` | ||
"import type { GatsbyConfig } from \\"gatsby\\"; | ||
|
||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
siteUrl: \`https://www.yourdomain.tld\` | ||
}, | ||
plugins: [\\"gatsby-plugin-hello\\", \\"gatsby-plugin-world\\"] | ||
}; | ||
|
||
export default config; | ||
" | ||
`; |
120 changes: 120 additions & 0 deletions
120
packages/gatsby-cli/src/__tests__/handlers/plugin-add.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { ensureDir, copyFile, readFile, rm } from "fs-extra" | ||
import { join } from "path" | ||
import { addPlugins } from "../../handlers/plugin-add" | ||
|
||
/** | ||
* Copy files from minimal starters instead of testing against static gatsby-configs | ||
* in fixtues so that these break if we change the starter configs in a breaking way. | ||
* | ||
* Not using `jest.each` since I find that much harder to read and debug. | ||
* @see {@link https://jestjs.io/docs/api#testeachtablename-fn-timeout} | ||
*/ | ||
|
||
const root = join(__dirname, `../../../../..`) | ||
const fixtures = join(__dirname, `../fixtures`) | ||
const config = { | ||
js: { | ||
starter: `${root}/starters/gatsby-starter-minimal/gatsby-config.js`, | ||
fixture: `${fixtures}/gatsby-config.js`, | ||
}, | ||
ts: { | ||
starter: `${root}/starters/gatsby-starter-minimal-ts/gatsby-config.ts`, | ||
fixture: `${fixtures}/gatsby-config.ts`, | ||
}, | ||
} | ||
const plugin = { | ||
hello: `gatsby-plugin-hello`, | ||
world: `gatsby-plugin-world`, | ||
} | ||
|
||
describe(`addPlugins`, () => { | ||
beforeAll(async () => { | ||
await ensureDir(fixtures) | ||
}) | ||
|
||
describe(`gatsby-config.js`, () => { | ||
beforeEach(async () => { | ||
await copyFile(config.js.starter, config.js.fixture) | ||
}) | ||
|
||
afterEach(async () => { | ||
await rm(config.js.fixture) | ||
}) | ||
|
||
it(`should not write with no plugins`, async () => { | ||
await addPlugins([], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a single plugin`, async () => { | ||
await addPlugins([plugin.hello], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write multiple plugins`, async () => { | ||
await addPlugins([plugin.hello, plugin.world], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a plugin with options`, async () => { | ||
await addPlugins( | ||
[plugin.hello], | ||
{ | ||
[plugin.hello]: { | ||
greet: true, | ||
}, | ||
}, | ||
fixtures, | ||
[] | ||
) | ||
const gatsbyConfig = (await readFile(config.js.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
describe(`gatsby-config.ts`, () => { | ||
beforeEach(async () => { | ||
await copyFile(config.ts.starter, config.ts.fixture) | ||
}) | ||
|
||
afterEach(async () => { | ||
await rm(config.ts.fixture) | ||
}) | ||
|
||
it(`should not write with no plugins`, async () => { | ||
await addPlugins([], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a single plugin`, async () => { | ||
await addPlugins([plugin.hello], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write multiple plugins`, async () => { | ||
await addPlugins([plugin.hello, plugin.world], {}, fixtures, []) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
|
||
it(`should write a plugin with options`, async () => { | ||
await addPlugins( | ||
[plugin.hello], | ||
{ | ||
[plugin.hello]: { | ||
greet: true, | ||
}, | ||
}, | ||
fixtures, | ||
[] | ||
) | ||
const gatsbyConfig = (await readFile(config.ts.fixture)).toString() | ||
expect(gatsbyConfig).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For anyone reading this (or my future self), I took a look at making the quote type of the output strings in the transform consistent (e.g. all backticks).
The only obstacle is in the
buildPluginNode
function weJSON.stringify
the options object, and if we try to make object keys backticks then the transform fails since that's invalid JSON (and we can't expect keys to only be alphabetic).Anyhow not a huge deal, but wanted to leave this for posterity. Will leave this out of this PR.