Skip to content

Commit 221d2a0

Browse files
committed
Handle copying assets from assets-only directories
Fixes #39 Previously when Gatsby was copying assets in the post-build step, it assumed that all assets were in the same directory as an html page as page paths can be written programatically, to ensure assets are copied to the right directory, they too need to follow however the page path is modified. This created a bug however when assets were placed in a directory without a page as then the code couldn't find a page associated with the assets. This commit checks now if page is returned undefined and if it is, copies the asset to /public to a directory with the same name e.g. /pages/images/foo.jpg is copied to /public/images/foo.jpg.
1 parent 1bd737d commit 221d2a0

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

lib/utils/post-build.coffee

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ module.exports = (program, cb) ->
3333
if page.file.name.slice(0,1) isnt '_'
3434
parsePath(page.requirePath).dirname is oldPath
3535

36-
newPath = parsePath(page.path).dirname + parsed.basename
36+
if page?
37+
newPath = parsePath(page.path).dirname + parsed.basename
38+
39+
# If a page wasn't found, this probably means the asset is in
40+
# a folder without a page e.g. an images directory. In this case,
41+
# there is no path rewriting so just copy to a directory with
42+
# the same name.
43+
else
44+
relativePath = path.relative(directory + "/pages", parsed.dirname)
45+
newPath = "#{relativePath}/#{parsed.basename}"
3746

3847
newPath = directory + "/public/" + newPath
3948
fs.copy(file, newPath, (err) ->

0 commit comments

Comments
 (0)