Skip to content

Commit e401237

Browse files
brotzkym-allanson
authored andcommitted
Topics/regroup non static files (#6346)
* First try at cleaning up build output * Adding flag to build, ordering pathPrefix correctly, and cleaned up styles/script paths. * Removing the deletion of files as referenced by m-allanson and pieh * Adding support for styles grouping * renaming output directories to js and css * First try at cleaning up build output * Adding flag to build, ordering pathPrefix correctly, and cleaned up styles/script paths. * Removing the deletion of files as referenced by m-allanson and pieh * Adding support for styles grouping * renaming output directories to js and css * Removing config flag * Removing config flag logic from styles * Removing 'text/css' from CSS Link * Woops, adding back the CSS as I was testing locally and commited the changes by mistkae * Adding '../' to the relative static route similar to CSS
1 parent d0123c2 commit e401237

File tree

6 files changed

+43
-21
lines changed

6 files changed

+43
-21
lines changed

packages/gatsby/cache-dir/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
"__PATH_PREFIX__": false,
77
"___emitter": false
88
}
9-
}
9+
}

packages/gatsby/cache-dir/static-entry.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ const createElement = React.createElement
5858

5959
export default (pagePath, callback) => {
6060
const pathPrefix = `${__PATH_PREFIX__}/`
61+
const pathToGroupStyles = `css/`
62+
const pathToGroupScripts = `js/`
6163

6264
let bodyHtml = ``
6365
let headComponents = []
@@ -197,6 +199,7 @@ export default (pagePath, callback) => {
197199
const scripts = scriptsAndStyles.filter(
198200
script => script.name && script.name.endsWith(`.js`)
199201
)
202+
200203
const styles = scriptsAndStyles.filter(
201204
style => style.name && style.name.endsWith(`.css`)
202205
)
@@ -225,7 +228,7 @@ export default (pagePath, callback) => {
225228
as="script"
226229
rel={script.rel}
227230
key={script.name}
228-
href={urlJoin(pathPrefix, script.name)}
231+
href={urlJoin(pathPrefix, pathToGroupScripts, script.name)}
229232
/>
230233
)
231234
})
@@ -249,23 +252,22 @@ export default (pagePath, callback) => {
249252
.forEach(style => {
250253
// Add <link>s for styles that should be prefetched
251254
// otherwise, inline as a <style> tag
252-
253255
if (style.rel === `prefetch`) {
254256
headComponents.push(
255257
<link
256258
as="style"
257259
rel={style.rel}
258260
key={style.name}
259-
href={urlJoin(pathPrefix, style.name)}
261+
href={urlJoin(pathPrefix, pathToGroupStyles, style.name)}
260262
/>
261263
)
262264
} else {
263265
headComponents.unshift(
264266
<style
265-
data-href={urlJoin(pathPrefix, style.name)}
267+
data-href={urlJoin(pathPrefix, style.name.slice(2))}
266268
dangerouslySetInnerHTML={{
267269
__html: fs.readFileSync(
268-
join(process.cwd(), `public`, style.name),
270+
join(process.cwd(), `public`, pathToGroupStyles, style.name),
269271
`utf-8`
270272
),
271273
}}
@@ -294,7 +296,9 @@ export default (pagePath, callback) => {
294296
// Filter out prefetched bundles as adding them as a script tag
295297
// would force high priority fetching.
296298
const bodyScripts = scripts.filter(s => s.rel !== `prefetch`).map(s => {
297-
const scriptPath = `${pathPrefix}${JSON.stringify(s.name).slice(1, -1)}`
299+
const scriptPath = `${pathPrefix}${pathToGroupScripts}${JSON.stringify(
300+
s.name
301+
).slice(1, -1)}`
298302
return <script key={scriptPath} src={scriptPath} async />
299303
})
300304

packages/gatsby/src/commands/build-html.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module.exports = async (program: any, activity: any) => {
2828
if (e) {
2929
return reject(e)
3030
}
31-
const outputFile = `${directory}/public/render-page.js`
31+
32+
const outputFile = `${directory}/public/js/render-page.js`
33+
3234
if (stats.hasErrors()) {
3335
let webpackErrors = stats.toJson().errors.filter(Boolean)
3436
return reject(

packages/gatsby/src/commands/develop-html.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ module.exports = async (program: any) => {
2424
if (e) {
2525
return reject(e)
2626
}
27-
const outputFile = `${directory}/public/render-page.js`
27+
28+
const outputFile = `${directory}/public/js/render-page.js`
29+
2830
if (stats.hasErrors()) {
2931
let webpackErrors = stats.toJson().errors
3032
console.log(`here`, webpackErrors[0])

packages/gatsby/src/utils/webpack-utils.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ module.exports = async ({
117117
stage: Stage,
118118
program: any,
119119
}): Promise<WebpackUtilsOptions> => {
120-
const assetRelativeRoot = `static/`
120+
/**
121+
* the leading `../` for assetRelativeRoot is required to ensure
122+
* the static files extracted by webpack are not part of the
123+
* "build-javascript" output path of `/js`. The same technique is
124+
* used for CSS
125+
*/
126+
const assetRelativeRoot = `../static/`
121127
const vendorRegex = /(node_modules|bower_components)/
122128
const supportedBrowsers = program.browserlist
123129

@@ -443,8 +449,8 @@ module.exports = async ({
443449
*/
444450
plugins.extractText = options =>
445451
new MiniCssExtractPlugin({
446-
filename: `[name].[contenthash].css`,
447-
chunkFilename: `[name].[contenthash].css`,
452+
filename: `../css/[name].[contenthash].css`,
453+
chunkFilename: `../css/[name].[contenthash].css`,
448454
...options,
449455
})
450456

packages/gatsby/src/utils/webpack.config.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ module.exports = async (
8787
return hmrBasePath + hmrSuffix
8888
}
8989

90+
function getOutputPaths() {
91+
const path = directoryPath(`public/js`)
92+
93+
let publicPath = program.prefixPaths
94+
? `${store.getState().config.pathPrefix}/js/`
95+
: `/js/`
96+
97+
return {
98+
path,
99+
publicPath,
100+
}
101+
}
102+
90103
debug(`Loading webpack config for stage "${stage}"`)
91104
function getOutput() {
92105
switch (stage) {
@@ -113,24 +126,20 @@ module.exports = async (
113126
// A temp file required by static-site-generator-plugin. See plugins() below.
114127
// Deleted by build-html.js, since it's not needed for production.
115128
return {
116-
path: directoryPath(`public`),
117129
filename: `render-page.js`,
118130
libraryTarget: `umd`,
119131
library: `lib`,
120132
umdNamedDefine: true,
121133
globalObject: `this`,
122-
publicPath: program.prefixPaths
123-
? `${store.getState().config.pathPrefix}/`
124-
: `/`,
134+
path: getOutputPaths().path,
135+
publicPath: getOutputPaths().publicPath,
125136
}
126137
case `build-javascript`:
127138
return {
128139
filename: `[name]-[chunkhash].js`,
129140
chunkFilename: `[name]-[chunkhash].js`,
130-
path: directoryPath(`public`),
131-
publicPath: program.prefixPaths
132-
? `${store.getState().config.pathPrefix}/`
133-
: `/`,
141+
path: getOutputPaths().path,
142+
publicPath: getOutputPaths().publicPath,
134143
}
135144
default:
136145
throw new Error(`The state requested ${stage} doesn't exist.`)
@@ -231,7 +240,6 @@ module.exports = async (
231240
)
232241
}
233242
}
234-
235243
const webpackStats = {
236244
...stats.toJson({ all: false, chunkGroups: true }),
237245
assetsByChunkName: assets,

0 commit comments

Comments
 (0)