# baresm

> web service worker to resolve es6 bare module specifiers

Latest version **0.1.0** (published 2020-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install baresm
pnpm add baresm
yarn add baresm
bun add baresm
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-01-31 |
| First published | 2020-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Thomas LaFreniere |
| Maintainers | laftho |
| Keywords | es6, modules, sw, bare, specifiers |

## Links

- npm: https://www.npmjs.com/package/baresm
- Repository: https://github.com/laftho/baresm
- Homepage: https://github.com/laftho/baresm#readme
- Issues: https://github.com/laftho/baresm/issues
- npm.io page: https://npm.io/package/baresm

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.1.0 (latest) — 2020-01-31
- 0.0.1 — 2020-01-31

## README

# baresm
Web service worker to resolve es6 bare module specifiers.

allows the use of `npm` installed `package.json` dependencies in `node_modules` as es6 module imports. eg.

index.html
```
<script type="module" src="index.mjs"></script>
```

index.mjs
```
import f, { n } from "some-lib";

f();
n();

```

# why?

Browsers that support es6 module loading only do so with relative or absolute paths. package.json dependencies referenced in import statements will not be resolved by browsers. `baresm.js` will intercept bare module specifiers in javascript modules and rewrite them to absolute paths allowing you to use package.json dependencies in your module without any packaging / build stage of your code.

# should I use this?

No. This is proof of concept and ultimately a stop-gap until browsers support bare module specifiers, if ever. The web specification for es6 module dependencies will likely look different to analyzing package.json files and resolving node_modules paths. Moreover, many typical/popular node_modules won't execute correctly without a packaging step directly in the browser and/or are very large and ultimatley unnecessary to deliver effectively your entire raw node_modules to browsers.

If you decide to use this:

- use HTTP/2, streams will make a huge difference for loading a ton of small files. 
- ensure your dependencies can execute directly in the browsers you're targetting.
- reconsider using this

# install

`npm install --save baresm`

## copy baresm.js to your web root

if you web server supports following symlinks, create a symlink as a sibling to your index.html, ie

`ln -s node_modules/baresm/baresm.js .`

otherwise, copy:

`cp node_modules/baresm/baresm.js .`

index.html
```
<script>
  if ("serviceWorker" in navigator) {
    window.addEventListener("load", () => navigator.serviceWorker.register("/baresm.js?your-version"));
  }
</script>
```

replace `your-version` with a version token that reflects your module and/or dependency changes.

## Alternatively use Service-Worker-Allowed header

have your index request serve `Service-Worker-Allowed` header as `/` so `/node_modules/baresm/baresm.js` may use the root scope `/`

`Service-Worker-Allowed: /`

index.html
```
<script>
  if ("serviceWorker" in navigator) {
    window.addEventListener("load", () => navigator.serviceWorker.register("/node_modules/baresm/baresm.js?your-version", { scope: "/" }));
  }
</script>
```

# caching

`baresm.js` will create a `node_modules` cache key and will effectively cache any fetch requests for your origin that are Content-Type `application/javascript`. It is advised to version your service worker registration to ensure you reload this cache whenever you change your code and/or package dependencies.

---
_Source: https://npm.io/package/baresm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
