-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refine exports and add readme * update readme
- Loading branch information
Showing
8 changed files
with
436 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,51 @@ | ||
# devjar | ||
> bundless runtime for your ESM JavaScript project in browser | ||
|
||
![image](https://repository-images.githubusercontent.com/483779830/23b4d7c8-dd8e-48b0-a3ea-c519e8236714) | ||
|
||
### Install | ||
|
||
```sh | ||
yarn add devjar | ||
``` | ||
|
||
### Usage | ||
|
||
```js | ||
import { useLiveCode } from 'devjar' | ||
|
||
function Playground() { | ||
const { ref, error, load } = useLiveCode({ | ||
getModulePath(modPath) { | ||
return `https://cdn.skypack.dev/${modPath}` | ||
} | ||
}) | ||
|
||
// logging failures | ||
if (error) { | ||
console.error(error) | ||
} | ||
|
||
// load code files and execute them as live code | ||
function run() { | ||
load({ | ||
'index.js': `export default function Main() { return 'hello world' }`, | ||
'./mod': `...` // other relative modules | ||
}) | ||
} | ||
|
||
// Attach the ref to an iframe element for runtime of code execution | ||
return ( | ||
<div> | ||
<button onClick={run}>run</h3> | ||
<iframe ref={ref} /> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
### License | ||
|
||
The MIT License (MIT). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
async function createModule(files, { getModulePath }) { | ||
let currentImportMap | ||
let shim | ||
|
||
async function setupImportMap() { | ||
if (shim) return shim | ||
window.esmsInitOptions = { | ||
shimMode: true, | ||
mapOverrides: true, | ||
} | ||
shim = import(/* webpackIgnore: true */ getModulePath('es-module-shims')) | ||
await shim | ||
} | ||
|
||
function updateImportMap(imports) { | ||
imports['react'] = getModulePath('react') | ||
imports['react-dom'] = getModulePath('react-dom') | ||
|
||
const script = document.createElement('script') | ||
script.type = 'importmap-shim' | ||
script.innerHTML = JSON.stringify({ imports }) | ||
document.body.appendChild(script) | ||
if (currentImportMap) { | ||
currentImportMap.parentNode.removeChild(currentImportMap) | ||
} | ||
currentImportMap = script | ||
} | ||
|
||
|
||
function createInlinedModule(code) { | ||
return `data:text/javascript;utf-8,${encodeURIComponent(code)}` | ||
} | ||
|
||
await setupImportMap() | ||
const imports = Object.fromEntries( | ||
Object.entries(files).map(([key, code]) => [ | ||
key, | ||
createInlinedModule(code), | ||
]) | ||
) | ||
|
||
updateImportMap(imports) | ||
return self.importShim('index.js') | ||
} | ||
|
||
export { createModule } |
Oops, something went wrong.
8f6364f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
devjar – ./
devjar.vercel.app
devjar-git-main-huozhi.vercel.app
devjar-huozhi.vercel.app