Skip to content

Commit 070a85f

Browse files
committed
💥Add commonjs, with-native, with-web options
1 parent 4533c64 commit 070a85f

File tree

9 files changed

+477
-263
lines changed

9 files changed

+477
-263
lines changed

README.md

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,61 @@
88

99
> Turns SVG files into React Native (Web) (+ optional ReasonML) components
1010
11+
## Install
12+
13+
```console
14+
npm install react-from-svg
15+
16+
# or
17+
18+
yarn add react-from-svg
19+
```
20+
1121
## Usage
1222

1323
```console
14-
react-from-svg [sourcePath] [outputPath] [--with-reason] [--remove-fill] [--remove-stroke] [--reason-module-path]
24+
react-from-svg --help
25+
26+
Usage
27+
$ react-from-svg <sourcePath> <outputPath> [--with-native|--with-web]
28+
29+
Options
30+
--with-native, -native Output code for react-native-svg
31+
--with-web, -web Output code for DOM. If --with-native is also used, will be output as .web.js files
32+
--with-reason, -bs Output ReasonML bindings code
33+
--remove-fill, -rf Remove all 'fill' properties from SVGs, convenient for icons
34+
--remove-stroke, -rs Remove all 'stroke' properties from SVGs, convenient for icons
35+
--commonjs, -cjs Export as commonjs instead of es6 import/export
36+
--bs-module-path, -bsp Allow to customise ReasonML output path
37+
38+
Example
39+
$ react-from-svg assets/svgs src/Svgs --remove-fill
1540
```
1641

1742
### Examples
1843

19-
Just React Native / React Native Web SVGs, with fill svg props removed, nice for
20-
icons
44+
#### React DOM, no options
2145

2246
```console
23-
react-from-svg assets/svgs src/Svgs --remove-fill
47+
react-from-svg assets/svgs src/Svgs --with-web
2448
```
2549

26-
React Native (Web) + ReasonML bindings SVGs
50+
#### React Native with fill svg props removed
2751

2852
```console
29-
react-from-svg assets/svgs src/Svgs --with-reason
53+
react-from-svg assets/svgs src/Svgs --with-native --remove-fill
3054
```
3155

32-
React Native (Web) + ReasonML bindings SVGs and absolute path : it's usefull if
33-
you defined a webpack alias and you don't generate your bucklescript output
34-
"in-source"
56+
#### React Native + ReasonML bindings SVGs
57+
58+
```console
59+
react-from-svg assets/svgs src/Svgs --with-native --with-reason
60+
```
61+
62+
#### React Native + ReasonML bindings SVGs and absolute path
63+
64+
It's usefull if you defined a webpack alias and you don't generate your
65+
bucklescript output `"in-source"`
3566

3667
```console
3768
react-from-svg assets/svgs src/Svgs --with-reason --reason-module-path=./src/components

__tests__/Test.re

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ open Expect;
55

66
let snap = (svg, ~removeFill, ~removeStroke) => {
77
svg
8-
->Transformer.transformSvg(removeFill, removeStroke)
8+
->Transformer.transformSvg(
9+
~removeFill,
10+
~removeStroke,
11+
~commonjs=false,
12+
~pascalCaseTag=true,
13+
~template=Templates.native,
14+
)
915
->expect
1016
->toMatchSnapshot;
1117
};

__tests__/__snapshots__/Test.proxy.js.snap

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import Svg, {
2525
Pattern,
2626
Mask,
2727
} from 'react-native-svg';
28-
2928
export default ({width, height, fill, stroke}) => {
3029
return (
3130
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"m15 3c-6.627 0-12 5.373-12 12 0 5.623 3.872 10.328 9.092 11.63-.056-.162-.092-.35-.092-.583v-2.051c-.487 0-1.303 0-1.508 0-.821 0-1.551-.353-1.905-1.009-.393-.729-.461-1.844-1.435-2.526-.289-.227-.069-.486.264-.451.615.174 1.125.596 1.605 1.222.478.627.703.769 1.596.769.433 0 1.081-.025 1.691-.121.328-.833.895-1.6 1.588-1.962-3.996-.411-5.903-2.399-5.903-5.098 0-1.162.495-2.286 1.336-3.233-.276-.94-.623-2.857.106-3.587 1.798 0 2.885 1.166 3.146 1.481.896-.307 1.88-.481 2.914-.481 1.036 0 2.024.174 2.922.483.258-.313 1.346-1.483 3.148-1.483.732.731.381 2.656.102 3.594.836.945 1.328 2.066 1.328 3.226 0 2.697-1.904 4.684-5.894 5.097 1.098.573 1.899 2.183 1.899 3.396v2.734c0 .104-.023.179-.035.268 4.676-1.639 8.035-6.079 8.035-11.315 0-6.627-5.373-12-12-12z\\"/></Svg>
@@ -59,7 +58,6 @@ import Svg, {
5958
Pattern,
6059
Mask,
6160
} from 'react-native-svg';
62-
6361
export default ({width, height, fill, stroke}) => {
6462
return (
6563
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"m15 3c-6.627 0-12 5.373-12 12 0 5.623 3.872 10.328 9.092 11.63-.056-.162-.092-.35-.092-.583v-2.051c-.487 0-1.303 0-1.508 0-.821 0-1.551-.353-1.905-1.009-.393-.729-.461-1.844-1.435-2.526-.289-.227-.069-.486.264-.451.615.174 1.125.596 1.605 1.222.478.627.703.769 1.596.769.433 0 1.081-.025 1.691-.121.328-.833.895-1.6 1.588-1.962-3.996-.411-5.903-2.399-5.903-5.098 0-1.162.495-2.286 1.336-3.233-.276-.94-.623-2.857.106-3.587 1.798 0 2.885 1.166 3.146 1.481.896-.307 1.88-.481 2.914-.481 1.036 0 2.024.174 2.922.483.258-.313 1.346-1.483 3.148-1.483.732.731.381 2.656.102 3.594.836.945 1.328 2.066 1.328 3.226 0 2.697-1.904 4.684-5.894 5.097 1.098.573 1.899 2.183 1.899 3.396v2.734c0 .104-.023.179-.035.268 4.676-1.639 8.035-6.079 8.035-11.315 0-6.627-5.373-12-12-12z\\"/></Svg>
@@ -93,7 +91,6 @@ import Svg, {
9391
Pattern,
9492
Mask,
9593
} from 'react-native-svg';
96-
9794
export default ({width, height, fill, stroke}) => {
9895
return (
9996
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"m15 3c-6.627 0-12 5.373-12 12 0 5.623 3.872 10.328 9.092 11.63-.056-.162-.092-.35-.092-.583v-2.051c-.487 0-1.303 0-1.508 0-.821 0-1.551-.353-1.905-1.009-.393-.729-.461-1.844-1.435-2.526-.289-.227-.069-.486.264-.451.615.174 1.125.596 1.605 1.222.478.627.703.769 1.596.769.433 0 1.081-.025 1.691-.121.328-.833.895-1.6 1.588-1.962-3.996-.411-5.903-2.399-5.903-5.098 0-1.162.495-2.286 1.336-3.233-.276-.94-.623-2.857.106-3.587 1.798 0 2.885 1.166 3.146 1.481.896-.307 1.88-.481 2.914-.481 1.036 0 2.024.174 2.922.483.258-.313 1.346-1.483 3.148-1.483.732.731.381 2.656.102 3.594.836.945 1.328 2.066 1.328 3.226 0 2.697-1.904 4.684-5.894 5.097 1.098.573 1.899 2.183 1.899 3.396v2.734c0 .104-.023.179-.035.268 4.676-1.639 8.035-6.079 8.035-11.315 0-6.627-5.373-12-12-12z\\"/></Svg>
@@ -127,7 +124,6 @@ import Svg, {
127124
Pattern,
128125
Mask,
129126
} from 'react-native-svg';
130-
131127
export default ({width, height, fill, stroke}) => {
132128
return (
133129
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\"/></Svg>
@@ -162,7 +158,6 @@ import Svg, {
162158
Pattern,
163159
Mask,
164160
} from 'react-native-svg';
165-
166161
export default ({width, height, fill, stroke}) => {
167162
return (
168163
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\"/></Svg>
@@ -197,7 +192,6 @@ import Svg, {
197192
Pattern,
198193
Mask,
199194
} from 'react-native-svg';
200-
201195
export default ({width, height, fill, stroke}) => {
202196
return (
203197
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\"/></Svg>
@@ -232,7 +226,6 @@ import Svg, {
232226
Pattern,
233227
Mask,
234228
} from 'react-native-svg';
235-
236229
export default ({width, height, fill, stroke}) => {
237230
return (
238231
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32\\"/></Svg>
@@ -267,7 +260,6 @@ import Svg, {
267260
Pattern,
268261
Mask,
269262
} from 'react-native-svg';
270-
271263
export default ({width, height, fill, stroke}) => {
272264
return (
273265
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\" stroke=\\"#000\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32\\"/></Svg>
@@ -302,7 +294,6 @@ import Svg, {
302294
Pattern,
303295
Mask,
304296
} from 'react-native-svg';
305-
306297
export default ({width, height, fill, stroke}) => {
307298
return (
308299
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M336 192h40a40 40 0 0140 40v192a40 40 0 01-40 40H136a40 40 0 01-40-40V232a40 40 0 0140-40h40M336 128l-80-80-80 80M256 321V48\\" fill=\\"none\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32\\"/></Svg>
@@ -337,7 +328,6 @@ import Svg, {
337328
Pattern,
338329
Mask,
339330
} from 'react-native-svg';
340-
341331
export default ({width, height, fill, stroke}) => {
342332
return (
343333
@@ -373,7 +363,6 @@ import Svg, {
373363
Pattern,
374364
Mask,
375365
} from 'react-native-svg';
376-
377366
export default ({width, height, fill, stroke}) => {
378367
return (
379368
@@ -409,7 +398,6 @@ import Svg, {
409398
Pattern,
410399
Mask,
411400
} from 'react-native-svg';
412-
413401
export default ({width, height, fill, stroke}) => {
414402
return (
415403
@@ -445,7 +433,6 @@ import Svg, {
445433
Pattern,
446434
Mask,
447435
} from 'react-native-svg';
448-
449436
export default ({width, height, fill, stroke}) => {
450437
return (
451438
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><G fillRule=\\"evenodd\\"><Path d=\\"m0 0h30v30h-30z\\"/><Path d=\\"m10.2955556 11.5277778h-3.39250004l-1.67166667-3.17944447h-2.22888889v3.17944447h-2.99916667v-11.48861113h5.1625c3.04833334 0 4.78555556 1.475 4.78555556 4.03166666 0 1.73722223-.72111111 3.01555556-2.04861111 3.70388889zm-7.2930556-9.09583336v3.52361112h2.17972222c1.21277778 0 1.9175-.62277778 1.9175-1.78638889 0-1.13083334-.70472222-1.73722223-1.9175-1.73722223zm8.8663889-2.39277777h9.0630555v2.39277777h-6.0638888v2.14694445h5.4738888v2.37638889l-5.4738888.01638889v2.16333333h6.2277777v2.3927778h-9.2269444z\\" fillRule=\\"nonzero\\" transform=\\"translate(7.5 15.833333)\\"/></G></Svg>
@@ -479,7 +466,6 @@ import Svg, {
479466
Pattern,
480467
Mask,
481468
} from 'react-native-svg';
482-
483469
export default ({width, height, fill, stroke}) => {
484470
return (
485471
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><G fillRule=\\"evenodd\\"><Path d=\\"m0 0h30v30h-30z\\"/><Path d=\\"m10.2955556 11.5277778h-3.39250004l-1.67166667-3.17944447h-2.22888889v3.17944447h-2.99916667v-11.48861113h5.1625c3.04833334 0 4.78555556 1.475 4.78555556 4.03166666 0 1.73722223-.72111111 3.01555556-2.04861111 3.70388889zm-7.2930556-9.09583336v3.52361112h2.17972222c1.21277778 0 1.9175-.62277778 1.9175-1.78638889 0-1.13083334-.70472222-1.73722223-1.9175-1.73722223zm8.8663889-2.39277777h9.0630555v2.39277777h-6.0638888v2.14694445h5.4738888v2.37638889l-5.4738888.01638889v2.16333333h6.2277777v2.3927778h-9.2269444z\\" fillRule=\\"nonzero\\" transform=\\"translate(7.5 15.833333)\\"/></G></Svg>
@@ -513,7 +499,6 @@ import Svg, {
513499
Pattern,
514500
Mask,
515501
} from 'react-native-svg';
516-
517502
export default ({width, height, fill, stroke}) => {
518503
return (
519504
<Svg viewBox=\\"0 0 30 30\\" width={width} height={height} fill={fill} stroke={stroke}><G fill=\\"none\\" fillRule=\\"evenodd\\"><Path d=\\"m0 0h30v30h-30z\\" fill=\\"#dd4b39\\"/><Path d=\\"m10.2955556 11.5277778h-3.39250004l-1.67166667-3.17944447h-2.22888889v3.17944447h-2.99916667v-11.48861113h5.1625c3.04833334 0 4.78555556 1.475 4.78555556 4.03166666 0 1.73722223-.72111111 3.01555556-2.04861111 3.70388889zm-7.2930556-9.09583336v3.52361112h2.17972222c1.21277778 0 1.9175-.62277778 1.9175-1.78638889 0-1.13083334-.70472222-1.73722223-1.9175-1.73722223zm8.8663889-2.39277777h9.0630555v2.39277777h-6.0638888v2.14694445h5.4738888v2.37638889l-5.4738888.01638889v2.16333333h6.2277777v2.3927778h-9.2269444z\\" fill=\\"#fff\\" fillRule=\\"nonzero\\" transform=\\"translate(7.5 15.833333)\\"/></G></Svg>
@@ -547,7 +532,6 @@ import Svg, {
547532
Pattern,
548533
Mask,
549534
} from 'react-native-svg';
550-
551535
export default ({width, height, fill, stroke}) => {
552536
return (
553537
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M256,160c16-63.16,76.43-95.41,208-96a15.94,15.94,0,0,1,16,16V368a16,16,0,0,1-16,16c-128,0-177.45,25.81-208,64-30.37-38-80-64-208-64-9.88,0-16-8.05-16-17.93V80A15.94,15.94,0,0,1,48,64C179.57,64.59,240,96.84,256,160Z\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/><Line x1=\\"256\\" y1=\\"160\\" x2=\\"256\\" y2=\\"448\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/></Svg>
@@ -582,7 +566,6 @@ import Svg, {
582566
Pattern,
583567
Mask,
584568
} from 'react-native-svg';
585-
586569
export default ({width, height, fill, stroke}) => {
587570
return (
588571
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M256,160c16-63.16,76.43-95.41,208-96a15.94,15.94,0,0,1,16,16V368a16,16,0,0,1-16,16c-128,0-177.45,25.81-208,64-30.37-38-80-64-208-64-9.88,0-16-8.05-16-17.93V80A15.94,15.94,0,0,1,48,64C179.57,64.59,240,96.84,256,160Z\\" stroke=\\"#000\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/><Line x1=\\"256\\" y1=\\"160\\" x2=\\"256\\" y2=\\"448\\" stroke=\\"#000\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/></Svg>
@@ -617,7 +600,6 @@ import Svg, {
617600
Pattern,
618601
Mask,
619602
} from 'react-native-svg';
620-
621603
export default ({width, height, fill, stroke}) => {
622604
return (
623605
<Svg viewBox=\\"0 0 512 512\\" width={width} height={height} fill={fill} stroke={stroke}><Path d=\\"M256,160c16-63.16,76.43-95.41,208-96a15.94,15.94,0,0,1,16,16V368a16,16,0,0,1-16,16c-128,0-177.45,25.81-208,64-30.37-38-80-64-208-64-9.88,0-16-8.05-16-17.93V80A15.94,15.94,0,0,1,48,64C179.57,64.59,240,96.84,256,160Z\\" fill=\\"none\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/><Line x1=\\"256\\" y1=\\"160\\" x2=\\"256\\" y2=\\"448\\" fill=\\"none\\" strokeLinecap=\\"round\\" strokeLinejoin=\\"round\\" strokeWidth=\\"32px\\"/></Svg>

bin.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
#!/usr/bin/env node
2+
"use strict";
3+
const meow = require("meow");
24

3-
function hasArg(args, option) {
4-
return Boolean(args.find(arg => arg[0] === option));
5-
}
6-
7-
function getArg(args, option) {
8-
const argWithValue = args.find(arg => arg[0] === option);
9-
return argWithValue && argWithValue.length == 2 ? argWithValue[1] : undefined;
10-
}
11-
12-
const args = process.argv.slice(0);
13-
args.shift(); // node
14-
args.shift(); // bin name
15-
const sourcePath = args.shift();
16-
const outputPath = args.shift();
5+
// UPDATE README IF YOU UPDATE THIS PLEASE
6+
const cli = meow(
7+
`
8+
Usage
9+
$ react-from-svg <sourcePath> <outputPath> [--with-native|--with-web]
1710
18-
const acceptedOptions = [
19-
"--with-reason",
20-
"--remove-fill",
21-
"--remove-stroke",
22-
"--reason-module-path",
23-
];
11+
Options
12+
--with-native, -native Output code for react-native-svg
13+
--with-web, -web Output code for DOM. If --with-native is also used, will be output as .web.js files
14+
--with-reason, -bs Output ReasonML bindings code
15+
--remove-fill, -rf Remove all 'fill' properties from SVGs, convenient for icons
16+
--remove-stroke, -rs Remove all 'stroke' properties from SVGs, convenient for icons
17+
--commonjs, -cjs Export as commonjs instead of es6 import/export
18+
--bs-module-path, -bsp Allow to customise ReasonML output path
2419
25-
const splitArgs = args.map(arg => arg.split("="));
20+
Example
21+
$ react-from-svg assets/svgs src/Svgs --remove-fill
22+
`,
23+
{
24+
flags: {
25+
"with-native": { type: "boolean", alias: "native" },
26+
"with-web": { type: "boolean", alias: "web" },
27+
"with-reason": { type: "boolean", alias: "bs" },
28+
"remove-fill": { type: "boolean", alias: "rf" },
29+
"remove-stroke": { type: "boolean", alias: "rs" },
30+
commonjs: { type: "boolean", alias: "cjs" },
31+
"bs-module-path": { type: "string", alias: "bsp" },
32+
},
33+
},
34+
);
2635

27-
if (!sourcePath) {
28-
throw new Error("source path is required");
29-
}
30-
if (!outputPath) {
31-
throw new Error("output path is required");
32-
}
33-
if (splitArgs.filter(arg => !acceptedOptions.includes(arg[0])).length > 0) {
34-
throw new Error("only accepted options are: " + acceptedOptions);
36+
if (cli.flags.withNative === undefined && cli.flags.withWeb === undefined) {
37+
console.error(
38+
"You should at least choose an option between --with-native or --with web!",
39+
);
40+
cli.showHelp();
41+
process.exit(1);
3542
}
3643

3744
///
@@ -68,12 +75,5 @@ setTimeout(function() {
6875
);
6976
}
7077

71-
transformer.make(
72-
sourcePath,
73-
outputPath,
74-
hasArg(splitArgs, "--with-reason"),
75-
hasArg(splitArgs, "--remove-fill"),
76-
hasArg(splitArgs, "--remove-stroke"),
77-
getArg(splitArgs, "--reason-module-path"),
78-
);
78+
transformer.make(cli.input, cli.flags);
7979
}, 0);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"dependencies": {
3838
"bs-platform": "^7.2.2",
3939
"glob": "^7.1.3",
40+
"meow": "^6.1.0",
4041
"mkdirp": "^0.5.1",
4142
"reason-future": "^2.4.0",
4243
"reason-react": "^0.7.0"

0 commit comments

Comments
 (0)