-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
771c43a
commit d57cb1c
Showing
6 changed files
with
81 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
interface Options { | ||
context: string, | ||
hashPrefix: string, | ||
context: string; | ||
hashPrefix: string; | ||
} | ||
|
||
type Generator = (localName: string, filepath: string) => string; | ||
|
||
declare function createGenerator(pattern: string, options?: Partial<Options>): Generator; | ||
declare function createGenerator( | ||
pattern: string, | ||
options?: Partial<Options> | ||
): Generator; | ||
|
||
export = createGenerator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,58 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const genericNames = require('../index'); | ||
const test = require('tape'); | ||
const path = require('path'); | ||
const genericNames = require("../index"); | ||
const test = require("tape"); | ||
const path = require("path"); | ||
|
||
// According to the CSS spec, identifiers cannot | ||
// start with a digit, two hyphens, or a hyphen | ||
// followed by a digit. | ||
// | ||
// relates: https://github.com/webpack/css-loader/commit/da27c07d0df25a38699344c13b6614c53a469fd9 | ||
|
||
test('identity', t => { | ||
const generate = genericNames('[local]'); | ||
test("identity", t => { | ||
const generate = genericNames("[local]"); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'foo'); | ||
t.equal(generate("foo", path.join(__dirname, "test/case/source.css")), "foo"); | ||
t.end(); | ||
}); | ||
|
||
test('leading digit', t => { | ||
const generate = genericNames('0[local]'); | ||
test("leading digit", t => { | ||
const generate = genericNames("0[local]"); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_0foo'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"_0foo" | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('leading digit in the token', t => { | ||
const generate = genericNames('[local]'); | ||
test("leading digit in the token", t => { | ||
const generate = genericNames("[local]"); | ||
|
||
t.equal(generate('0foo', path.join(__dirname, 'test/case/source.css')), '_0foo'); | ||
t.equal( | ||
generate("0foo", path.join(__dirname, "test/case/source.css")), | ||
"_0foo" | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('leading two hyphens', t => { | ||
const generate = genericNames('--[local]'); | ||
test("leading two hyphens", t => { | ||
const generate = genericNames("--[local]"); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_--foo'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"_--foo" | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('leading hyphen and digit', t => { | ||
const generate = genericNames('-0[local]'); | ||
test("leading hyphen and digit", t => { | ||
const generate = genericNames("-0[local]"); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), '_-0foo'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"_-0foo" | ||
); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,42 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
const genericNames = require('../index'); | ||
const test = require('tape'); | ||
const path = require('path'); | ||
const genericNames = require("../index"); | ||
const test = require("tape"); | ||
const path = require("path"); | ||
|
||
const pattern = '[name]__[local]___[hash:base64:5]'; | ||
const pattern = "[name]__[local]___[hash:base64:5]"; | ||
|
||
test('use `cwd` if no context was provided', t => { | ||
test("use `cwd` if no context was provided", t => { | ||
const generate = genericNames(pattern); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___3D34a'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"source__foo___3D34a" | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('generate distinct hash for the provided context', t => { | ||
const generate = genericNames(pattern, {context: path.join(__dirname, '/test') }); | ||
test("generate distinct hash for the provided context", t => { | ||
const generate = genericNames(pattern, { | ||
context: path.join(__dirname, "/test") | ||
}); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___19xFw'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"source__foo___19xFw" | ||
); | ||
t.end(); | ||
}); | ||
|
||
test('generate distinct hash for the provided hashPrefix', t => { | ||
const generate = genericNames(pattern, {context: path.join(__dirname, '/test'), hashPrefix: '--'}); | ||
test("generate distinct hash for the provided hashPrefix", t => { | ||
const generate = genericNames(pattern, { | ||
context: path.join(__dirname, "/test"), | ||
hashPrefix: "--" | ||
}); | ||
|
||
t.equal(generate('foo', path.join(__dirname, 'test/case/source.css')), 'source__foo___3T0Un'); | ||
t.equal( | ||
generate("foo", path.join(__dirname, "test/case/source.css")), | ||
"source__foo___3T0Un" | ||
); | ||
t.end(); | ||
}); |