Repository URL to install this package:
|
Version:
3.5.2 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/* eslint-disable max-statements */
/**
* @file resolves the entry assets available from our client bundle
* @see scripts/.plugins
* @name getClientBundleEntryAssets
*/
const path_1 = require("path");
const server_1 = require("@skava/persistence/dist/adapters/server");
const getConfigForEnv_1 = tslib_1.__importDefault(require("./getConfigForEnv"));
let resultCache;
function validateAssetsPath(assetsFilePath) {
if (!server_1.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
*
* @note this was changed from replacing localhost with the server url
* now we have 1 router, so we will change that in the server :)
*/
function hackPath(assetObjJs = '') {
validateHackPath(assetObjJs);
return assetObjJs
.replace('http://localhost:3000', '')
.replace('https://localhost:3000', '');
}
/**
* @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_1.default('assetsDir');
const assetFileName = getConfigForEnv_1.default('bundleAssetsFileName');
const assetFilePathRelative = `${assetDir}/${assetFileName}`;
const assetsFilePath = path_1.resolve(assetFilePathRelative);
validateAssetsPath(assetsFilePath);
const assetsJSONCache = server_1.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 fromOriginalToRemapped = name => {
// .js thanks to assets plugin...
const value = assetsJSONCache[name].js;
const remapped = hackPath(value);
return remapped;
};
const jsList = outputNames.map(fromOriginalToRemapped);
const assetObj = {
js: hackPath(index.js),
jsList,
};
resultCache = assetObj;
// @note was this for some reason
// resultCache = index
return resultCache;
}
exports.getClientBundleEntryAssets = getClientBundleEntryAssets;
getClientBundleEntryAssets.getClientBundleEntryAssets = getClientBundleEntryAssets;
exports.default = getClientBundleEntryAssets;
//# sourceMappingURL=getClientBundleEntryAssets.js.map