From dcbf1deeb05cc13a5f917ab8fc8456dfe2febef4 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Fri, 22 Feb 2019 12:32:59 -0600 Subject: [PATCH 1/4] chore: modern build --- packages/babel-preset-gatsby/src/index.js | 39 +++++++------- .../gatsby/src/commands/build-javascript.js | 25 ++++----- .../gatsby/src/utils/html-renderer-queue.js | 11 +--- packages/gatsby/src/utils/webpack.config.js | 3 +- packages/gatsby/src/utils/worker.js | 48 ++++------------- .../src/utils/workers/build-javascript.js | 28 ++++++++++ packages/gatsby/src/utils/workers/index.js | 2 + .../gatsby/src/utils/workers/render-html.js | 39 ++++++++++++++ yarn.lock | 53 +++++++++++-------- 9 files changed, 144 insertions(+), 104 deletions(-) create mode 100644 packages/gatsby/src/utils/workers/build-javascript.js create mode 100644 packages/gatsby/src/utils/workers/index.js create mode 100644 packages/gatsby/src/utils/workers/render-html.js diff --git a/packages/babel-preset-gatsby/src/index.js b/packages/babel-preset-gatsby/src/index.js index d0842a53188b8..68db99502d940 100644 --- a/packages/babel-preset-gatsby/src/index.js +++ b/packages/babel-preset-gatsby/src/index.js @@ -5,27 +5,28 @@ const resolve = m => require.resolve(m) const loadCachedConfig = () => { let pluginBabelConfig = {} if (process.env.NODE_ENV !== `test`) { - try { - pluginBabelConfig = require(path.join( - process.cwd(), - `./.cache/babelState.json` - )) - } catch (err) { - if (err.message.includes(`Cannot find module`)) { - // This probably is being used outside of the Gatsby CLI. - throw Error( - `\`babel-preset-gatsby\` has been loaded, which consumes config generated by the Gatsby CLI. Set \`NODE_ENV=test\` to bypass, or run \`gatsby build\` first.` - ) - } else { - throw err - } - } + pluginBabelConfig = require(path.join( + process.cwd(), + `./.cache/babelState.json` + )) } return pluginBabelConfig } +const modernConfig = { + loose: false, + targets: { + esmodules: true, + }, + useBuiltIns: false, +} + module.exports = function preset(_, options = {}) { - let { targets = null } = options + if (process.env.MODERN) { + options = modernConfig + } + + let { targets = null, loose = true, useBuiltIns = `usage` } = options const pluginBabelConfig = loadCachedConfig() const stage = process.env.GATSBY_BUILD_STAGE || `test` @@ -45,9 +46,9 @@ module.exports = function preset(_, options = {}) { [ resolve(`@babel/preset-env`), { - loose: true, + loose, modules: stage === `test` ? `commonjs` : false, - useBuiltIns: `usage`, + useBuiltIns, targets, }, ], @@ -78,4 +79,4 @@ module.exports = function preset(_, options = {}) { ], ], } -} +} \ No newline at end of file diff --git a/packages/gatsby/src/commands/build-javascript.js b/packages/gatsby/src/commands/build-javascript.js index 57ae357faa3a0..32f9e0b253c0e 100644 --- a/packages/gatsby/src/commands/build-javascript.js +++ b/packages/gatsby/src/commands/build-javascript.js @@ -1,18 +1,9 @@ -/* @flow */ const webpack = require(`webpack`) -const webpackConfig = require(`../utils/webpack.config`) - -module.exports = async program => { - const { directory } = program - - const compilerConfig = await webpackConfig( - program, - directory, - `build-javascript` - ) +const getWebpackConfig = require(`../utils/webpack.config`) +const build = config => { return new Promise((resolve, reject) => { - webpack(compilerConfig).run((err, stats) => { + webpack(config).run((err, stats) => { if (err) { reject(err) return @@ -28,3 +19,13 @@ module.exports = async program => { }) }) } + +module.exports = async program => { + const compilerConfig = await getWebpackConfig( + program, + program.directory, + `build-javascript` + ) + + await build(compilerConfig) +} diff --git a/packages/gatsby/src/utils/html-renderer-queue.js b/packages/gatsby/src/utils/html-renderer-queue.js index b8182dece2c2e..610d6878eb9f2 100644 --- a/packages/gatsby/src/utils/html-renderer-queue.js +++ b/packages/gatsby/src/utils/html-renderer-queue.js @@ -1,15 +1,8 @@ const Promise = require(`bluebird`) const convertHrtime = require(`convert-hrtime`) -const Worker = require(`jest-worker`).default -const numWorkers = require(`physical-cpu-count`) || 1 const { chunk } = require(`lodash`) -const workerPool = new Worker(require.resolve(`./worker`), { - numWorkers, - forkOptions: { - silent: false, - }, -}) +const worker = require(`./worker`) module.exports = (htmlComponentRendererPath, pages, activity) => new Promise((resolve, reject) => { @@ -29,7 +22,7 @@ module.exports = (htmlComponentRendererPath, pages, activity) => segments, pageSegment => new Promise((resolve, reject) => { - workerPool + worker .renderHTML({ htmlComponentRendererPath, paths: pageSegment, diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index c3cbcb05db7e0..045dfe4ea330a 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -23,8 +23,7 @@ const hasLocalEslint = require(`./local-eslint-config-finder`) module.exports = async ( program, directory, - suppliedStage, - webpackPort = 1500 + suppliedStage ) => { const directoryPath = withBasePath(directory) diff --git a/packages/gatsby/src/utils/worker.js b/packages/gatsby/src/utils/worker.js index ac74894853aa3..8f2a0b68ea19d 100644 --- a/packages/gatsby/src/utils/worker.js +++ b/packages/gatsby/src/utils/worker.js @@ -1,39 +1,9 @@ -const fs = require(`fs-extra`) -const path = require(`path`) -const Promise = require(`bluebird`) - -// copied from https://github.com/markdalgleish/static-site-generator-webpack-plugin/blob/master/index.js#L161 -const generatePathToOutput = outputPath => { - let outputFileName = outputPath.replace(/^(\/|\\)/, ``) // Remove leading slashes for webpack-dev-server - - if (!/\.(html?)$/i.test(outputFileName)) { - outputFileName = path.join(outputFileName, `index.html`) - } - - return path.join(process.cwd(), `public`, outputFileName) -} - -export function renderHTML({ htmlComponentRendererPath, paths, envVars }) { - // This is being executed in child process, so we need to set some vars - // for modules that aren't bundled by webpack. - envVars.forEach(([key, value]) => (process.env[key] = value)) - - return Promise.map( - paths, - path => - new Promise((resolve, reject) => { - const htmlComponentRenderer = require(htmlComponentRendererPath) - try { - htmlComponentRenderer.default(path, (throwAway, htmlString) => { - resolve(fs.outputFile(generatePathToOutput(path), htmlString)) - }) - } catch (e) { - // add some context to error so we can display more helpful message - e.context = { - path, - } - reject(e) - } - }) - ) -} +const Worker = require(`jest-worker`).default +const numWorkers = require(`physical-cpu-count`) || 1 + +module.exports = new Worker(require.resolve(`./workers`), { + numWorkers, + forkOptions: { + silent: false + }, +}) \ No newline at end of file diff --git a/packages/gatsby/src/utils/workers/build-javascript.js b/packages/gatsby/src/utils/workers/build-javascript.js new file mode 100644 index 0000000000000..800a702dd663d --- /dev/null +++ b/packages/gatsby/src/utils/workers/build-javascript.js @@ -0,0 +1,28 @@ +const webpack = require(`webpack`) +const getWebpackConfig = require(`../webpack.config`) +const { store } = require(`../../redux`) + +export async function buildJavascript(program) { + const compilerConfig = await getWebpackConfig( + program, + program.directory, + `build-javascript` + ) + + return new Promise((resolve, reject) => { + webpack(compilerConfig).run((err, stats) => { + if (err) { + reject(err) + return + } + + const jsonStats = stats.toJson() + if (jsonStats.errors && jsonStats.errors.length > 0) { + reject(jsonStats.errors) + return + } + + resolve() + }) + }) +} diff --git a/packages/gatsby/src/utils/workers/index.js b/packages/gatsby/src/utils/workers/index.js new file mode 100644 index 0000000000000..efd3d684f460d --- /dev/null +++ b/packages/gatsby/src/utils/workers/index.js @@ -0,0 +1,2 @@ +exports.renderHTML = require(`./render-html`).renderHTML +exports.buildJavascript = require(`./build-javascript`).buildJavascript \ No newline at end of file diff --git a/packages/gatsby/src/utils/workers/render-html.js b/packages/gatsby/src/utils/workers/render-html.js new file mode 100644 index 0000000000000..ac74894853aa3 --- /dev/null +++ b/packages/gatsby/src/utils/workers/render-html.js @@ -0,0 +1,39 @@ +const fs = require(`fs-extra`) +const path = require(`path`) +const Promise = require(`bluebird`) + +// copied from https://github.com/markdalgleish/static-site-generator-webpack-plugin/blob/master/index.js#L161 +const generatePathToOutput = outputPath => { + let outputFileName = outputPath.replace(/^(\/|\\)/, ``) // Remove leading slashes for webpack-dev-server + + if (!/\.(html?)$/i.test(outputFileName)) { + outputFileName = path.join(outputFileName, `index.html`) + } + + return path.join(process.cwd(), `public`, outputFileName) +} + +export function renderHTML({ htmlComponentRendererPath, paths, envVars }) { + // This is being executed in child process, so we need to set some vars + // for modules that aren't bundled by webpack. + envVars.forEach(([key, value]) => (process.env[key] = value)) + + return Promise.map( + paths, + path => + new Promise((resolve, reject) => { + const htmlComponentRenderer = require(htmlComponentRendererPath) + try { + htmlComponentRenderer.default(path, (throwAway, htmlString) => { + resolve(fs.outputFile(generatePathToOutput(path), htmlString)) + }) + } catch (e) { + // add some context to error so we can display more helpful message + e.context = { + path, + } + reject(e) + } + }) + ) +} diff --git a/yarn.lock b/yarn.lock index 0ab9f15b0d9b3..be40e3b346910 100644 --- a/yarn.lock +++ b/yarn.lock @@ -443,6 +443,13 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-syntax-jsx@^7.2.0": + version "7.2.0" + resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-jsx/-/plugin-syntax-jsx-7.2.0.tgz#0b85a3b4bc7cdf4cc4b8bf236335b907ca22e7c7" + integrity sha512-VyN4QANJkRW6lDBmENzRszvZf3/4AXaj9YR7GwrWeeN9tEBPuXbmDYVU9bYBN0D70zCWVwUy0HWq2553VCb6Hw== + dependencies: + "@babel/helper-plugin-utils" "^7.0.0" + "@babel/plugin-syntax-object-rest-spread@^7.0.0": version "7.0.0" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-object-rest-spread/-/plugin-syntax-object-rest-spread-7.0.0.tgz#37d8fbcaf216bd658ea1aebbeb8b75e88ebc549b" @@ -942,14 +949,21 @@ follow-redirects "^1.2.5" is-buffer "^1.1.5" -"@emotion/babel-preset-css-prop@10.0.5": - version "10.0.5" - resolved "https://registry.yarnpkg.com/@emotion/babel-preset-css-prop/-/babel-preset-css-prop-10.0.5.tgz#955485c6f621247ee35dc1e6bd8fef0ba1d3286f" - integrity sha512-Mf1651pWaqraE2jJdX1/+ArCBLhubxNoNrs0mfYOriIRxwnmjoKQLIEYTMNl/K7ZkIu2PNwvLKGv74fo3wYovA== +"@emotion/babel-plugin-jsx-pragmatic@^0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@emotion/babel-plugin-jsx-pragmatic/-/babel-plugin-jsx-pragmatic-0.1.0.tgz#fc980ee7f50f7b949ca76b4897e992739076b93d" + integrity sha512-qO0St0wzQ7adbDPl0GzbptNBVg0G773uX4o07sSEzMnlsE+sAZn6CtmDJU69efALHjGfsuOAKhL/zBBEy5JGcA== + dependencies: + "@babel/plugin-syntax-jsx" "^7.2.0" + +"@emotion/babel-preset-css-prop@^10.0.5": + version "10.0.7" + resolved "https://registry.yarnpkg.com/@emotion/babel-preset-css-prop/-/babel-preset-css-prop-10.0.7.tgz#ddb57f69ee6a8131f8c9ac4dd68dbec54bd6a6aa" + integrity sha512-GfOJtSm9daEL0KfdWDusy2vTJvXQpO82mTLextuXnTsSFiTaOt7WNP7z3T1onLEHCsgRHTsgjnBHX2qr2oT+yw== dependencies: "@babel/plugin-transform-react-jsx" "^7.1.6" - babel-plugin-emotion "^10.0.5" - babel-plugin-jsx-pragmatic "^1.0.2" + "@emotion/babel-plugin-jsx-pragmatic" "^0.1.0" + babel-plugin-emotion "^10.0.7" object-assign "^4.1.1" "@emotion/hash@0.7.1": @@ -962,10 +976,10 @@ resolved "https://registry.yarnpkg.com/@emotion/memoize/-/memoize-0.7.1.tgz#e93c13942592cf5ef01aa8297444dc192beee52f" integrity sha512-Qv4LTqO11jepd5Qmlp3M1YEjBumoTHcHFdgPTQ+sFlIL5myi/7xu/POwP7IRu6odBdmLXdtIs1D6TuW6kbwbbg== -"@emotion/serialize@^0.11.3": - version "0.11.3" - resolved "https://registry.yarnpkg.com/@emotion/serialize/-/serialize-0.11.3.tgz#c4af2d96e3ddb9a749b7b567daa7556bcae45af2" - integrity sha512-6Q+XH/7kMdHwtylwZvdkOVMydaGZ989axQ56NF7urTR7eiDMLGun//pFUy31ha6QR4C6JB+KJVhZ3AEAJm9Z1g== +"@emotion/serialize@^0.11.4": + version "0.11.4" + resolved "https://registry.yarnpkg.com/@emotion/serialize/-/serialize-0.11.4.tgz#691e615184a23cd3b9ae9b1eaa79eb8798e52379" + integrity sha512-JKmn+Qnc8f6OZKSHmNq1RpO27raIi6Kj0uqBaSOUVMW6NI0M3wLpV4pK5hZO4I+1WuCC39hOBPgQ/GcgoHbDeg== dependencies: "@emotion/hash" "0.7.1" "@emotion/memoize" "0.7.1" @@ -3065,15 +3079,15 @@ babel-plugin-dynamic-import-node@^1.2.0: dependencies: babel-plugin-syntax-dynamic-import "^6.18.0" -babel-plugin-emotion@^10.0.5: - version "10.0.5" - resolved "https://registry.yarnpkg.com/babel-plugin-emotion/-/babel-plugin-emotion-10.0.5.tgz#05ec47cde94f984b0b2aebdd41f81876cf9cbb24" - integrity sha512-ezct2vKACg4juSV0/A/4QIDJu2+5Sjna/8rX/LXY8D0qG8YEP3fu8pe5FqZ9yFGa8WOJ1sivf3/QKM/5C8naIg== +babel-plugin-emotion@^10.0.7: + version "10.0.7" + resolved "https://registry.yarnpkg.com/babel-plugin-emotion/-/babel-plugin-emotion-10.0.7.tgz#3634ada6dee762140f27db07387feaec8d2cb619" + integrity sha512-5PdLJYme3tFN97M3tBbEUS/rJVkS9EMbo7rs7/7BAUEUVMWehm1kb5DEbp16Rs+UsI3rTXRan1iqpL022T8XxA== dependencies: "@babel/helper-module-imports" "^7.0.0" "@emotion/hash" "0.7.1" "@emotion/memoize" "0.7.1" - "@emotion/serialize" "^0.11.3" + "@emotion/serialize" "^0.11.4" babel-plugin-macros "^2.0.0" babel-plugin-syntax-jsx "^6.18.0" convert-source-map "^1.5.0" @@ -3110,13 +3124,6 @@ babel-plugin-jest-hoist@^24.0.0: resolved "https://registry.yarnpkg.com/babel-plugin-jest-hoist/-/babel-plugin-jest-hoist-24.0.0.tgz#3adf030b6fd67e4311479a54b24077bdfc226ec9" integrity sha512-ipefE7YWNyRNVaV/MonUb/I5nef53ZRFR74P9meMGmJxqt8s1BJmfhw11YeIMbcjXN4fxtWUaskZZe8yreXE1Q== -babel-plugin-jsx-pragmatic@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/babel-plugin-jsx-pragmatic/-/babel-plugin-jsx-pragmatic-1.0.2.tgz#41e2beb8642235f34b2a7ab12ca39e07201b8e59" - integrity sha1-QeK+uGQiNfNLKnqxLKOeByAbjlk= - dependencies: - babel-plugin-syntax-jsx "^6.0.0" - babel-plugin-lodash@^3.2.11: version "3.3.4" resolved "https://registry.yarnpkg.com/babel-plugin-lodash/-/babel-plugin-lodash-3.3.4.tgz#4f6844358a1340baed182adbeffa8df9967bc196" @@ -3209,7 +3216,7 @@ babel-plugin-syntax-function-bind@^6.8.0: resolved "http://registry.npmjs.org/babel-plugin-syntax-function-bind/-/babel-plugin-syntax-function-bind-6.13.0.tgz#48c495f177bdf31a981e732f55adc0bdd2601f46" integrity sha1-SMSV8Xe98xqYHnMvVa3AvdJgH0Y= -babel-plugin-syntax-jsx@^6.0.0, babel-plugin-syntax-jsx@^6.18.0, babel-plugin-syntax-jsx@^6.3.13, babel-plugin-syntax-jsx@^6.8.0: +babel-plugin-syntax-jsx@^6.18.0, babel-plugin-syntax-jsx@^6.3.13, babel-plugin-syntax-jsx@^6.8.0: version "6.18.0" resolved "http://registry.npmjs.org/babel-plugin-syntax-jsx/-/babel-plugin-syntax-jsx-6.18.0.tgz#0af32a9a6e13ca7a3fd5069e62d7b0f58d0d8946" integrity sha1-CvMqmm4Tyno/1QaeYtew9Y0NiUY= From a70232d56c5c54fb382643288b175a5a4e1c5ecc Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Fri, 22 Feb 2019 12:33:13 -0600 Subject: [PATCH 2/4] chore: qsdfadsf --- packages/babel-preset-gatsby/src/index.js | 2 +- packages/gatsby/src/commands/build-javascript.js | 5 ++--- packages/gatsby/src/utils/webpack.config.js | 6 +----- packages/gatsby/src/utils/worker.js | 4 ++-- packages/gatsby/src/utils/workers/build-javascript.js | 2 +- packages/gatsby/src/utils/workers/index.js | 2 +- 6 files changed, 8 insertions(+), 13 deletions(-) diff --git a/packages/babel-preset-gatsby/src/index.js b/packages/babel-preset-gatsby/src/index.js index 68db99502d940..7d0acec287ab6 100644 --- a/packages/babel-preset-gatsby/src/index.js +++ b/packages/babel-preset-gatsby/src/index.js @@ -79,4 +79,4 @@ module.exports = function preset(_, options = {}) { ], ], } -} \ No newline at end of file +} diff --git a/packages/gatsby/src/commands/build-javascript.js b/packages/gatsby/src/commands/build-javascript.js index 32f9e0b253c0e..9da9dae066415 100644 --- a/packages/gatsby/src/commands/build-javascript.js +++ b/packages/gatsby/src/commands/build-javascript.js @@ -1,8 +1,8 @@ const webpack = require(`webpack`) const getWebpackConfig = require(`../utils/webpack.config`) -const build = config => { - return new Promise((resolve, reject) => { +const build = config => + new Promise((resolve, reject) => { webpack(config).run((err, stats) => { if (err) { reject(err) @@ -18,7 +18,6 @@ const build = config => { resolve() }) }) -} module.exports = async program => { const compilerConfig = await getWebpackConfig( diff --git a/packages/gatsby/src/utils/webpack.config.js b/packages/gatsby/src/utils/webpack.config.js index 045dfe4ea330a..3a5d74db3c10f 100644 --- a/packages/gatsby/src/utils/webpack.config.js +++ b/packages/gatsby/src/utils/webpack.config.js @@ -20,11 +20,7 @@ const hasLocalEslint = require(`./local-eslint-config-finder`) // 3) build-javascript: Build JS and CSS chunks for production // 4) build-html: build all HTML files -module.exports = async ( - program, - directory, - suppliedStage -) => { +module.exports = async (program, directory, suppliedStage) => { const directoryPath = withBasePath(directory) process.env.GATSBY_BUILD_STAGE = suppliedStage diff --git a/packages/gatsby/src/utils/worker.js b/packages/gatsby/src/utils/worker.js index 8f2a0b68ea19d..ce94f92c7e9b3 100644 --- a/packages/gatsby/src/utils/worker.js +++ b/packages/gatsby/src/utils/worker.js @@ -4,6 +4,6 @@ const numWorkers = require(`physical-cpu-count`) || 1 module.exports = new Worker(require.resolve(`./workers`), { numWorkers, forkOptions: { - silent: false + silent: false, }, -}) \ No newline at end of file +}) diff --git a/packages/gatsby/src/utils/workers/build-javascript.js b/packages/gatsby/src/utils/workers/build-javascript.js index 800a702dd663d..acd340d088968 100644 --- a/packages/gatsby/src/utils/workers/build-javascript.js +++ b/packages/gatsby/src/utils/workers/build-javascript.js @@ -7,7 +7,7 @@ export async function buildJavascript(program) { program, program.directory, `build-javascript` - ) + ) return new Promise((resolve, reject) => { webpack(compilerConfig).run((err, stats) => { diff --git a/packages/gatsby/src/utils/workers/index.js b/packages/gatsby/src/utils/workers/index.js index efd3d684f460d..bd60041538830 100644 --- a/packages/gatsby/src/utils/workers/index.js +++ b/packages/gatsby/src/utils/workers/index.js @@ -1,2 +1,2 @@ exports.renderHTML = require(`./render-html`).renderHTML -exports.buildJavascript = require(`./build-javascript`).buildJavascript \ No newline at end of file +exports.buildJavascript = require(`./build-javascript`).buildJavascript From f32ca1e28f6ddd46916a4ad4fb6d645b0d590b64 Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 5 Mar 2019 09:11:21 -0600 Subject: [PATCH 3/4] chore: revert --- packages/gatsby/src/utils/html-renderer-queue.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/utils/html-renderer-queue.js b/packages/gatsby/src/utils/html-renderer-queue.js index d23731c842bf4..4a40dfa826364 100644 --- a/packages/gatsby/src/utils/html-renderer-queue.js +++ b/packages/gatsby/src/utils/html-renderer-queue.js @@ -1,6 +1,15 @@ const Promise = require(`bluebird`) const convertHrtime = require(`convert-hrtime`) +const Worker = require(`jest-worker`).default const { chunk } = require(`lodash`) +const cpuCoreCount = require(`./cpu-core-count`) + +const workerPool = new Worker(require.resolve(`./worker`), { + numWorkers: cpuCoreCount(true), + forkOptions: { + silent: false, + }, +}) module.exports = (htmlComponentRendererPath, pages, activity) => new Promise((resolve, reject) => { @@ -20,7 +29,7 @@ module.exports = (htmlComponentRendererPath, pages, activity) => segments, pageSegment => new Promise((resolve, reject) => { - worker + workerPool .renderHTML({ htmlComponentRendererPath, paths: pageSegment, From 1dba5f00cf2aeb92516a03737ced1856f44d62df Mon Sep 17 00:00:00 2001 From: Dustin Schau Date: Tue, 5 Mar 2019 10:00:02 -0600 Subject: [PATCH 4/4] chore: get mostly kinda almost working --- packages/babel-preset-gatsby/src/index.js | 2 +- packages/gatsby-cli/src/create-cli.js | 5 ++ packages/gatsby/cache-dir/production-app.js | 72 +++++++++---------- packages/gatsby/cache-dir/static-entry.js | 45 +++++++++--- .../gatsby/src/commands/build-javascript.js | 16 +++++ packages/gatsby/src/commands/build.js | 1 + packages/gatsby/src/utils/webpack.config.js | 12 ++-- 7 files changed, 104 insertions(+), 49 deletions(-) diff --git a/packages/babel-preset-gatsby/src/index.js b/packages/babel-preset-gatsby/src/index.js index 7d0acec287ab6..efdc58dd5d1e2 100644 --- a/packages/babel-preset-gatsby/src/index.js +++ b/packages/babel-preset-gatsby/src/index.js @@ -14,7 +14,7 @@ const loadCachedConfig = () => { } const modernConfig = { - loose: false, + loose: true, targets: { esmodules: true, }, diff --git a/packages/gatsby-cli/src/create-cli.js b/packages/gatsby-cli/src/create-cli.js index 9c87fcf0c4c7a..c88f0b299a7d6 100644 --- a/packages/gatsby-cli/src/create-cli.js +++ b/packages/gatsby-cli/src/create-cli.js @@ -163,6 +163,11 @@ function buildLocalCommands(cli, isLocalSite) { default: false, describe: `Build site with link paths prefixed (set pathPrefix in your gatsby-config.js).`, }) + .option(`legacy`, { + type: `boolean`, + default: false, + describe: `Build site without modern (.mjs) build support`, + }) .option(`no-uglify`, { type: `boolean`, default: false, diff --git a/packages/gatsby/cache-dir/production-app.js b/packages/gatsby/cache-dir/production-app.js index 0e174ab30376e..02a28b494cac5 100644 --- a/packages/gatsby/cache-dir/production-app.js +++ b/packages/gatsby/cache-dir/production-app.js @@ -28,7 +28,7 @@ setApiRunnerForLoader(apiRunner) navigationInit() // Let the site/plugins run code very early. -apiRunnerAsync(`onClientEntry`).then(() => { +apiRunnerAsync(`onClientEntry`).then(async () => { // Let plugins register a service worker. The plugin just needs // to return true. if (apiRunner(`registerServiceWorker`).length > 0) { @@ -83,44 +83,44 @@ apiRunnerAsync(`onClientEntry`).then(() => { ) } - loader.getResourcesForPathname(browserLoc.pathname).then(() => { - const Root = () => - createElement( - Router, - { - basepath: __PATH_PREFIX__, - }, - createElement(RouteHandler, { path: `/*` }) - ) + await loader.getResourcesForPathname(browserLoc.pathname) - const WrappedRoot = apiRunner( - `wrapRootElement`, - { element: }, - , - ({ result }) => { - return { element: result } - } - ).pop() + const Root = () => + createElement( + Router, + { + basepath: __PATH_PREFIX__, + }, + createElement(RouteHandler, { path: `/*` }) + ) + + const WrappedRoot = apiRunner( + `wrapRootElement`, + { element: }, + , + ({ result }) => { + return { element: result } + } + ).pop() - let NewRoot = () => WrappedRoot + let NewRoot = () => WrappedRoot - const renderer = apiRunner( - `replaceHydrateFunction`, - undefined, - ReactDOM.hydrate - )[0] + const renderer = apiRunner( + `replaceHydrateFunction`, + undefined, + ReactDOM.hydrate + )[0] - domReady(() => { - renderer( - , - typeof window !== `undefined` - ? document.getElementById(`___gatsby`) - : void 0, - () => { - postInitialRenderWork() - apiRunner(`onInitialClientRender`) - } - ) - }) + domReady(() => { + renderer( + , + typeof window !== `undefined` + ? document.getElementById(`___gatsby`) + : void 0, + () => { + postInitialRenderWork() + apiRunner(`onInitialClientRender`) + } + ) }) }) diff --git a/packages/gatsby/cache-dir/static-entry.js b/packages/gatsby/cache-dir/static-entry.js index 3e0cb708ba184..04f932a0ef9cf 100644 --- a/packages/gatsby/cache-dir/static-entry.js +++ b/packages/gatsby/cache-dir/static-entry.js @@ -356,14 +356,43 @@ export default (pagePath, callback) => { // Filter out prefetched bundles as adding them as a script tag // would force high priority fetching. const bodyScripts = scripts - .filter(s => s.rel !== `prefetch`) - .map(s => { - const scriptPath = `${__PATH_PREFIX__}/${JSON.stringify(s.name).slice( - 1, - -1 - )}` - return