forked from tancredi/fantasticon
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for LESS asset type (#11)
- Loading branch information
Showing
8 changed files
with
199 additions
and
1 deletion.
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
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
61 changes: 61 additions & 0 deletions
61
src/generators/asset-types/__tests__/__snapshots__/less.ts.snap
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`\`LESS\` asset generator renders LESS correctly with \`selector\` option 1`] = ` | ||
"@test-font: "test"; | ||
@font-face { | ||
font-display: block; | ||
font-family: @test-font; | ||
src: "::src-attr::"; | ||
} | ||
.my-selector::before { | ||
font-family: test !important; | ||
font-style: normal; | ||
font-weight: normal !important; | ||
font-variant: normal; | ||
text-transform: none; | ||
line-height: 1; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
@test-map: { | ||
my-icon: "\\f101"; | ||
} | ||
.my-selector.tf-my-icon::before { | ||
content: @test-map[my-icon]; | ||
} | ||
" | ||
`; | ||
|
||
exports[`\`LESS\` asset generator renders LESS correctly with prefix and tag name options 1`] = ` | ||
"@test-font: "test"; | ||
@font-face { | ||
font-display: block; | ||
font-family: @test-font; | ||
src: "::src-attr::"; | ||
} | ||
b[class^="tf-"]::before, b[class*=" tf-"]::before { | ||
font-family: test !important; | ||
font-style: normal; | ||
font-weight: normal !important; | ||
font-variant: normal; | ||
text-transform: none; | ||
line-height: 1; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
@test-map: { | ||
my-icon: "\\f101"; | ||
} | ||
.tf-my-icon::before { | ||
content: @test-map[my-icon]; | ||
} | ||
" | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import lessGen from '../less'; | ||
import { renderSrcAttribute } from '../../../utils/css'; | ||
import { resolve } from 'path'; | ||
|
||
const renderSrcMock = renderSrcAttribute as any as jest.Mock; | ||
|
||
const mockOptions = { | ||
name: 'test', | ||
prefix: 'tf', | ||
tag: 'b', | ||
codepoints: { 'my-icon': 0xf101 }, | ||
assets: { 'my-icon': null }, | ||
templates: { | ||
less: resolve(__dirname, '../../../../templates/less.hbs') | ||
} | ||
} as any; | ||
|
||
jest.mock('../../../utils/css', () => ({ | ||
renderSrcAttribute: jest.fn(() => '"::src-attr::"') | ||
})); | ||
|
||
describe('`LESS` asset generator', () => { | ||
beforeEach(() => { | ||
renderSrcMock.mockClear(); | ||
}); | ||
|
||
test('renders LESS correctly with prefix and tag name options', async () => { | ||
expect( | ||
await lessGen.generate(mockOptions, Buffer.from('')) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
test('renders LESS correctly with `selector` option', async () => { | ||
expect( | ||
await lessGen.generate( | ||
{ ...mockOptions, selector: '.my-selector' }, | ||
Buffer.from('') | ||
) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
test('calls renderSrcAttribute correctly and includes its return value in the rendered template', async () => { | ||
const fontBuffer = Buffer.from('::svg-content::'); | ||
|
||
const result = await lessGen.generate(mockOptions, fontBuffer); | ||
|
||
expect(renderSrcMock).toHaveBeenCalledTimes(1); | ||
expect(renderSrcMock).toHaveBeenCalledWith(mockOptions, fontBuffer); | ||
expect(result).toContain('::src-attr::'); | ||
}); | ||
|
||
test('renders expected selector blocks', async () => { | ||
const less = await lessGen.generate(mockOptions, Buffer.from('')); | ||
|
||
expect(less).toContain( | ||
'b[class^="tf-"]::before, b[class*=" tf-"]::before {' | ||
); | ||
expect(less).toContain('.tf-my-icon::before {'); | ||
}); | ||
|
||
test('renders expected variables', async () => { | ||
const less = await lessGen.generate(mockOptions, Buffer.from('')); | ||
|
||
expect(less).toContain('@test-font:'); | ||
expect(less).toContain('@test-map:'); | ||
}); | ||
|
||
test('renders expected selector blocks with `selector` option', async () => { | ||
const less = await lessGen.generate( | ||
{ ...mockOptions, selector: '.my-selector' }, | ||
Buffer.from('') | ||
); | ||
|
||
expect(less).toContain('.my-selector::before {'); | ||
expect(less).toContain('.my-selector.tf-my-icon::before {'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { FontGenerator } from '../../types/generator'; | ||
import { FontAssetType } from '../../types/misc'; | ||
import { renderTemplate } from '../../utils/template'; | ||
import { renderSrcAttribute } from '../../utils/css'; | ||
|
||
const generator: FontGenerator<Buffer> = { | ||
dependsOn: FontAssetType.SVG, | ||
|
||
generate: (options, svg: Buffer) => | ||
renderTemplate( | ||
options.templates.less, | ||
{ ...options, fontSrc: renderSrcAttribute(options, svg) }, | ||
{ helpers: { codepoint: str => str.toString(16) } } | ||
) | ||
}; | ||
|
||
export default generator; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@{{ name }}-font: "{{ name }}"; | ||
|
||
@font-face { | ||
font-display: block; | ||
font-family: @{{ name }}-font; | ||
src: {{{ fontSrc }}}; | ||
} | ||
|
||
{{# if selector }} | ||
{{ selector }}::before { | ||
{{ else }} | ||
{{ tag }}[class^="{{prefix}}-"]::before, {{ tag }}[class*=" {{prefix}}-"]::before { | ||
{{/ if }} | ||
font-family: {{ name }} !important; | ||
font-style: normal; | ||
font-weight: normal !important; | ||
font-variant: normal; | ||
text-transform: none; | ||
line-height: 1; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
@{{ name }}-map: { | ||
{{# each codepoints }} | ||
{{ @key }}: "\\{{ codepoint this }}"; | ||
{{/ each }} | ||
} | ||
|
||
{{# each codepoints }} | ||
{{# if ../selector }} | ||
{{ ../selector }}.{{ ../prefix }}-{{ @key }}::before { | ||
{{ else }} | ||
{{ tag }}.{{ ../prefix }}-{{ @key }}::before { | ||
{{/ if }} | ||
content: @{{ ../name }}-map[{{ @key }}]; | ||
} | ||
{{/ each }} |