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