-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(gatsby): load config and plugins in worker (#31773)
* refactor(gatsby): single function to load config and plugins * expose loadConfigAndPlugins function in worker * don't rely on cwd when loading plugins * add test veryfing config can be loaded and APIs executed in worker * adjust cli integration tests to cover for activity change (merging loading config and plugins * fix functions (flag need to be handled before plugins loading) * fix build error stack traces when ... gatsby-telemetry is imported in worker * satisfy ts * ensure double slashes (?)
- Loading branch information
Showing
16 changed files
with
193 additions
and
90 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import reporter from "gatsby-cli/lib/reporter" | ||
import telemetry from "gatsby-telemetry" | ||
|
||
import { IFlattenedPlugin } from "./load-plugins/types" | ||
|
||
import { preferDefault } from "../bootstrap/prefer-default" | ||
import { getConfigFile } from "../bootstrap/get-config-file" | ||
import { loadPlugins } from "../bootstrap/load-plugins" | ||
import { internalActions } from "../redux/actions" | ||
import loadThemes from "../bootstrap/load-themes" | ||
import { store } from "../redux" | ||
import handleFlags from "../utils/handle-flags" | ||
import availableFlags from "../utils/flags" | ||
|
||
export async function loadConfigAndPlugins({ | ||
siteDirectory, | ||
processFlags = false, | ||
}: { | ||
siteDirectory: string | ||
processFlags: boolean | ||
}): Promise<{ | ||
config: any | ||
flattenedPlugins: Array<IFlattenedPlugin> | ||
}> { | ||
// Try opening the site's gatsby-config.js file. | ||
const { configModule, configFilePath } = await getConfigFile( | ||
siteDirectory, | ||
`gatsby-config` | ||
) | ||
let config = preferDefault(configModule) | ||
|
||
// The root config cannot be exported as a function, only theme configs | ||
if (typeof config === `function`) { | ||
reporter.panic({ | ||
id: `10126`, | ||
context: { | ||
configName: `gatsby-config`, | ||
siteDirectory, | ||
}, | ||
}) | ||
} | ||
|
||
if (config && processFlags) { | ||
// Setup flags | ||
if (config) { | ||
// Get flags | ||
const { enabledConfigFlags, unknownFlagMessage, message } = handleFlags( | ||
availableFlags, | ||
config.flags | ||
) | ||
|
||
if (unknownFlagMessage !== ``) { | ||
reporter.warn(unknownFlagMessage) | ||
} | ||
|
||
// set process.env for each flag | ||
enabledConfigFlags.forEach(flag => { | ||
process.env[flag.env] = `true` | ||
}) | ||
|
||
// Print out message. | ||
if (message !== ``) { | ||
reporter.info(message) | ||
} | ||
|
||
// track usage of feature | ||
enabledConfigFlags.forEach(flag => { | ||
if (flag.telemetryId) { | ||
telemetry.trackFeatureIsUsed(flag.telemetryId) | ||
} | ||
}) | ||
|
||
// Track the usage of config.flags | ||
if (config.flags) { | ||
telemetry.trackFeatureIsUsed(`ConfigFlags`) | ||
} | ||
} | ||
} | ||
|
||
// theme gatsby configs can be functions or objects | ||
if (config) { | ||
const plugins = await loadThemes(config, { | ||
configFilePath, | ||
rootDir: siteDirectory, | ||
}) | ||
config = plugins.config | ||
} | ||
|
||
store.dispatch(internalActions.setSiteConfig(config)) | ||
|
||
const flattenedPlugins = await loadPlugins(config, siteDirectory) | ||
|
||
return { config, flattenedPlugins } | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createTestWorker, GatsbyTestWorkerPool } from "./test-helpers" | ||
import { store } from "../../../redux" | ||
import * as path from "path" | ||
|
||
let worker: GatsbyTestWorkerPool | undefined | ||
|
||
beforeEach(() => { | ||
store.dispatch({ type: `DELETE_CACHE` }) | ||
}) | ||
|
||
afterEach(() => { | ||
if (worker) { | ||
worker.end() | ||
worker = undefined | ||
} | ||
}) | ||
|
||
it(`can load config and execute node API in worker`, async () => { | ||
worker = createTestWorker() | ||
|
||
const siteDirectory = path.join(__dirname, `fixtures`, `sample-site`) | ||
|
||
// plugin options for custom local plugin contains function (() => `foo`) | ||
await worker.loadConfigAndPlugins({ siteDirectory }) | ||
|
||
// plugin API execute function from plugin options and store result in `global` | ||
await worker.runAPI(`createSchemaCustomization`) | ||
|
||
// getting result stored in `global` | ||
expect(await worker.getAPIRunResult()).toEqual(`foo`) | ||
}) |
10 changes: 10 additions & 0 deletions
10
packages/gatsby/src/utils/worker/__tests__/fixtures/sample-site/gatsby-config.js
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,10 @@ | ||
module.exports = { | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-plugin-test`, | ||
options: { | ||
fn: () => `foo` | ||
} | ||
} | ||
], | ||
} |
3 changes: 3 additions & 0 deletions
3
...src/utils/worker/__tests__/fixtures/sample-site/plugins/gatsby-plugin-test/gatsby-node.js
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,3 @@ | ||
exports.createSchemaCustomization = (_, pluginOptions) => { | ||
global.test = pluginOptions.fn() | ||
} |
3 changes: 3 additions & 0 deletions
3
...y/src/utils/worker/__tests__/fixtures/sample-site/plugins/gatsby-plugin-test/package.json
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,3 @@ | ||
{ | ||
"main": "gatsby-node.js" | ||
} |
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
1 change: 1 addition & 0 deletions
1
packages/gatsby/src/utils/worker/child.ts → ...es/gatsby/src/utils/worker/child/index.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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
// Note: this doesn't check for conflicts between module exports | ||
export { renderHTMLProd, renderHTMLDev } from "./render-html" | ||
export { loadConfigAndPlugins } from "./load-config-and-plugins" |
1 change: 1 addition & 0 deletions
1
packages/gatsby/src/utils/worker/child/load-config-and-plugins.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 @@ | ||
export { loadConfigAndPlugins } from "../../../bootstrap/load-config-and-plugins" |
Oops, something went wrong.