From c66c4de76a81524a3e9e8bd6ed44784fa0ed56d9 Mon Sep 17 00:00:00 2001 From: alan-jay Date: Fri, 19 Apr 2019 15:48:25 +0900 Subject: [PATCH] Fix styles to be sorted on static-entry.js --- packages/gatsby/cache-dir/static-entry.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/gatsby/cache-dir/static-entry.js b/packages/gatsby/cache-dir/static-entry.js index aa3b9cfb95daa..3301aac6aea79 100644 --- a/packages/gatsby/cache-dir/static-entry.js +++ b/packages/gatsby/cache-dir/static-entry.js @@ -22,6 +22,16 @@ const chunkMapping = JSON.parse( fs.readFileSync(`${process.cwd()}/public/chunk-map.json`, `utf-8`) ) +// Compare styles by names +const compareStyleNames = ({ name: nameA }, { name: nameB }) => { + const [numberA, numberB] = [nameA, nameB].map(name => + Number(name.replace(/^(\d+).*/, `$1`)) + ) + + // do not swap if names don't start with number + return numberA - numberB || 0 +} + // const testRequireError = require("./test-require-error") // For some extremely mysterious reason, webpack adds the above module *after* // this module so that when this code runs, testRequireError is undefined. @@ -254,9 +264,9 @@ 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`) - ) + const styles = scriptsAndStyles + .filter(style => style.name && style.name.endsWith(`.css`)) + .sort(compareStyleNames) apiRunner(`onRenderBody`, { setHeadComponents,