|
1 | 1 | import { createHtml } from '../html.ts' |
2 | 2 | import log from '../log.ts' |
3 | | -import Project from '../project.ts' |
| 3 | +import Project, { injectHmr } from '../project.ts' |
4 | 4 | import { createRouter } from '../router.ts' |
5 | 5 | import { path, serve, ws } from '../std.ts' |
6 | 6 | import util, { hashShort } from '../util.ts' |
@@ -134,11 +134,7 @@ export async function start(appDir: string, port: number, isDev = false) { |
134 | 134 | } else { |
135 | 135 | body = mod.jsContent |
136 | 136 | if (project.isHMRable(mod.id)) { |
137 | | - body = injectHmr({ |
138 | | - id: mod.id, |
139 | | - sourceFilePath: mod.sourceFilePath, |
140 | | - jsContent: body |
141 | | - }) |
| 137 | + body = injectHmr({ ...mod, jsContent: body }) |
142 | 138 | } |
143 | 139 | } |
144 | 140 | req.respond({ |
@@ -213,42 +209,4 @@ export async function start(appDir: string, port: number, isDev = false) { |
213 | 209 | } |
214 | 210 | } |
215 | 211 |
|
216 | | -function injectHmr({ id, sourceFilePath, jsContent }: { id: string, sourceFilePath: string, jsContent: string }) { |
217 | | - let hmrImportPath = path.relative( |
218 | | - path.dirname(sourceFilePath), |
219 | | - '/-/deno.land/x/aleph/hmr.js' |
220 | | - ) |
221 | | - if (!hmrImportPath.startsWith('.') && !hmrImportPath.startsWith('/')) { |
222 | | - hmrImportPath = './' + hmrImportPath |
223 | | - } |
224 | 212 |
|
225 | | - const text = [ |
226 | | - `import { createHotContext, RefreshRuntime, performReactRefresh } from ${JSON.stringify(hmrImportPath)};`, |
227 | | - `import.meta.hot = createHotContext(${JSON.stringify(id)});` |
228 | | - ] |
229 | | - const reactRefresh = id.endsWith('.js') |
230 | | - if (reactRefresh) { |
231 | | - text.push('') |
232 | | - text.push( |
233 | | - `const prevRefreshReg = window.$RefreshReg$;`, |
234 | | - `const prevRefreshSig = window.$RefreshSig$;`, |
235 | | - `Object.assign(window, {`, |
236 | | - ` $RefreshReg$: (type, id) => RefreshRuntime.register(type, ${JSON.stringify(id)} + " " + id),`, |
237 | | - ` $RefreshSig$: RefreshRuntime.createSignatureFunctionForTransform`, |
238 | | - `});`, |
239 | | - ) |
240 | | - } |
241 | | - text.push('') |
242 | | - text.push(jsContent) |
243 | | - text.push('') |
244 | | - if (reactRefresh) { |
245 | | - text.push( |
246 | | - 'window.$RefreshReg$ = prevRefreshReg;', |
247 | | - 'window.$RefreshSig$ = prevRefreshSig;', |
248 | | - 'import.meta.hot.accept(performReactRefresh);' |
249 | | - ) |
250 | | - } else { |
251 | | - text.push('import.meta.hot.accept();') |
252 | | - } |
253 | | - return text.join('\n') |
254 | | -} |
0 commit comments