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/regroup non static files #6346

Merged
merged 18 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"__PATH_PREFIX__": false,
"___emitter": false
}
}
}
16 changes: 10 additions & 6 deletions packages/gatsby/cache-dir/static-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const createElement = React.createElement

export default (pagePath, callback) => {
const pathPrefix = `${__PATH_PREFIX__}/`
const pathToGroupStyles = `css/`
const pathToGroupScripts = `js/`

let bodyHtml = ``
let headComponents = []
Expand Down Expand Up @@ -197,6 +199,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 @@ -225,7 +228,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 All @@ -249,23 +252,22 @@ export default (pagePath, callback) => {
.forEach(style => {
// Add <link>s for styles that should be prefetched
// otherwise, inline as a <style> tag

if (style.rel === `prefetch`) {
headComponents.push(
<link
as="style"
rel={style.rel}
key={style.name}
href={urlJoin(pathPrefix, style.name)}
href={urlJoin(pathPrefix, pathToGroupStyles, style.name)}
/>
)
} else {
headComponents.unshift(
<style
Copy link
Contributor Author

@brotzky brotzky Jul 12, 2018

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks 👍

data-href={urlJoin(pathPrefix, style.name)}
data-href={urlJoin(pathPrefix, style.name.slice(2))}
dangerouslySetInnerHTML={{
__html: fs.readFileSync(
join(process.cwd(), `public`, style.name),
join(process.cwd(), `public`, pathToGroupStyles, style.name),
`utf-8`
),
}}
Expand Down Expand Up @@ -294,7 +296,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
4 changes: 3 additions & 1 deletion packages/gatsby/src/commands/build-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = async (program: any, activity: any) => {
if (e) {
return reject(e)
}
const outputFile = `${directory}/public/render-page.js`

const outputFile = `${directory}/public/js/render-page.js`

if (stats.hasErrors()) {
let webpackErrors = stats.toJson().errors.filter(Boolean)
return reject(
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby/src/commands/develop-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module.exports = async (program: any) => {
if (e) {
return reject(e)
}
const outputFile = `${directory}/public/render-page.js`

const outputFile = `${directory}/public/js/render-page.js`

if (stats.hasErrors()) {
let webpackErrors = stats.toJson().errors
console.log(`here`, webpackErrors[0])
Expand Down
12 changes: 9 additions & 3 deletions packages/gatsby/src/utils/webpack-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ module.exports = async ({
stage: Stage,
program: any,
}): Promise<WebpackUtilsOptions> => {
const assetRelativeRoot = `static/`
/**
* the leading `../` for assetRelativeRoot is required to ensure
* the static files extracted by webpack are not part of the
* "build-javascript" output path of `/js`. The same technique is
* used for CSS
*/
const assetRelativeRoot = `../static/`
Copy link
Contributor Author

@brotzky brotzky Jul 13, 2018

Choose a reason for hiding this comment

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

@m-allanson This had to be treated the same way as the ../css/ case because of how there's only a build-javascript stage. #6346 (comment)

Here are all the build stages:

type Stage = "develop" | "develop-html" | "build-javascript" | "build-html"

I think in the future it could be a good idea to add discrete build stages for CSS (although CSS in JS makes this a question mark?) and static assets instead of relying on this current workaround.

Here's an example of the output folder of your example repository with this commit

screen shot 2018-07-12 at 7 00 45 pm

const vendorRegex = /(node_modules|bower_components)/
const supportedBrowsers = program.browserlist

Expand Down Expand Up @@ -435,8 +441,8 @@ module.exports = async ({
*/
plugins.extractText = options =>
new MiniCssExtractPlugin({
filename: `[name].[contenthash].css`,
chunkFilename: `[name].[contenthash].css`,
filename: `../css/[name].[contenthash].css`,
Copy link
Contributor

Choose a reason for hiding this comment

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

why is ../css/ needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, the css is generated in the stage build-javascript using the MiniCssExtractPlugin, which grabs the output path from webpack's config webpackConfig.output.path.

From what I could tell, there's no way of changing the output path (only publicPath?) using MiniCssExtractPlugin so adjusting the filenames is a workaround.

What's happening is the system things it's putting the CSS into the /js folder, but it actually goes back into the root, and creates a /css folder to place the CSS files in.

If there's a different way to handle this, such as creating build-css or a different webpackConfig.output.path during the time of CSS generation that would solve this workaround.

chunkFilename: `../css/[name].[contenthash].css`,
...options,
})

Expand Down
26 changes: 17 additions & 9 deletions packages/gatsby/src/utils/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ module.exports = async (
return hmrBasePath + hmrSuffix
}

function getOutputPaths() {
const path = directoryPath(`public/js`)

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

return {
path,
publicPath,
}
}

debug(`Loading webpack config for stage "${stage}"`)
function getOutput() {
switch (stage) {
Expand All @@ -113,24 +126,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 @@ -231,7 +240,6 @@ module.exports = async (
)
}
}

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