Repository URL to install this package:
|
Version:
3.1.0 ▾
|
/* eslint-disable max-statements */
/**
* @file resolves the entry assets available from our client bundle
* @see scripts/.plugins
* @name getClientBundleEntryAssets
*/
import { resolve } from 'path'
import { exists, readJSON } from '@skava/persistence/dist/adapters/server'
import getConfigForEnv from './getConfigForEnv'
import { getAbsoluteUrl } from './getDynamicPaths'
let resultCache
function validateAssetsPath(assetsFilePath: string) {
if (!exists(assetsFilePath)) {
const errorMessage = `
We could not find the "${assetsFilePath}" file,
which contains a list of the assets of the client bundle.
Please ensure that the client bundle has been built.
`
const noAssets = new Error(errorMessage)
throw noAssets
}
}
function validateAssetEntryPointForIndex(index) {
if (typeof index === 'undefined') {
throw new Error('No asset data found for expected "index" entry chunk of client bundle.')
}
}
function validateHackPath(assetObjJs) {
if (!assetObjJs || !assetObjJs.replace) {
const value = JSON.stringify(assetObjJs, null, 2)
const error = 'must remap a string path. received: ' + value
throw new TypeError(error)
}
}
/**
* 1. add dynamic port
* 2. assert https unless localhost
* 3. validate
*/
function hackPath(assetObjJs = '') {
validateHackPath(assetObjJs)
const FINAL_URL = getAbsoluteUrl()
assetObjJs = assetObjJs
.replace('http://localhost:3000', FINAL_URL)
.replace('https://localhost:3000', FINAL_URL)
return assetObjJs
}
/**
* @desc retrieves the js/css for the named chunks that belong to our client bundle.
*
* @note the order of the chunk names is important. The same ordering will be
* used when rendering the scripts.
*
* This is useful to us for a couple of reasons:
* - It allows us to target the assets for a specific chunk, thereby only
* loading the assets we know we will need for a specific request.
* - The assets are hashed, and therefore they can't be "manually" added
* to the render logic. Having this method allows us to easily fetch
* the respective assets simply by using a chunk name. :)
*
* @example dist/dist/bundle/client/assets.json
* {"index":{"js":"http://0.0.0.0:5555/client/index.js"}}
*
* @example dist/dist/bundle/server/assets.json
* {"index":{"js":"http://0.0.0.0:5555/server/index.js"}}
*/
function getClientBundleEntryAssets() {
// Return the assets json cache if it exists.
// In development mode we always read the assets json file from disk to avoid
// any cases where an older version gets cached.
// @NOTE disabled @hack
// if (process.env.BUILD_FLAG_IS_DEV === 'false' && resultCache) {
// return resultCache
// }
const assetDir = getConfigForEnv('assetsDir')
const assetFileName = getConfigForEnv('bundleAssetsFileName')
const assetFilePathRelative = `${assetDir}/${assetFileName}`
const assetsFilePath = resolve(assetFilePathRelative)
validateAssetsPath(assetsFilePath)
const assetsJSONCache = readJSON(assetsFilePath)
const outputPaths = Object.values(assetsJSONCache)
const outputNames = Object.keys(assetsJSONCache)
// @todo finish making this dynamic
const index = assetsJSONCache.index || assetsJSONCache.js || assetsJSONCache.main
validateAssetEntryPointForIndex(index)
const assetObj = {
js: hackPath(index.js),
}
const fromOriginalToRemapped = name => {
// .js thanks to assets plugin...
const value = assetsJSONCache[name].js
const remapped = hackPath(value)
// log('REMAPPED', {
// from: value,
// to: remapped,
// })
return remapped
}
/**
* @todo typings
*/
assetObj.jsList = outputNames.map(fromOriginalToRemapped)
resultCache = assetObj
// @note was this for some reason
// resultCache = index
return resultCache
}
getClientBundleEntryAssets.getClientBundleEntryAssets = getClientBundleEntryAssets
export { getClientBundleEntryAssets }
export default getClientBundleEntryAssets