-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hot-hook/register entrypoint loader
- Loading branch information
1 parent
4d1a480
commit ffef5f0
Showing
10 changed files
with
269 additions
and
46 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
"exports": { | ||
".": "./build/src/hot.js", | ||
"./loader": "./build/src/loader.js", | ||
"./runner": "./build/src/runner.js", | ||
"./register": "./build/src/register.js", | ||
"./import-meta": { | ||
"types": "./import-meta.d.ts" | ||
} | ||
|
@@ -33,7 +33,8 @@ | |
"dependencies": { | ||
"chokidar": "^3.6.0", | ||
"fast-glob": "^3.3.2", | ||
"picomatch": "^4.0.2" | ||
"picomatch": "^4.0.2", | ||
"read-package-up": "^11.0.0" | ||
}, | ||
"author": "Julien Ripouteau <[email protected]>", | ||
"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,17 @@ | ||
import { resolve } from 'node:path' | ||
import { hot } from './hot.js' | ||
import { readPackageUp } from 'read-package-up' | ||
|
||
const pkgJson = await readPackageUp() | ||
if (!pkgJson) { | ||
throw new Error('Could not find package.json') | ||
} | ||
|
||
const { packageJson, path: packageJsonPath } = pkgJson | ||
const hotHookConfig = packageJson['hot-hook'] | ||
|
||
await hot.init({ | ||
root: hotHookConfig?.root ? resolve(packageJsonPath, packageJson['hot-hook'].root) : undefined, | ||
boundaries: packageJson['hot-hook']?.boundaries, | ||
ignore: packageJson['hot-hook']?.ignore, | ||
}) |
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,131 @@ | ||
import { join } from 'node:path' | ||
import { pEvent } from 'p-event' | ||
import supertest from 'supertest' | ||
import { test } from '@japa/runner' | ||
import { setTimeout } from 'node:timers/promises' | ||
|
||
import { createHandlerFile, fakeInstall, runProcess } from './helpers.js' | ||
|
||
test.group('Register', () => { | ||
test('Works fine with', async ({ fs }) => { | ||
await fakeInstall(fs.basePath) | ||
|
||
await fs.createJson('package.json', { type: 'module' }) | ||
await fs.create( | ||
'server.js', | ||
`import * as http from 'http' | ||
import { join } from 'node:path' | ||
const server = http.createServer(async (request, response) => { | ||
const app = await import('./app.js', import.meta.hot?.boundary) | ||
await app.default(request, response) | ||
}) | ||
server.listen(3333, () => console.log('Server is running'))` | ||
) | ||
|
||
await createHandlerFile({ path: 'app.js', response: 'Hello World!' }) | ||
|
||
const server = runProcess('server.js', { | ||
cwd: fs.basePath, | ||
env: { NODE_DEBUG: 'hot-hook' }, | ||
nodeOptions: ['--import=hot-hook/register'], | ||
}) | ||
|
||
await server.waitForOutput('Server is running') | ||
|
||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World!') | ||
|
||
await setTimeout(100) | ||
await createHandlerFile({ path: 'app.js', response: 'Hello World! Updated' }) | ||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World! Updated') | ||
|
||
await setTimeout(100) | ||
await createHandlerFile({ path: 'app.js', response: 'Hello World! Updated new' }) | ||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World! Updated new') | ||
}) | ||
|
||
test('send full reload message', async ({ fs, assert }) => { | ||
await fakeInstall(fs.basePath) | ||
|
||
await fs.createJson('package.json', { type: 'module' }) | ||
await fs.create( | ||
'server.js', | ||
`import * as http from 'http' | ||
import { join } from 'node:path' | ||
const server = http.createServer(async (request, response) => { | ||
const app = await import('./app.js') | ||
await app.default(request, response) | ||
}) | ||
server.listen(3333, () => console.log('Server is running'))` | ||
) | ||
|
||
await createHandlerFile({ path: 'app.js', response: 'Hello World!' }) | ||
|
||
const server = runProcess('server.js', { | ||
cwd: fs.basePath, | ||
env: { NODE_DEBUG: 'hot-hook' }, | ||
nodeOptions: ['--import=hot-hook/register'], | ||
}) | ||
|
||
await server.waitForOutput('Server is running') | ||
|
||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World!') | ||
await setTimeout(100) | ||
|
||
await createHandlerFile({ path: 'app.js', response: 'Hello World! Updated' }) | ||
const result = await pEvent( | ||
server.child, | ||
'message', | ||
(message: any) => | ||
message?.type === 'hot-hook:full-reload' && message.path === join(fs.basePath, 'app.js') | ||
) | ||
assert.isDefined(result) | ||
}) | ||
|
||
test('Can define hardcoded boundaries from package json', async ({ fs }) => { | ||
await fakeInstall(fs.basePath) | ||
|
||
await fs.createJson('package.json', { | ||
'type': 'module', | ||
'hot-hook': { | ||
boundaries: ['./app.js'], | ||
}, | ||
}) | ||
await fs.create( | ||
'server.js', | ||
`import * as http from 'http' | ||
import { join } from 'node:path' | ||
const server = http.createServer(async (request, response) => { | ||
const app = await import('./app.js') | ||
await app.default(request, response) | ||
}) | ||
server.listen(3333, () => console.log('Server is running')) | ||
` | ||
) | ||
|
||
await createHandlerFile({ path: 'app.js', response: 'Hello World!' }) | ||
|
||
const server = runProcess('server.js', { | ||
cwd: fs.basePath, | ||
env: { NODE_DEBUG: 'hot-hook' }, | ||
nodeOptions: ['--import=hot-hook/register'], | ||
}) | ||
|
||
await server.waitForOutput('Server is running') | ||
|
||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World!') | ||
|
||
await setTimeout(100) | ||
await createHandlerFile({ path: 'app.js', response: 'Hello World! Updated' }) | ||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World! Updated') | ||
|
||
await setTimeout(100) | ||
await createHandlerFile({ path: 'app.js', response: 'Hello World! Updated new' }) | ||
await supertest('http://localhost:3333').get('/').expect(200).expect('Hello World! Updated new') | ||
}) | ||
}) |
Oops, something went wrong.