Skip to content

Commit

Permalink
fix(gatsby-plugin-netlify): add all .js files from webpack.stats.json…
Browse files Browse the repository at this point in the history
… to _headers (#12521)

## Description

The issue was that only the first file was taken from each key from webpack.stats.json. Modified it to get all of them and render them accordingly.

## Related Issues

Fixes to #9828
  • Loading branch information
mihaiblaga89 authored and pieh committed Apr 19, 2019
1 parent 414bc61 commit 983dbd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 13 additions & 2 deletions packages/gatsby-plugin-netlify/src/build-headers-program.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from "lodash"
import { writeFile, existsSync } from "fs-extra"
import { parse } from "path"
import kebabHash from "kebab-hash"
import { HEADER_COMMENT } from "./constants"

Expand Down Expand Up @@ -41,8 +42,18 @@ function createScriptHeaderGenerator(manifest, pathPrefix) {
return null
}

// Always add starting slash, as link entries start with slash as relative to deploy root
return linkTemplate(`${pathPrefix}/${chunk}`)
// convert to array if it's not already
const chunks = _.isArray(chunk) ? chunk : [chunk]

return chunks
.filter(script => {
const parsed = parse(script)
// handle only .js, .css content is inlined already
// and doesn't need to be pushed
return parsed.ext === `.js`
})
.map(script => linkTemplate(`${pathPrefix}/${script}`))
.join(`\n `)
}
}

Expand Down
12 changes: 2 additions & 10 deletions packages/gatsby-plugin-netlify/src/plugin-data.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import _ from "lodash"
import path from "path"

export function buildPrefixer(prefix, ...paths) {
return (...subpaths) => path.join(prefix, ...paths, ...subpaths)
}

// Webpack stats map to an array if source maps are enabled.
// We normalize to make direct map.
function normalizeStats(stats) {
return _.mapValues(stats.assetsByChunkName, script =>
_.isArray(script) ? script[0] : script
)
}

// This function assembles data across the manifests and store to match a similar
// shape of `static-entry.js`. With it, we can build headers that point to the correct
// hashed filenames and ensure we pull in the componentChunkName.
export default function makePluginData(store, assetsManifest, pathPrefix) {
const { program, pages: storePages } = store.getState()
const publicFolder = buildPrefixer(program.directory, `public`)
const stats = require(publicFolder(`webpack.stats.json`))
const chunkManifest = normalizeStats(stats)
// Get all the files, not just the first
const chunkManifest = stats.assetsByChunkName
const pages = storePages

// We combine the manifest of JS and the manifest of assets to make a lookup table.
Expand Down

0 comments on commit 983dbd9

Please sign in to comment.