Skip to content

Commit

Permalink
feat: migrate on webpack API for getting filename
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `pathData` as first argument of a custom function for the `filename` option was changed, now we pass only `pathData.filename`
  • Loading branch information
alexander-akait authored Sep 11, 2021
1 parent 5415029 commit 59fe68c
Show file tree
Hide file tree
Showing 14 changed files with 1,881 additions and 2,078 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ module.exports = {
filename(pathData) {
// The `pathData` argument contains all placeholders - `path`/`name`/`ext`/etc
// Available properties described above, for the `String` notation
if (/\.svg$/.test(pathData.file)) {
if (/\.svg$/.test(pathData.filename)) {
return "assets/svg/[path][base].gz";
}

Expand Down
3,474 changes: 1,651 additions & 1,823 deletions package-lock.json

Large diffs are not rendered by default.

47 changes: 9 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,44 +230,15 @@ class CompressionPlugin {
await cacheItem.storePromise(output);
}

const match = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(name);
const [, replacerFile] = match;
const replacerQuery = match[2] || "";
const replacerFragment = match[3] || "";
const replacerExt = path.extname(replacerFile);
const replacerBase = path.basename(replacerFile);
const replacerName = replacerBase.slice(
0,
replacerBase.length - replacerExt.length
);
const replacerPath = replacerFile.slice(
0,
replacerFile.length - replacerBase.length
);
const pathData = {
file: replacerFile,
query: replacerQuery,
fragment: replacerFragment,
path: replacerPath,
base: replacerBase,
name: replacerName,
ext: replacerExt || "",
};

let newFilename = this.options.filename;

if (typeof newFilename === "function") {
newFilename = newFilename(pathData);
}

const newName = newFilename.replace(
/\[(file|query|fragment|path|base|name|ext)]/g,
(p0, p1) => pathData[p1]
);

const newFilename = compilation.getPath(this.options.filename, {
filename: name,
});
const newInfo = { compressed: true };

if (info.immutable && /(\[name]|\[base]|\[file])/.test(newFilename)) {
if (
info.immutable &&
/(\[name]|\[base]|\[file])/.test(this.options.filename)
) {
newInfo.immutable = true;
}

Expand All @@ -281,11 +252,11 @@ class CompressionPlugin {
compilation.deleteAsset(name);
} else {
compilation.updateAsset(name, source, {
related: { [relatedName]: newName },
related: { [relatedName]: newFilename },
});
}

compilation.emitAsset(newName, output.source, newInfo);
compilation.emitAsset(newFilename, output.source, newInfo);
})()
);
}
Expand Down
Loading

0 comments on commit 59fe68c

Please sign in to comment.