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

configurable output paths #12501

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fc340fc
configuration cache path
mathieudutour Mar 11, 2019
f145279
make public page configurable
mathieudutour Mar 11, 2019
b7f586f
fix lint
mathieudutour Mar 11, 2019
d7656ae
fix tests
mathieudutour Mar 11, 2019
2e6d25d
Merge branch 'master' into f/configurable-output-paths
mathieudutour Mar 11, 2019
8b65700
fix broken merge
mathieudutour Mar 11, 2019
6533139
fix tests after merge
mathieudutour Mar 11, 2019
7cfcf08
try to use preval
mathieudutour Mar 12, 2019
bd4b7a9
fix tests
mathieudutour Mar 12, 2019
1107112
Merge branch 'master' into f/configurable-output-paths
mathieudutour Mar 12, 2019
06ce2f5
fix typo
mathieudutour Mar 12, 2019
f8495d9
use webpack alias instead of preval
mathieudutour Mar 12, 2019
6fc6f9e
more consistent naming
mathieudutour Mar 12, 2019
9469466
fix typo
mathieudutour Mar 12, 2019
2802e4a
fix tests on windows
mathieudutour Mar 12, 2019
ea6e4c5
fix conflicts
mathieudutour Mar 12, 2019
4edce60
fix tests on windows
mathieudutour Mar 12, 2019
bc07fe7
add some docs
mathieudutour Mar 12, 2019
0e24bd8
pass the cache path as an option
mathieudutour Mar 12, 2019
9eb7c3d
Merge branch 'master' into f/configurable-output-paths
mathieudutour Mar 12, 2019
2f6c4dc
consistent path.join format
mathieudutour Mar 12, 2019
1142dc3
fix test on windows
mathieudutour Mar 12, 2019
09f6aa5
somehow fix windows tests
mathieudutour Mar 12, 2019
6fdf8c1
use cache if available
mathieudutour Mar 12, 2019
802540f
need to force delete because the public path might not be in cwd anymore
mathieudutour Mar 15, 2019
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
8 changes: 4 additions & 4 deletions benchmarks/query/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ function allTypeQuery(typeName) {
}

// Create template in .cache for the received type
function createTemplateFile(typeName) {
function createTemplateFile(cache, typeName) {
const templateSrc = createPageTemplateJs(typeName)
const templateFilename = `./.cache/${typeName}Template.js`
const templateFilename = cache.rootPath(`./${typeName}Template.js`)
fs.writeFileSync(templateFilename, templateSrc)
return templateFilename
}
Expand All @@ -99,10 +99,10 @@ async function createNode(graphql, typeName) {
}

// Create page for each type
exports.createPages = async ({ actions, graphql }) => {
exports.createPages = async ({ actions, graphql, cache }) => {
for (let i = 0; i < types.length; i++) {
const typeName = types[i]
const templateFilename = createTemplateFile(typeName)
const templateFilename = createTemplateFile(cache, typeName)
const edges = await createNode(graphql, typeName)
_.forEach(edges, ({ node }) => {
actions.createPage({
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/building-with-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ import favicon from "./favicon.png"
let inlinedStyles = ""
if (process.env.NODE_ENV === "production") {
try {
inlinedStyles = require("!raw-loader!../public/styles.css")
inlinedStyles = require("!raw-loader!gatsby-public-dir/styles.css")
} catch (e) {
console.log(e)
}
Expand Down
7 changes: 7 additions & 0 deletions docs/docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ If set to true, this will expose a `/__refresh` webhook that is able to receive

You can trigger this endpoint locally for example on Unix-based operating systems (like Ubuntu and MacOS) you can use `curl -X POST http://localhost:8000/__refresh`.

Gatsby also allows you to use environment variables instead of using command line options for:

- `--build-dir`: `GATSBY_BUILD_DIR`
- `--cache-dir`: `GATSBY_CACHE_DIR`

The command line options will take precedence over the environment variables.

## Additional Environments (Staging, Test, etc)

As noted above `NODE_ENV` is a reserved environment variable in Gatsby as it is needed by the build system to make key optimizations when compiling React and other modules. For this reason it is necessary to make use of a secondary environment variable for additional environment support, and manually make the environment variables available to the client-side code.
Expand Down
18 changes: 12 additions & 6 deletions docs/docs/gatsby-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ Once you've installed a Gatsby site, go to the root directory of your project an

#### Options

| Option | Description |
| :-------------: | ----------------------------------------------- |
| `-H`, `--host` | Set host. Defaults to localhost |
| `-p`, `--port` | Set port. Defaults to 8000 |
| `-o`, `--open` | Open the site in your (default) browser for you |
| `-S`, `--https` | Use HTTPS |
| Option | Description |
| :-------------: | ---------------------------------------------------- |
| `-H`, `--host` | Set host. Defaults to localhost |
| `-p`, `--port` | Set port. Defaults to 8000 |
| `-o`, `--open` | Open the site in your (default) browser for you |
| `-S`, `--https` | Use HTTPS |
| `--build-dir` | Set the build directory location. Defaults to public |
| `--cache-dir` | Set the cache directory location. Defaults to .cache |

Follow the [Local HTTPS guide](/docs/local-https/)
to find out how you can set up an HTTPS development server using Gatsby.
Expand All @@ -70,6 +72,8 @@ At the root of a Gatsby site, compile your application and make it ready for dep
| `--prefix-paths` | Build site with link paths prefixed (set pathPrefix in your config) |
| `--no-uglify` | Build site without uglifying JS bundles (for debugging) |
| `--open-tracing-config-file` | Tracer configuration file (open tracing compatible). See /docs/performance-tracing/ |
| `--build-dir` | Set the build directory location. Defaults to public |
| `--cache-dir` | Set the cache directory location. Defaults to .cache |

### `serve`

Expand All @@ -85,6 +89,8 @@ At the root of a Gatsby site, serve the production build of your site for testin
| `-p`, `--port` | Set port. Defaults to 9000 |
| `-o`, `--open` | Open the site in your (default) browser for you |
| `--prefix-paths` | Serve site with link paths prefixed (if built with pathPrefix in your gatsby-config.js). |
| `--build-dir` | Set the build directory location. Defaults to public |
| `--cache-dir` | Set the cache directory location. Defaults to .cache |

### `info`

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/production-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ The first thing our app does is run the [onClientEntry](/docs/browser-apis/#onCl

It's worth noting that the browser API runner is completely different to `api-runner-node` which is explained in [How APIs/Plugins Are Run](/docs/how-plugins-apis-are-run/). `api-runner-node` runs in Node.js and has to deal with complex server based execution paths. Whereas running APIs on the browser is simply a matter of iterating through the site's registered browser plugins and running them one after the other (see [api-runner-browser.js](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/cache-dir/api-runner-browser.js#L9)).

One thing to note is that it gets the list of plugins from `./cache/api-runner-browser-plugins.js`, which is generated [early in bootstrap](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/bootstrap/index.js#L338).
One thing to note is that it gets the list of plugins from `./.cache/api-runner-browser-plugins.js`, which is generated [early in bootstrap](https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/bootstrap/index.js#L338).

### DOM Hydration

Expand Down
8 changes: 2 additions & 6 deletions examples/styleguide/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const path = require(`path`)
const fs = require(`fs`)
const appRootDir = require(`app-root-dir`).get()

const componentPageTemplate = path.resolve(
`src/templates/ComponentPage/index.js`
)
const tableOfContentsTemplate = path.resolve(`src/templates/TOC/index.js`)

exports.createPages = ({ graphql, actions }) => {
exports.createPages = ({ graphql, actions, cache }) => {
const { createPage } = actions

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -83,10 +82,7 @@ exports.createPages = ({ graphql, actions }) => {
}, [])
.join(`\n`) + `\n`

fs.writeFileSync(
path.join(appRootDir, `.cache/components.js`),
exportFileContents
)
fs.writeFileSync(cache.rootPath(`components.js`), exportFileContents)

allComponents.forEach(data => {
const { filePath } = data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react"
import PropTypes from "prop-types"
import { LiveProvider, LiveEditor, LiveError, LivePreview } from "react-live"

import * as components from "../../../../../.cache/components"
import * as components from "gatsby-cache-dir/components"

import "./prism-theme.css"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ it(`Transforms queries in useStaticQuery`, () => {

export default () => {
const siteTitle = useStaticQuery(graphql\`{site { siteMetadata { title }}}\`)

return (
<h1>{siteTitle.site.siteMetadata.title}</h1>
)
Expand Down Expand Up @@ -79,7 +79,7 @@ it(`Transforms queries defined in own variable in useStaticQuery`, () => {
export default () => {
const query = graphql\`{site { siteMetadata { title }}}\`
const siteTitle = useStaticQuery(query)

return (
<h1>{siteTitle.site.siteMetadata.title}</h1>
)
Expand All @@ -95,7 +95,7 @@ it(`Transforms queries and preserves destructuring in useStaticQuery`, () => {
export default () => {
const query = graphql\`{site { siteMetadata { title }}}\`
const { site } = useStaticQuery(query)

return (
<h1>{site.siteMetadata.title}</h1>
)
Expand All @@ -111,7 +111,7 @@ it(`Transforms queries and preserves variable type in useStaticQuery`, () => {
export default () => {
const query = graphql\`{site { siteMetadata { title }}}\`
let { site } = useStaticQuery(query)

return (
<h1>{site.siteMetadata.title}</h1>
)
Expand Down Expand Up @@ -142,18 +142,18 @@ it(`Transforms only the call expression in useStaticQuery`, () => {
matchesSnapshot(`
import React from "react"
import { graphql, useStaticQuery } from "gatsby"

const useSiteMetadata = () => {
return useStaticQuery(
graphql\`{site { siteMetadata { title }}}\`
).site.siteMetadata
}

export default () => {
const siteMetadata = useSiteMetadata()

return <h1>{siteMetadata.title}</h1>
}
}
`)
})

Expand All @@ -165,7 +165,7 @@ it(`Only runs transforms if useStaticQuery is imported from gatsby`, () => {
export default () => {
const query = graphql\`{site { siteMetadata { title }}}\`
const siteTitle = useStaticQuery(query)

return (
<h1>{siteTitle.site.siteMetadata.title}</h1>
)
Expand Down
20 changes: 14 additions & 6 deletions packages/babel-plugin-remove-graphql-queries/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ export default function({ types: t }) {
) {
const identifier = t.identifier(`staticQueryData`)
const filename = state.file.opts.filename
const shortResultPath = `public/static/d/${this.queryHash}.json`
const resultPath = nodePath.join(process.cwd(), shortResultPath)
const shortResultPath = `static/d/${this.queryHash}.json`
const resultPath = nodePath.join(
(state.opts || {}).publicPath ||
nodePath.join(process.cwd(), `public`),
shortResultPath
)
// Add query
path2.parent.attributes.push(
t.jSXAttribute(
Expand All @@ -162,7 +166,7 @@ export default function({ types: t }) {
nodePath.parse(filename).dir,
resultPath
)
: shortResultPath
: `public/${shortResultPath}`
)
)
path.unshiftContainer(`body`, importDeclaration)
Expand All @@ -179,8 +183,12 @@ export default function({ types: t }) {
) {
const identifier = t.identifier(`staticQueryData`)
const filename = state.file.opts.filename
const shortResultPath = `public/static/d/${this.queryHash}.json`
const resultPath = nodePath.join(process.cwd(), shortResultPath)
const shortResultPath = `static/d/${this.queryHash}.json`
const resultPath = nodePath.join(
(state.opts || {}).publicPath ||
nodePath.join(process.cwd(), `public`),
shortResultPath
)

// Remove query variable since it is useless now
if (this.templatePath.parentPath.isVariableDeclarator()) {
Expand Down Expand Up @@ -211,7 +219,7 @@ export default function({ types: t }) {
nodePath.parse(filename).dir,
resultPath
)
: shortResultPath
: `public/${shortResultPath}`
)
)
path.unshiftContainer(`body`, importDeclaration)
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-preset-gatsby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ npm install --dev babel-preset-gatsby
`{ [string]: number | string }`, defaults to `{ "browsers": ["last 4 versions", "safari >= 7", "ie >= 9"] }` in production and `{ "browsers": ["last 2 versions", "not ie <= 11", "not android 4.4.3"] }` in development when targeting the browser and `{ "node": 6 }` in production and `{ "node": "current" }` in development when targeting Node.js.

Use this option to configure [custom target browsers](https://www.gatsbyjs.org/docs/babel/).

### `cachePath`

`string`, defaults to `path.join(process.cwd(), '.cache')`.

Use this option to configure cache location.
10 changes: 5 additions & 5 deletions packages/babel-preset-gatsby/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const path = require(`path`)

const resolve = m => require.resolve(m)

const loadCachedConfig = () => {
const loadCachedConfig = cachePath => {
let pluginBabelConfig = {}
if (process.env.NODE_ENV !== `test`) {
try {
pluginBabelConfig = require(path.join(
process.cwd(),
`./.cache/babelState.json`
cachePath || path.join(process.cwd(), `./.cache`),
mathieudutour marked this conversation as resolved.
Show resolved Hide resolved
`babelState.json`
))
} catch (err) {
if (err.message.includes(`Cannot find module`)) {
Expand All @@ -25,9 +25,9 @@ const loadCachedConfig = () => {
}

module.exports = function preset(_, options = {}) {
let { targets = null } = options
let { targets = null, cachePath } = options

const pluginBabelConfig = loadCachedConfig()
const pluginBabelConfig = loadCachedConfig(cachePath)
const stage = process.env.GATSBY_BUILD_STAGE || `test`

if (!targets) {
Expand Down
42 changes: 41 additions & 1 deletion packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const handlerP = fn => (...args) => {

function buildLocalCommands(cli, isLocalSite) {
const defaultHost = `localhost`
const defaultCacheDir = `.cache`
const defaultBuildDir = `public`
const directory = path.resolve(`.`)

// 'not dead' query not available in browserslist used in Gatsby v1
Expand Down Expand Up @@ -142,10 +144,22 @@ function buildLocalCommands(cli, isLocalSite) {
.option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
})
.option(`cache-dir`, {
type: `string`,
default: defaultCacheDir,
describe: `Set cache location. Defaults to ${defaultCacheDir}`,
})
.option(`build-dir`, {
type: `string`,
default: defaultBuildDir,
describe: `Set build directory location. Defaults to ${defaultBuildDir}`,
}),
handler: handlerP(
getCommandHandler(`develop`, (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`
process.env.GATSBY_CACHE_DIR = args.cacheDir
process.env.GATSBY_BUILD_DIR = args.buildDir
cmd(args)
// Return an empty promise to prevent handlerP from exiting early.
// The development server shouldn't ever exit until the user directly
Expand All @@ -172,10 +186,22 @@ function buildLocalCommands(cli, isLocalSite) {
.option(`open-tracing-config-file`, {
type: `string`,
describe: `Tracer configuration file (open tracing compatible). See https://www.gatsbyjs.org/docs/performance-tracing/`,
})
.option(`cache-dir`, {
type: `string`,
default: defaultCacheDir,
describe: `Set cache location. Defaults to ${defaultCacheDir}`,
})
.option(`build-dir`, {
type: `string`,
default: defaultBuildDir,
describe: `Set build directory location. Defaults to ${defaultBuildDir}`,
}),
handler: handlerP(
getCommandHandler(`build`, (args, cmd) => {
process.env.NODE_ENV = `production`
process.env.GATSBY_CACHE_DIR = args.cacheDir
process.env.GATSBY_BUILD_DIR = args.buildDir
return cmd(args)
})
),
Expand Down Expand Up @@ -206,9 +232,23 @@ function buildLocalCommands(cli, isLocalSite) {
type: `boolean`,
default: false,
describe: `Serve site with link paths prefixed (if built with pathPrefix in your gatsby-config.js).`,
})
.option(`cache-dir`, {
type: `string`,
default: defaultCacheDir,
describe: `Set cache location. Defaults to ${defaultCacheDir}`,
})
.option(`build-dir`, {
type: `string`,
default: defaultBuildDir,
describe: `Set build directory location. Defaults to ${defaultBuildDir}`,
}),

handler: getCommandHandler(`serve`),
handler: getCommandHandler(`serve`, (args, cmd) => {
process.env.GATSBY_CACHE_DIR = args.cacheDir
process.env.GATSBY_BUILD_DIR = args.buildDir
return cmd(args)
}),
})

cli.command({
Expand Down
10 changes: 8 additions & 2 deletions packages/gatsby-plugin-feed/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ global.Date = jest.fn(() => DATE_TO_USE)
global.Date.UTC = _Date.UTC
global.Date.now = _Date.now

const cache = {
publicPath(filePath) {
return path.join(`public`, filePath)
},
}

describe(`Test plugin feed`, async () => {
fs.existsSync = jest.fn()
fs.existsSync.mockReturnValue(true)
Expand Down Expand Up @@ -40,7 +46,7 @@ describe(`Test plugin feed`, async () => {
},
},
})
await onPostBuild({ graphql }, {})
await onPostBuild({ graphql, cache }, {})
const [filePath, contents] = internals.writeFile.mock.calls[0]
expect(filePath).toEqual(path.join(`public`, `rss.xml`))
expect(contents).toMatchSnapshot()
Expand Down Expand Up @@ -113,7 +119,7 @@ describe(`Test plugin feed`, async () => {
},
],
}
await onPostBuild({ graphql }, options)
await onPostBuild({ graphql, cache }, options)
const [filePath, contents] = internals.writeFile.mock.calls[0]
expect(filePath).toEqual(path.join(`public`, `rss_new.xml`))
expect(contents).toMatchSnapshot()
Expand Down
Loading