Skip to content

Instantly share code, notes, and snippets.

@dlmr

dlmr/main.html Secret

Last active October 3, 2017 10:08
Show Gist options
  • Save dlmr/8512b5172a3345866749124e9d661103 to your computer and use it in GitHub Desktop.
Save dlmr/8512b5172a3345866749124e9d661103 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script>{{ custom.preloadContent | safe }}</script>
</head>
<body>
Hello World
</body>
</html>
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,
}],
};
}
}
};
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