Skip to content

Commit

Permalink
wip: handle addWatchFile in resolveId hook
Browse files Browse the repository at this point in the history
does not work
  • Loading branch information
bluwy committed Nov 13, 2023
1 parent b1a456c commit 81ba7b8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
28 changes: 9 additions & 19 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import { FS_PREFIX } from '../constants'
import type { ResolvedConfig } from '../config'
import { createPluginHookUtils, getHookHandler } from '../plugins'
import { buildErrorMessage } from './middlewares/error'
import type { ModuleGraph, ModuleNode } from './moduleGraph'
import type { ModuleGraph } from './moduleGraph'

const noop = () => {}

Expand Down Expand Up @@ -183,10 +183,7 @@ export async function createPluginContainer(

const watchFiles = new Set<string>()
// _addedFiles from the `load()` hook gets saved here so it can be reused in the `transform()` hook
const moduleNodeToLoadAddedImports = new WeakMap<
ModuleNode,
Set<string> | null
>()
const idToAddedImports = new Map<string, Set<string> | null>()

const minimalContext: MinimalPluginContext = {
meta: {
Expand Down Expand Up @@ -278,13 +275,6 @@ export async function createPluginContainer(
}
}

function updateModuleLoadAddedImports(id: string, ctx: Context) {
const module = moduleGraph?.getModuleById(id)
if (module) {
moduleNodeToLoadAddedImports.set(module, ctx._addedImports)
}
}

// we should create a new context for each async hook pipeline so that the
// active plugin in that pipeline can be tracked in a concurrency-safe manner.
// using a class to make creating new contexts more efficient
Expand Down Expand Up @@ -543,11 +533,6 @@ export async function createPluginContainer(
}
this.sourcemapChain.push(inMap)
}
// Inherit `_addedImports` from the `load()` hook
const node = moduleGraph?.getModuleById(id)
if (node) {
this._addedImports = moduleNodeToLoadAddedImports.get(node) ?? null
}
}

_getCombinedSourcemap() {
Expand Down Expand Up @@ -714,6 +699,7 @@ export async function createPluginContainer(

if (id) {
partial.id = isExternalUrl(id) ? id : normalizePath(id)
idToAddedImports.set(partial.id, ctx._addedImports)
return partial as PartialResolvedId
} else {
return null
Expand All @@ -724,6 +710,8 @@ export async function createPluginContainer(
const ssr = options?.ssr
const ctx = new Context()
ctx.ssr = !!ssr
// Inherit `_addedImports` from `resolveId()` hook
ctx._addedImports = idToAddedImports.get(id) ?? null
for (const plugin of getSortedPlugins('load')) {
if (closed && !ssr) throwClosedServerError()
if (!plugin.load) continue
Expand All @@ -736,11 +724,11 @@ export async function createPluginContainer(
if (isObject(result)) {
updateModuleInfo(id, result)
}
updateModuleLoadAddedImports(id, ctx)
idToAddedImports.set(id, ctx._addedImports)
return result
}
}
updateModuleLoadAddedImports(id, ctx)
idToAddedImports.set(id, ctx._addedImports)
return null
},

Expand All @@ -749,6 +737,8 @@ export async function createPluginContainer(
const ssr = options?.ssr
const ctx = new TransformContext(id, code, inMap as SourceMap)
ctx.ssr = !!ssr
// Inherit `_addedImports` from `load()` hook
ctx._addedImports = idToAddedImports.get(id) ?? null
for (const plugin of getSortedPlugins('transform')) {
if (closed && !ssr) throwClosedServerError()
if (!plugin.transform) continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ test('should re-run transform when dependencies are edited', async () => {

editFile('plugin-dep-load.js', (str) => str)
await untilUpdated(() => page.textContent('#transform-count'), '3')

editFile('plugin-dep-resolve-id.js', (str) => str)
await untilUpdated(() => page.textContent('#transform-count'), '3')
})
1 change: 1 addition & 0 deletions playground/transform-plugin/plugin-dep-resolve-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file for detecting changes in tests
10 changes: 10 additions & 0 deletions playground/transform-plugin/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ let transformCount = 1

const transformPlugin = {
name: 'transform',
resolveId: {
order: 'pre',
handler(id) {
if (id === './index.js') {
// Ensure `index.js` is reloaded if 'plugin-dep-resolve-id.js' is changed
this.addWatchFile('./plugin-dep-resolve-id.js')
return file
}
},
},
load(id) {
if (id === file) {
// Ensure `index.js` is reloaded if 'plugin-dep-load.js' is changed
Expand Down

0 comments on commit 81ba7b8

Please sign in to comment.