Skip to content

Commit 138c493

Browse files
committed
feat(native): automatically convert inline style in native
Closes #588
1 parent 0f9dcb2 commit 138c493

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`svgo does not load runtime configuration with \`runtimeConfig: false\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
3+
exports[`svgo does not load runtime configuration with \`runtimeConfig: false\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><style>.svgo__shape{fill:red}</style><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\" class=\\"svgo__shape\\"/><path d=\\"M51 51 37 37\\" class=\\"svgo__shape\\"/></g></g></svg>"`;
44
5-
exports[`svgo does not remove viewBox with \`icon\` option 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
5+
exports[`svgo does not remove viewBox with \`icon\` option 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><style>.prefix__shape{fill:red}</style><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\" class=\\"prefix__shape\\"/><path d=\\"M51 51 37 37\\" class=\\"prefix__shape\\"/></g></g></svg>"`;
66
7-
exports[`svgo does not remove viewBox with when \`dimensions\` is false 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
7+
exports[`svgo does not remove viewBox with when \`dimensions\` is false 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><style>.prefix__shape{fill:red}</style><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\" class=\\"prefix__shape\\"/><path d=\\"M51 51 37 37\\" class=\\"prefix__shape\\"/></g></g></svg>"`;
88
9-
exports[`svgo optimizes svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
9+
exports[`svgo does remove style when \`native\` is true 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g stroke=\\"#063855\\" stroke-width=\\"2\\" class=\\"prefix__blocks\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><path d=\\"M51 37 37 51M51 51 37 37\\" style=\\"fill:red\\"/></g></svg>"`;
1010
11-
exports[`svgo supports \`config.svgoConfig\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g id=\\"Blocks\\" class=\\"blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
11+
exports[`svgo optimizes svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><style>.prefix__shape{fill:red}</style><g id=\\"prefix__Blocks\\" class=\\"prefix__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"prefix__Shape\\" class=\\"prefix__shape\\"/><path d=\\"M51 51 37 37\\" class=\\"prefix__shape\\"/></g></g></svg>"`;
1212
13-
exports[`svgo uses \`state.filePath\` to detect configuration 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51 37 37\\"/><style></style></g></g></svg>"`;
13+
exports[`svgo supports \`config.svgoConfig\` 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><style>.shape{fill:red}</style><g id=\\"Blocks\\" class=\\"blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"Shape\\" class=\\"shape\\"/><path d=\\"M51 51 37 37\\" class=\\"shape\\"/></g></g></svg>"`;
14+
15+
exports[`svgo uses \`state.filePath\` to detect configuration 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><style>.svgo__shape{fill:red}</style><g id=\\"svgo__Blocks\\" class=\\"svgo__blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37 37 51\\" id=\\"svgo__Shape\\" class=\\"svgo__shape\\"/><path d=\\"M51 51 37 37\\" class=\\"svgo__shape\\"/></g></g></svg>"`;

packages/plugin-svgo/src/config.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import { getSvgoConfig } from './config'
22

3+
const state = { componentName: 'Icon' }
4+
35
describe('#getSvgoConfig', () => {
46
describe('with no specific config', () => {
57
it('returns config with `prefixIds: true`', async () => {
68
const config = {}
7-
const state = {}
89
expect(await getSvgoConfig(config, state)).toEqual({
9-
plugins: ['preset-default', 'prefixIds'],
10+
plugins: [
11+
{
12+
name: 'preset-default',
13+
params: { overrides: {} },
14+
},
15+
'prefixIds',
16+
],
1017
})
1118
})
1219
})
1320

1421
describe('with `config.icons` enabled', () => {
1522
it('returns config with `removeViewBox: false`', async () => {
1623
const config = { icon: true }
17-
const state = {}
1824
expect(await getSvgoConfig(config, state)).toEqual({
1925
plugins: [
2026
{
@@ -30,7 +36,6 @@ describe('#getSvgoConfig', () => {
3036
describe('with `config.dimensions` disabled', () => {
3137
it('returns config with `removeViewBox: false`', async () => {
3238
const config = { dimensions: false }
33-
const state = {}
3439
expect(await getSvgoConfig(config, state)).toEqual({
3540
plugins: [
3641
{

packages/plugin-svgo/src/config.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ const explorer = cosmiconfigSync('svgo', {
1717
})
1818

1919
const getSvgoConfigFromSvgrConfig = (config: Config): any => {
20-
const preset =
21-
config.icon || config.dimensions === false
22-
? {
23-
name: 'preset-default',
24-
params: {
25-
overrides: {
26-
removeViewBox: false,
27-
},
28-
},
29-
}
30-
: 'preset-default'
20+
const params = { overrides: {} as any }
21+
if (config.icon || config.dimensions === false) {
22+
params.overrides.removeViewBox = false
23+
}
24+
if (config.native) {
25+
params.overrides.inlineStyles = {
26+
onlyMatchedOnce: false,
27+
}
28+
}
29+
3130
return {
32-
plugins: [preset, 'prefixIds'],
31+
plugins: [
32+
{
33+
name: 'preset-default',
34+
params,
35+
},
36+
'prefixIds',
37+
],
3338
}
3439
}
3540

packages/plugin-svgo/src/index.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ const baseSvg = `<?xml version="1.0" encoding="UTF-8"?>
99
<title>Dismiss</title>
1010
<desc>Created with Sketch.</desc>
1111
<defs></defs>
12+
<style>.shape{fill: red}</style>
1213
<g id="Blocks" class="blocks" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="square">
1314
<g id="Dismiss" stroke="#063855" stroke-width="2">
14-
<path d="M51,37 L37,51" id="Shape"></path>
15-
<path d="M51,51 L37,37" id="Shape"></path>
15+
<path d="M51,37 L37,51" id="Shape" class="shape"></path>
16+
<path d="M51,51 L37,37" id="Shape" class="shape"></path>
1617
<style>
1718
#Shape {}
1819
</style>
@@ -47,7 +48,6 @@ describe('svgo', () => {
4748
},
4849
state,
4950
)
50-
5151
expect(result).toMatchSnapshot()
5252
})
5353

@@ -67,19 +67,21 @@ describe('svgo', () => {
6767
{ svgo: true, runtimeConfig: false },
6868
{ ...state, filePath: path.join(__dirname, '../__fixtures__/svgo') },
6969
)
70-
7170
expect(result).toMatchSnapshot()
7271
})
7372

7473
it('does not remove viewBox with `icon` option', () => {
7574
const result = svgo(baseSvg, { svgo: true, icon: true }, state)
76-
7775
expect(result).toMatchSnapshot()
7876
})
7977

8078
it('does not remove viewBox with when `dimensions` is false', () => {
8179
const result = svgo(baseSvg, { svgo: true, dimensions: false }, state)
80+
expect(result).toMatchSnapshot()
81+
})
8282

83+
it('does remove style when `native` is true', () => {
84+
const result = svgo(baseSvg, { svgo: true, native: true }, state)
8385
expect(result).toMatchSnapshot()
8486
})
8587
})

0 commit comments

Comments
 (0)