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): enable modern builds for gatsby #14289

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 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
12 changes: 12 additions & 0 deletions e2e-tests/modern-builds/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Project dependencies
.cache
node_modules
yarn-error.log

# Build directory
/public
.DS_Store

# Cypress output
cypress/videos/
cypress/screenshots/
3 changes: 3 additions & 0 deletions e2e-tests/modern-builds/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# production-runtime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# production-runtime
# modern-builds


A Gatsby project to test our production runtime for Themes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A Gatsby project to test our production runtime for Themes
A Gatsby project to test out modern builds

4 changes: 4 additions & 0 deletions e2e-tests/modern-builds/cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"baseUrl": "http://localhost:9000",
"chromeWebSecurity": false
}
5 changes: 5 additions & 0 deletions e2e-tests/modern-builds/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File can be deleted since it isn't used

"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
9 changes: 9 additions & 0 deletions e2e-tests/modern-builds/cypress/integration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* global cy */

describe(`Preload`, () => {
it(`preloads on first visit`, () => {
cy.visit(`/`).waitForRouteChange()
})

it(`preloads Gatsby Link tags`, () => {})
})
17 changes: 17 additions & 0 deletions e2e-tests/modern-builds/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ***********************************************************
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably get rid of this folder and its contents as well

// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
}
1 change: 1 addition & 0 deletions e2e-tests/modern-builds/cypress/support/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "gatsby-cypress"
24 changes: 24 additions & 0 deletions e2e-tests/modern-builds/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if (typeof window !== `undefined`) {
window.___PageComponentLifecycleCallsLog = []
}

const addLogEntry = (action, location) => {
const idElement = document.querySelector(`[data-testid="dom-marker"]`)
window.___PageComponentLifecycleCallsLog.push({
action,
pathname: location.pathname,
domContent: idElement ? idElement.innerText : null,
})
}

exports.onPreRouteUpdate = ({ location }) => {
addLogEntry(`onPreRouteUpdate`, location)
}

exports.onRouteUpdate = ({ location }) => {
addLogEntry(`onRouteUpdate`, location)
}

exports.onPrefetchPathname = ({ pathname }) => {
addLogEntry(`onPrefetchPathname`, pathname)
}
8 changes: 8 additions & 0 deletions e2e-tests/modern-builds/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
siteMetadata: {
title: `Modern Builds are awesome`,
description: `Testing modern builds, yeah!`,
author: `Ward Peeters`,
},
plugins: [],
}
34 changes: 34 additions & 0 deletions e2e-tests/modern-builds/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "production-runtime",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"name": "production-runtime",
"name": "modern-builds",

"description": "Gatsby default starter",
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
"version": "1.0.0",
"author": "Sidhartha Chatterjee <sid@gatsbyjs.com>",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"author": "Sidhartha Chatterjee <sid@gatsbyjs.com>",
"author": "Ward Peeters <ward@gatsbyjs.com>",

"dependencies": {
"gatsby": "^2.13.14",
"react": "^16.8.0",
"react-dom": "^16.8.0"
},
"license": "MIT",
"scripts": {
"build": "cross-env ENABLE_MODERN_BUILDS=1 gatsby build",
"develop": "cross-env ENABLE_MODERN_BUILDS=1 gatsby develop",
"format": "prettier --write \"src/**/*.js\"",
"serve": "gatsby serve",
"start": "npm run develop",
"test": "cross-env CYPRESS_SUPPORT=y npm run build && npm run start-server-and-test",
"start-server-and-test": "start-server-and-test serve http://localhost:9000 cy:run",
"cy:open": "cypress open",
"cy:run": "cypress run --browser chrome"
},
"devDependencies": {
"cross-env": "^5.2.0",
"cypress": "^3.1.3",
"gatsby-cypress": "^0.1.7",
"prettier": "^1.15.2",
"start-server-and-test": "^1.7.11"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
wardpeet marked this conversation as resolved.
Show resolved Hide resolved
}
}
22 changes: 22 additions & 0 deletions e2e-tests/modern-builds/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react"
import { graphql, Link } from "gatsby"

export default ({ data }) => (
<>
<p data-testid="title">{data.site.siteMetadata.title}</p>
<p data-testid="author">{data.site.siteMetadata.author}</p>

<Link to="/page-2">Go to page 2</Link>
</>
)

export const pageQuery = graphql`
query {
site {
siteMetadata {
title
author
}
}
}
`
3 changes: 3 additions & 0 deletions e2e-tests/modern-builds/src/pages/page-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from "react"

export default ({ data }) => <p>Page 2</p>
43 changes: 30 additions & 13 deletions packages/babel-preset-gatsby/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,9 @@ it(`Specifies proper presets and plugins for test stage`, () => {
})

it(`Specifies proper presets and plugins for build-html stage`, () => {
const currentGatsbyBuildStage = process.env.GATSBY_BUILD_STAGE
let presets, plugins
try {
process.env.GATSBY_BUILD_STAGE = `build-html`
const config = preset()
presets = config.presets
plugins = config.plugins
} finally {
process.env.GATSBY_BUILD_STAGE = currentGatsbyBuildStage
}
const config = preset(null, { stage: `build-html` })
const presets = config.presets
const plugins = config.plugins

expect(presets).toEqual([
[
Expand Down Expand Up @@ -131,6 +124,7 @@ it(`Specifies proper presets and plugins for build-html stage`, () => {
it(`Allows to configure browser targets`, () => {
const targets = `last 1 version`
const { presets } = preset(null, {
stage: `build-javascript`,
targets,
})

Expand All @@ -147,11 +141,34 @@ it(`Allows to configure browser targets`, () => {
])
})

it(`Allows to configure modern builds`, () => {
const targets = `last 1 version`
const { presets } = preset(null, {
stage: `build-javascript`,
targets,
modern: true,
})

expect(presets[0]).toEqual([
expect.stringContaining(path.join(`@babel`, `preset-env`)),
{
corejs: 2,
exclude: [`transform-typeof-symbol`],
loose: true,
modules: false,
useBuiltIns: `usage`,
targets: {
esmodules: true,
},
},
])
})

describe(`in production mode`, () => {
it(`adds babel-plugin-transform-react-remove-prop-types`, () => {
process.env.GATSBY_BUILD_STAGE = `build-javascript`

const { plugins } = preset()
const { plugins } = preset(null, {
stage: `build-javascript`,
})

expect(plugins).toEqual(
expect.arrayContaining([
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = function preset(_, options = {}) {
let { targets = null } = options

const pluginBabelConfig = loadCachedConfig()
const stage = process.env.GATSBY_BUILD_STAGE || `test`
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`
const absoluteRuntimePath = path.dirname(
require.resolve(`@babel/runtime/package.json`)
)
Expand All @@ -45,6 +45,13 @@ module.exports = function preset(_, options = {}) {
}
}

// when options.modern is set we overwrite targets to contain esmodules = true
if (options.modern) {
targets = {
esmodules: true,
}
}

return {
presets: [
[
Expand Down
23 changes: 21 additions & 2 deletions packages/gatsby/cache-dir/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ export class BaseLoader {
this.loadPage(rawPath)
}

// if (resourceName.slice(0, 12) === `component---`) {
// let componentUrls = createComponentUrls(resourceName)
// if (process.env.MODERN_BUILD) {
// componentUrls = componentUrls.filter(resource =>
// resource.endsWith(`.mjs`)
// )
// }
// return Promise.all(componentUrls.map(url => prefetchHelper(url)))
// } else {
// const url = createJsonURL(jsonDataPaths[resourceName])
// return prefetchHelper(url)

getResourceURLsForPathname(rawPath) {
const pagePath = cleanPath(rawPath)
const page = this.pageDataDb.get(pagePath)
Expand All @@ -320,11 +332,18 @@ export class BaseLoader {
}
}

const createComponentUrls = componentChunkName =>
window.___chunkMapping[componentChunkName].map(
const createComponentUrls = componentChunkName => {
let componentUrls = window.___chunkMapping[componentChunkName].map(
chunk => __PATH_PREFIX__ + chunk
)

if (process.env.MODERN_BUILD) {
componentUrls = componentUrls.filter(resource => resource.endsWith(`.mjs`))
}

return componentUrls
}

export class ProdLoader extends BaseLoader {
constructor(asyncRequires, matchPaths) {
const loadComponent = chunkName =>
Expand Down
Loading