Skip to content

Commit 59fe68c

Browse files
feat: migrate on webpack API for getting filename
BREAKING CHANGE: `pathData` as first argument of a custom function for the `filename` option was changed, now we pass only `pathData.filename`
1 parent 5415029 commit 59fe68c

14 files changed

+1881
-2078
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ module.exports = {
275275
filename(pathData) {
276276
// The `pathData` argument contains all placeholders - `path`/`name`/`ext`/etc
277277
// Available properties described above, for the `String` notation
278-
if (/\.svg$/.test(pathData.file)) {
278+
if (/\.svg$/.test(pathData.filename)) {
279279
return "assets/svg/[path][base].gz";
280280
}
281281

package-lock.json

Lines changed: 1651 additions & 1823 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -230,44 +230,15 @@ class CompressionPlugin {
230230
await cacheItem.storePromise(output);
231231
}
232232

233-
const match = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(name);
234-
const [, replacerFile] = match;
235-
const replacerQuery = match[2] || "";
236-
const replacerFragment = match[3] || "";
237-
const replacerExt = path.extname(replacerFile);
238-
const replacerBase = path.basename(replacerFile);
239-
const replacerName = replacerBase.slice(
240-
0,
241-
replacerBase.length - replacerExt.length
242-
);
243-
const replacerPath = replacerFile.slice(
244-
0,
245-
replacerFile.length - replacerBase.length
246-
);
247-
const pathData = {
248-
file: replacerFile,
249-
query: replacerQuery,
250-
fragment: replacerFragment,
251-
path: replacerPath,
252-
base: replacerBase,
253-
name: replacerName,
254-
ext: replacerExt || "",
255-
};
256-
257-
let newFilename = this.options.filename;
258-
259-
if (typeof newFilename === "function") {
260-
newFilename = newFilename(pathData);
261-
}
262-
263-
const newName = newFilename.replace(
264-
/\[(file|query|fragment|path|base|name|ext)]/g,
265-
(p0, p1) => pathData[p1]
266-
);
267-
233+
const newFilename = compilation.getPath(this.options.filename, {
234+
filename: name,
235+
});
268236
const newInfo = { compressed: true };
269237

270-
if (info.immutable && /(\[name]|\[base]|\[file])/.test(newFilename)) {
238+
if (
239+
info.immutable &&
240+
/(\[name]|\[base]|\[file])/.test(this.options.filename)
241+
) {
271242
newInfo.immutable = true;
272243
}
273244

@@ -281,11 +252,11 @@ class CompressionPlugin {
281252
compilation.deleteAsset(name);
282253
} else {
283254
compilation.updateAsset(name, source, {
284-
related: { [relatedName]: newName },
255+
related: { [relatedName]: newFilename },
285256
});
286257
}
287258

288-
compilation.emitAsset(newName, output.source, newInfo);
259+
compilation.emitAsset(newFilename, output.source, newInfo);
289260
})()
290261
);
291262
}

0 commit comments

Comments
 (0)