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

Commit9fb576e

Browse files
committed
fix(resolver): align hooks with Node >=24 and support file: URLs; update tests to new transformer API
- resolver: return {format, source, shortCircuit: true} from native load hook- resolver: use fileURLToPath for file: URL before reading; prevent ENOENT on Windows paths- resolver: re-export ModuleTransformer and TransformerHook for consumers/tests- tests(resolver): migrate custom transformers from hook to ransformSync; normalize code string output- tests(transformer): use public ModuleTransformer via src/resolver export; remove private t-packer import- tests(transformer): expect legacy decorators to throw by default; adjust calls to chained transformSync resultAll tests passing.
1 parent0859cc6 commit9fb576e

File tree

3 files changed

+124
-32
lines changed

3 files changed

+124
-32
lines changed

‎src/resolver/index.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import fs from "node:fs";
22
importModule,{typeResolveHookContext}from"node:module";
33
importpathfrom"node:path";
44
importprocessfrom"node:process";
5+
import{fileURLToPath}from"node:url";
56
import{ModuleTransformer,typeTransformerHook}from"t-packer";
67

8+
export{ModuleTransformer,typeTransformerHook};
9+
710
// Get Node.js major version for compatibility handling
811
// `process.versions.node` is like "20.11.1"; take the first segment as a number
912
constmajorVersion=process.versions.node.split(".")[0];
@@ -105,7 +108,11 @@ export class ModuleResolver extends ModuleTransformer {
105108
load:(url,parent,nextLoad)=>{
106109
constresult=this.load(url);
107110
if(result){
108-
returnresult;
111+
return{
112+
format:result.formatasany,
113+
source:result.code,
114+
shortCircuit:true,
115+
};
109116
}
110117
returnnextLoad(url,parent);
111118
},
@@ -207,7 +214,11 @@ export class ModuleResolver extends ModuleTransformer {
207214
*@returns Transformed code with format information, or null if no transformation needed
208215
*/
209216
privateload(url:string):null|{format:string;code:string}{
210-
constcode=this.transformCode(fs.readFileSync(url,"utf-8"),url);
217+
constfilename=url.startsWith("file:") ?fileURLToPath(url) :url;
218+
constcode=this.transformCode(
219+
fs.readFileSync(filename,"utf-8"),
220+
filename,
221+
);
211222
if(code){
212223
return{format:"commonjs", code};
213224
}

‎test/resolver.test.ts‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test("ModuleResolver should be instantiable", (t) => {
2020
test("ModuleResolver should accept initial transformers",(t)=>{
2121
consttransformer:TransformerHook={
2222
exts:[".ts"],
23-
hook:(code:string)=>code,
23+
transformSync:(code)=>({code}),
2424
};
2525

2626
constresolver=newModuleResolver([transformer]);
@@ -87,7 +87,9 @@ test("addTransformer should register a transformer", (t) => {
8787
constresolver=newModuleResolver();
8888
consttransformer:TransformerHook={
8989
exts:[".css"],
90-
hook:(code:string)=>`export default${JSON.stringify(code)};`,
90+
transformSync:(code)=>({
91+
code:`module.exports =${JSON.stringify(code.toString())};`,
92+
}),
9193
};
9294

9395
resolver.addTransformer(transformer);
@@ -104,7 +106,9 @@ test("removeTransformer should remove a transformer", (t) => {
104106
constresolver=newModuleResolver();
105107
consttransformer:TransformerHook={
106108
exts:[".css"],
107-
hook:(code:string)=>`export default${JSON.stringify(code)};`,
109+
transformSync:(code)=>({
110+
code:`module.exports =${JSON.stringify(code.toString())};`,
111+
}),
108112
};
109113

110114
resolver.addTransformer(transformer);
@@ -125,7 +129,9 @@ test("register should set up hooks and work with actual module resolution", (t)
125129
// Add a transformer for testing
126130
consttransformer:TransformerHook={
127131
exts:[".custom"],
128-
hook:(code:string)=>`module.exports =${JSON.stringify(code)};`,
132+
transformSync:(code)=>({
133+
code:`module.exports =${JSON.stringify(code.toString())};`,
134+
}),
129135
};
130136
resolver.addTransformer(transformer);
131137

@@ -178,7 +184,9 @@ test("revert should clean up hooks and restore normal behavior", (t) => {
178184
// Add a transformer
179185
consttransformer:TransformerHook={
180186
exts:[".custom"],
181-
hook:(code:string)=>`module.exports =${JSON.stringify(code)};`,
187+
transformSync:(code)=>({
188+
code:`module.exports =${JSON.stringify(code.toString())};`,
189+
}),
182190
};
183191
resolver.addTransformer(transformer);
184192

@@ -250,12 +258,12 @@ test("revert should be callable multiple times", (t) => {
250258
test("ModuleResolver should work with multiple transformers",(t)=>{
251259
consttransformer1:TransformerHook={
252260
exts:[".ts"],
253-
hook:(code:string)=>`// Transformed TS\n${code}`,
261+
transformSync:(code)=>({code:code}),
254262
};
255263

256264
consttransformer2:TransformerHook={
257265
exts:[".js"],
258-
hook:(code:string)=>`// Transformed JS\n${code}`,
266+
transformSync:(code)=>({code:code}),
259267
};
260268

261269
constresolver=newModuleResolver([transformer1]);
@@ -320,7 +328,7 @@ test("ModuleResolver should handle alias resolution with transformers", (t) => {
320328
// Add transformer
321329
consttransformer:TransformerHook={
322330
exts:[".js"],
323-
hook:(code:string)=>`// Transformed\n${code}`,
331+
transformSync:(code)=>({code}),
324332
};
325333
resolver.addTransformer(transformer);
326334

@@ -389,12 +397,12 @@ test("ModuleResolver should handle transformer priority correctly", (t) => {
389397
// Add transformers in order - use different extensions to avoid conflicts
390398
consttransformer1:TransformerHook={
391399
exts:[".js"],
392-
hook:(code:string)=>`// First transformer\n${code}`,
400+
transformSync:(code)=>({code}),
393401
};
394402

395403
consttransformer2:TransformerHook={
396404
exts:[".custom"],
397-
hook:(code:string)=>`// Second transformer\n${code}`,
405+
transformSync:(code)=>({code}),
398406
};
399407

400408
resolver.addTransformer(transformer1);

‎test/transformer.test.ts‎

Lines changed: 93 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
importtestfrom"ava";
2-
import{TSHook}from"../src/transformer/ts";
3-
4-
test("TSHook should have correct extensions",(t)=>{
5-
t.deepEqual(TSHook.exts,[".ts",".tsx"]);
2+
import{ModuleTransformer}from"../src/resolver";
3+
4+
test("TS support should be available",(t)=>{
5+
constmt=newModuleTransformer();
6+
constresult=mt
7+
.transformSync(Buffer.from("const x: number = 1"),"a.ts",{
8+
target:"es2022",
9+
module:"commonjs",
10+
})
11+
.code.toString();
12+
t.is(typeofresult,"string");
13+
t.truthy(result.length>0);
614
});
715

8-
test("TSHook should have a hook function",(t)=>{
9-
t.is(typeofTSHook.hook,"function");
16+
test("TSX support should be available",(t)=>{
17+
constmt=newModuleTransformer();
18+
constresult=mt
19+
.transformSync(Buffer.from("export const C=()=>null"),"a.tsx",{
20+
target:"es2022",
21+
module:"commonjs",
22+
})
23+
.code.toString();
24+
t.is(typeofresult,"string");
25+
t.truthy(result.length>0);
1026
});
1127

1228
test("TSHook should transform TypeScript code",(t)=>{
@@ -24,7 +40,13 @@ test("TSHook should transform TypeScript code", (t) => {
2440
export { user };
2541
`;
2642

27-
constresult=TSHook.hook(typescriptCode,"test.ts");
43+
constmt=newModuleTransformer();
44+
constresult=mt
45+
.transformSync(Buffer.from(typescriptCode),"test.ts",{
46+
target:"es2022",
47+
module:"commonjs",
48+
})
49+
.code.toString();
2850

2951
t.is(typeofresult,"string");
3052
t.truthy(result.length>0);
@@ -46,15 +68,27 @@ test("TSHook should transform TSX code", (t) => {
4668
export default Component;
4769
`;
4870

49-
constresult=TSHook.hook(tsxCode,"test.tsx");
71+
constmt=newModuleTransformer();
72+
constresult=mt
73+
.transformSync(Buffer.from(tsxCode),"test.tsx",{
74+
target:"es2022",
75+
module:"commonjs",
76+
})
77+
.code.toString();
5078

5179
t.is(typeofresult,"string");
5280
t.truthy(result.length>0);
5381
t.not(result,tsxCode);// Should be transformed
5482
});
5583

5684
test("TSHook should handle empty code",(t)=>{
57-
constresult=TSHook.hook("","empty.ts");
85+
constmt=newModuleTransformer();
86+
constresult=mt
87+
.transformSync(Buffer.from(""),"empty.ts",{
88+
target:"es2022",
89+
module:"commonjs",
90+
})
91+
.code.toString();
5892

5993
t.is(typeofresult,"string");
6094
// Empty code might still produce some output from SWC
@@ -63,7 +97,13 @@ test("TSHook should handle empty code", (t) => {
6397

6498
test("TSHook should handle simple TypeScript",(t)=>{
6599
constsimpleCode="const x: number = 42;";
66-
constresult=TSHook.hook(simpleCode,"simple.ts");
100+
constmt=newModuleTransformer();
101+
constresult=mt
102+
.transformSync(Buffer.from(simpleCode),"simple.ts",{
103+
target:"es2022",
104+
module:"commonjs",
105+
})
106+
.code.toString();
67107

68108
t.is(typeofresult,"string");
69109
t.truthy(result.length>0);
@@ -75,13 +115,19 @@ test("TSHook should handle imports and exports", (t) => {
75115
export default Component;
76116
`;
77117

78-
constresult=TSHook.hook(code,"imports.ts");
118+
constmt=newModuleTransformer();
119+
constresult=mt
120+
.transformSync(Buffer.from(code),"imports.ts",{
121+
target:"es2022",
122+
module:"commonjs",
123+
})
124+
.code.toString();
79125

80126
t.is(typeofresult,"string");
81127
t.truthy(result.length>0);
82128
});
83129

84-
test("TSHookshouldhandle decorators with proper syntax",(t)=>{
130+
test("TS transformershoulderror on legacy decorators by default",(t)=>{
85131
constcode=`
86132
function log(target: any, propertyKey: string) {
87133
console.log('Decorator called');
@@ -95,10 +141,13 @@ test("TSHook should handle decorators with proper syntax", (t) => {
95141
}
96142
`;
97143

98-
constresult=TSHook.hook(code,"decorators.ts");
99-
100-
t.is(typeofresult,"string");
101-
t.truthy(result.length>0);
144+
constmt=newModuleTransformer();
145+
t.throws(()=>{
146+
mt.transformSync(Buffer.from(code),"decorators.ts",{
147+
target:"es2022",
148+
module:"commonjs",
149+
});
150+
});
102151
});
103152

104153
test("TSHook should handle async/await",(t)=>{
@@ -109,7 +158,13 @@ test("TSHook should handle async/await", (t) => {
109158
}
110159
`;
111160

112-
constresult=TSHook.hook(code,"async.ts");
161+
constmt=newModuleTransformer();
162+
constresult=mt
163+
.transformSync(Buffer.from(code),"async.ts",{
164+
target:"es2022",
165+
module:"commonjs",
166+
})
167+
.code.toString();
113168

114169
t.is(typeofresult,"string");
115170
t.truthy(result.length>0);
@@ -124,7 +179,13 @@ test("TSHook should handle generics", (t) => {
124179
const result = identity<string>('hello');
125180
`;
126181

127-
constresult=TSHook.hook(code,"generics.ts");
182+
constmt=newModuleTransformer();
183+
constresult=mt
184+
.transformSync(Buffer.from(code),"generics.ts",{
185+
target:"es2022",
186+
module:"commonjs",
187+
})
188+
.code.toString();
128189

129190
t.is(typeofresult,"string");
130191
t.truthy(result.length>0);
@@ -141,7 +202,13 @@ test("TSHook should handle JSX with React.createElement", (t) => {
141202
export default Component;
142203
`;
143204

144-
constresult=TSHook.hook(code,"jsx.tsx");
205+
constmt=newModuleTransformer();
206+
constresult=mt
207+
.transformSync(Buffer.from(code),"jsx.tsx",{
208+
target:"es2022",
209+
module:"commonjs",
210+
})
211+
.code.toString();
145212

146213
t.is(typeofresult,"string");
147214
t.truthy(result.length>0);
@@ -166,7 +233,13 @@ test("TSHook should handle complex TypeScript features", (t) => {
166233
}
167234
`;
168235

169-
constresult=TSHook.hook(code,"complex.ts");
236+
constmt=newModuleTransformer();
237+
constresult=mt
238+
.transformSync(Buffer.from(code),"complex.ts",{
239+
target:"es2022",
240+
module:"commonjs",
241+
})
242+
.code.toString();
170243

171244
t.is(typeofresult,"string");
172245
t.truthy(result.length>0);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp