Skip to content

Commit df82f89

Browse files
authored
fix: reset dupe plugin tracker on dev server close (#486)
Fixes false positive warnings like this in some cases: ``` Multiple instances of @netlify/vite-plugin have been loaded. This may cause unexpected behavior if the plugin is configured differently in each instance. If you have one instance of this plugin in your Vite config, you may safely remove it. ```
1 parent 16b73ea commit df82f89

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

packages/vite-plugin/src/main.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,25 @@ describe.for([['5.0.0'], ['6.0.0'], ['7.0.0']])('Vite %s', ([viteVersion]) => {
9898
await server.close()
9999
})
100100

101+
test('does not warn about duplicate plugin instances when server restarts', async () => {
102+
const mockLogger = createMockViteLogger()
103+
104+
const firstServer = await startTestServer({
105+
customLogger: mockLogger,
106+
plugins: [netlify({ middleware: false })],
107+
})
108+
expect(mockLogger.warn).not.toHaveBeenCalled()
109+
await firstServer.server.close()
110+
111+
const secondServer = await startTestServer({
112+
customLogger: mockLogger,
113+
plugins: [netlify({ middleware: false })],
114+
})
115+
expect(mockLogger.warn).not.toHaveBeenCalled()
116+
117+
await secondServer.server.close()
118+
})
119+
101120
test('Populates Netlify runtime environment (globals and env vars)', async () => {
102121
const fixture = new Fixture()
103122
.withFile(

packages/vite-plugin/src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export default function netlify(options: NetlifyPluginOptions = {}): any {
9191
await netlifyDev.start()
9292

9393
viteDevServer.httpServer.once('close', () => {
94+
// The whole vite dev instance has stopped, so reset the duplicate plugin tracker.
95+
// For example, this happens when a user makes a change to `vite.config.ts` and a new server starts.
96+
// We shouldn't print a false positive warning in these cases.
97+
delete process.env.__VITE_PLUGIN_NETLIFY_LOADED__
98+
9499
netlifyDev.stop()
95100
})
96101

0 commit comments

Comments
 (0)