-
Notifications
You must be signed in to change notification settings - Fork 0
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 #5 from nparashar150/setup/babel-webpack
Setup babel and webpack for JSX compilation to JS Object
- Loading branch information
Showing
14 changed files
with
289 additions
and
212 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
{ | ||
"presets": ["@babel/preset-env", "@babel/preset-react"] | ||
"presets": ["@babel/preset-env"], | ||
"plugins": [ | ||
[ | ||
"@babel/plugin-transform-react-jsx", | ||
{ | ||
"pragma": "OwnReact.createElement" | ||
} | ||
] | ||
] | ||
} |
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 @@ | ||
import { createElement } from "./createElement"; | ||
import { styleToString } from "./utils/styleToString"; | ||
import { render } from "./render"; | ||
|
||
const OwnReact = { | ||
createElement, | ||
styleToString, | ||
render, | ||
}; | ||
|
||
export default OwnReact; |
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,9 @@ | ||
/** | ||
* | ||
* @param {HTMLElement} element | ||
* @param {String} containerId | ||
*/ | ||
export const render = (element, containerId) => { | ||
const root = document.getElementById(containerId); | ||
root.appendChild(element); | ||
}; |
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 |
---|---|---|
|
@@ -5,17 +5,22 @@ | |
"repository": "https://github.com/nparashar150/own-react-lib", | ||
"author": "nparashar150 <[email protected]>", | ||
"scripts": { | ||
"dev": "webpack-dev-server --mode=development", | ||
"start": "webpack --mode=development", | ||
"build": "webpack --mode=production", | ||
"start": "webpack --mode=development", | ||
"dev": "webpack-dev-server --mode=development", | ||
"babel": "nodemon --watch ./src ./scripts/babel-webpack-compile.js", | ||
"lint": "eslint \"src/**/*.{js,jsx}\" \"lib/**/*.{js,jsx}\"", | ||
"lint:fix": "eslint \"src/**/*.{js,jsx}\" \"lib/**/*.{js,jsx}\" --fix" | ||
"lint:fix": "eslint \"src/**/*.{js,jsx}\" \"lib/**/*.{js,jsx}\" --fix", | ||
"babel-webpack-dev": "concurrently \"yarn babel\" \"yarn dev\"" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@babel/cli": "^7.19.3", | ||
"@babel/core": "^7.19.1", | ||
"@babel/plugin-transform-react-jsx": "^7.19.0", | ||
"@babel/preset-env": "^7.19.1", | ||
"@babel/preset-react": "^7.18.6", | ||
"babel-loader": "^8.2.5", | ||
"concurrently": "^7.4.0", | ||
"css-loader": "^6.7.1", | ||
"eslint": "^8.23.1", | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.__btn { | ||
cursor: pointer; | ||
font-weight: 600; | ||
font-size: 1.25rem; | ||
outline-offset: 1px; | ||
padding: 1.25rem 3rem; | ||
border-radius: 0.5rem; | ||
border: 1px solid #1a1a1a; | ||
outline: 1px solid transparent; | ||
transition: all 0.375s ease-in-out; | ||
} | ||
|
||
.__btn:hover, | ||
.__btn:focus { | ||
color: #fafafa; | ||
outline-offset: 1px; | ||
border: 1px solid #fafafa; | ||
background-color: #1a1a1a; | ||
outline: 1px solid #1a1a1a; | ||
} |
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,28 +1,11 @@ | ||
import { createElement } from "../../../lib/createElement"; | ||
import OwnReact from "../../../lib/index"; | ||
import "./button.css"; | ||
|
||
export const button = (parentElement, props, innerText) => { | ||
if (!parentElement) return; | ||
|
||
// styling in JS object is converted to CSS string | ||
const buttonStyles = { | ||
backgroundColor: "#EEE", | ||
color: "#1a1a1a", | ||
padding: "10px", | ||
border: "none", | ||
borderRadius: "5px", | ||
cursor: "pointer", | ||
const Button = (children) => { | ||
const handleClick = () => { | ||
alert("Button clicked"); | ||
}; | ||
|
||
const button = createElement({ | ||
element: "button", | ||
props: { | ||
...props, | ||
style: buttonStyles, | ||
}, | ||
state: {}, | ||
children: innerText, | ||
parentElement, | ||
}); | ||
|
||
return button; | ||
return <button className="__btn" onClick={() => handleClick()}>{children}</button>; | ||
}; | ||
|
||
export default Button; |
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,111 +1,18 @@ | ||
import { createElement } from "../lib/createElement"; | ||
import { button } from "./components/button/button"; | ||
import OwnReact from "../lib/index"; | ||
import Button from "./components/button/button"; | ||
|
||
(function createDOMNode() { | ||
const root = document.body; | ||
function handleOnClick() { | ||
alert("You have clicked twice!!"); | ||
} | ||
const divCreatedFromFunction = createElement({ | ||
// elementType | ||
element: "div", | ||
// props | ||
props: { | ||
id: "root", | ||
className: "root", | ||
ondblclick: handleOnClick, | ||
style: "text-align: center;", | ||
}, | ||
state: { | ||
// state | ||
name: "John Doe", | ||
}, | ||
children: [ | ||
// children | ||
{ | ||
element: "pre", // elementType | ||
props: { | ||
// props | ||
className: "heading", | ||
}, | ||
state: { | ||
// state | ||
name: "John Doe's child", | ||
}, | ||
children: [ | ||
// children | ||
{ | ||
// elementType | ||
element: "h1", | ||
props: { | ||
// props | ||
className: "heading", | ||
}, | ||
state: { | ||
// state | ||
name: "John Doe's child", | ||
}, | ||
// children | ||
children: "This is compiled under webpack", | ||
}, | ||
], | ||
}, | ||
], | ||
parentElement: root, | ||
}); | ||
|
||
// creating element inside element created from createElement function | ||
createElement({ | ||
// elementType | ||
element: "div", | ||
// props | ||
props: { id: "nestedElement", className: "nestedElement" }, | ||
state: { | ||
// state | ||
name: "John Doe", | ||
}, | ||
children: [ | ||
// children | ||
{ | ||
element: "pre", // elementType | ||
props: { | ||
// props | ||
className: "heading", | ||
}, | ||
state: { | ||
// state | ||
name: "John Doe's child", | ||
}, | ||
children: [ | ||
// children | ||
{ | ||
// elementType | ||
element: "h1", | ||
props: { | ||
// props | ||
className: "heading", | ||
}, | ||
state: { | ||
// state | ||
name: "John Doe's child", | ||
}, | ||
// children | ||
children: "Wow!! it supports nested Creation also", | ||
}, | ||
], | ||
}, | ||
], | ||
parentElement: divCreatedFromFunction, | ||
}); | ||
|
||
button( | ||
root, | ||
{ | ||
onclick: () => { | ||
alert("You have clicked me!!"); | ||
}, | ||
}, | ||
"Click Me" | ||
const App = () => { | ||
return ( | ||
<div id="root2"> | ||
<Button>Click me</Button> | ||
<h1>My First Own React App</h1> | ||
<p> | ||
<div className="testing"> | ||
<h1>My First Own React App with custom Babel and Webpack config</h1> | ||
<p>This is another nesting level</p> | ||
</div> | ||
</p> | ||
</div> | ||
); | ||
})(); | ||
export { createElement }; | ||
}; | ||
OwnReact.render(App(), "root"); |
Oops, something went wrong.