-
-
Save dlmr/8512b5172a3345866749124e9d661103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script>{{ custom.preloadContent | safe }}</script> | |
</head> | |
<body> | |
Hello World | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
webpack: (target) => { | |
const getAbsolutePath = require('roc').getAbsolutePath; | |
const path = require('path'); | |
const settings = getSettings(); | |
const assetsDirectory = getAbsolutePath(settings.build.output.web); | |
const statsFile = path.join(assetsDirectory, 'webpack-analysis.json'); | |
if (target === 'node') { | |
return { | |
externals: [{ | |
[statsFile]: true, | |
}], | |
}; | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path'; | |
import fs from 'fs'; | |
import { getAbsolutePath } from 'roc'; | |
import BodyClass from '../shared/components/side-effects/BodyClass'; | |
import * as Spritesheet from '../shared/components/side-effects/spritesheet'; | |
// get chunks by name and extensions | |
const getChunks = (stats, name, ext = 'js') => { | |
let chunk = stats.assetsByChunkName[name]; | |
// a chunk could be a string or an array, so make sure it is an array | |
if (!(Array.isArray(chunk))) { | |
chunk = [chunk]; | |
} | |
return chunk | |
.filter((chunkName) => path.extname(chunkName) === `.${ext}`) | |
}; | |
let preloadContent; | |
const getPreloadContent = (settings) => { | |
if (!preloadContent) { | |
const assetsDirectory = getAbsolutePath(settings.build.output.web); | |
const stats = require(path.join(assetsDirectory, 'webpack-analysis.json')); | |
const preloadFilename = getChunks(stats, 'preload')[0]; | |
preloadContent = fs.readFileSync(path.join(assetsDirectory, preloadFilename), 'utf8'); | |
} | |
return preloadContent; | |
} | |
export default function getTemplateValues({ settings }) { | |
return { | |
bodyClass: BodyClass.rewind(), | |
svgSprites: Spritesheet.rewind(), | |
preloadContent: getPreloadContent(settings), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment