devjar 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/{core.mjs → core.js} +39 -35
- package/lib/index.js +2 -0
- package/lib/{render.mjs → render.js} +1 -1
- package/package.json +10 -10
- package/lib/index.mjs +0 -2
- /package/lib/{module.mjs → module.js} +0 -0
package/README.md
CHANGED
package/lib/{core.mjs → core.js}
RENAMED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { useEffect, useCallback, useState, useId, useRef } from 'react'
|
|
2
|
-
import { createModule } from './module.
|
|
2
|
+
import { createModule } from './module.js'
|
|
3
3
|
import { transform } from 'sucrase'
|
|
4
4
|
import { init, parse } from 'es-module-lexer'
|
|
5
5
|
|
|
6
6
|
let esModuleLexerInit
|
|
7
|
-
|
|
8
7
|
const isRelative = s => s.startsWith('./')
|
|
9
8
|
|
|
10
9
|
function transformCode(_code, getModuleUrl, externals) {
|
|
@@ -25,13 +24,11 @@ function replaceImports(source, getModuleUrl, externals) {
|
|
|
25
24
|
|
|
26
25
|
// start, end, statementStart, statementEnd, assertion, name
|
|
27
26
|
imports.forEach(({ s, e, ss, se, a, n }) => {
|
|
28
|
-
let isCss = false
|
|
29
27
|
code += source.slice(lastIndex, ss) // content from last import to beginning of this line
|
|
30
28
|
|
|
31
29
|
|
|
32
30
|
// handle imports
|
|
33
31
|
if (n.endsWith('.css')) {
|
|
34
|
-
isCss = true
|
|
35
32
|
// Map './styles.css' -> '@styles.css', and collect it
|
|
36
33
|
const cssPath = `${'@' + n.slice(2)}`
|
|
37
34
|
cssImports.push(cssPath)
|
|
@@ -85,12 +82,12 @@ function createRenderer(createModule_, getModuleUrl) {
|
|
|
85
82
|
|
|
86
83
|
async function render(files) {
|
|
87
84
|
const mod = await createModule_(files, { getModuleUrl })
|
|
88
|
-
const
|
|
89
|
-
const
|
|
85
|
+
const ReactMod = await self.importShim('react')
|
|
86
|
+
const ReactDOMMod = await self.importShim('react-dom')
|
|
90
87
|
|
|
91
|
-
const _jsx =
|
|
92
|
-
const root = document.getElementById('
|
|
93
|
-
class ErrorBoundary extends
|
|
88
|
+
const _jsx = ReactMod.createElement
|
|
89
|
+
const root = document.getElementById('__reactRoot')
|
|
90
|
+
class ErrorBoundary extends ReactMod.Component {
|
|
94
91
|
constructor(props) {
|
|
95
92
|
super(props)
|
|
96
93
|
this.state = { error: null }
|
|
@@ -106,16 +103,16 @@ function createRenderer(createModule_, getModuleUrl) {
|
|
|
106
103
|
}
|
|
107
104
|
}
|
|
108
105
|
|
|
109
|
-
const isReact18 = !!
|
|
106
|
+
const isReact18 = !!ReactDOMMod.createRoot
|
|
110
107
|
if (isReact18 && !reactRoot) {
|
|
111
|
-
reactRoot =
|
|
108
|
+
reactRoot = ReactDOMMod.createRoot(root)
|
|
112
109
|
}
|
|
113
110
|
const Component = mod.default
|
|
114
111
|
const element = _jsx(ErrorBoundary, null, _jsx(Component))
|
|
115
112
|
if (isReact18) {
|
|
116
113
|
reactRoot.render(element)
|
|
117
114
|
} else {
|
|
118
|
-
|
|
115
|
+
ReactDOMMod.render(element, root)
|
|
119
116
|
}
|
|
120
117
|
}
|
|
121
118
|
|
|
@@ -123,8 +120,8 @@ function createRenderer(createModule_, getModuleUrl) {
|
|
|
123
120
|
}
|
|
124
121
|
|
|
125
122
|
function createMainScript({ uid }) {
|
|
126
|
-
const code = (
|
|
127
|
-
|
|
123
|
+
const code = (`\
|
|
124
|
+
'use strict';
|
|
128
125
|
const _createModule = ${createModule.toString()};
|
|
129
126
|
const _createRenderer = ${createRenderer.toString()};
|
|
130
127
|
|
|
@@ -137,21 +134,27 @@ globalThis.__render__ = _createRenderer(_createModule, getModuleUrl);
|
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
function createEsShimOptionsScript() {
|
|
140
|
-
return
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
137
|
+
return `\
|
|
138
|
+
window.esmsInitOptions = {
|
|
139
|
+
polyfillEnable: ['css-modules', 'json-modules'],
|
|
140
|
+
onerror: error => console.log(error),
|
|
141
|
+
}`
|
|
144
142
|
}
|
|
145
143
|
|
|
146
144
|
function useScript() {
|
|
147
145
|
return useRef(typeof window !== 'undefined' ? document.createElement('script') : null)
|
|
148
146
|
}
|
|
149
147
|
|
|
150
|
-
function
|
|
148
|
+
function createScript(scriptRef, { content, src, type } = {}) {
|
|
151
149
|
const script = scriptRef.current
|
|
152
150
|
if (type) script.type = type
|
|
153
151
|
|
|
154
|
-
|
|
152
|
+
if (content) {
|
|
153
|
+
script.src = `data:text/javascript;utf-8,${encodeURIComponent(content)}`
|
|
154
|
+
}
|
|
155
|
+
if (src) {
|
|
156
|
+
script.src = src
|
|
157
|
+
}
|
|
155
158
|
return script
|
|
156
159
|
}
|
|
157
160
|
|
|
@@ -160,7 +163,8 @@ function useLiveCode({ getModuleUrl }) {
|
|
|
160
163
|
const [error, setError] = useState()
|
|
161
164
|
const rerender = useState({})[1]
|
|
162
165
|
const appScriptRef = useScript()
|
|
163
|
-
const
|
|
166
|
+
const esShimOptionsScriptRef = useScript()
|
|
167
|
+
const tailwindcssScriptRef = useScript()
|
|
164
168
|
const uid = useId()
|
|
165
169
|
|
|
166
170
|
// Let getModuleUrl executed on parent window side since it might involve
|
|
@@ -185,28 +189,28 @@ function useLiveCode({ getModuleUrl }) {
|
|
|
185
189
|
if (!iframe || !iframe.contentDocument) return
|
|
186
190
|
|
|
187
191
|
const doc = iframe.contentDocument
|
|
192
|
+
const body = doc.body
|
|
188
193
|
const div = document.createElement('div')
|
|
189
|
-
div.id = '
|
|
194
|
+
div.id = '__reactRoot'
|
|
190
195
|
|
|
191
196
|
const appScriptContent = createMainScript({ uid })
|
|
192
197
|
const scriptOptionsContent = createEsShimOptionsScript()
|
|
193
198
|
|
|
194
|
-
const esmShimOptionsScript =
|
|
195
|
-
const appScript =
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
doc.body.appendChild(esmShimOptionsScript)
|
|
203
|
-
doc.body.appendChild(appScript)
|
|
199
|
+
const esmShimOptionsScript = createScript(esShimOptionsScriptRef, { content: scriptOptionsContent })
|
|
200
|
+
const appScript = createScript(appScriptRef, { content: appScriptContent, type: 'module' })
|
|
201
|
+
const tailwindScript = createScript(tailwindcssScriptRef, { src: 'https://cdn.tailwindcss.com' })
|
|
202
|
+
|
|
203
|
+
body.appendChild(div)
|
|
204
|
+
body.appendChild(esmShimOptionsScript)
|
|
205
|
+
body.appendChild(appScript)
|
|
206
|
+
body.appendChild(tailwindScript)
|
|
204
207
|
|
|
205
208
|
return () => {
|
|
206
209
|
if (!iframe || !iframe.contentDocument) return
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
+
body.removeChild(div)
|
|
211
|
+
body.removeChild(esmShimOptionsScript)
|
|
212
|
+
body.removeChild(appScript)
|
|
213
|
+
body.removeChild(tailwindScript)
|
|
210
214
|
}
|
|
211
215
|
}, [])
|
|
212
216
|
|
package/lib/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "devjar",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
|
-
".": "./lib/index.
|
|
6
|
+
".": "./lib/index.js",
|
|
7
7
|
"./package.json": "./package.json"
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
@@ -20,17 +20,17 @@
|
|
|
20
20
|
"react": "^18.2.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"es-module-lexer": "
|
|
24
|
-
"es-module-shims": "1.
|
|
25
|
-
"sucrase": "3.
|
|
23
|
+
"es-module-lexer": "1.4.1",
|
|
24
|
+
"es-module-shims": "1.8.2",
|
|
25
|
+
"sucrase": "3.35.0"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"codice": "
|
|
28
|
+
"codice": "^0.2.0",
|
|
29
29
|
"devjar": "link:./",
|
|
30
|
-
"
|
|
31
|
-
"next": "^13.2.0",
|
|
30
|
+
"next": "^14.1.0",
|
|
32
31
|
"react": "^18.2.0",
|
|
33
32
|
"react-dom": "^18.2.0",
|
|
34
|
-
"sugar-high": "^0.
|
|
35
|
-
}
|
|
33
|
+
"sugar-high": "^0.5.6"
|
|
34
|
+
},
|
|
35
|
+
"packageManager": "pnpm@7.33.5"
|
|
36
36
|
}
|
package/lib/index.mjs
DELETED
|
File without changes
|