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 all 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
33 changes: 30 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ class MiniCssExtractPlugin {
renderedModules,
compilation.runtimeTemplate.requestShortener
),
filenameTemplate: this.options.filename,
filenameTemplate: this.getFilename(
chunk,
this.options.filename,
this.options.processedFilename
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this option? There is no documentation about it and it is not used by the test cases.

),
pathOptions: {
chunk,
contentHashType: NS,
Expand All @@ -194,7 +198,11 @@ class MiniCssExtractPlugin {
renderedModules,
compilation.runtimeTemplate.requestShortener
),
filenameTemplate: this.options.chunkFilename,
filenameTemplate: this.getFilename(
chunk,
this.options.chunkFilename,
this.options.processedChunkFilename
),
pathOptions: {
chunk,
contentHashType: NS,
Expand Down Expand Up @@ -260,7 +268,13 @@ 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,

Choose a reason for hiding this comment

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

use destructuring instead of repeating this.options. the same comment applies to several places in this file.

this.options.processedChunkFilename
)
),
{
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: (length) =>
Expand Down Expand Up @@ -366,6 +380,19 @@ class MiniCssExtractPlugin {
});
}

getFilename(chunk, filename, processedFilename) {

Choose a reason for hiding this comment

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

Is filename actually a full or partial path? If so, follow the node convention and call this path.

If processedFilename the path after a transformation? If so, then this should be the path/filename and the non-transformed path/filename should be the resultPath/resultFilename.

I'd like to see this use a single opts parameter, and the signature change to { chunk, fileName, resultFileName }, or if filename is actually a path, { chunk, path, resultPath }

if (!processedFilename) {

Choose a reason for hiding this comment

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

we don't need to do an if with a ternary here:

    processedFilename = processedFilename || this.isFunction(filename)
    ? filename(chunk)
    : filename;

processedFilename = this.isFunction(filename)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use return here. Reassigning parameters result in deopt.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use typeof instead please?

Choose a reason for hiding this comment

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

I second the parameter reassign, and did not include it in my review because it is mentioned here. this should not be done, and the linter should be throwing a warning there about it. Run npm lint before the next commit to assure all warnings have been mitigated.

? filename(chunk)
: filename;
}
return processedFilename;
}

isFunction(functionToCheck) {

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',
}),
],
};