Skip to content

📦 ✨ Run npm dependencies directly in the browser. No Browserify, Webpack or import maps required. https://www.pika.dev/blog/pika-web-a-future-without-webpack/

License

Notifications You must be signed in to change notification settings

calebdwilliams/pika-web

 
 

Repository files navigation

Logo

@pika/web • Run npm dependencies directly in the browser. No Browserify, Webpack or import maps required.

Logo


npm on Dec 6, 2018 - "JavaScript in 2018 is somewhat notorious for requiring a lot of tooling to get going, which is quite a reversal from the situation in 2014... All of our survey respondents would like to see less tooling [and] less configuration required to get started."


@pika/web brings that nostalgic, 2014 simplicity to 2019 web development:

  • Simple 💪 No bundlers required. Load modern packages natively in the browser.
  • Flexible 🧘‍♂️ Handles dependency trees of any size, even ones that includes legacy Common.js packages.
  • HTTP/2 Optimized ⚡️ No more huge JS bundles. Browsers only download dependencies when they change.

@pika/web installs npm packages as single .js files to a new web_modules/ directory. If your dependency exports an ES "module" entrypoint in its package.json manifest, it is supported. Even if it internally depends on other npm packages (even legacy Common.js packages) @pika/web should be able to handle it.

Bundling packages on a per-module basis like this makes it easy to build a web application that runs fast and caches well. Updating a single dependency won't force a complete re-download of your web application. More on performance below.

┻┳| ┳┻| _ ┻┳| •.•) 💬 "Tip: Use pikapkg.com to find modern, web-ready packages on npm" ┳┻|⊂ノ ┻┳|

Quickstart

npm install --save-dev @pika/web
yarn add --dev @pika/web
# 1. Run @pika/web in your project:
$ npx @pika/web

# 2. Replace all NPM package imports in your web app with web-native URLs:
- import { createElement, Component } from "preact";
- import htm from "htm";
+ import { createElement, Component } from "/web_modules/preact.js";
+ import htm from "/web_modules/htm.js";

# Optional: Use Babel to skip "Step 2" and let our plugin rewrite your imports automatically:
echo '{"plugins": [["@pika/web/assets/babel-plugin.js"]]}' > .babelrc

# 3. Run your code directly in the browser and see the magic!
✨ ~(‾▿‾~)(~‾▿‾)~ ✨

# 4. Add a package.json "prepare" script to run @pika/web on every npm install:
{"scripts": {"prepare": "pika-web"}}

Examples? We got 'em

Performance

When @pika/web installs your dependencies, it bundles each package into a single ESM JavaScript file. Example: If @pika/web installs 10 npm packages into web_modules/, you can expect 10 JavaScript files and maybe 1-2 additional files of code shared between them.

Max Jung's post on "The Right Way to Bundle Your Assets for Faster Sites over HTTP/2" is the best study on HTTP/2 performance & bundling we could find online. @pika/web's installation most closely matches the study's moderate, "50 file" bundling strategy. Jung's post found that for HTTP/2, "differences among concatenation levels below 1000 [small files] (50, 6 or 1) were negligible."

Browser Support

@pika/web installs ES Module (ESM) dependencies from npm, which run wherever ESM syntax is supported. This includes 86%+ of all browsers in use today: All modern browsers (Firefox, Chrome, Edge, Safari) going back at least a year, but notably not IE11 or UC Browser for Android.

Additionally, @pika/web runs all dependencies through Babel via @preset/env to transpile any language features not yet supported in most modern browsers. You can customize how dependencies are transpiled by setting your own "browserslist" key in your package.json manifest:

  /* package.json - Recommended target for ESM-supporting browsers (default) */
  "browserslist": ">0.75%, not ie 11, not op_mini all"

Options

Note: All package.json options are scoped under the "@pika/web" property.

  • "webDependencies": You can define an optional whitelist of "webDependencies" in your package.json manifest. This is useful if your entire "dependencies" object is too large, or if you'd like to install dependencies by file path.
  "dependencies": { "htm": "^1.0.0", "preact": "^8.0.0", /* ... */ },
  "@pika/web": {
    "webDependencies": [
      "htm",
      "preact",
      "preact/hooks", // A package within a package
      "unistore/full/preact.es.js" // An ESM file within a package
    ]
  },

A Note on React

React is not yet published with ES Module support, and the way it's build makes it impossible to bundle as an entrypoint (Error: '__moduleExports' is not exported by node_modules/react/index.js). However, it is still possible to use React with @pika/web thanks to @sdegutis's @reactesm project & npm/yarn's alias feature:

npm install react@npm:@reactesm/react react-dom@npm:@reactesm/react-dom
   yarn add react@npm:@reactesm/react react-dom@npm:@reactesm/react-dom

This command installs ESM versions of the latest react & react-dom, which @pika/web will then use when it installs your web_modules/ directory. This works with any ESM-compatible React libraries as well!

import React, { useState } from './web_modules/react.js';

A Note on JSX

Remember that JSX won't run in any browser. To use JSX with @pika/web:

  1. Use Babel to build your src/ directory to an output lib/ directory, and load that in the browser.
  2. Use a JSX-like library like Jason Miller's htm that can run in the browser.

Special Thanks: Rollup

@pika/web is powered internally by Rollup. We believe that bundlers shouldn't be a requirement for modern web app development, but none of this would be possible without the awesome work done by Rollup contributors. If you use and enjoy our software, consider contributing back to Rollup on Open Collective.

About

📦 ✨ Run npm dependencies directly in the browser. No Browserify, Webpack or import maps required. https://www.pika.dev/blog/pika-web-a-future-without-webpack/

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 86.0%
  • JavaScript 14.0%