Skip to content

Commit 80c8f28

Browse files
committed
feat: pass whole chunk to filename function
add example to README.md
1 parent fab6de3 commit 80c8f28

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
@@ -342,6 +342,17 @@ module.exports = {
342342
};
343343
```
344344

345+
#### Filename as function instead of string
346+
347+
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.
348+
349+
```javascript
350+
const miniCssExtractPlugin = new MiniCssExtractPlugin({
351+
filename: ({ name, chunkhash }) =>
352+
`${name.replace('/js/', '/css/')}.[chunkhash:8].css`
353+
})
354+
```
355+
345356
#### Long Term Caching
346357

347358
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
@@ -186,22 +186,8 @@ class MiniCssExtractPlugin {
186186
(module) => module.type === MODULE_TYPE
187187
);
188188
const { filename } = this.options;
189-
let filenameTemplate = filename;
190-
if (typeof filename === 'function') {
191-
const {
192-
hash: chunkhash,
193-
contentHash: contenthash,
194-
name,
195-
id,
196-
} = chunk;
197-
198-
filenameTemplate = filename({
199-
chunkhash,
200-
contenthash,
201-
name,
202-
id,
203-
});
204-
}
189+
const filenameTemplate =
190+
typeof filename === 'function' ? filename(chunk) : filename;
205191

206192
if (renderedModules.length > 0) {
207193
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)