New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/sui svg #422
Merged
Merged
Feat/sui svg #422
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
142511c
feat(sui-svg): first commit
2639e7e
feat(sui-svg): remove not needed dependencies
d8e7440
feat(sui-svg): update dependencies + new dev server approach
9371238
refactor(sui-svg): add linter ignore console.logs
5eeee00
refactor(sui-svg): add shell env
781302a
refactor(sui-svg): update param
d0d240b
feat(sui-bundler): add option to make no pre loaders run
e843eeb
docs(sui-bundler): add option docs
c3ad25f
docs(sui-svg): add Readme + link to tool in main readme
3939e11
chore(sui-svg): remove not needed dependency
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module.exports = function({config, preLoader}) { | ||
return { | ||
...config, | ||
module: { | ||
...config.module, | ||
rules: config.module.rules.filter( | ||
rule => preLoader || rule.enforce !== 'pre' | ||
) | ||
} | ||
} | ||
} |
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,2 @@ | ||
node_modules | ||
package-lock.json |
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,39 @@ | ||
# sui-svg | ||
|
||
> Converts your SVG files into React Components | ||
|
||
Features: | ||
|
||
- CLI for building the components | ||
- CLI for showing up a demo locally | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm i -SE @s-ui/svg | ||
``` | ||
|
||
## Usage | ||
|
||
Add bundling scripts to your **package.json** | ||
|
||
```json | ||
{ | ||
"name": "my-awesome-package", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"build": "sui-svg build", | ||
"start": "npm run build && sui-svg demo" | ||
} | ||
} | ||
``` | ||
|
||
## Expected folder structure | ||
|
||
A `src` folder with all the `.svg` files to be converted inside | ||
|
||
## Output | ||
|
||
A `lib` folder with the generated components will be created/overridden | ||
|
||
**Advice:** no `index.js` will be generated inside the lib folder, each component should be imported independently for performance reasons |
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,75 @@ | ||
#!/usr/bin/env node | ||
/* eslint no-console:0 */ | ||
const Walker = require('walker') | ||
const svgr = require('@svgr/core').default | ||
const fs = require('fs') | ||
const program = require('commander') | ||
const {join} = require('path') | ||
const template = require('../templates/pure-component') | ||
const toCamelCase = require('@s-ui/js/lib/string').toCamelCase | ||
|
||
const BASE_DIR = process.cwd() | ||
const COMPONENT_EXTENSION = 'js' | ||
const ENCODING = 'utf-8' | ||
const LIB_FOLDER = join(BASE_DIR, '.', 'lib') | ||
const SVG_FOLDER = join(BASE_DIR, '.', 'src') | ||
|
||
program | ||
.on('--help', () => { | ||
console.log(' Description:') | ||
console.log('') | ||
console.log(' Builds React lib based on svg files') | ||
console.log('') | ||
console.log(' Setup your repo with a svg folder') | ||
console.log(' Every svg file inside this folder will be converted into') | ||
console.log(' a React component') | ||
console.log('') | ||
}) | ||
.parse(process.argv) | ||
|
||
const convertFileName = function(fileName) { | ||
const camelFile = toCamelCase(fileName) | ||
return `${camelFile[0].toUpperCase()}${camelFile.slice(1)}` | ||
} | ||
|
||
const getLibFile = function(file) { | ||
const fileName = file.match('^.*/(.*).svg$')[1] | ||
return `${LIB_FOLDER}/${convertFileName(fileName)}.${COMPONENT_EXTENSION}` | ||
} | ||
|
||
function directoryExists(path) { | ||
try { | ||
return fs.statSync(path).isDirectory() | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
function ensureDirectoryExistence(dirname) { | ||
if (directoryExists(dirname)) { | ||
return true | ||
} | ||
fs.mkdirSync(dirname) | ||
} | ||
|
||
ensureDirectoryExistence(LIB_FOLDER) | ||
Walker(SVG_FOLDER).on('file', function(file) { | ||
console.log(`Converting ${file}`) | ||
fs.readFile(file, (err, data) => { | ||
if (err) throw err | ||
svgr( | ||
data, | ||
{ | ||
template, | ||
expandProps: false | ||
}, | ||
{componentName: 'SVGComponent'} | ||
).then(jsCode => { | ||
fs.writeFile(getLibFile(file), jsCode, ENCODING, function(error) { | ||
if (error) { | ||
throw error | ||
} | ||
}) | ||
}) | ||
}) | ||
}) |
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,24 @@ | ||
#!/usr/bin/env node | ||
/* eslint no-console:0 */ | ||
const program = require('commander') | ||
const path = require('path') | ||
const {getSpawnPromise} = require('@s-ui/helpers/cli') | ||
|
||
program | ||
.on('--help', () => { | ||
console.log(' Description:') | ||
console.log('') | ||
console.log(' Shows a demo of your SVG library') | ||
console.log('') | ||
}) | ||
.parse(process.argv) | ||
|
||
const devServerExec = require.resolve('@s-ui/bundler/bin/sui-bundler-dev') | ||
getSpawnPromise( | ||
devServerExec, | ||
['-c', path.join(__dirname, '..', 'src'), '--no-pre-loader'], | ||
{ | ||
shell: false, | ||
env: process.env | ||
} | ||
).then(process.exit, process.exit) |
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,13 @@ | ||
#!/usr/bin/env node | ||
/* eslint no-console:0 */ | ||
|
||
const program = require('commander') | ||
const pkg = require('../package.json') | ||
const version = pkg.version | ||
|
||
program.version(version, ' --version') | ||
|
||
program.command('build', 'Builds React components from svg files') | ||
program.command('demo', 'Loads a local static website') | ||
|
||
program.parse(process.argv) |
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,36 @@ | ||
{ | ||
"name": "@s-ui/svg", | ||
"version": "1.0.0", | ||
"description": "Converts svg files into React Components", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "exit 0" | ||
}, | ||
"bin": { | ||
"sui-svg": "./bin/sui-svg.js" | ||
}, | ||
"dependencies": { | ||
"@s-ui/component-peer-dependencies": "latest", | ||
"@compositor/kit": "1.0.47", | ||
"@s-ui/bundler": "3", | ||
"@s-ui/helpers": "1", | ||
"@s-ui/js": "1", | ||
"@svgr/core": "3.1.0", | ||
"commander": "2.19.0", | ||
"styled-components": "4.0.2", | ||
"walker": "1.0.7" | ||
}, | ||
"devDependencies": { | ||
"@s-ui/lint": "2", | ||
"validate-commit-msg": "2.14.0" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"eslintConfig": { | ||
"extends": [ | ||
"./node_modules/@s-ui/lint/eslintrc.js" | ||
] | ||
}, | ||
"sasslintConfig": "./node_modules/@s-ui/lint/sass-lint.yml" | ||
} |
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,25 @@ | ||
/* global __BASE_DIR__ */ | ||
|
||
import React from 'react' | ||
import {render} from 'react-dom' | ||
import PropTypes from 'prop-types' | ||
import {Library, Example} from '@compositor/kit' | ||
|
||
const ctx = require.context(`${__BASE_DIR__}/lib`, true, /\.*\.js$/) | ||
const FnToReactComponent = ({component: Component}) => <Component /> | ||
|
||
const App = () => ( | ||
<Library> | ||
{ctx.keys().map((key, idx) => ( | ||
<Example name={key} key={idx}> | ||
<FnToReactComponent component={ctx(key).default} /> | ||
</Example> | ||
))} | ||
</Library> | ||
) | ||
|
||
render(<App />, document.getElementById('app')) | ||
|
||
FnToReactComponent.propTypes = { | ||
component: PropTypes.func | ||
} |
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,12 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>SVG's</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
<body> | ||
<div id="app" /> | ||
</body> | ||
</html> |
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,33 @@ | ||
const template = (code, config, state) => { | ||
return `import React, {PureComponent} from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
export default class ${state.componentName} extends PureComponent { | ||
state = {render: this.props.ssr} | ||
|
||
componentDidMount() { | ||
if (this.state.render === false) { | ||
this.setState({render: true}) | ||
} | ||
} | ||
|
||
render() { | ||
if (this.state.render === false) return null | ||
|
||
return ( | ||
${code} | ||
) | ||
} | ||
} | ||
|
||
${state.componentName}.propTypes = { | ||
ssr: PropTypes.bool | ||
} | ||
|
||
${state.componentName}.defaultProps = { | ||
ssr: false | ||
} | ||
` | ||
} | ||
|
||
module.exports = template |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
???
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.
It is a dependency of Kit
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.
A peer dependency? :/ Aw men.