Skip to content
Source
Sign inSign up
TanStack/devtools

fix(devtools-vite): inject data-tsd-source before other plugins transform #548

MergedAlemTuzlakmerged 2 commits intomain fromrefs/pull/548/headmerged by AlemTuzlak View on GitHub

Showing 3 changed files with 73 additions and 0 deletions.

x
0 of 3 viewed, 3 left
5 additions and 0 deletions.changeset/inject-source-order-pre.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/devtools-vite': patch
+---
+​
+Run source injection before the transforms of other plugins (unless they also use `order: 'pre'`), also when `devtools()` is not first in the plugins array. Before this fix, a framework plugin that changed the code of only one environment shifted the line numbers, so `data-tsd-source` differed between SSR and client and caused hydration warnings.
6 additions and 0 deletionspackages/devtools-vite/src/plugin.tsModified
View file
@@ -60,6 +60,12 @@
return config.mode === 'development' && injectSourceConfig.enabled
},
transform: {
+ // Run before the transforms of plugins without `order: 'pre'`, also when
+ // devtools() is not first in the plugins array. A plugin that changes
+ // the code of one environment only would shift the lines and make the
+ // SSR and client locations differ, which breaks hydration. An earlier
+ // plugin whose transform also has `order: 'pre'` still runs first.
+ order: 'pre',
filter: {
id: {
exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//],
62 additions and 0 deletionspackages/devtools-vite/tests/inject-source-order.test.tsAdded
View file
@@ -0,0 +1,62 @@
+// @vitest-environment node
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'