Skip to content

Commit 654b87e

Browse files
committed
Incorporate linkPrefix into webpack's publicPath
This correctly references asset paths, e.g. FontAwesome, when static sites are targeted at sub-paths.
1 parent 37944c5 commit 654b87e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/utils/webpack.config.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin'
33
import ExtractTextPlugin from 'extract-text-webpack-plugin'
44
import Config from 'webpack-configurator'
55
const debug = require('debug')('gatsby:webpack-config')
6+
import fs from 'fs'
67
import path from 'path'
78
import _ from 'lodash'
89
import invariant from 'invariant'
10+
import toml from 'toml'
911

1012
import babelConfig from './babel-config'
1113

@@ -31,8 +33,28 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes
3133
const babelStage = suppliedStage
3234
const stage = (suppliedStage === 'develop-html') ? 'develop' : suppliedStage
3335

36+
let siteConfig = {}
37+
38+
try {
39+
siteConfig = toml.parse(fs.readFileSync(path.join(directory, 'config.toml')))
40+
} catch (e) {
41+
if (e.code !== 'ENOENT') {
42+
throw e
43+
}
44+
}
45+
3446
debug(`Loading webpack config for stage "${stage}"`)
3547
function output () {
48+
let publicPath = '/'
49+
50+
if (program.prefixLinks) {
51+
publicPath = siteConfig.linkPrefix
52+
53+
if (!publicPath.endsWith('/')) {
54+
publicPath += '/'
55+
}
56+
}
57+
3658
switch (stage) {
3759
case 'develop':
3860
return {
@@ -46,7 +68,7 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes
4668
return {
4769
path: `${directory}/public`,
4870
filename: 'bundle-for-css.js',
49-
publicPath: '/',
71+
publicPath,
5072
}
5173
case 'build-html':
5274
// A temp file required by static-site-generator-plugin. See plugins() below.
@@ -55,11 +77,13 @@ module.exports = (program, directory, suppliedStage, webpackPort = 1500, routes
5577
path: `${directory}/public`,
5678
filename: 'render-page.js',
5779
libraryTarget: 'umd',
80+
publicPath,
5881
}
5982
case 'build-javascript':
6083
return {
6184
filename: 'bundle.js',
6285
path: `${directory}/public`,
86+
publicPath,
6387
}
6488
default:
6589
throw new Error(`The state requested ${stage} doesn't exist.`)

0 commit comments

Comments
 (0)