-
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.
remove explicit syntax and add SSR capability
- Loading branch information
Showing
17 changed files
with
1,609 additions
and
1,400 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,4 +1,4 @@ | ||
node_modules/ | ||
lib/ | ||
dist/ | ||
node_modules/ | ||
lib/ | ||
dist/ | ||
types/ |
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,4 +1,4 @@ | ||
src/ | ||
types/tsconfig.tsbuildinfo | ||
*.config.js | ||
src/ | ||
types/tsconfig.tsbuildinfo | ||
*.config.js | ||
tsconfig.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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Ryan Carniato | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
MIT License | ||
Copyright (c) 2018 Ryan Carniato | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,87 +1,87 @@ | ||
# Knockout JSX | ||
|
||
This library is a replacement for Knockout.js' renderer. It trades Knockout's data-bind and DOM traversing for precompiled JSX. Using these techniques allows for dramatic performance improvements across the board putting Knockout in the company of some of the fastest libraries. See [JS Frameworks Benchmark](https://github.com/krausest/js-framework-benchmark). | ||
|
||
It accomplishes this with using [Babel Plugin JSX DOM Expressions](https://github.com/ryansolid/babel-plugin-jsx-dom-expressions). It compiles JSX to DOM statements and by using inner parenthesis syntax ```{( )}``` wraps expressions in functions that can be called by the library of choice. In this case ko.computed wrap these expressions ensuring the view stays up to date. Unlike Virtual DOM only the changed nodes are affected and the whole tree is not re-rendered over and over. | ||
|
||
To use include 'babel-plugin-jsx-dom-expressions' in your babelrc, webpack babel loader, or rollup babel plugin | ||
|
||
```js | ||
"plugins": [["jsx-dom-expressions", {moduleName: 'ko-jsx'}]] | ||
``` | ||
|
||
# Installation | ||
```sh | ||
> npm install ko-jsx babel-plugin-jsx-dom-expressions | ||
``` | ||
|
||
## API | ||
|
||
There is no ko.applyBinding. Instead the app starts with: | ||
|
||
```js | ||
render(AppViewModel, mountEl) | ||
``` | ||
|
||
For example: | ||
|
||
```jsx | ||
import { render } from 'ko-jsx' | ||
|
||
const Greeter = ({name, onClick}) => | ||
<div onClick={onClick}>Hello {(name() || 'World')}</div> | ||
|
||
function App() { | ||
const name = ko.observable('John'); | ||
return <> | ||
<h1>Greeting Example</h1> | ||
<Greeter name={name} onClick={() => name('Jake')}/> | ||
</>; | ||
} | ||
|
||
render(App, document.getElementById('main')); | ||
``` | ||
|
||
Control flow works the way you generally would JSX. However for performant list rendering I have added a fn on `subscribable` called `memoMap` that will optimally handle arrays. | ||
|
||
```jsx | ||
const list = ko.observableArray(["Alpha", "Beta", "Gamma"]) | ||
|
||
<ul>{ | ||
list.memoMap(item => <li>{item}</li>) | ||
}</ul> | ||
``` | ||
### Example | ||
[Counter](https://codesandbox.io/s/knockout-jsx-counter-dqtc2) | ||
|
||
### Non-Precompiled | ||
|
||
For those who do not wish to use Babel to precompile, the Knockout JSX supports Tagged Template Literals or HyperScript render APIs. These are available when you install the companion library and throw import of 'ko-jsx/html' and 'ko-jsx/h'. Refer to the docs on [Lit DOM Expressions](https://github.com/ryansolid/lit-dom-expressions), and [Hyper DOM Expressions](https://github.com/ryansolid/hyper-dom-expressions), respectively. | ||
|
||
```js | ||
import ko from 'knockout'; | ||
import { h, render } from 'ko-jsx/h'; | ||
|
||
const Greeter = (name, onclick) => | ||
h('div', {onclick}, 'Hello', () => name() || 'World'); | ||
|
||
function App() { | ||
const name = ko.observable('John'); | ||
return h('div', [ | ||
h('h1', 'Greeting Example'), | ||
h(Greeter, {name, onclick: () => name('Jake')}) | ||
]); | ||
} | ||
|
||
render(App, document.getElementById('main')); | ||
``` | ||
|
||
## Compatibility | ||
|
||
This does not use any of the Knockout render chain so data-bindings and custom bindings don't work. Knockout Components won't work. Essentially this library only takes the observable change detection part of Knockout. It is compatible with Webcomponents in general. In theory you could call ko.applyBinding and set data-bind attribute value but it seems wasted. | ||
|
||
## Why? | ||
|
||
Knockout.js at it's core is an elegant and efficient solution to tracking change detection. It also as far as modern declarative javascript libraries is one of the oldest. While it's continued to improve over time, in recent years Virtual DOM approaches have gained more popularity. Conceptually it always seemed that Knockout should outperform those techniques but in many areas it's been a dog in benchmarks. After seeing the great [Surplus.js](https://github.com/adamhaile/surplus) it was clear that these sort of libraries still had steam. In the process of working through my own library I realized the approaches used could be generalized to any fine grained library. | ||
|
||
# Knockout JSX | ||
|
||
This library is a replacement for Knockout.js' renderer. It trades Knockout's data-bind and DOM traversing for precompiled JSX. Using these techniques allows for dramatic performance improvements across the board putting Knockout in the company of some of the fastest libraries. See [JS Frameworks Benchmark](https://github.com/krausest/js-framework-benchmark). | ||
|
||
It accomplishes this with using [Babel Plugin JSX DOM Expressions](https://github.com/ryansolid/babel-plugin-jsx-dom-expressions). It compiles JSX to DOM statements and wraps expressions in functions that can be called by the library of choice. In this case ko.computed wrap these expressions ensuring the view stays up to date. Unlike Virtual DOM only the changed nodes are affected and the whole tree is not re-rendered over and over. | ||
|
||
To use include 'babel-plugin-jsx-dom-expressions' in your babelrc, webpack babel loader, or rollup babel plugin | ||
|
||
```js | ||
"plugins": [["jsx-dom-expressions", {moduleName: 'ko-jsx'}]] | ||
``` | ||
|
||
# Installation | ||
```sh | ||
> npm install ko-jsx babel-plugin-jsx-dom-expressions | ||
``` | ||
|
||
## API | ||
|
||
There is no ko.applyBinding. Instead the app starts with: | ||
|
||
```js | ||
render(AppViewModel, mountEl) | ||
``` | ||
|
||
For example: | ||
|
||
```jsx | ||
import { render } from 'ko-jsx' | ||
|
||
const Greeter = ({name, onClick}) => | ||
<div onClick={onClick}>Hello {name() || 'World'}</div> | ||
|
||
function App() { | ||
const name = ko.observable('John'); | ||
return <> | ||
<h1>Greeting Example</h1> | ||
<Greeter name={name} onClick={() => name('Jake')}/> | ||
</>; | ||
} | ||
|
||
render(App, document.getElementById('main')); | ||
``` | ||
|
||
Control flow works the way you generally would JSX. However for performant list rendering I have added a fn on `subscribable` called `memoMap` that will optimally handle arrays. | ||
|
||
```jsx | ||
const list = ko.observableArray(["Alpha", "Beta", "Gamma"]) | ||
|
||
<ul>{ | ||
list.memoMap(item => <li>{item}</li>) | ||
}</ul> | ||
``` | ||
### Example | ||
[Counter](https://codesandbox.io/s/knockout-jsx-counter-dqtc2) | ||
|
||
### Non-Precompiled | ||
|
||
For those who do not wish to use Babel to precompile, the Knockout JSX supports Tagged Template Literals or HyperScript render APIs. These are available when you install the companion library and throw import of 'ko-jsx/html' and 'ko-jsx/h'. Refer to the docs on [Lit DOM Expressions](https://github.com/ryansolid/lit-dom-expressions), and [Hyper DOM Expressions](https://github.com/ryansolid/hyper-dom-expressions), respectively. | ||
|
||
```js | ||
import ko from 'knockout'; | ||
import { h, render } from 'ko-jsx/h'; | ||
|
||
const Greeter = (name, onclick) => | ||
h('div', {onclick}, 'Hello', () => name() || 'World'); | ||
|
||
function App() { | ||
const name = ko.observable('John'); | ||
return h('div', [ | ||
h('h1', 'Greeting Example'), | ||
h(Greeter, {name, onclick: () => name('Jake')}) | ||
]); | ||
} | ||
|
||
render(App, document.getElementById('main')); | ||
``` | ||
|
||
## Compatibility | ||
|
||
This does not use any of the Knockout render chain so data-bindings and custom bindings don't work. Knockout Components won't work. Essentially this library only takes the observable change detection part of Knockout. It is compatible with Webcomponents in general. In theory you could call ko.applyBinding and set data-bind attribute value but it seems wasted. | ||
|
||
## Why? | ||
|
||
Knockout.js at it's core is an elegant and efficient solution to tracking change detection. It also as far as modern declarative javascript libraries is one of the oldest. While it's continued to improve over time, in recent years Virtual DOM approaches have gained more popularity. Conceptually it always seemed that Knockout should outperform those techniques but in many areas it's been a dog in benchmarks. After seeing the great [Surplus.js](https://github.com/adamhaile/surplus) it was clear that these sort of libraries still had steam. In the process of working through my own library I realized the approaches used could be generalized to any fine grained library. | ||
|
||
Mostly this library is a demonstration of a concept. Fine grained detection libraries shouldn't shy from the technological advancement Virtual DOM libraries brought, when even the oldest of the fine grained libraries still has considerable game in this light. |
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,10 +1,10 @@ | ||
module.exports = { | ||
output: 'src/runtime.js', | ||
includeTypes: true, | ||
variables: { | ||
imports: [ | ||
`import { computed as wrap } from './core'` | ||
], | ||
includeContext: false | ||
} | ||
module.exports = { | ||
output: 'src/runtime.js', | ||
includeTypes: true, | ||
variables: { | ||
imports: [ | ||
`import { computed as wrap } from './core'` | ||
], | ||
includeContext: false | ||
} | ||
} |
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,7 +1,7 @@ | ||
{ | ||
"name": "ko-jsx/h", | ||
"main": "../lib/h.js", | ||
"module": "../dist/h.js", | ||
"types": "../types/h.d.ts", | ||
"sideEffects": false | ||
{ | ||
"name": "ko-jsx/h", | ||
"main": "../lib/h.js", | ||
"module": "../dist/h.js", | ||
"types": "../types/h.d.ts", | ||
"sideEffects": false | ||
} |
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,7 +1,7 @@ | ||
{ | ||
"name": "ko-jsx/html", | ||
"main": "../lib/html.js", | ||
"module": "../dist/html.js", | ||
"types": "../types/html.d.ts", | ||
"sideEffects": false | ||
{ | ||
"name": "ko-jsx/html", | ||
"main": "../lib/html.js", | ||
"module": "../dist/html.js", | ||
"types": "../types/html.d.ts", | ||
"sideEffects": false | ||
} |
Oops, something went wrong.