![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@spon/core
Advanced tools
There are breaking changes between 2x and 1x. Please see the v1 branch for v1 docs.
2x has a smaller scope than 1x. withPlugins
and eventBus
has been moved to @spon/plugins
and
connect
has been moved to @spon/connect
. Refs, createNode, and addEventPromise have been removed
from version 2x
npm install @spon/core
or yarn add @spon/core
@spon/core
is a little framework used to asynchronous load javascript modules based on dom attributes.
Add the following code to your main javascript entry point (app.js)
import { loadApp, loadModule } from '@spon/core'
import logger from './behaviours/logger'
// load from data-behaviours
loadApp(name => import(`./behaviours/${name}`), document.body)
// load from file
loadModule({
module: logger, // required
id: 'hello', // required
node: document.getElementById('logger') // default undefined,
keepAlive: true // default undefined
})
Create a html file with the following snippet
<div data-behaviour="example" id="required-id">...</div>
js/behaviours/example
/**
* @function example
* @param {Object} props
* @property {HTMLElement} props.node
* @return {Function} a function to unmount
*/
function example({ node }) {
return () => {
console.log('i am called when the module is destroyed')
}
}
export default example
You can also set the module to only load at certain breakpoints:
<div data-behaviour="example" id="required-id" data-query="(max-width: 1024px)">
...
</div>
example()
will only be called when the viewport is smaller than 1024px. Once the module is mounted and the viewport increases to greater than 1024 the returned function will be called. Use this to remove any event listeners or destroy any custom modules
/**
* @function example
* @param {Object} props
* @property {HTMLElement} props.node
* @return {Function} a function to unmount
*/
function example({ node }) {
const slide = new SomeSlideLibrary(node)
return () => {
slide.destroy()
}
}
export default example
Behaviours with the 'data-keep-alive' attribute will not be destroyed when navigating betweeen pages. This is only valid if you are using ajax pagaination.
Example:
./store/indx.js
import { init } from '@rematch/core'
import connectStore from '@spon/connect'
const store = init({
models: {
count: {
state: 0,
reducers: {
increment(state, payload) {
return state + payload
}
}
}
}
})
// this creates a function that is used to bind modules to the store
export const connect = connectStore(store)
export default store
import { connect } from './store'
import { domEvents, withPlugins } from '@spon/plugins'
/* eslint-disable no-console */
function counter({ node, name, plugins: { addEvents }, store, render }) {
addEvents({
'click button': () => {
store.increment(1)
}
})
render(
({ current }) => {
node.textContent = current.count
},
['count']
)
return () => {
console.log(`destroy: ${name}`)
}
}
const mapState = store => {
return {
count: store.count
}
}
const mapDispatch = ({ count }) => ({ ...count })
export default withPlugins(domEvents)(
connect({ mapState, mapDispatch })(counter)
)
export default withPlugins(domEvents)(counter)
export default connect(domEvents)(counter)
FAQs
spon
The npm package @spon/core receives a total of 16 weekly downloads. As such, @spon/core popularity was classified as not popular.
We found that @spon/core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.