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 CHANGED
@@ -13,7 +13,7 @@ Notice: devjar only works for browser runtime at the moment. It will always rend
13
13
  ### Install
14
14
 
15
15
  ```sh
16
- yarn add devjar
16
+ pnpm add devjar
17
17
  ```
18
18
 
19
19
 
@@ -1,10 +1,9 @@
1
1
  import { useEffect, useCallback, useState, useId, useRef } from 'react'
2
- import { createModule } from './module.mjs'
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 React_ = await self.importShim('react')
89
- const ReactDOM_ = await self.importShim('react-dom')
85
+ const ReactMod = await self.importShim('react')
86
+ const ReactDOMMod = await self.importShim('react-dom')
90
87
 
91
- const _jsx = React_.createElement
92
- const root = document.getElementById('root')
93
- class ErrorBoundary extends React_.Component {
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 = !!ReactDOM_.createRoot
106
+ const isReact18 = !!ReactDOMMod.createRoot
110
107
  if (isReact18 && !reactRoot) {
111
- reactRoot = ReactDOM_.createRoot(root)
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
- ReactDOM_.render(element, root)
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
- `'use strict';
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 `window.esmsInitOptions = {
141
- polyfillEnable: ['css-modules', 'json-modules'],
142
- onerror: error => console.log(error),
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 setScript(scriptRef, scriptContent, { type } = {}) {
148
+ function createScript(scriptRef, { content, src, type } = {}) {
151
149
  const script = scriptRef.current
152
150
  if (type) script.type = type
153
151
 
154
- script.src = `data:text/javascript;utf-8,${encodeURIComponent(scriptContent)}`
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 esShimOptionsScript = useScript()
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 = 'root'
194
+ div.id = '__reactRoot'
190
195
 
191
196
  const appScriptContent = createMainScript({ uid })
192
197
  const scriptOptionsContent = createEsShimOptionsScript()
193
198
 
194
- const esmShimOptionsScript = setScript(esShimOptionsScript, scriptOptionsContent)
195
- const appScript = setScript(appScriptRef, appScriptContent, { type: 'module' })
196
- // const script = scriptRef.current
197
- // script.type = 'module'
198
- // script.id = 'main'
199
- // script.src = `data:text/javascript;utf-8,${encodeURIComponent(scriptContent)}`
200
-
201
- doc.body.appendChild(div)
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
- doc.body.removeChild(div)
208
- doc.body.removeChild(esmShimOptionsScript)
209
- doc.body.removeChild(appScript)
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
@@ -0,0 +1,2 @@
1
+ export { DevJar } from './render.js'
2
+ export { useLiveCode } from './core.js'
@@ -1,5 +1,5 @@
1
1
  import React, { useEffect, useRef } from 'react'
2
- import { useLiveCode } from './core.mjs'
2
+ import { useLiveCode } from './core.js'
3
3
 
4
4
  const defaultOnError = typeof window !== 'undefined' ? console.error : (() => {})
5
5
 
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "devjar",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "exports": {
6
- ".": "./lib/index.mjs",
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": "0.10.5",
24
- "es-module-shims": "1.5.9",
25
- "sucrase": "3.23.0"
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": "latest",
28
+ "codice": "^0.2.0",
29
29
  "devjar": "link:./",
30
- "lodash-es": "^4.17.21",
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.4.5"
35
- }
33
+ "sugar-high": "^0.5.6"
34
+ },
35
+ "packageManager": "pnpm@7.33.5"
36
36
  }
package/lib/index.mjs DELETED
@@ -1,2 +0,0 @@
1
- export { DevJar } from './render.mjs'
2
- export { useLiveCode } from './core.mjs'
File without changes