1234567891011121314151617181920212223242526 |
- /**
- * The regular expression to find asset names in a string
- *
- * @type {RegExp}
- */
- const defaultAssetRegex = /[{]{2}([^}]+)[}]{2}/g;
- /**
- * Replaces asset names with the first of the asset files from a webpack compilation.
- *
- * @param {string} targetSource - a string in which to replace the assets
- * @param {Object} assetFiles - a map of available asset names and their containing files
- * @return {string}
- */
- function replaceAssets (targetSource, assetFiles) {
- return targetSource.replace(defaultAssetRegex, function replaceAsset (match, assetName) {
- if (!(assetFiles[assetName] && assetFiles[assetName].length)) {
- console.warn(`no asset for '${assetName}' was found`);
- return match;
- }
- return assetFiles[assetName][0];
- });
- }
- module.exports = replaceAssets;
|