UNPKG

@mapbox/batfish

Version:

The React-powered static-site generator you didn't know you wanted

85 lines (64 loc) 2.38 kB
// 'use strict'; const _ = require('lodash'); const path = require('path'); const fs = require('fs'); const pTry = require('p-try'); const joinUrlParts = require('./join-url-parts'); const errorTypes = require('./error-types'); const wrapError = require('./wrap-error'); const UglifyJs = require('uglify-js'); const getWebpackAssetAbsolutePath = require('./get-webpack-asset-absolute-path'); // We need to define this type because Flow can't understand the non-literal // require that pulls in static-render-pages.js below. function buildHtml( batfishConfig , cssFilename ) { return pTry(() => { const assetsDirectory = path.join( batfishConfig.outputDirectory, batfishConfig.publicAssetsPath ); // This file reading is synced to make scoping easier, and with so few // files doesn't matter for performance. const rawAssets = fs.readFileSync( path.join(assetsDirectory, 'assets.json'), 'utf8' ); const assets = Object.freeze(JSON.parse(rawAssets)); const manifestJs = fs.readFileSync( getWebpackAssetAbsolutePath(batfishConfig, assets.manifest.js), 'utf8' ); const uglified = UglifyJs.minify(manifestJs); if (uglified.error) throw uglified.error; const uglifiedManifestJs = uglified.code; let cssUrl; if (!_.isEmpty(batfishConfig.stylesheets) && cssFilename) { cssUrl = joinUrlParts( batfishConfig.siteBasePath, batfishConfig.publicAssetsPath, path.basename(cssFilename) ); } try { const staticRenderPages = require(path.join( assetsDirectory, 'static-render-pages.js' )).default; return staticRenderPages( batfishConfig, assets, uglifiedManifestJs, cssUrl ).catch((error) => { throw wrapError(error, errorTypes.WebpackNodeExecutionError); }); } catch (requireError) { throw wrapError(requireError, errorTypes.WebpackNodeParseError); } }); } module.exports = buildHtml;