Skip to content

Commit bf22024

Browse files
shahankitevilebottnawi
authored andcommitted
feat: add dir, name and ext placeholders in filename option (#144)
1 parent 0c35503 commit bf22024

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,20 @@ The target asset filename.
127127

128128
`[file]` is replaced with the original asset filename.
129129
`[path]` is replaced with the path of the original asset.
130+
`[dir]` is replaced with the directory of the original asset.
131+
`[name]` is replaced with the filename of the original asset.
132+
`[ext]` is replaced with the extension of the original asset.
130133
`[query]` is replaced with the query.
131134

132135
```js
133136
// in your webpack.config.js
134137
new CompressionPlugin({
135138
filename: '[path].gz[query]',
136139
});
140+
141+
new CompressionPlugin({
142+
filename: '[dir][name].gz[ext][query]',
143+
});
137144
```
138145

139146
#### `Function`

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Author Tobias Koppers @sokra
66
import os from 'os';
77
import crypto from 'crypto';
88
import url from 'url';
9+
import path from 'path';
910

1011
import async from 'neo-async';
1112
import RawSource from 'webpack-sources/lib/RawSource';
@@ -143,17 +144,22 @@ class CompressionPlugin {
143144
}
144145

145146
const parse = url.parse(file);
147+
const { pathname } = parse;
148+
const { dir, name, ext } = path.parse(pathname);
146149
const info = {
147150
file,
148-
path: parse.pathname,
151+
path: pathname,
152+
dir: dir ? `${dir}/` : '',
153+
name,
154+
ext,
149155
query: parse.query ? `?${parse.query}` : '',
150156
};
151157

152158
const newAssetName =
153159
typeof filename === 'function'
154160
? filename(info)
155161
: filename.replace(
156-
/\[(file|path|query)\]/g,
162+
/\[(file|path|query|dir|name|ext)\]/g,
157163
(p0, p1) => info[p1]
158164
);
159165

test/__snapshots__/filename-option.test.js.snap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,46 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`when applied with \`function\` option matches snapshot for \`[dir][name].super-compressed.gz[ext][query]\` value ({String}): assets 1`] = `
4+
Array [
5+
Array [
6+
"0.async.js?ver=81982f432266ef1edd1a",
7+
130,
8+
],
9+
Array [
10+
"0.async.super-compressed.gz.js?ver=81982f432266ef1edd1a",
11+
117,
12+
],
13+
Array [
14+
"5b1f36bc41ab31f5b801d48ba1d65781.png",
15+
78117,
16+
],
17+
Array [
18+
"5b1f36bc41ab31f5b801d48ba1d65781.super-compressed.gz.png",
19+
73160,
20+
],
21+
Array [
22+
"96621d3c37d96ad3bf792fcc848d912f.super-compressed.gz.svg",
23+
393,
24+
],
25+
Array [
26+
"96621d3c37d96ad3bf792fcc848d912f.svg",
27+
672,
28+
],
29+
Array [
30+
"js.js?var=81982f432266ef1edd1a",
31+
10221,
32+
],
33+
Array [
34+
"js.super-compressed.gz.js?var=81982f432266ef1edd1a",
35+
2802,
36+
],
37+
]
38+
`;
39+
40+
exports[`when applied with \`function\` option matches snapshot for \`[dir][name].super-compressed.gz[ext][query]\` value ({String}): errors 1`] = `Array []`;
41+
42+
exports[`when applied with \`function\` option matches snapshot for \`[dir][name].super-compressed.gz[ext][query]\` value ({String}): warnings 1`] = `Array []`;
43+
344
exports[`when applied with \`function\` option matches snapshot for \`[path].super-compressed.gz[query]\` value ({String}): assets 1`] = `
445
Array [
546
Array [

test/filename-option.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ describe('when applied with `function` option', () => {
3939
});
4040
});
4141

42+
it('matches snapshot for `[dir][name].super-compressed.gz[ext][query]` value ({String})', () => {
43+
new Plugin({
44+
minRatio: 1,
45+
filename: '[dir][name].super-compressed.gz[ext][query]',
46+
}).apply(compiler);
47+
48+
return compile(compiler).then((stats) => {
49+
const errors = stats.compilation.errors.map(cleanErrorStack);
50+
const warnings = stats.compilation.warnings.map(cleanErrorStack);
51+
52+
expect(errors).toMatchSnapshot('errors');
53+
expect(warnings).toMatchSnapshot('warnings');
54+
expect(getAssetsInfo(stats.compilation.assets)).toMatchSnapshot('assets');
55+
});
56+
});
57+
4258
it('matches snapshot for custom function ({Function})', () => {
4359
new Plugin({
4460
minRatio: 1,

0 commit comments

Comments
 (0)