Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit142df31

Browse files
authored
Fix ast issue (#208)
* Fix ast issue with re-exports* ast fix* Fix deprecated vite stuff* version bump
1 parent774d804 commit142df31

File tree

8 files changed

+497
-57
lines changed

8 files changed

+497
-57
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name":"react-router-devtools",
33
"description":"Devtools for React Router - debug, trace, find hydration errors, catch bugs and inspect server/client data with react-router-devtools",
44
"author":"Alem Tuzlak",
5-
"version":"5.0.5",
5+
"version":"5.0.6",
66
"license":"MIT",
77
"keywords": [
88
"react-router",

‎src/vite/plugin.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
251251
process.rdt_port=server.config.server.port??5173
252252
port=process.rdt_port
253253
})
254-
//@ts-ignore - vite 5/6 compat
255-
constchannel=server.hot.channels.find((channel)=>channel.name==="ws")??server.environments?.client.hot
254+
constchannel=server.hot
256255
consteditor=args?.editor??DEFAULT_EDITOR_CONFIG
257256
constopenInEditor=async(path:string|undefined,lineNum:string|undefined)=>{
258257
if(!path){
@@ -268,9 +267,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
268267
}
269268
if(routine==="request-event"){
270269
unusedEvents.set(parsedData.id+parsedData.startTime,parsedData)
271-
for(constclientofserver.hot.channels){
272-
client.send("request-event",JSON.stringify(parsedData))
273-
}
270+
server.hot.send("request-event",JSON.stringify(parsedData))
274271

275272
return
276273
}
@@ -291,9 +288,7 @@ export const reactRouterDevTools: (args?: ReactRouterViteConfig) => Plugin[] = (
291288
routeInfo.set(id,{loader:[],action:[data]})
292289
}
293290
}
294-
for(constclientofserver.hot.channels){
295-
client.send("route-info",JSON.stringify({ type, data}))
296-
}
291+
server.hot.send("route-info",JSON.stringify({ type, data}))
297292
})
298293
)
299294

‎src/vite/utils/data-functions-augment.test.ts‎

Lines changed: 187 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,30 @@ describe("transform", () => {
9292
)
9393
constexpected=removeWhitespace(`
9494
import { withLoaderWrapper as _withLoaderWrapper } from "react-router-devtools/server";
95-
import { loader } from "./loader.js";
96-
export { loader as _loader };
95+
import { loader as _loader } from "./loader.js";
96+
export const loader = _withLoaderWrapper(_loader, "test");
97+
`)
98+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
99+
})
100+
101+
it("should wrap the loader export when it's imported from another file and exported and used",()=>{
102+
constresult=augmentDataFetchingFunctions(
103+
`
104+
import { loader } from "./loader.js";
105+
const test = () => {
106+
const data = loader();
107+
}
108+
export { loader };
109+
`,
110+
"test",
111+
"/file/path"
112+
)
113+
constexpected=removeWhitespace(`
114+
import { withLoaderWrapper as _withLoaderWrapper } from "react-router-devtools/server";
115+
import { loader as _loader } from "./loader.js";
116+
const test = () => {
117+
const data = _loader();
118+
};
97119
export const loader = _withLoaderWrapper(_loader, "test");
98120
`)
99121
expect(removeWhitespace(result.code)).toStrictEqual(expected)
@@ -217,8 +239,30 @@ describe("transform", () => {
217239
)
218240
constexpected=removeWhitespace(`
219241
import { withClientLoaderWrapper as _withClientLoaderWrapper } from "react-router-devtools/client";
220-
import { clientLoader } from "./client-loader.js";
221-
export { clientLoader as _clientLoader };
242+
import { clientLoader as _clientLoader } from "./client-loader.js";
243+
export const clientLoader = _withClientLoaderWrapper(_clientLoader, "test");
244+
`)
245+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
246+
})
247+
248+
it("should wrap the client loader export when it's re-exported from another file and used by other code",()=>{
249+
constresult=augmentDataFetchingFunctions(
250+
`
251+
import { clientLoader } from "./client-loader.js";
252+
const test = () => {
253+
const data = clientLoader();
254+
}
255+
export { clientLoader };
256+
`,
257+
"test",
258+
"/file/path"
259+
)
260+
constexpected=removeWhitespace(`
261+
import { withClientLoaderWrapper as _withClientLoaderWrapper } from "react-router-devtools/client";
262+
import { clientLoader as _clientLoader } from "./client-loader.js";
263+
const test = () => {
264+
const data = _clientLoader();
265+
};
222266
export const clientLoader = _withClientLoaderWrapper(_clientLoader, "test");
223267
`)
224268
expect(removeWhitespace(result.code)).toStrictEqual(expected)
@@ -235,8 +279,30 @@ describe("transform", () => {
235279
)
236280
constexpected=removeWhitespace(`
237281
import { withClientLoaderWrapper as _withClientLoaderWrapper } from "react-router-devtools/client";
238-
import { clientLoader } from "./client-loader.js";
239-
export { clientLoader as _clientLoader };
282+
import { clientLoader as _clientLoader } from "./client-loader.js";
283+
export const clientLoader = _withClientLoaderWrapper(_clientLoader, "test");
284+
`)
285+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
286+
})
287+
288+
it("should wrap the client loader export when it's imported from another file and exported and used by other code",()=>{
289+
constresult=augmentDataFetchingFunctions(
290+
`
291+
import { clientLoader } from "./client-loader.js";
292+
const test = () => {
293+
const data = clientLoader();
294+
}
295+
export { clientLoader };
296+
`,
297+
"test",
298+
"/file/path"
299+
)
300+
constexpected=removeWhitespace(`
301+
import { withClientLoaderWrapper as _withClientLoaderWrapper } from "react-router-devtools/client";
302+
import { clientLoader as _clientLoader } from "./client-loader.js";
303+
const test = () => {
304+
const data = _clientLoader();
305+
};
240306
export const clientLoader = _withClientLoaderWrapper(_clientLoader, "test");
241307
`)
242308
expect(removeWhitespace(result.code)).toStrictEqual(expected)
@@ -373,8 +439,30 @@ describe("transform", () => {
373439
)
374440
constexpected=removeWhitespace(`
375441
import { withActionWrapper as _withActionWrapper } from "react-router-devtools/server";
376-
import { action } from "./action.js";
377-
export { action as _action };
442+
import { action as _action } from "./action.js";
443+
export const action = _withActionWrapper(_action, "test");
444+
`)
445+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
446+
})
447+
448+
it("should wrap the action export when it's imported from another file and exported and used by other code",()=>{
449+
constresult=augmentDataFetchingFunctions(
450+
`
451+
import { action } from "./action.js";
452+
const test = () => {
453+
const data = action();
454+
}
455+
export { action };
456+
`,
457+
"test",
458+
"/file/path"
459+
)
460+
constexpected=removeWhitespace(`
461+
import { withActionWrapper as _withActionWrapper } from "react-router-devtools/server";
462+
import { action as _action } from "./action.js";
463+
const test = () => {
464+
const data = _action();
465+
};
378466
export const action = _withActionWrapper(_action, "test");
379467
`)
380468
expect(removeWhitespace(result.code)).toStrictEqual(expected)
@@ -478,8 +566,31 @@ describe("transform", () => {
478566
)
479567
constexpected=removeWhitespace(`
480568
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
481-
import { clientAction } from "./client-action.js";
482-
export { clientAction as _clientAction };
569+
import { clientAction as _clientAction } from "./client-action.js";
570+
export const clientAction = _withClientActionWrapper(_clientAction, "test");
571+
`)
572+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
573+
})
574+
575+
it("should transform the client action export when it's re-exported from another file and keep it working if used somewhere",()=>{
576+
constresult=augmentDataFetchingFunctions(
577+
`
578+
import { clientAction } from "./client-action.js";
579+
580+
const test = () => {
581+
const data = clientAction();
582+
}
583+
export { clientAction };
584+
`,
585+
"test",
586+
"/file/path"
587+
)
588+
constexpected=removeWhitespace(`
589+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
590+
import { clientAction as _clientAction } from "./client-action.js";
591+
const test = () => {
592+
const data = _clientAction();
593+
};
483594
export const clientAction = _withClientActionWrapper(_clientAction, "test");
484595
`)
485596
expect(removeWhitespace(result.code)).toStrictEqual(expected)
@@ -512,13 +623,77 @@ describe("transform", () => {
512623
)
513624
constexpected=removeWhitespace(`
514625
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
515-
import { clientAction } from "./client-action.js";
516-
export { clientAction as _clientAction };
626+
import { clientAction as _clientAction } from "./client-action.js";
517627
export const clientAction = _withClientActionWrapper(_clientAction, "test");
518628
`)
519629
expect(removeWhitespace(result.code)).toStrictEqual(expected)
520630
})
521631

632+
it("should transform the client action export when it's imported from another file and exported and used by other code",()=>{
633+
constresult=augmentDataFetchingFunctions(
634+
`
635+
import { clientAction } from "./client-action.js";
636+
const test = () => {
637+
const data = clientAction();
638+
}
639+
export { clientAction };
640+
`,
641+
"test",
642+
"/file/path"
643+
)
644+
constexpected=removeWhitespace(`
645+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
646+
import { clientAction as _clientAction } from "./client-action.js";
647+
const test = () => {
648+
const data = _clientAction();
649+
};
650+
export const clientAction = _withClientActionWrapper(_clientAction, "test");
651+
`)
652+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
653+
})
654+
655+
it("should transform the client action export when it's imported from another file and exported and keep the export around if more things are exported",()=>{
656+
constresult=augmentDataFetchingFunctions(
657+
`
658+
import { clientAction } from "./client-action.js";
659+
const test = () => {
660+
const data = clientAction();
661+
}
662+
export { clientAction, test };
663+
`,
664+
"test",
665+
"/file/path"
666+
)
667+
constexpected=removeWhitespace(`
668+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
669+
import { clientAction as _clientAction } from "./client-action.js";
670+
const test = () => {
671+
const data = _clientAction();
672+
};
673+
export { test };
674+
export const clientAction = _withClientActionWrapper(_clientAction, "test");
675+
`)
676+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
677+
})
678+
it("should transform the client action export when it's imported from another file and exported and keep the export around if more things are exported",()=>{
679+
constresult=augmentDataFetchingFunctions(
680+
`
681+
import { withClientActionContextWrapper as _withClientActionContextWrapper } from "react-router-devtools/context";
682+
import { clientAction as _clientAction } from "./client-action.js";
683+
export const clientAction = _withClientActionContextWrapper(_clientAction, "test");
684+
`,
685+
"test",
686+
"/file/path"
687+
)
688+
constexpected=removeWhitespace(`
689+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
690+
import { withClientActionContextWrapper as _withClientActionContextWrapper } from "react-router-devtools/context";
691+
import { clientAction as _clientAction } from "./client-action.js";
692+
export const clientAction = _withClientActionWrapper(_withClientActionContextWrapper(_clientAction, "test"), "test");
693+
`)
694+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
695+
})
696+
522697
it("should wrap the clientAction export when it's exported via export { clientAction } and declared within the file",()=>{
523698
constresult=augmentDataFetchingFunctions(
524699
`

‎src/vite/utils/data-functions-augment.ts‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const ALL_EXPORTS = [...SERVER_COMPONENT_EXPORTS, ...CLIENT_COMPONENT_EXPORTS]
1010
consttransform=(ast:ParseResult<Babel.File>,routeId:string)=>{
1111
constserverHocs:Array<[string,Babel.Identifier]>=[]
1212
constclientHocs:Array<[string,Babel.Identifier]>=[]
13+
constimports:Array<[string,Babel.Identifier]>=[]
14+
1315
functiongetServerHocId(path:NodePath,hocName:string){
1416
constuid=path.scope.generateUidIdentifier(hocName)
1517
consthasHoc=serverHocs.find(([name])=>name===hocName)
@@ -35,6 +37,26 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
3537

3638
constimportDeclarations:Babel.ImportDeclaration[]=[]
3739
trav(ast,{
40+
ImportDeclaration(path){
41+
constspecifiers=path.node.specifiers
42+
for(constspecifierofspecifiers){
43+
if(!t.isImportSpecifier(specifier)||!t.isIdentifier(specifier.imported)){
44+
continue
45+
}
46+
constname=specifier.imported.name
47+
if(!ALL_EXPORTS.includes(name)){
48+
continue
49+
}
50+
constisReimported=specifier.local.name!==name
51+
constuniqueName=isReimported ?specifier.local :path.scope.generateUidIdentifier(name)
52+
imports.push([name,uniqueName])
53+
specifier.local=uniqueName
54+
// Replace the import specifier with a new one
55+
if(!isReimported){
56+
path.scope.rename(name,uniqueName.name)
57+
}
58+
}
59+
},
3860
ExportDeclaration(path){
3961
if(path.isExportNamedDeclaration()){
4062
constdecl=path.get("declaration")
@@ -148,14 +170,13 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
148170
}
149171
}else{
150172
transformations.push(()=>{
151-
constuniqueName=path.scope.generateUidIdentifier(name).name
152-
path.replaceWith(
153-
t.exportNamedDeclaration(
154-
null,
155-
[t.exportSpecifier(t.identifier(name),t.identifier(uniqueName))],
156-
path.node.source
157-
)
173+
constexistingImport=imports.find(([existingName])=>existingName===name)
174+
constuniqueName=existingImport?.[1].name??path.scope.generateUidIdentifier(name).name
175+
176+
constremainingSpecifiers=path.node.specifiers.filter(
177+
(exportSpecifier)=>!(t.isIdentifier(exportSpecifier.exported)&&exportSpecifier.exported.name===name)
158178
)
179+
path.replaceWith(t.exportNamedDeclaration(null,remainingSpecifiers,path.node.source))
159180

160181
// Insert the wrapped export after the modified export statement
161182
path.insertAfter(
@@ -169,6 +190,10 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
169190
[]
170191
)
171192
)
193+
constnewRemainingSpecifiers=path.node.specifiers.length
194+
if(newRemainingSpecifiers===0){
195+
path.remove()
196+
}
172197
})
173198
}
174199
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp