Skip to content

Latest commit

 

History

History
651 lines (437 loc) · 14.1 KB

slides.md

File metadata and controls

651 lines (437 loc) · 14.1 KB
theme title website handle favicon highlighter lineNumbers layout
./theme
`unjs` - Unified JavaScript solutions
lichter.io
TheAlexLichter
shiki
true
intro

unjs

Unified JavaScript solutions

vuejs.de Conf 2022

<style> h1 { @apply !text-5xl; } h2 { @apply !text-2xl !my-16; } h3 { @apply !text-xl } </style>

layout: two-cols heading: About me

Alexander Lichter


layout: intro

JavaScript runs everywhere


JavaScript runs everywhere



Firefox Developer Edition Logo Firefox Developer Edition Logo Deno Logo Cloudflare Workers Logo

clicks: 5

Picking NPM packages becomes more difficult

  • Ideally any package supports...
  • CJS (legacy)
  • ESM
  • running via Deno
  • TS and provides own types
const { someFunction, someOtherVariable } = require('my-lib')

import { someFunction, someOtherVariable } from 'my-lib'

import { someFunction, someOtherVariable } from 'https://unpkg.com/my-lib/dist/index.mjs'

Software requirements can change


Software requirements can will change

  • What if you change the platform you deploy to?
    • e.g. from a docker container into lambdas or workers
  • What if you decide to utilize SSR?
    • and which packages break now 👀

"The solution"

  • Use "modern" packages
  • Ideally those who are universally usable
  • With as little sub-dependencies as possible
  • And who support all "common" formats




layout: intro class: filter blur-md

unjs

Logo of unjs


During Nuxt 2 development...

  • ...more and more functionalities were introduced
  • At first - more or less coupled to the core
  • "Nuxt-related" logic -> extracted as Nuxt module
  • "General" logic -> extracted as standalone NPM package

Why external packages?

  • Decoupled from the Nuxt core
  • Easier to test
  • Better to maintain
  • Can be reused throughout projects...
  • ...even in these that are not related to Nuxt

xkcd about dependencies


nuxt-contrib

Screenshot of the nuxt-contrib GitHub org


nuxt-contrib

  • Contained packages used in Nuxt or Nuxt projects
  • From connect middleware over testing tools to utilities
  • Evolved further to be more than just "packages from the Nuxt team"

layout: intro class: filter blur-md

unjs

Logo of unjs


layout: intro

unjs

Logo of JavaScript


layout: intro

unjs

Logo of unjs


layout: intro


Let's take a look


layout: intro

Who has dealt with transforming URLs? ✋🏻


layout: intro

How?


👽️ ufo - URL utils for humans

  • URL normalization
  • Joining and resolving URLs while keeping query and hash
  • Remove or add trailing/leading slash
  • deduplicate slashes from URLs in general
  • and way more

ufo - Show me the code!

import { joinURL } from 'ufo'

joinURL('/my/', 'nice', 'url/')
// Will yield: /my/nice/url/ 
joinURL('/a?query=yes', '/b', '/c#hash')
// ???

ufo - Show me the code!

import { joinURL, resolveURL, cleanDoubleSlashes, parseURL } from 'ufo'

joinURL('/my/', 'nice', 'url/')
// Will yield: /my/nice/url/ 
joinURL('/a?query=yes', '/b', '/c#hash')
// /a?query=yes/b/c#hash - oh oh!
resolveURL('/a?query=yes', '/b#someOtherHash', '/c#hash')
// /a/b/c?query=yes#hash
cleanDoubleSlashes('https://conf.vuejs.de////schedule//')
// https://conf.vuejs.de/schedule/
parseURL('/unstorage-h3-file-server-example?file=index.js')
// { "pathname": "/unstorage-h3-file-server-example", "search": "?file=index.js", "hash": "" }

layout: intro

⚡️ H3 - Minimal h(ttp) framework


H3 - Why another framework?

  • There are so many existing http frameworks for node.js already:
  • connect
  • express
  • fastify
  • koa
  • hapi.js
  • and more...

H3 - Why another framework?

  • H3 is...
  • ...platform-agnostic
  • ...compatible with existing stacks
  • ...minimal
  • ...modern
  • ...extendable
  • ...coming with a built-in router

H3 - Usage

import { listen } from 'listhen'
import { createApp, createRouter } from 'h3'

const app = createApp()

const router = createRouter()
  .get('/', () => 'Hallo Berlin!')
  .get('/color/:color', (event) => `I like ${event.context.params.color}!`)

app.use(router)

listen(app)

H3 - Used and mentioned by

  • Nitro(pack) - We will have a look at it next!
  • Nuxt 3 and Nuxt Bridge (via Nitro)
  • GestaltJS - "A modern full-stack and batteries-included framework"
  • serve - "A lightweight starting point for API applications"
  • frourio-h3 - An unofficial port of the full-stack TS framework

🌍💾 unstorage - Universal Storage Layer

  • Powerful universal storage layer
  • Unified API, < 5 kB
  • Supports in-memory, filesystem, localstorage, redis and HTTP...
  • ...GitHub repositories (read-only) and Cloudflare's KV-store via http and bindings
  • Can be extended by creating custom drivers too!
  • Also supports being used as storage server

unstorage - Example using as storage server

import { listen } from 'listhen';
import { createApp, createRouter } from 'h3';
import { createStorage } from 'unstorage';
import fsDriver from 'unstorage/drivers/fs';
import { createStorageServer } from 'unstorage/server';

const storage = createStorage({
  driver: fsDriver({ base: './' }),
});
const storageServer = createStorageServer(storage);

const app = createApp();

const router = createRouter()
  .add('/storage/*', storageServer.handle)
  .get('/', () => 'We have a file server now')

app.use(router);
listen(app);

Here we go


unstorage - Used and mentioned by


⚗️ Nitro - Build and deploy universal JavaScript servers

  • Toolchain and runtime framework at once
  • Comes with HMR out of the box
  • Supports various route rules (see Daniel's talk! 👀)
  • Customizable, configurable and extendable
  • Uses lots of unjs packages (e.g. h3, unstorage and ufo)

layout: intro

Deploy a JS server to any provider

without changing the codebase (at most, config)

<style> h1 { @apply !text-5xl; } h2 { @apply !text-2xl; } </style>

Nitro - 👀


Nitro - Used and mentioned by


And there are way more packages

  • We saw some during the examples already. And in addition:
  • unplugin - Unified plugin system for all major bundlers (Vite, Rollup, Webpack, esbuild)
  • untyped - Generate types and markdown from a config object
  • consola - Beautiful universal console logger
  • hookable - Async hook system
  • ...and way more! Check out the GitHub organisation

Contributors

  • When you browse around the unjs organization, you will see these images quite often:
Anthony Fu Daniel Roe Pooya Parsa
  • But also lots of other individual contributors of each project!

layout: intro

Thanks to all of them 👏🏻👏🏻👏🏻


Outro

  • Picking an NPM package that supports (almost) all platforms is not easy Writing and maintaining these too
  • Extracting isolated features helps maintainability and reusability throughout the ecosystem
  • unjs packages power not only Nuxt, but also many other frameworks...
    • ...and can be used for your app too!
Hacktoberfest 22 is on! Hacktoberfest 22 Image

layout: intro

Thanks a lot to my sponsors!

My GitHub sponsors


layout: two-cols heading: Thank you for your attention!

Alexander Lichter

<style> ul { @apply space-y-2 mt-10 text-xl h-full; } </style>

layout: intro

Slides / Repo