Skip to content

Commit

Permalink
feat: implement regExp option
Browse files Browse the repository at this point in the history
  • Loading branch information
lkmill committed Jul 19, 2022
1 parent b53ae2a commit 541d280
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
interface Options {
context: string;
hashPrefix: string;
regExp?: RegExp;
}

type Generator = (localName: string, filepath: string) => string;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var path = require("path");
* @param {object} options
* @param {string} options.context
* @param {string} options.hashPrefix
* @param {RegExp} options.regExp
* @return {function}
*/
module.exports = function createGenerator(pattern, options) {
Expand Down Expand Up @@ -37,6 +38,7 @@ module.exports = function createGenerator(pattern, options) {
"\x00" +
localName,
context: context,
regExp: options.regExp,
};

var genericName = interpolateName(loaderContext, name, loaderOptions);
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ test("generate distinct hash for the provided hashPrefix", t => {
);
t.end();
});

test("use group matches if regExp was provided", t => {
const generate = genericNames('[1]__[2]', { regExp: /([^/]*)\/([^/]*)$/ })

t.equal(
generate("foo", path.join(__dirname, "test/case/source.css")),
"case__source-css"
);
t.end()
})

0 comments on commit 541d280

Please sign in to comment.