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

Topics/output dir #4756

Merged
merged 7 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion docs/docs/building-with-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ import React from "react";
import favicon from "./favicon.png";

let inlinedStyles = "";
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === "production") {
try {
inlinedStyles = require("!raw-loader!../public/styles.css");
inlinedStyles = require(`!raw-loader!../${buildDirectory}/styles.css`);
} catch (e) {
console.log(e);
}
Expand Down
3 changes: 2 additions & 1 deletion examples/client-only-paths/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { TypographyStyle } from "react-typography"
import typography from "./utils/typography"

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-postcss-sass/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { TypographyStyle } from "react-typography"
const typography = new Typography()

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-remark/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { TypographyStyle } from "react-typography"
import typography from "./utils/typography"

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-sass/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { TypographyStyle } from "react-typography"
const typography = new Typography()

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-styled-components/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import typography from "./utils/typography"
let stylesStr
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-styletron/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { TypographyStyle } from "react-typography"
import typography from "./utils/typography"

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-stylus/src/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { TypographyStyle } from "react-typography"
const typography = new Typography()

let stylesStr
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
stylesStr = require(`!raw-loader!../${buildDirectory}/styles.css`)
} catch (e) {
console.log(e)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/using-typescript/src/html.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import * as React from "react";

// Load production style
let styles: string;
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (process.env.NODE_ENV === `production`) {
try {
styles = require("!raw-loader!../public/styles.css");
styles = require(`!raw-loader!../${buildDirectory}/styles.css`);
} catch (err) {
console.log(err);
}
Expand Down
3 changes: 2 additions & 1 deletion infrastructure/build-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ const Main = async () => {
updateBuild(buildId, "FAILURE")
process.exit(code)
}
const publicDir = `${pathToSite}/public`
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
const publicDir = `${pathToSite}/${buildDirectory}`
console.log(`uploading files from ${publicDir}`)

// 1. Push built files to s3 bucket
Expand Down
23 changes: 23 additions & 0 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,22 @@ function buildLocalCommands(cli, isLocalSite) {
alias: `https`,
type: `boolean`,
describe: `Use HTTPS. See https://www.gatsbyjs.org/docs/local-https/ for an initial setup guide`,
})
.option(`build-dir`, {
alias: `buildDirectory`,
type: `string`,
default: `public`,
describe: `Set build directory. Defaults to public`,
}),
handler: handlerP(
getCommandHandler(`develop`, (args, cmd) => {
process.env.NODE_ENV = process.env.NODE_ENV || `development`

process.env.GATSBY_BUILD_DIR = path.resolve(args.buildDirectory) || `public`
Copy link

Choose a reason for hiding this comment

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

Don't you want to respect what someone might have set in GATSBY_BUILD_DIR?

Seems like you could do this by making the default for the option be process.env.GATSBY_BUILD_DIR || 'public'.

Then buildDirectory would resolve to 'custom' here:

GATSBY_BUILD_DIR=custom gatsby develop

And 'overridden' here:

GATSBY_BUILD_DIR=custom gatsby develop --build-dir=overridden

And 'public' here:

gatsby develop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tschaub Hmm maybe I didn't get what you want to say but if someone set his env to

GATSBY_BUILD_DIR = my_folder_custom

and start gatsby with

gatsby build

or

gatsby develop

the folder that would be considered would be my_custom_folder because the only logic that could override GATSBY_BUILD_DIR would be into the --build-dir= arg

Copy link
Contributor

Choose a reason for hiding this comment

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

@Calderis this line seems to set process.env.GATSBY_BUILD_DIR without checking the value of GATSBY_BUILD_DIR first, should it be this instead?

process.env.GATSBY_BUILD_DIR = process.env.GATSBY_BUILD_DIR || path.resolve(args.buildDirectory)  || `public`

if (!fs.existsSync(process.env.GATSBY_BUILD_DIR)) {
fs.mkdirSync(process.env.GATSBY_BUILD_DIR)
}

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 @@ -128,10 +140,21 @@ function buildLocalCommands(cli, isLocalSite) {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
}).option(`build-dir`, {
alias: `buildDirectory`,
type: `string`,
default: `public`,
describe: `Set build directory. Defaults to public`,
}),
handler: handlerP(
getCommandHandler(`build`, (args, cmd) => {
process.env.NODE_ENV = `production`

process.env.GATSBY_BUILD_DIR = path.resolve(args.buildDirectory) || `public`
if (!fs.existsSync(process.env.GATSBY_BUILD_DIR)) {
fs.mkdirSync(process.env.GATSBY_BUILD_DIR)
}

return cmd(args)
})
),
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-feed/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import RSS from "rss"
import merge from "lodash.merge"
import { defaultOptions, runQuery, writeFile } from "./internals"

const publicPath = `./public`
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
const publicPath = `./${buildDirectory}`

// A default function to transform query data into feed entries.
const serialize = ({ query: { site, allMarkdownRemark } }) =>
Expand Down
7 changes: 4 additions & 3 deletions packages/gatsby-plugin-manifest/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const path = require(`path`)
const Promise = require(`bluebird`)
const sharp = require(`sharp`)
const defaultIcons = require(`./common.js`).defaultIcons
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`

sharp.simd(true)

function generateIcons(icons, srcIcon) {
return Promise.map(icons, icon => {
const size = parseInt(icon.sizes.substring(0, icon.sizes.lastIndexOf(`x`)))
const imgPath = path.join(`public`, icon.src)
const imgPath = path.join(buildDirectory, icon.src)

return sharp(srcIcon)
.resize(size)
Expand All @@ -34,7 +35,7 @@ exports.onPostBuild = (args, pluginOptions) =>

// Determine destination path for icons.
const iconPath = path.join(
`public`,
buildDirectory,
manifest.icons[0].src.substring(0, manifest.icons[0].src.lastIndexOf(`/`))
)

Expand All @@ -44,7 +45,7 @@ exports.onPostBuild = (args, pluginOptions) =>
}

fs.writeFileSync(
path.join(`public`, `manifest.json`),
path.join(buildDirectory, `manifest.json`),
JSON.stringify(manifest)
)

Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ An example:
Link paths are specially handed by this plugin. Since most files are processed
and cache-busted through Gatsby (with a file hash), the plugin will transform
any base file names to the hashed variants. If the file is not hashed, it will
ensure the path is valid relative to the output `public` folder. You should be
ensure the path is valid relative to the build `public` folder. You should be
able to reference assets imported through javascript in the `static` folder.

Do not specify the public path in the config, as the plugin will provide it for
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-netlify/src/plugin-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ function applyLayouts(pages, layouts) {
// hashed filenames and ensure we pull in the componentChunkName and layoutComponentChunkName.
export default function makePluginData(store, assetsManifest, pathPrefix) {
const { program, layouts, pages: storePages } = store.getState()
const publicFolder = buildPrefixer(program.directory, `public`)
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
const publicFolder = buildPrefixer(program.directory, buildDirectory)
const stats = require(publicFolder(`stats.json`))
const chunkManifest = normalizeStats(stats)
const pages = applyLayouts(storePages, layouts)
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby-plugin-offline/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports.createPages = ({ boundActionCreators }) => {
}

exports.onPostBuild = (args, pluginOptions) => {
const rootDir = `public`
const rootDir = process.env.GATSBY_BUILD_DIR || `public`

const options = {
staticFileGlobs: [
Expand All @@ -27,7 +27,7 @@ exports.onPostBuild = (args, pluginOptions) => {
],
stripPrefix: rootDir,
// If `pathPrefix` is configured by user, we should replace
// the `public` prefix with `pathPrefix`.
// the build directory (default: `public`) prefix with `pathPrefix`.
// See more at:
// https://github.com/GoogleChrome/sw-precache#replaceprefix-string
replacePrefix: args.pathPrefix || ``,
Expand Down Expand Up @@ -56,5 +56,5 @@ exports.onPostBuild = (args, pluginOptions) => {

const combinedOptions = _.defaults(pluginOptions, options)

return precache.write(`public/sw.js`, combinedOptions)
return precache.write(`${rootDir}/sw.js`, combinedOptions)
}
15 changes: 8 additions & 7 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ const processFile = (file, jobs, cb, reporter) => {
],
})
.then(imageminBuffer => {
fs.writeFile(job.outputPath, imageminBuffer, onFinish)
fs.writeFile(job.buildPath, imageminBuffer, onFinish)
})
.catch(onFinish)
)
Expand All @@ -191,14 +191,14 @@ const processFile = (file, jobs, cb, reporter) => {
plugins: [imageminWebp({ quality: args.quality })],
})
.then(imageminBuffer => {
fs.writeFile(job.outputPath, imageminBuffer, onFinish)
fs.writeFile(job.buildPath, imageminBuffer, onFinish)
})
.catch(onFinish)
)
.catch(onFinish)
// any other format (jpeg, tiff) - don't compress it just handle output
} else {
clonedPipeline.toFile(job.outputPath, onFinish)
clonedPipeline.toFile(job.buildPath, onFinish)
}
})
}
Expand All @@ -210,7 +210,7 @@ const q = queue((task, callback) => {

const queueJob = (job, reporter) => {
const inputFileKey = job.file.absolutePath.replace(/\./g, `%2E`)
const outputFileKey = job.outputPath.replace(/\./g, `%2E`)
const outputFileKey = job.buildPath.replace(/\./g, `%2E`)
const jobPath = `${inputFileKey}.${outputFileKey}`

// Check if the job has already been queued. If it has, there's nothing
Expand All @@ -220,7 +220,7 @@ const queueJob = (job, reporter) => {
}

// Check if the output file already exists so we don't redo work.
if (fs.existsSync(job.outputPath)) {
if (fs.existsSync(job.buildPath)) {
return
}

Expand Down Expand Up @@ -304,7 +304,8 @@ function queueImageResizing({ file, args = {}, reporter }) {
const imgSrc = `/${file.name}-${
file.internal.contentDigest
}-${argsDigestShort}.${fileExtension}`
const filePath = path.join(process.cwd(), `public`, `static`, imgSrc)
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
const filePath = path.join(process.cwd(), buildDirectory, `static`, imgSrc)

// Create function to call when the image is finished.
let outsideResolve, outsideReject
Expand Down Expand Up @@ -342,7 +343,7 @@ function queueImageResizing({ file, args = {}, reporter }) {
outsideResolve,
outsideReject,
inputPath: file.absolutePath,
outputPath: filePath,
buildPath: filePath,
}

queueJob(job, reporter)
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-sitemap/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import path from "path"
import sitemap from "sitemap"
import { defaultOptions, runQuery, writeFile } from "./internals"

const publicPath = `./public`
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
const publicPath = `./${buildDirectory}`

exports.onPostBuild = async ({ graphql, pathPrefix }, pluginOptions) => {
const options = { ...pluginOptions }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ describe(`gatsby-remark-copy-linked-files`, () => {

describe(`options.destinationDir`, () => {
const imagePath = `images/sample-image.gif`
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`

it(`throws an error if the destination directory is not within 'public'`, async () => {
it(`throws an error if the destination directory is not within '${buildDirectory}'`, async () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const invalidDestinationDir = `../destination`
expect.assertions(2)
Expand All @@ -192,7 +193,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
const validDestinationDir = `path/to/dir`
const expectedNewPath = path.posix.join(
process.cwd(),
`public`,
buildDirectory,
validDestinationDir,
`/undefined-undefined.gif`
)
Expand Down Expand Up @@ -246,7 +247,7 @@ describe(`gatsby-remark-copy-linked-files`, () => {
const markdownAST = remark.parse(`![some absolute image](${imagePath})`)
const expectedNewPath = path.posix.join(
process.cwd(),
`public`,
buildDirectory,
`/undefined-undefined.gif`
)
expect.assertions(3)
Expand Down
7 changes: 3 additions & 4 deletions packages/gatsby-remark-copy-linked-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const _ = require(`lodash`)
const cheerio = require(`cheerio`)
const imageSize = require(`probe-image-size`)

const DEPLOY_DIR = `public`

const invalidDestinationDirMessage = dir =>
`[gatsby-remark-copy-linked-files You have supplied an invalid destination directory. The destination directory must be a child but was: ${dir}`

Expand All @@ -22,15 +20,16 @@ const newFileName = linkNode =>
`${linkNode.name}-${linkNode.internal.contentDigest}.${linkNode.extension}`

const newPath = (linkNode, destinationDir) => {
const buildDirectory = process.env.GATSBY_BUILD_DIR || `public`
if (destinationDir) {
return path.posix.join(
process.cwd(),
DEPLOY_DIR,
buildDirectory,
destinationDir,
newFileName(linkNode)
)
}
return path.posix.join(process.cwd(), DEPLOY_DIR, newFileName(linkNode))
return path.posix.join(process.cwd(), buildDirectory, newFileName(linkNode))
}

const newLinkURL = (linkNode, destinationDir, pathPrefix) => {
Expand Down
Loading