Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
fix: improve hmr handling
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2022
1 parent c567af3 commit 9557980
Show file tree
Hide file tree
Showing 14 changed files with 18 additions and 27 deletions.
1 change: 0 additions & 1 deletion playground/src/fixtures/a.ts

This file was deleted.

2 changes: 1 addition & 1 deletion playground/src/main.ts
@@ -1,5 +1,5 @@
import './style.css'
import * as fixtures from '../../test/fixtures'
import * as fixtures from '../../src/__tests__/fixture-a'

const app = document.getElementById('app')!

Expand Down
File renamed without changes.
Expand Up @@ -42,7 +42,7 @@ export const parent = import.meta.glob('../../playground/src/*.ts', { as: 'url'

export const rootMixedRelative = import.meta.glob([
'/*.ts',
'../*.ts',
'../fixture-b/*.ts',
], { as: 'url' })

export const cleverCwd1 = import.meta.glob(
Expand All @@ -51,7 +51,7 @@ export const cleverCwd1 = import.meta.glob(

export const cleverCwd2 = import.meta.glob([
'./modules/*.ts',
'../*.ts',
'../fixture-b/*.ts',
'!**/index.ts',
],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/__tests__/fixture-b/a.ts
@@ -0,0 +1 @@
export const name = 'a'
File renamed without changes.
File renamed without changes.
23 changes: 11 additions & 12 deletions src/__tests__/fixture.test.ts
Expand Up @@ -9,7 +9,7 @@ describe('fixture', async () => {
const options = { takeover: true }

it('transform', async () => {
const id = resolve(__dirname, './fixtures/index.ts')
const id = resolve(__dirname, './fixture-a/index.ts')
const code = (await transformWithEsbuild(await fs.readFile(id, 'utf-8'), id)).code
const root = process.cwd()

Expand Down Expand Up @@ -66,31 +66,30 @@ describe('fixture', async () => {
export const rootMixedRelative = {
\\"/build.config.ts\\": () => import(\\"../../../build.config.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/client.d.ts\\": () => import(\\"../../../client.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/fixture.test.ts\\": () => import(\\"../fixture.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/parse.test.ts\\": () => import(\\"../parse.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/utils.test.ts\\": () => import(\\"../utils.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/fixture-b/a.ts\\": () => import(\\"../fixture-b/a.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/fixture-b/b.ts\\": () => import(\\"../fixture-b/b.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/fixture-b/index.ts\\": () => import(\\"../fixture-b/index.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/takeover.d.ts\\": () => import(\\"../../../takeover.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/types.ts\\": () => import(\\"../../../types.ts?url&lang.ts\\").then(m => m[\\"default\\"])
};
export const cleverCwd1 = {
\\"./node_modules/framework/pages/hello.page.js\\": () => import(\\"./node_modules/framework/pages/hello.page.js\\")
};
export const cleverCwd2 = {
\\"../fixture.test.ts\\": () => import(\\"../fixture.test.ts\\"),
\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),
\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),
\\"../parse.test.ts\\": () => import(\\"../parse.test.ts\\"),
\\"../utils.test.ts\\": () => import(\\"../utils.test.ts\\")
\\"../fixture-b/a.ts\\": () => import(\\"../fixture-b/a.ts\\"),
\\"../fixture-b/b.ts\\": () => import(\\"../fixture-b/b.ts\\")
};
"
`)
})

it('virtual modules', async () => {
const root = resolve(__dirname, './fixtures')
const root = resolve(__dirname, './fixture-a')
const code = [
'import.meta.glob(\'/modules/*.ts\')',
'import.meta.glob([\'/../*.ts\'])',
'import.meta.glob([\'/../fixture-b/*.ts\'])',
].join('\n')
expect((await transform(code, 'virtual:module', root, resolveId, options))?.s.toString())
.toMatchInlineSnapshot(`
Expand All @@ -100,9 +99,9 @@ describe('fixture', async () => {
\\"/modules/index.ts\\": () => import(\\"/modules/index.ts\\")
}
{
\\"/../fixture.test.ts\\": () => import(\\"/../fixture.test.ts\\"),
\\"/../parse.test.ts\\": () => import(\\"/../parse.test.ts\\"),
\\"/../utils.test.ts\\": () => import(\\"/../utils.test.ts\\")
\\"/../fixture-b/a.ts\\": () => import(\\"/../fixture-b/a.ts\\"),
\\"/../fixture-b/b.ts\\": () => import(\\"/../fixture-b/b.ts\\"),
\\"/../fixture-b/index.ts\\": () => import(\\"/../fixture-b/index.ts\\")
}"
`,
)
Expand Down
14 changes: 3 additions & 11 deletions src/plugin.ts
Expand Up @@ -53,17 +53,9 @@ export function importGlobPlugin(options: PluginOptions = {}): Plugin {
server = _server
const handleFileAddUnlink = (file: string) => {
const modules = getAffectedModules(file)
_server.ws.send({
type: 'update',
updates: modules.map((mod) => {
_server.moduleGraph.invalidateModule(mod)
return {
acceptedPath: mod.id!,
path: mod.id!,
timestamp: Date.now(),
type: 'js-update',
}
}),
modules.forEach((i) => {
if (i?.file)
_server.moduleGraph.onFileChange(i.file)
})
}
server.watcher.on('unlink', handleFileAddUnlink)
Expand Down

0 comments on commit 9557980

Please sign in to comment.