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

Regroup non-static files #6285

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
4803d61
First try at cleaning up build output
brotzky Jul 4, 2018
582fb41
Fix typo in type annotations (#6288)
sudodoki Jul 4, 2018
50dd934
Fix broken images (#6282)
giraffesyo Jul 4, 2018
0986a67
add first draft of API Lifecycle docs (#6229)
tsiq-swyx Jul 4, 2018
7ed8da9
Update adding-tags-and-categories-to-blog-posts.md (#6294)
XHMM Jul 4, 2018
e1b8be9
Adding flag to build, ordering pathPrefix correctly, and cleaned up s…
brotzky Jul 4, 2018
77371b9
Removing the deletion of files as referenced by m-allanson and pieh
brotzky Jul 4, 2018
59cf18a
Adding support for styles grouping
brotzky Jul 4, 2018
a9bc9b8
Update site-showcase-submissions.md concerning featured sites (#6281)
Jul 5, 2018
fdcf23a
final draft ready for review
Jul 5, 2018
2acc8b9
[www] Fix #6295 (#6312)
fk Jul 6, 2018
912751e
Fix typo (#6296)
giraffesyo Jul 6, 2018
0513a6c
Pass apiRunner into loader (#6272)
m-allanson Jul 6, 2018
1041f85
Fix tracedSVG fragment support in Gatsby 2.0 (#6297)
tylermenezes Jul 6, 2018
50cb0bb
Add empty pages.json. (#5824) (#6304)
ascorbic Jul 6, 2018
37d007a
switch components reducer to use Map for state (#6307)
pieh Jul 6, 2018
0add064
return null object for ssr pageRenderer - fixes importing gatsby-brow…
pieh Jul 6, 2018
f5d143b
Track queries between builds (#6322)
pieh Jul 6, 2018
eb17042
edits suggested by Phil
shannonbux Jul 6, 2018
b8c8040
add blog showcase (#6287)
zerosoul Jul 6, 2018
f9f4cd6
Adding hyperlinks to images
shannonbux Jul 6, 2018
06e7360
Made gatsby-plugin-netlify-cms work with gatsby v2 (#6292)
emanueleperuffo Jul 6, 2018
93c3554
Added site titles
shannonbux Jul 6, 2018
e02df8f
added tags
shannonbux Jul 6, 2018
6ec8c70
minor edits
shannonbux Jul 6, 2018
11206e4
chore(release): Publish
m-allanson Jul 6, 2018
150d862
other minor edits
shannonbux Jul 6, 2018
3637083
Merge pull request #6317 from gatsbyjs/graphic-design-blogpost
shannonbux Jul 6, 2018
c2bdb2d
[gatsby-plugin-offline] Add criticlal scripts and links to static fil…
kkemple Jul 6, 2018
6f789f6
chore(release): Publish
m-allanson Jul 6, 2018
366bd97
renaming output directories to js and css
brotzky Jul 6, 2018
972fb6b
First try at cleaning up build output
brotzky Jul 4, 2018
4042ae1
Adding flag to build, ordering pathPrefix correctly, and cleaned up s…
brotzky Jul 4, 2018
695cb68
Removing the deletion of files as referenced by m-allanson and pieh
brotzky Jul 4, 2018
699e5ca
Adding support for styles grouping
brotzky Jul 4, 2018
eed6616
renaming output directories to js and css
brotzky Jul 6, 2018
d8a8377
Merge branch 'topics/regroup-non-static-files' of github.com:narative…
brotzky Jul 6, 2018
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
16 changes: 11 additions & 5 deletions packages/gatsby-cli/src/create-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ function buildLocalCommands(cli, isLocalSite) {
type: `boolean`,
default: false,
describe: `Build site with link paths prefixed (set prefix in your config).`,
}).option(`no-uglify`, {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
}),
})
.option(`no-uglify`, {
type: `boolean`,
default: false,
describe: `Build site without uglifying JS bundles (for debugging).`,
})
.option(`group-files`, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a flag to enable grouping of files.
gatsby build --group-files

Copy link
Contributor

Choose a reason for hiding this comment

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

Why a flag? We try to avoid adding config wherever possible.

Copy link
Contributor Author

@brotzky brotzky Jul 4, 2018

Choose a reason for hiding this comment

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

@KyleAMathews -- my bad!
What's the best way to add a "feature flag" for this? I tried looking through the code but couldn't grasp it right away. Is there a standard way to handle this?

I was trying to address this comment
#6285 (review)

This could be a breaking change for plugins that rely on the current public directory structure. I wonder if this should be enabled via a feature flag so it can get some solid real-world testing?

The flag would be temporary for people to try it out and then enabled by default after some time. After that the config option would be removed. The purpose is to provide a smoother transition with a testing phase before implementing a potential breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, there shouldn't be any plugins which directly operate on outputted css/js so I think this change is pretty safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

@m-allanson @pieh @jquense can you think of any reason we shouldn't just make this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

w/o the flag

Copy link
Contributor Author

@brotzky brotzky Jul 6, 2018

Choose a reason for hiding this comment

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

Without the flags the code would be a lot cleaner. Less config options exposed to users and I can remove ifs, globals, and other logic from this PR. I'm in favour of removing the flag if it only impacts 1-2 plugins.

type: `boolean`,
default: false,
describe: `Build site with grouping of files in subdirectories.`,
}),
handler: handlerP(
getCommandHandler(`build`, (args, cmd) => {
process.env.NODE_ENV = `production`
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/cache-dir/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},
"globals": {
"__PATH_PREFIX__": false,
"__GROUP_FILES__": false,
"___emitter": false
}
}
}
16 changes: 12 additions & 4 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ const createElement = React.createElement
export default (pagePath, callback) => {
const pathPrefix = `${__PATH_PREFIX__}/`

// If enabled, group files in their respective folders otherwise generated
// non-static files will be left in the root of the output directory.
const pathToGroupStyles = __GROUP_FILES__ ? `styles/` : ``
Copy link
Contributor

Choose a reason for hiding this comment

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

how about we use js/css instead? Those feel more standard to me + they're shorter (save all the bytes).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@KyleAMathews agreed. The original names were based off variables in the code. +1 for saving all the bytes.

const pathToGroupScripts = __GROUP_FILES__ ? `scripts/` : ``

let bodyHtml = ``
let headComponents = []
let htmlAttributes = {}
Expand Down Expand Up @@ -193,6 +198,7 @@ export default (pagePath, callback) => {
const scripts = scriptsAndStyles.filter(
script => script.name && script.name.endsWith(`.js`)
)

const styles = scriptsAndStyles.filter(
style => style.name && style.name.endsWith(`.css`)
)
Expand Down Expand Up @@ -221,7 +227,7 @@ export default (pagePath, callback) => {
as="script"
rel={script.rel}
key={script.name}
href={urlJoin(pathPrefix, script.name)}
href={urlJoin(pathPrefix, pathToGroupScripts, script.name)}
/>
)
})
Expand Down Expand Up @@ -252,7 +258,7 @@ export default (pagePath, callback) => {
as="style"
rel={style.rel}
key={style.name}
href={urlJoin(pathPrefix, style.name)}
href={urlJoin(pathPrefix, pathToGroupStyles, style.name)}
/>
)
} else {
Expand All @@ -262,7 +268,7 @@ export default (pagePath, callback) => {
data-href={urlJoin(pathPrefix, style.name)}
dangerouslySetInnerHTML={{
__html: fs.readFileSync(
join(process.cwd(), `public`, style.name),
join(process.cwd(), `public`, pathToGroupStyles, style.name),
`utf-8`
),
}}
Expand Down Expand Up @@ -291,7 +297,9 @@ 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 = `${pathPrefix}${JSON.stringify(s.name).slice(1, -1)}`
const scriptPath = `${pathPrefix}${pathToGroupScripts}${JSON.stringify(
s.name
).slice(1, -1)}`
return <script key={scriptPath} src={scriptPath} async />
})

Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ module.exports = async (args: BootstrapArgs) => {
await del([
`public/*.{html,css}`,
`public/**/*.{html,css}`,
`public/scripts`,
`public/styles`,
`!public/static`,
`!public/static/**/*.{html,css}`,
])
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby/src/commands/build-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { createErrorFromString } = require(`gatsby-cli/lib/reporter/errors`)
const renderHTML = require(`../utils/html-renderer`)

module.exports = async (program: any) => {
const { directory } = program
const { directory, groupFiles } = program

debug(`generating static HTML`)
// Reduce pages objects to an array of paths.
Expand All @@ -28,7 +28,11 @@ module.exports = async (program: any) => {
if (e) {
return reject(e)
}
const outputFile = `${directory}/public/render-page.js`

const outputFile = groupFiles
? `${directory}/public/scripts/render-page.js`
: `${directory}/public/render-page.js`

if (stats.hasErrors()) {
let webpackErrors = stats.toJson().errors.filter(Boolean)
return reject(
Expand Down
8 changes: 6 additions & 2 deletions packages/gatsby/src/commands/develop-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const webpackConfig = require(`../utils/webpack.config`)
const renderHTML = require(`../utils/html-renderer`)

module.exports = async (program: any) => {
const { directory } = program
const { directory, groupFiles } = program

debug(`generating static HTML`)

Expand All @@ -24,7 +24,11 @@ module.exports = async (program: any) => {
if (e) {
return reject(e)
}
const outputFile = `${directory}/public/render-page.js`

const outputFile = groupFiles
? `${directory}/public/scripts/render-page.js`
: `${directory}/public/render-page.js`

if (stats.hasErrors()) {
let webpackErrors = stats.toJson().errors
console.log(`here`, webpackErrors[0])
Expand Down
48 changes: 32 additions & 16 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ module.exports = async (
return hmrBasePath + hmrSuffix
}

function getOutputPaths() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was duplicate code in the webpack output configs, so I moved it into a function that also handles the grouping of files.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

const path = program.groupFiles
? directoryPath(`public/scripts`)
: directoryPath(`public`)

let publicPath = program.prefixPaths
? `${store.getState().config.pathPrefix}/`
: `/`

if (program.groupFiles) {
publicPath = `${publicPath}scripts/`
}

return {
path,
publicPath,
}
}

debug(`Loading webpack config for stage "${stage}"`)
function getOutput() {
switch (stage) {
Expand All @@ -109,24 +128,20 @@ module.exports = async (
// A temp file required by static-site-generator-plugin. See plugins() below.
// Deleted by build-html.js, since it's not needed for production.
return {
path: directoryPath(`public`),
filename: `render-page.js`,
libraryTarget: `umd`,
library: `lib`,
umdNamedDefine: true,
globalObject: `this`,
publicPath: program.prefixPaths
? `${store.getState().config.pathPrefix}/`
: `/`,
path: getOutputPaths().path,
publicPath: getOutputPaths().publicPath,
}
case `build-javascript`:
return {
filename: `[name]-[chunkhash].js`,
chunkFilename: `[name]-[chunkhash].js`,
path: directoryPath(`public`),
publicPath: program.prefixPaths
? `${store.getState().config.pathPrefix}/`
: `/`,
path: getOutputPaths().path,
publicPath: getOutputPaths().publicPath,
}
default:
throw new Error(`The state requested ${stage} doesn't exist.`)
Expand Down Expand Up @@ -167,12 +182,14 @@ module.exports = async (
plugins.moment(),

// Add a few global variables. Set NODE_ENV to production (enables
// optimizations for React) and what the link prefix is (__PATH_PREFIX__).
// optimizations for React), what the link prefix is (__PATH_PREFIX__),
// and __GROUP_FILES__ to organize output files.
plugins.define({
"process.env": processEnv(stage, `development`),
__PATH_PREFIX__: JSON.stringify(
program.prefixPaths ? store.getState().config.pathPrefix : ``
),
__GROUP_FILES__: !!program.groupFiles,
}),
]

Expand All @@ -186,12 +203,12 @@ module.exports = async (
clearConsole: false,
compilationSuccessInfo: {
messages: [
`You can now view your site in the browser running at ${program.ssl ? `https` : `http`}://${
program.host
}:${program.port}`,
`Your graphql debugger is running at ${program.ssl ? `https` : `http`}://${program.host}:${
program.port
}/___graphql`,
`You can now view your site in the browser running at ${
program.ssl ? `https` : `http`
}://${program.host}:${program.port}`,
`Your graphql debugger is running at ${
program.ssl ? `https` : `http`
}://${program.host}:${program.port}/___graphql`,
],
},
}),
Expand Down Expand Up @@ -227,7 +244,6 @@ module.exports = async (
)
}
}

const webpackStats = {
...stats.toJson({ all: false, chunkGroups: true }),
assetsByChunkName: assets,
Expand Down