-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #629 from gregberge/rewriting-docs
docs: rewriting
- Loading branch information
Showing
19 changed files
with
40,491 additions
and
350 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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,114 @@ | ||
--- | ||
section: Usage | ||
title: CLI | ||
order: 10 | ||
title: Command Line | ||
order: 20 | ||
--- | ||
|
||
# CLI | ||
# Command Line | ||
|
||
SVGR can be run from the CLI. Run it without argument to see the [options](/docs/options). | ||
Use SVGR from command line to transform a single file or a whole directory. | ||
|
||
<carbon-ad /> | ||
|
||
## Install | ||
|
||
```bash | ||
npm install @svgr/cli --save-dev | ||
Depending you usage, you can install `@svgr/cli` locally in your project: | ||
|
||
```sh | ||
npm install --save-dev @svgr/cli | ||
# or use yarn | ||
yarn add @svgr/cli --dev | ||
yarn add --dev @svgr/cli | ||
``` | ||
|
||
## Usage | ||
or use it on the fly using `npx @svgr/cli`. | ||
|
||
```bash | ||
npx @svgr/cli icons/clock-icon.svg | ||
``` | ||
## Options | ||
|
||
## Recipes | ||
All [SVGR options](https://react-svgr.com/docs/options/) are available from command line or in configuration. Command line options have precedence over config file options. | ||
|
||
### Transform a whole directory | ||
## Transform a single file | ||
|
||
A whole directory can be processed, all SVG files (matching `.svg` or `.SVG`) | ||
are transformed into React components. | ||
### From file | ||
|
||
Transforms a single file by specifying file as the single argument. | ||
|
||
```sh | ||
# Usage: npx @svgr/cli [-d out-dir] [--ignore-existing] [src-dir] | ||
$ npx @svgr/cli -d icons icons | ||
icons/web/clock-icon.svg -> icons/web/ClockIcon.js | ||
icons/web/wifi-icon.svg -> icons/web/WifiIcon.js | ||
icons/spinner/cog-icon.svg -> icons/spinner/CogIcon.js | ||
icons/spinner/spinner-icon.svg -> icons/spinner/SpinnerIcon.js | ||
npx @svgr/cli -- icons/clock-icon.svg | ||
``` | ||
|
||
> Add `--ignore-existing` to avoid overriding existing files. | ||
### From standard input | ||
|
||
### Use stdin | ||
`@svgr/cli` supports standard input, just redirect file contents using `<` operator. | ||
|
||
``` | ||
$ npx @svgr/cli < icons/web/wifi-icon.svg | ||
```sh | ||
npx @svgr/cli < icons/clock-icon.svg | ||
``` | ||
|
||
### Use stdin / stdout | ||
### Output the result in another file | ||
|
||
``` | ||
$ npx @svgr/cli < icons/web/wifi-icon.svg > icons/web/WifiIcon.js | ||
``` | ||
You can easily create another file using `>` operator. | ||
|
||
### Transform icons | ||
```sh | ||
npx @svgr/cli -- icons/clock-icon.svg > dist/ClockIcon.js | ||
``` | ||
|
||
To create icons, two options are important: | ||
## Transform a whole directory | ||
|
||
- `--icon`: viewBox is preserved and SVG inherits text size | ||
- `--replace-attr-values "#000=currentColor"`: "#000" is replaced by | ||
"currentColor" and SVG inherits text color | ||
Transforms a whole directory using `--out-dir` option. All SVG presents in this directory tree are transformed into React components. | ||
|
||
``` | ||
$ npx @svgr/cli --icon --replace-attr-values "#000=currentColor" my-icon.svg | ||
```sh | ||
# Usage: npx @svgr/cli [--out-dir dir] [--ignore-existing] [src-dir] | ||
$ npx @svgr/cli --out-dir dist -- icons | ||
icons/web/clock-icon.svg -> dist/web/ClockIcon.js | ||
icons/web/wifi-icon.svg -> dist/web/WifiIcon.js | ||
icons/spinner/cog-icon.svg -> dist/spinner/CogIcon.js | ||
icons/spinner/spinner-icon.svg -> dist/spinner/SpinnerIcon.js | ||
``` | ||
|
||
> You can replace several values using `,` as separator: `--replace-attr-values "#000=currentColor,#fff=currentColor"` | ||
### Avoid replacing existing files | ||
|
||
## Target React Native | ||
Even if it is not recommended, you may be tempted to modify generated files. If you do so, you should use the `--ignore-existing` option to avoid replacing existing files. | ||
|
||
It is possible to target React Native using [react-native-svg](https://github.com/react-native-community/react-native-svg). | ||
|
||
``` | ||
$ npx @svgr/cli --native my-icon.svg | ||
```sh | ||
npx @svgr/cli --out-dir dist --ignore-existing -- icons | ||
``` | ||
|
||
## Use a specific template | ||
### Use a different filename case | ||
|
||
You can use a specific template. | ||
By default, filenames uses "PascalCase", you can specify a different case for generated files using `--filename-case` option. | ||
|
||
```sh | ||
npx @svgr/cli --out-dir dist --filename-case kebab -- icons | ||
``` | ||
$ npx @svgr/cli --template path/to/template.js my-icon.svg | ||
|
||
### Disable index generation | ||
|
||
By default an index is generated, giving you opportunity to import all your components from the directory. If you does not want auto-generated index, turn it off using `--no-index` option. | ||
|
||
```sh | ||
npx @svgr/cli --out-dir dist --no-index -- icons | ||
``` | ||
|
||
An example of template: | ||
### Use a custom index template | ||
|
||
Advanced use cases could lead you to customize the index template. The `--index-template <template>` argument let you specify a custom one. | ||
|
||
```js | ||
function template( | ||
{ template }, | ||
opts, | ||
{ imports, componentName, props, jsx, exports }, | ||
) { | ||
return template.ast` | ||
${imports} | ||
const ${componentName} = (${props}) => ${jsx} | ||
${exports} | ||
` | ||
// index-template.js | ||
const path = require('path') | ||
|
||
function defaultIndexTemplate(filePaths) { | ||
const exportEntries = filePaths.map((filePath) => { | ||
const basename = path.basename(filePath, path.extname(filePath)) | ||
const exportName = /^\d/.test(basename) ? `Svg${basename}` : basename | ||
return `export { default as ${exportName} } from './${basename}'` | ||
}) | ||
return exportEntries.join('\n') | ||
} | ||
|
||
module.exports = template | ||
module.exports = defaultIndexTemplate | ||
``` | ||
|
||
Template must return a Babel AST, the template function take three arguments: | ||
|
||
- `api`: API methods provided by Babel | ||
- `opts`: SVGR options | ||
- `values`: Pre-computed values to use (or not) in your templates | ||
```sh | ||
npx @svgr/cli --out-dir dist --index-template index-template.js -- icons | ||
``` |
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
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
Oops, something went wrong.
e9469c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: