webpack-laravel-mix-manifest
Version:
🐶A webpack plugin that generates Laravel framework compatible mix-manifest.josn file.
82 lines (81 loc) • 2.59 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Manifest = void 0;
const path_1 = __importDefault(require("path"));
class Manifest {
/**
* Generated manifest record.
* @type {Record<string, string>}
*/
manifest;
/**
* Create a new manifest.
*/
constructor() {
this.manifest = {};
}
/**
* Transform the Webpack stats into the shape we need.
* @param state Value of webpack stats to json output.
*/
transform(stats) {
const assets = this.flattenAssets(stats);
Object.keys(assets).forEach((entryName) => {
this.add(assets[entryName], entryName);
});
return this;
}
/**
* Using this manifest built to string.
*/
rebuild() {
return JSON.stringify(this.manifest, null, 2);
}
/**
* Add the given path to the manifest file.
* @param paths Need given paths.
* @param entryName output entry name.
*/
add(paths, entryName) {
if (Array.isArray(paths) || typeof paths === 'object') {
let children = paths;
if (!Array.isArray(paths) && typeof paths === 'object') {
children = Object.values(paths);
}
children.forEach((path) => this.add(path, entryName));
return this;
}
// search original in path.
const path = this.normalizePath(paths);
const original = path.replace(/\?.*/, '');
// Get basename and file extension.
const basename = path_1.default.basename(original);
const extension = path_1.default.extname(original);
// Generate key and save it to manifest.
const key = original.replace(basename, path_1.default.basename(entryName + extension));
this.manifest[key] = path;
return this;
}
/**
* Flatten the generated stats assets into an ollection.
* @param stats Value of webpack stats to json output.
*/
flattenAssets(stats) {
return Object.assign({}, stats.assetsByChunkName);
}
/**
* Prepare the provided path for processing.
* @param path Need normalize path string.
*/
normalizePath(path) {
let newPath = path.replace(/\\/g, '/').replace(/\/+/g, '/');
if (!newPath.startsWith('/')) {
return '/' + newPath;
}
return newPath;
}
}
exports.Manifest = Manifest;