You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

devjar

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

devjar - npm Package Compare versions

Comparing version
0.2.0
to
0.2.1
+13
lib/index.d.ts
import React from 'react'
type LiveCodeHandles = {
load(files: Record<string, string>): void
ref: React.Ref
error?: unknown
}
type Options = {
getModulePath(modulePath: string): string
}
export function useLiveCode(options: Options): LiveCodeHandles
+28
-15

@@ -19,6 +19,7 @@ import { useEffect, useCallback, useState, useRef } from 'react'

function replaceImports(_code, getModulePath, externals) {
const [imports] = parse(_code)
let code = ''
let lastIndex = 0
imports.forEach(({ ss, s, e, se, n }) => {
let hasReactImports = false
const [imports] = parse(_code)
imports.forEach(({ s, e, ss, se, n }) => {
code += _code.slice(lastIndex, ss)

@@ -31,18 +32,30 @@ code += _code.substring(ss, s)

lastIndex = se
if (n === 'react') {
const statement = _code.slice(ss, se)
if (statement.includes('React')) {
hasReactImports = true
}
}
})
code += _code.substring(lastIndex)
if (!hasReactImports) {
code = `import React from 'react';\n${code}`
}
return code
}
function createRenderer(_React0, _ReactDOM0, _createModule, _getModulePath) {
function createRenderer(createModule_, getModulePath) {
let reactRoot
async function render(files) {
const mod = await _createModule(files, { getModulePath: _getModulePath })
const _React = await self.importShim('react')
const _ReactDOM = await self.importShim('react-dom')
const mod = await createModule_(files, { getModulePath })
const React_ = await self.importShim('react')
const ReactDOM_ = await self.importShim('react-dom')
const _jsx = _React.createElement
const _jsx = React_.createElement
const root = document.getElementById('root')
class ErrorBoundary extends _React.Component {
class ErrorBoundary extends React_.Component {
state = {

@@ -62,5 +75,5 @@ error: null,

const isReact18 = !!_ReactDOM.createRoot
const isReact18 = !!ReactDOM_.createRoot
if (isReact18 && !reactRoot) {
reactRoot = _ReactDOM.createRoot(root)
reactRoot = ReactDOM_.createRoot(root)
}

@@ -72,3 +85,3 @@ const Component = mod.default

} else {
_ReactDOM.render(element, root)
ReactDOM_.render(element, root)
}

@@ -83,7 +96,7 @@ }

`'use strict';
const createModule = ${createModule.toString()};
const createRenderer = ${createRenderer.toString()};
const getModulePath = ${getModulePath.toString()};
const _createModule = ${createModule.toString()};
const _createRenderer = ${createRenderer.toString()};
const _getModulePath = ${getModulePath.toString()};
globalThis.__render__ = createRenderer(0, 0, createModule, getModulePath);
globalThis.__render__ = _createRenderer(_createModule, _getModulePath);
`)

@@ -90,0 +103,0 @@ return code

{
"name": "devjar",
"version": "0.2.0",
"version": "0.2.1",
"type": "module",

@@ -13,2 +13,3 @@ "exports": {

],
"types": "./lib/index.d.ts",
"scripts": {

@@ -15,0 +16,0 @@ "build": "next build ./docs",

@@ -20,2 +20,4 @@ # devjar

const { ref, error, load } = useLiveCode({
// The CDN url of each imported module path in your code
// e.g. `import React from 'react'` will load react from skypack.dev/react
getModulePath(modPath) {

@@ -34,4 +36,7 @@ return `https://cdn.skypack.dev/${modPath}`

load({
'index.js': `export default function Main() { return 'hello world' }`,
'./mod': `...` // other relative modules
// `index.js` is the entry of every project
'index.js': `export default function App() { return 'hello world' }`,
// other relative modules can be used in the live coding
'./mod': `export default function Mod() { return 'mod' }`,
})

@@ -43,3 +48,3 @@ }

<div>
<button onClick={run}>run</h3>
<button onClick={run}>run</button>
<iframe ref={ref} />

@@ -46,0 +51,0 @@ </div>