diff --git a/src/index.js b/src/index.js index 6aa5436f..74e14351 100644 --- a/src/index.js +++ b/src/index.js @@ -170,7 +170,11 @@ class MiniCssExtractPlugin { renderedModules, compilation.runtimeTemplate.requestShortener ), - filenameTemplate: this.options.filename, + filenameTemplate: this.getFilename( + chunk, + this.options.filename, + this.options.processedFilename + ), pathOptions: { chunk, contentHashType: NS, @@ -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, @@ -260,7 +268,13 @@ class MiniCssExtractPlugin { if (Object.keys(chunkMap).length > 0) { const chunkMaps = chunk.getChunkMaps(); const linkHrefPath = mainTemplate.getAssetPath( - JSON.stringify(this.options.chunkFilename), + JSON.stringify( + this.getFilename( + chunk, + this.options.chunkFilename, + this.options.processedChunkFilename + ) + ), { hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`, hashWithLength: (length) => @@ -366,6 +380,19 @@ class MiniCssExtractPlugin { }); } + getFilename(chunk, filename, processedFilename) { + if (!processedFilename) { + processedFilename = this.isFunction(filename) + ? filename(chunk) + : filename; + } + return processedFilename; + } + + isFunction(functionToCheck) { + return typeof functionToCheck === 'function'; + } + getCssChunkObject(mainChunk) { const obj = {}; for (const chunk of mainChunk.getAllAsyncChunks()) { diff --git a/test/cases/filename-function/app.js b/test/cases/filename-function/app.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/filename-function/app.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/filename-function/expected/index.css b/test/cases/filename-function/expected/index.css new file mode 100644 index 00000000..aea53e43 --- /dev/null +++ b/test/cases/filename-function/expected/index.css @@ -0,0 +1,2 @@ +body { background: red; } + diff --git a/test/cases/filename-function/expected/this.is.app.css b/test/cases/filename-function/expected/this.is.app.css new file mode 100644 index 00000000..aea53e43 --- /dev/null +++ b/test/cases/filename-function/expected/this.is.app.css @@ -0,0 +1,2 @@ +body { background: red; } + diff --git a/test/cases/filename-function/index.js b/test/cases/filename-function/index.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/filename-function/index.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/filename-function/style.css b/test/cases/filename-function/style.css new file mode 100644 index 00000000..31fc5b8a --- /dev/null +++ b/test/cases/filename-function/style.css @@ -0,0 +1 @@ +body { background: red; } diff --git a/test/cases/filename-function/webpack.config.js b/test/cases/filename-function/webpack.config.js new file mode 100644 index 00000000..6f0c267f --- /dev/null +++ b/test/cases/filename-function/webpack.config.js @@ -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', + }), + ], +};