Skip to content

Commit 6173467

Browse files
authored
feat: @nativescript/vite (#10948)
1 parent 1e55f0c commit 6173467

File tree

126 files changed

+27084
-6604
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+27084
-6604
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/dist
55
/tmp
66
/out-tsc
7+
.ns-vite-build
78

89
# dependencies
910
**/node_modules

apps/automated/nativescript.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ export default {
99
},
1010
cli: {
1111
packageManager: 'npm',
12+
additionalPathsToClean: ['.ns-vite-build'],
1213
},
14+
// bundler: 'vite',
15+
// bundlerConfigPath: 'vite.config.ts',
1316
} as NativeScriptConfig;

apps/automated/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@nativescript/android": "alpha",
1515
"@nativescript/ios": "alpha",
1616
"@nativescript/visionos": "~8.9.0",
17+
"@nativescript/vite": "file:../../dist/packages/vite",
1718
"@nativescript/webpack": "file:../../dist/packages/webpack5",
1819
"circular-dependency-plugin": "^5.2.2",
1920
"typescript": "~5.8.0"

apps/automated/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@
7373
}
7474
}
7575
},
76-
"implicitDependencies": ["webpack5"]
76+
"implicitDependencies": ["webpack5", "vite"]
7777
}

apps/automated/src/app-root.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Application, Frame, Page } from '@nativescript/core';
2+
3+
export function onLoaded(args) {
4+
try {
5+
console.log('[automated] app-root onLoaded');
6+
const rootPage = args.object as Page;
7+
// Create a Frame and navigate to main-page to ensure code-behind is bound
8+
const frame = new Frame();
9+
try {
10+
frame.navigate('main-page');
11+
} catch (e) {
12+
try {
13+
console.error('[automated] app-root onLoaded: navigate to main-page failed', e);
14+
} catch {}
15+
}
16+
// Replace the temporary Page root with the Frame containing the page
17+
try {
18+
if ((Application as any).resetRootView) {
19+
(Application as any).resetRootView({ create: () => frame });
20+
}
21+
} catch (e) {
22+
try {
23+
console.error('[automated] app-root onLoaded: resetRootView failed', e);
24+
} catch {}
25+
}
26+
} catch (e) {
27+
try {
28+
console.error('[automated] app-root onLoaded failed', e);
29+
} catch {}
30+
}
31+
}

apps/automated/src/app-root.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
<Frame defaultPage="main-page">
2-
</Frame>
1+
<Page loaded="onLoaded">
2+
<Label text="Preparing tests..." />
3+
</Page>

apps/automated/src/http/http-tests.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export var test_getString = function (done: (err: Error, res?: string) => void)
1717
function (e) {
1818
//// Argument (e) is Error!
1919
done(e);
20-
}
20+
},
2121
);
2222
};
2323

@@ -67,7 +67,7 @@ export var test_getJSON = function (done) {
6767
//// Argument (e) is Error!
6868
//console.log(e);
6969
done(e);
70-
}
70+
},
7171
);
7272
};
7373

@@ -115,7 +115,7 @@ export var test_getJSONP = function (done) {
115115
},
116116
function (e) {
117117
done(e);
118-
}
118+
},
119119
);
120120
};
121121

@@ -156,7 +156,7 @@ export var test_gzip_request_explicit = function (done) {
156156
},
157157
function (e) {
158158
done(e);
159-
}
159+
},
160160
);
161161
};
162162

@@ -180,7 +180,7 @@ export var test_gzip_request_implicit = function (done) {
180180
},
181181
function (e) {
182182
done(e);
183-
}
183+
},
184184
);
185185
};
186186

@@ -205,7 +205,7 @@ export var test_getImage = function (done) {
205205
(err) => {
206206
// Argument (e) is Error!
207207
done(err);
208-
}
208+
},
209209
);
210210
};
211211

@@ -258,7 +258,7 @@ export var test_getFile = function (done) {
258258
function (e) {
259259
//// Argument (e) is Error!
260260
done(e);
261-
}
261+
},
262262
);
263263
};
264264

@@ -280,7 +280,7 @@ export var test_getContentAsFile = function (done) {
280280
function (e) {
281281
//// Argument (e) is Error!
282282
done(e);
283-
}
283+
},
284284
);
285285
};
286286

@@ -347,7 +347,7 @@ export var test_request_responseStatusCodeShouldBeDefined = function (done) {
347347
function (e) {
348348
//// Argument (e) is Error!
349349
done(e);
350-
}
350+
},
351351
);
352352
};
353353

@@ -363,7 +363,7 @@ export var test_headRequest_responseStatusCodeShouldBeDefined = function (done)
363363
},
364364
function (e) {
365365
done(e);
366-
}
366+
},
367367
);
368368
};
369369

@@ -387,7 +387,7 @@ export var test_request_responseHeadersShouldBeDefined = function (done) {
387387
function (e) {
388388
//// Argument (e) is Error!
389389
done(e);
390-
}
390+
},
391391
);
392392
};
393393

@@ -409,7 +409,7 @@ export var test_request_responseContentShouldBeDefined = function (done) {
409409
function (e) {
410410
//// Argument (e) is Error!
411411
done(e);
412-
}
412+
},
413413
);
414414
};
415415

@@ -428,7 +428,7 @@ export var test_request_responseContentToStringShouldReturnString = function (do
428428
},
429429
function (e) {
430430
done(e);
431-
}
431+
},
432432
);
433433
};
434434

@@ -447,7 +447,7 @@ export var test_request_responseContentToJSONShouldReturnJSON = function (done)
447447
},
448448
function (e) {
449449
done(e);
450-
}
450+
},
451451
);
452452
};
453453

@@ -468,7 +468,7 @@ export var test_request_responseContentToImageShouldReturnCorrectImage = functio
468468
},
469469
function (e) {
470470
done(e);
471-
}
471+
},
472472
);
473473
};
474474

@@ -487,7 +487,7 @@ export var test_request_responseContentToFileFromUrlShouldReturnCorrectFile = fu
487487
},
488488
function (e) {
489489
done(e);
490-
}
490+
},
491491
);
492492
};
493493
export var test_request_responseContentToFileFromUrlShouldReturnCorrectFileAndCreateDirPathIfNecesary = function (done) {
@@ -507,7 +507,7 @@ export var test_request_responseContentToFileFromUrlShouldReturnCorrectFileAndCr
507507
},
508508
function (e) {
509509
done(e);
510-
}
510+
},
511511
);
512512
};
513513

@@ -526,7 +526,7 @@ export var test_request_responseContentToFileFromContentShouldReturnCorrectFile
526526
},
527527
function (e) {
528528
done(e);
529-
}
529+
},
530530
);
531531
};
532532

@@ -551,7 +551,7 @@ export var test_request_headersSentAndReceivedProperly = function (done) {
551551
},
552552
function (e) {
553553
done(e);
554-
}
554+
},
555555
);
556556
};
557557

@@ -597,7 +597,7 @@ export var test_request_contentSentAndReceivedProperly = function (done) {
597597
},
598598
function (e) {
599599
done(e);
600-
}
600+
},
601601
);
602602
};
603603

@@ -627,7 +627,7 @@ export var test_request_FormDataContentSentAndReceivedProperly = function (done)
627627
},
628628
function (e) {
629629
done(e);
630-
}
630+
},
631631
);
632632
};
633633

@@ -655,7 +655,7 @@ export var test_request_NonStringHeadersSentAndReceivedProperly = function (done
655655
},
656656
function (e) {
657657
done(e);
658-
}
658+
},
659659
);
660660
};
661661

@@ -684,12 +684,12 @@ export var test_request_jsonAsContentSentAndReceivedProperly = function (done) {
684684
function (e) {
685685
done(e);
686686
// console.log("Error occurred " + e);
687-
}
687+
},
688688
);
689689
};
690690

691691
export var test_getString_WorksProperlyInWorker = function (done) {
692-
const worker = new Worker('./http-string-worker');
692+
const worker = new Worker(new URL('./http-string-worker', import.meta.url));
693693
console.log('Worker Created');
694694
worker.onmessage = function (msg) {
695695
console.log('Message received');

apps/automated/src/main-page.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { Trace, Page } from '@nativescript/core';
22

3-
import * as tests from './test-runner';
4-
53
let executeTests = true;
64

75
Trace.enable();
@@ -18,10 +16,18 @@ Trace.addCategories(Trace.categories.Test + ',' + Trace.categories.Error);
1816
// ));
1917

2018
function runTests() {
21-
setTimeout(() => tests.runAll(''), 10);
19+
setTimeout(async () => {
20+
try {
21+
const tests = await import('./test-runner');
22+
tests.runAll('');
23+
} catch (e) {
24+
console.error('[automated] failed to load test-runner', e);
25+
}
26+
}, 10);
2227
}
2328

2429
export function onNavigatedTo(args) {
30+
console.log('onNavigatedTo');
2531
args.object.off(Page.loadedEvent, onNavigatedTo);
2632
if (executeTests) {
2733
executeTests = false;

apps/automated/src/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ Application.on(Application.lowMemoryEvent, function (args: ApplicationEventData)
8787
// Error events.
8888
Application.on(Application.uncaughtErrorEvent, function (args: UnhandledErrorEventData) {
8989
console.log('NativeScriptError:', args.error);
90-
console.log(args.error.nativeException ?? (<any>args.error).nativeError);
90+
console.log(args.error.nativeException ?? (args.error as any).nativeError);
9191
console.log(args.error.stackTrace ?? args.error.stack);
9292
});
9393

9494
Application.on(Application.discardedErrorEvent, function (args: DiscardedErrorEventData) {
9595
console.log('[Discarded] NativeScriptError:', args.error);
96-
console.log(args.error.nativeException ?? (<any>args.error).nativeError);
96+
console.log(args.error.nativeException ?? (args.error as any).nativeError);
9797
console.log(args.error.stackTrace ?? args.error.stack);
9898
});
9999

@@ -149,5 +149,4 @@ if (typeof NSDate !== 'undefined') {
149149
}
150150

151151
console.log(`TIME TO LOAD APP: ${time} ms`);
152-
153152
Application.run({ moduleName: 'app-root' });

apps/automated/src/test-runner.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ function printRunTestStats() {
351351

352352
let finalMessage = `\n` + `=== ALL TESTS COMPLETE ===\n` + `${allTests.length - failedTestCount} OK, ${failedTestCount} failed\n` + `DURATION: ${totalTime} ms\n` + `=== END OF TESTS ===\n`;
353353

354+
Trace.setCategories(Trace.categories.Test);
355+
Trace.enable();
354356
TKUnit.write(finalMessage, Trace.messageType.info);
355357

356358
failedTestInfo.forEach((message, i, arr) => {
@@ -497,7 +499,11 @@ export function runAll(testSelector?: string) {
497499

498500
const testModule = allTests[name];
499501

500-
const test = testModule.createTestCase ? testModule.createTestCase() : testModule;
502+
// In ESM environments (like Vite), module namespace objects are not extensible.
503+
// Some tests expect to set arbitrary properties like `name` on the test instance.
504+
// If a module doesn't provide `createTestCase()`, wrap its exports in a plain
505+
// mutable object to safely attach metadata without mutating the namespace object.
506+
const test = testModule.createTestCase ? testModule.createTestCase() : ({ ...testModule } as any);
501507
test.name = name;
502508

503509
testsQueue.push(new TestInfo(startLog, test));

0 commit comments

Comments
 (0)