npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marko/serve

v4.2.8

Published

Utility to serve Marko files with a single command

Downloads

622

Readme

When you serve a directory, every .marko file in that directory becomes a page. A browser is automatically launched and live-reloads as you make changes. It's the simplicity of a static file server plus the power of the Marko UI language.

Features

  • 🚀 Fastest way to build a Marko app
  • 💖 No need to configure webpack, babel, etc.
  • ⚡️ Pages live-reload as you make changes
  • 📁 Directory-based routes
  • 💯 Supports route parameters (/blog/:id)
  • 🛠 Serve a single component to work on it in isolation

And when you build your production-ready app:

  • 🔥 Blazing fast server-side rendering
  • 📦 Optimized bundles with automatic code splitting
  • ✨ Modern JS & CSS for modern browsers, legacy JS & CSS for legacy browsers

Getting Started

Hello World

Start by creating and entering a new directory, then serve it using npx (requires npm 5.2.0+):

mkdir my-new-app
cd my-new-app/
npx @marko/serve .

By running npx @marko/serve, a browser tab automatically opens for the current working directory. Since our new directory is empty, you should see an empty directory index:

A browser viewing the URL of localhost:3000/, which reads “Index of /”.

Let's make a web page! Create a hello.marko file within my-new-app/ with the following:

<h1>Hello World</h1>

Once you save this file, the directory index will reload and show hello.marko as a file:

The same URL as before, but now the “Index of /” shows a hyperlink to “hello.marko”.

Follow the hello.marko hyperlink to view your new page:

The URL of localhost:3000/hello shows a large heading of “Hello World”.

A custom index

Navigate back to the directory index. Let's create an index.marko file with the following:

<h1>Home</h1>

Once you save this file, the directory index will reload and show our custom index instead:

The URL of localhost:3000/ no longer shows the the “Index of /” page, but instead a heading of “Home”.

Adding a component

Let's add a menu so we can navigate between our pages. Since it’ll be on every page, we'll create it as a component instead of duplicating code for each page.

  1. Create a components/ directory, then add a main-menu.marko file inside with the following:
<nav>
  <a href="/">Home</a>
  -
  <a href="/hello">Hello</a>
</nav>
  1. Then, add the <main-menu> component to both pages:

    <h1>Home</h1>
    <main-menu/>
    <h1>Hello World</h1>
    <main-menu/>
  2. We can now use the menu to navigate between pages!

    The Home page at localhost:3000/ now shows hyperlinks to itself and to “Hello”.

Route params

What if we want our app to say "Hello" to more than the world? Do we need a new .marko file for each thing we want to say hello to?

Nope. This is where route parameters come in. Route parameters let you use dynamic values from the URL in your templates. Like normal pages, these are powered by your directory structure, but add a special syntax: filenames that contain keywords in square brackets (like [example]) create a parameter with the same name as the text between the brackets.

  1. Rename hello.marko to hello/[name].marko, and update its contents to:

    <h1>Hello ${input.params.name}</h1>
    <main-menu/>
  2. Try visiting http://localhost:3000/hello/params in your browser.

The page at localhost:3000/hello/params shows a heading of “Hello params”.

  1. The possibilities are endless! Try adding a few to your menu:

    <nav>
      <a href="/">Home</a>
      -
      <a href="/hello/marko">Marko</a>
      -
      <a href="/hello/params">Params</a>
      -
      <a href="/hello/world">World</a>
    </nav>

Go forth and build

When you're ready to let the world see what you've built, run the build command to get a production-ready app:

npx @marko/build .

This produces a build/ directory that contains the app and its assets, all optimized and compressed.

We no longer need @marko/serve, @marko/build, or any other dependencies. We can run the server using only node:

node build/index.js

Open your browser to http://localhost:3000/ and you'll see the same app, only faster.

The homepage with a menu of links to “Home”, “Marko”, “Params”, and “World”.

This build/ directory can now be deployed to your favorite hosting service. We're excited to see what you make! ✨

CLI

Installation

npm install --save-dev @marko/serve

Examples

marko-serve .                           # serve the current directory
marko-serve ./pages                     # serve a “pages” directory
marko-serve ./components/example.marko  # serve a single component
marko-serve . --inspect-brk             # debug by passing a node argument through

Options

  • --port -p: The port to serve on (default 3000)
  • --no-browser: Don't automatically open the browser
  • --verbose: Show the entire raw build output
  • Any node CLI arguments are passed to the Node.js server process

API

Warning: Don't import the @marko/serve package directly yet. A programmatic API is coming soon.