Skip to content

feat: allow filename to be a {Function} #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class MiniCssExtractPlugin {
renderedModules,
compilation.runtimeTemplate.requestShortener
),
filenameTemplate: this.options.filename,
filenameTemplate: this.getFilename(chunk, this.options.filename),
pathOptions: {
chunk,
contentHashType: NS,
Expand All @@ -194,7 +194,10 @@ class MiniCssExtractPlugin {
renderedModules,
compilation.runtimeTemplate.requestShortener
),
filenameTemplate: this.options.chunkFilename,
filenameTemplate: this.getFilename(
chunk,
this.options.chunkFilename
),
pathOptions: {
chunk,
contentHashType: NS,
Expand Down Expand Up @@ -260,7 +263,9 @@ class MiniCssExtractPlugin {
if (Object.keys(chunkMap).length > 0) {
const chunkMaps = chunk.getChunkMaps();
const linkHrefPath = mainTemplate.getAssetPath(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got three nested calls here. that's a nightmare for debugging. break these values out into separate consts and pass the variables to each function.

JSON.stringify(this.options.chunkFilename),
JSON.stringify(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a "safe" stringify done here, or at the very least this done in a try/catch that can safely trap and display a meaningful error message.

this.getFilename(chunk, this.options.chunkFilename)
),
{
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: (length) =>
Expand Down Expand Up @@ -366,6 +371,16 @@ class MiniCssExtractPlugin {
});
}

getFilename(chunk, filename) {
console.log('--chunk--');
console.log(chunk);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove console.log

return this.isFunction(filename) ? filename(chunk) : filename;
}

isFunction(functionToCheck) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and use typeof something === 'function'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need a separate function here, because it's only called once. Replace the isFunction call with the typeof check instead.

return typeof functionToCheck === 'function';
}

getCssChunkObject(mainChunk) {
const obj = {};
for (const chunk of mainChunk.getAllAsyncChunks()) {
Expand Down
1 change: 1 addition & 0 deletions test/cases/filename-function/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
2 changes: 2 additions & 0 deletions test/cases/filename-function/expected/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
body { background: red; }

2 changes: 2 additions & 0 deletions test/cases/filename-function/expected/this.is.app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
body { background: red; }

1 change: 1 addition & 0 deletions test/cases/filename-function/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
1 change: 1 addition & 0 deletions test/cases/filename-function/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { background: red; }
25 changes: 25 additions & 0 deletions test/cases/filename-function/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Self = require('../../../');

module.exports = {
entry: {
index: './index.js',
app: './app.js',
},
module: {
rules: [
{
test: /\.css$/,
use: [
Self.loader,
'css-loader',
],
},
],
},
plugins: [
new Self({
filename: (chunk) => chunk.name == 'app' ? 'this.is.app.css' : `[name].css`,
chunkFilename: (chunk) => '[name].css',
}),
],
};