Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kmturley/esm-package-browser-server
Example ESM package browser server
https://github.com/kmturley/esm-package-browser-server
Last synced: 24 days ago
JSON representation
Example ESM package browser server
- Host: GitHub
- URL: https://github.com/kmturley/esm-package-browser-server
- Owner: kmturley
- License: mit
- Created: 2024-08-30T21:11:39.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-08-30T21:11:41.000Z (4 months ago)
- Last Synced: 2024-12-15T04:48:20.282Z (25 days ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# esm-package-browser-server
Example ESM package which exports both browser and server modules and isomorphic JavaScript code which can run in browser and/or server.
## Usage
Browser `example-browser.js` JavaScript file:
import { shared, browser } from "esm-package-browser-server";
console.log(shared('Example'));
console.log(browser('Example'));And html file:
{
"imports": {
"esm-package-browser-server": "./node_modules/esm-package-browser-server/src/browser.js"
}
}
Server `example-server.js` JavaScript file:
import { shared, server } from "esm-package-browser-server";
console.log(shared('Example'));
console.log(server('Example'));And run command:
node example-server.js
Shared function should be able to be run both browser/server, but the the other code not accessible unless on correct platform.
## How it works
Using [conditional exports](https://nodejs.org/api/packages.html#packages_conditional_exports):
"exports": {
"node": "./src/server.js",
"default": "./src/browser.js"
},Node process will use `./src/server.js` others will fallback/map to `./src/browser.js`.
## Contact
For more information please contact kmturley