Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jan 9, 2024
1 parent bf629ad commit 22d1575
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 70 deletions.
34 changes: 0 additions & 34 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2759,23 +2759,6 @@ Repository: sindresorhus/open
---------------------------------------

## p-limit
License: MIT
By: Sindre Sorhus
Repository: sindresorhus/p-limit

> MIT License
>
> Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---------------------------------------

## parse5
License: MIT
By: Ivan Nikulin, https://github.com/inikulin/parse5/graphs/contributors
Expand Down Expand Up @@ -3898,20 +3881,3 @@ Repository: github:eemeli/yaml
> OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
> TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
> THIS SOFTWARE.
---------------------------------------

## yocto-queue
License: MIT
By: Sindre Sorhus
Repository: sindresorhus/yocto-queue

> MIT License
>
> Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 0 additions & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
"mrmime": "^2.0.0",
"okie": "^1.0.1",
"open": "^8.4.2",
"p-limit": "^5.0.0",
"parse5": "^7.1.2",
"periscopic": "^4.0.2",
"picocolors": "^1.0.0",
Expand Down
58 changes: 58 additions & 0 deletions packages/vite/src/node/ssr/runtime/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,64 @@ import type { HMRPayload } from 'types/hmrPayload'
import type { ViteRuntime } from './runtime'
import { unwrapId } from './utils'

class Queue {
private queue: {
promise: () => Promise<void>
resolve: (value?: unknown) => void
reject: (err?: unknown) => void
}[] = []
private pending = false

enqueue(promise: () => Promise<void>) {
return new Promise<any>((resolve, reject) => {
this.queue.push({
promise,
resolve,
reject,
})
this.dequeue()
})
}

dequeue() {
if (this.pending) {
return false
}
const item = this.queue.shift()
if (!item) {
return false
}
try {
this.pending = true
item
.promise()
.then((value) => {
this.pending = false
item.resolve(value)
this.dequeue()
})
.catch((err) => {
this.pending = false
item.reject(err)
this.dequeue()
})
} catch (err) {
this.pending = false
item.reject(err)
this.dequeue()
}
return true
}
}

// updates to HMR should go one after another. It is possible to trigger another update during the invalidation for example.
export function createHMRHandler(
runtime: ViteRuntime,
): (payload: HMRPayload) => Promise<void> {
const queue = new Queue()
return (payload) => queue.enqueue(() => handleHMRUpdate(runtime, payload))
}

export async function handleHMRUpdate(
runtime: ViteRuntime,
payload: HMRPayload,
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { ModuleCacheMap } from './moduleCache'
export { ViteRuntime } from './runtime'
export { ESModulesRunner } from './esmRunner'

export { handleHMRUpdate } from './hmrHandler'
export { handleHMRUpdate, createHMRHandler } from './hmrHandler'

export type {
ViteModuleRunner,
Expand Down
15 changes: 4 additions & 11 deletions packages/vite/src/node/ssr/runtime/node/mainThreadRuntime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import limit from 'p-limit'
import type { ViteDevServer } from '../../../index'
import { ViteRuntime } from '../runtime'
import { ESModulesRunner } from '../esmRunner'
import { handleHMRUpdate } from '../hmrHandler'
import { createHMRHandler } from '../hmrHandler'
import type { ViteServerClientOptions } from '../types'
import { ServerHMRConnector } from './serverHmrConnector'

Expand All @@ -12,7 +11,7 @@ export async function createViteRuntime(
): Promise<ViteRuntime> {
const hmrConnector =
server.config.server.hmr === false ? null : new ServerHMRConnector(server)
const client = new ViteRuntime(
const tuneimt = new ViteRuntime(
{
root: server.config.root,
fetchModule(id, importer) {
Expand All @@ -24,13 +23,7 @@ export async function createViteRuntime(
new ESModulesRunner(),
)

// TODO: just implement a simple queue instead of using o-limit
// process hmr updates one by one - it's possible to trigger one update inside another
// for example, triggering .invalidate will send update events while update event is in process
const limiter = limit(1)
hmrConnector?.onUpdate((payload) =>
limiter(() => handleHMRUpdate(client, payload)),
)
hmrConnector?.onUpdate(createHMRHandler(tuneimt))

return client
return tuneimt
}
36 changes: 16 additions & 20 deletions playground/hmr-ssr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,10 @@ afterAll(async () => {
const hmr = (key: string) => (globalThis.__HMR__[key] as string) || ''

const updated = (file: string, via?: string) => {
const filepath = resolvePath(
'..',
file.startsWith('/') ? file.slice(1) : file,
)
if (via) {
return `[vite] hot updated: ${filepath} via ${resolvePath('..', via)}`
return `[vite] hot updated: ${file} via ${via}`
}
return `[vite] hot updated: ${filepath}`
return `[vite] hot updated: ${file}`
}

const invalidated = (file: string) => {
Expand Down Expand Up @@ -89,7 +85,7 @@ describe('hmr works correctly', () => {
'foo was: 1',
'(self-accepting 1) foo is now: 2',
'(self-accepting 2) foo is now: 2',
updated('hmr.ts'),
updated('/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -106,7 +102,7 @@ describe('hmr works correctly', () => {
'foo was: 2',
'(self-accepting 1) foo is now: 3',
'(self-accepting 2) foo is now: 3',
updated('hmr.ts'),
updated('/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -129,7 +125,7 @@ describe('hmr works correctly', () => {
'(single dep) nested foo is now: 1',
'(multi deps) foo is now: 2',
'(multi deps) nested foo is now: 1',
updated('hmrDep.js', 'hmr.ts'),
updated('/hmrDep.js', '/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -149,7 +145,7 @@ describe('hmr works correctly', () => {
'(single dep) nested foo is now: 1',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 1',
updated('hmrDep.js', 'hmr.ts'),
updated('/hmrDep.js', '/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -172,7 +168,7 @@ describe('hmr works correctly', () => {
'(single dep) nested foo is now: 2',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 2',
updated('hmrDep.js', 'hmr.ts'),
updated('/hmrDep.js', '/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -192,7 +188,7 @@ describe('hmr works correctly', () => {
'(single dep) nested foo is now: 3',
'(multi deps) foo is now: 3',
'(multi deps) nested foo is now: 3',
updated('hmrDep.js', 'hmr.ts'),
updated('/hmrDep.js', '/hmr.ts'),
'>>> vite:afterUpdate -- update',
],
true,
Expand All @@ -211,11 +207,11 @@ describe('hmr works correctly', () => {
'>>> vite:beforeUpdate -- update',
`>>> vite:invalidate -- ${resolvePath('../invalidation/child.js')}`,
invalidated('invalidation/child.js'),
updated('invalidation/child.js'),
updated('/invalidation/child.js'),
'>>> vite:afterUpdate -- update',
'>>> vite:beforeUpdate -- update',
'(invalidation) parent is executing',
updated('invalidation/parent.js'),
updated('/invalidation/parent.js'),
'>>> vite:afterUpdate -- update',
],
true,
Expand Down Expand Up @@ -374,7 +370,7 @@ describe('acceptExports', () => {

const fileName = 'target.ts'
const file = `${testDir}/${fileName}`
const url = resolvePath('..', file)
const url = `/${file}`

let dep = 'dep0'

Expand All @@ -396,7 +392,7 @@ describe('acceptExports', () => {

test('the callback is called with the new version the module', async () => {
const callbackFile = `${testDir}/callback.ts`
const callbackUrl = resolvePath('..', callbackFile)
const callbackUrl = `/${callbackFile}`

await untilConsoleLogAfter(
() => {
Expand Down Expand Up @@ -582,7 +578,7 @@ describe('acceptExports', () => {
},
HOT_UPDATED,
(logs) => {
expect(logs).toEqual(['>>> side FX !!', updated(`${testDir}/${file}`)])
expect(logs).toEqual(['>>> side FX !!', updated(`/${testDir}/${file}`)])
},
)
})
Expand All @@ -609,7 +605,7 @@ describe('acceptExports', () => {
},
HOT_UPDATED,
(logs) => {
expect(logs).toEqual(['-> unused <-', updated(url.slice(1))])
expect(logs).toEqual(['-> unused <-', updated(url)])
},
)
})
Expand Down Expand Up @@ -848,9 +844,9 @@ test('hmr works for self-accepted module within circular imported files', async
)
await untilUpdated(() => el(), 'cc')
expect(serverLogs.length).greaterThanOrEqual(1)
// Should still keep hmr update, but it'll error on the browser-side and will refresh itself.
// Match on full log not possible because of color markers
expect(serverLogs.at(-1)!).toContain('page reload')
expect(serverLogs.at(-1)!).toContain('(circular imports)')
expect(serverLogs.at(-1)!).toContain('hmr update')
})

test('hmr should not reload if no accepted within circular imported files', async () => {
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 22d1575

Please sign in to comment.