Skip to content

Commit

Permalink
Refactors esbuild config
Browse files Browse the repository at this point in the history
Implements pre publish script
  • Loading branch information
JesuHrz committed Feb 5, 2023
1 parent 5896428 commit 534d7c7
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 188 deletions.
44 changes: 33 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,25 @@
</p>

# KILLA
Killa is a small and lightweight state management library for vanilla and soon for React.

```bash
npm install killa
```

### Installing for the Browser
To use directly minified version in the browser:

```html
<script src="https://unpkg.com/[email protected]/dist/killa.min.js"></script>
```

Or from jsdelivr:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/killa.min.js"></script>
```

## How to create your first store

In order to create your first store you need to provide an object which will manage your state. **(The internal state is inmutable)**
Expand Down Expand Up @@ -38,7 +52,7 @@ store.getState() // { counter: 0 }
```

## How to update your store

****
```js
import killa from 'killa'

Expand All @@ -53,15 +67,15 @@ store.setState(() => {
store.getState() // { counter: 1 }
```

## How to subscribe to events
## How to subscribe to state events

```js
import killa from 'killa'

const store = killa.createStore({ counter: 0 })

// This subscribe will be called every time any value of the status is updated.
// We could say that this would be a global subscribe.
// This subscriber will be called every time any value of the status
// is updated. We could say that this would be a global subscribe.
store.subscribe((state, prevState) => {
console.log('Updated state', state) // { counter: 1 }
console.log('Previous state', prevState) // { counter: 0 }
Expand All @@ -76,20 +90,25 @@ store.setState(() => {
store.getState() // { counter: 1 }
```

You can also subscribe a single event
But you can also subscribe a single event

```js
import killa from 'killa'

const store = killa.createStore({ counter: 0, type: '' })
const store = killa.createStore({ counter: 0, type: '', filter: '' })

// This subscribe will be called after updating the counter state.
// This subscriber will be called after updating the counter state.
store.subscribe((state, prevState) => {
console.log('Updated state', state) // { counter: 1 }
console.log('Previous state', prevState) // { counter: 0 }
console.log('Updated state', state) // { counter: 1, type: '', filter: '' }
console.log('Previous state', prevState) // { counter: 0, type: '', filter: '' }
}, (state) => state.counter)

// This subscribe will not be called since the type state was not updated.
// This subscriber will be called when the state of counter or filter is updated.
store.subscribe((state) => {
console.log('Counter and filter state subscriber', state.counter)
}, (state) => ({ counter: state.counter, filter: state.filter }))

// This subscriber will not be called since the type state was not updated.
store.subscribe((state, prevState) => {
console.log('Updated state', state)
console.log('Previous state', prevState)
Expand All @@ -102,5 +121,8 @@ store.setState((state) => {
}
})

store.getState() // { counter: 1 }
store.getState() // { counter: 1, type: '', filter: '' }
```

## Support
Chrome 58, Firefox 57, IE 11, Edge 16, Safari 11, & Node.js 12.
15 changes: 9 additions & 6 deletions esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ const path = require('path')
const glob = require('glob')
const esbuild = require('esbuild')

const buildForESMAndCJS = async ({ format, ...options }) => {
const buildForCustomEnvironment = async ({ format = 'cjs', outdir = 'dist/', ...options }) => {
const entryPoints = glob.sync(path.resolve(process.cwd(), 'src/**/*.js'))

const result = await esbuild.build({
entryPoints,
outdir,
packages: 'external',
outdir: `dist/${format}`,
format,
platform: 'node',
target: [
'node12'
],
...options
})

console.log(`Build for ${format.toUpperCase()} 🚀`, result)
console.log('Build for Custom environment 🚀', result)
}

const buildForBrowser = async () => {
Expand All @@ -30,16 +33,16 @@ const buildForBrowser = async () => {
'chrome58',
'edge16',
'firefox57',
'safari11'
'safari11',
'node12'
]
})

console.log('Build for Browser 🚀', result)
}

const init = async () => {
buildForESMAndCJS({ format: 'esm' })
buildForESMAndCJS({ format: 'cjs' })
buildForCustomEnvironment()
buildForBrowser()
}

Expand Down
Loading

0 comments on commit 534d7c7

Please sign in to comment.