Skip to content

Commit 702b23e

Browse files
committed
feat: pass whole chunk to filename function
add example to README.md
1 parent 1f06b99 commit 702b23e

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ module.exports = {
268268
}
269269
```
270270

271+
#### Filename as function instead of string
272+
273+
By using a function instead of a string, you can use chunk data to customize the filename. This is particularly useful when dealing with multiple entry points and wanting to get more control out of the filename for a given entry point/chunk. In the example below, the we'll change the filename to output the css to a different directory.
274+
275+
```javascript
276+
const miniCssExtractPlugin = new MiniCssExtractPlugin({
277+
filename: ({ name, chunkhash }) =>
278+
`${name.replace('/js/', '/css/')}.[chunkhash:8].css`
279+
})
280+
```
281+
271282
#### Long Term Caching
272283

273284
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.

src/index.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,8 @@ class MiniCssExtractPlugin {
170170
(module) => module.type === MODULE_TYPE
171171
);
172172
const { filename } = this.options;
173-
let filenameTemplate = filename;
174-
if (typeof filename === 'function') {
175-
const {
176-
hash: chunkhash,
177-
contentHash: contenthash,
178-
name,
179-
id,
180-
} = chunk;
181-
182-
filenameTemplate = filename({
183-
chunkhash,
184-
contenthash,
185-
name,
186-
id,
187-
});
188-
}
173+
const filenameTemplate =
174+
typeof filename === 'function' ? filename(chunk) : filename;
189175

190176
if (renderedModules.length > 0) {
191177
result.push({

test/cases/filename/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ module.exports = {
1717
},
1818
plugins: [
1919
new Self({
20-
filename: ({ name, chunkhash }) =>
21-
`${name.replace('/js/', '/css/')}.${chunkhash.substring(0, 8)}.css`,
20+
filename: ({ name }) =>
21+
`${name.replace('/js/', '/css/')}.css`,
2222
}),
2323
],
2424
};

0 commit comments

Comments
 (0)