From 8ff3cfb0aeb9d15ae15f6af541f1a174b7cc05ea Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Wed, 9 May 2018 18:57:18 +0200 Subject: [PATCH] [v2] small refactor of api-runner-browser to fix loader test (#5343) * small refactor of api-runner-browser to fix tests * remove stray '$' and unneeded eslint-disable-next-line --- .../cache-dir/api-runner-browser-plugins.js | 14 ++++++++++++++ packages/gatsby/cache-dir/api-runner-browser.js | 15 +-------------- packages/gatsby/src/bootstrap/index.js | 15 ++------------- 3 files changed, 17 insertions(+), 27 deletions(-) create mode 100644 packages/gatsby/cache-dir/api-runner-browser-plugins.js diff --git a/packages/gatsby/cache-dir/api-runner-browser-plugins.js b/packages/gatsby/cache-dir/api-runner-browser-plugins.js new file mode 100644 index 0000000000000..239224f976dbf --- /dev/null +++ b/packages/gatsby/cache-dir/api-runner-browser-plugins.js @@ -0,0 +1,14 @@ +// During bootstrap, we write requires at top of this file which looks +// basically like: +// module.exports = [ +// { +// plugin: require("/path/to/plugin1/gatsby-browser.js"), +// options: { ... }, +// }, +// { +// plugin: require("/path/to/plugin2/gatsby-browser.js"), +// options: { ... }, +// }, +// ] + +module.exports = [] diff --git a/packages/gatsby/cache-dir/api-runner-browser.js b/packages/gatsby/cache-dir/api-runner-browser.js index b8004fa23b84f..b5a2b0ad5b2d8 100644 --- a/packages/gatsby/cache-dir/api-runner-browser.js +++ b/packages/gatsby/cache-dir/api-runner-browser.js @@ -1,18 +1,6 @@ -// During bootstrap, we write requires at top of this file which looks -// basically like: -// var plugins = [ -// { -// plugin: require("/path/to/plugin1/gatsby-browser.js"), -// options: { ... }, -// }, -// { -// plugin: require("/path/to/plugin2/gatsby-browser.js"), -// options: { ... }, -// }, -// ] +const plugins = require(`./api-runner-browser-plugins`) exports.apiRunner = (api, args, defaultReturn) => { - // eslint-disable-next-line no-undef let results = plugins.map(plugin => { if (!plugin.plugin[api]) { return undefined @@ -34,7 +22,6 @@ exports.apiRunner = (api, args, defaultReturn) => { } exports.apiRunnerAsync = (api, args, defaultReturn) => - // eslint-disable-next-line no-undef plugins.reduce( (previous, next) => next.plugin[api] diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index f09f27895a046..d137e7b11a8a4 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -208,17 +208,6 @@ module.exports = async (args: BootstrapArgs) => { plugin => plugin.resolve ) - let browserAPIRunner = `` - - try { - browserAPIRunner = fs.readFileSync( - `${siteDir}/api-runner-browser.js`, - `utf-8` - ) - } catch (err) { - report.panic(`Failed to read ${siteDir}/api-runner-browser.js`, err) - } - const browserPluginsRequires = browserPlugins .map( plugin => @@ -229,7 +218,7 @@ module.exports = async (args: BootstrapArgs) => { ) .join(`,`) - browserAPIRunner = `var plugins = [${browserPluginsRequires}]\n${browserAPIRunner}` + const browserAPIRunner = `module.exports = [${browserPluginsRequires}]\n` let sSRAPIRunner = `` @@ -251,7 +240,7 @@ module.exports = async (args: BootstrapArgs) => { sSRAPIRunner = `var plugins = [${ssrPluginsRequires}]\n${sSRAPIRunner}` fs.writeFileSync( - `${siteDir}/api-runner-browser.js`, + `${siteDir}/api-runner-browser-plugins.js`, browserAPIRunner, `utf-8` )