Skip to content

Commit fd695d1

Browse files
committed
feat: wip vite setup
1 parent 5b80974 commit fd695d1

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

lib/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ export const CACACHE_DIRECTORY_NAME = "_cacache";
224224
export const FILES_CHANGE_EVENT_NAME = "filesChangeEvent";
225225
export const INITIAL_SYNC_EVENT_NAME = "initialSyncEvent";
226226
export const PREPARE_READY_EVENT_NAME = "prepareReadyEvent";
227-
export const WEBPACK_COMPILATION_COMPLETE = "webpackCompilationComplete";
228227
export const BUNDLER_COMPILATION_COMPLETE = "bundlerCompilationComplete";
229228

230229
export class DebugCommandErrors {

lib/controllers/prepare-controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import { hook } from "../common/helpers";
1313
import { injector } from "../common/yok";
1414
import {
1515
AnalyticsEventLabelDelimiter,
16+
BUNDLER_COMPILATION_COMPLETE,
1617
CONFIG_FILE_NAME_JS,
1718
CONFIG_FILE_NAME_TS,
1819
PACKAGE_JSON_FILE_NAME,
1920
PLATFORMS_DIR_NAME,
2021
PREPARE_READY_EVENT_NAME,
2122
SupportedPlatform,
2223
TrackActionNames,
23-
WEBPACK_COMPILATION_COMPLETE,
2424
} from "../constants";
2525
import { IOptions, IWatchIgnoreListService } from "../declarations";
2626
import {
@@ -119,7 +119,7 @@ export class PrepareController extends EventEmitter {
119119
) {
120120
await this.$bundlerCompilerService.stopBundlerCompiler(platformLowerCase);
121121
this.$bundlerCompilerService.removeListener(
122-
WEBPACK_COMPILATION_COMPLETE,
122+
BUNDLER_COMPILATION_COMPLETE,
123123
this.webpackCompilerHandler,
124124
);
125125
this.watchersData[projectDir][
@@ -297,7 +297,7 @@ export class PrepareController extends EventEmitter {
297297

298298
this.webpackCompilerHandler = handler.bind(this);
299299
this.$bundlerCompilerService.on(
300-
WEBPACK_COMPILATION_COMPLETE,
300+
BUNDLER_COMPILATION_COMPLETE,
301301
this.webpackCompilerHandler,
302302
);
303303

lib/services/bundler/bundler-compiler-service.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,31 @@ export class BundlerCompilerService
224224
});
225225

226226
childProcess.on("close", async (arg: any) => {
227-
await this.$cleanupService.removeKillProcess(
228-
childProcess.pid.toString(),
229-
);
230-
231227
const exitCode = typeof arg === "number" ? arg : arg && arg.code;
232228
this.$logger.trace(
233229
`${capitalizeFirstLetter(projectData.bundler)} process exited with code ${exitCode} when we expected it to be long living with watch.`,
234230
);
231+
if (this.getBundler() === "vite" && exitCode === 0) {
232+
// note experimental: investigate watch mode
233+
const bundlePath = path.join(
234+
projectData.projectDir,
235+
"dist/bundle.js",
236+
);
237+
console.log("bundlePath:", bundlePath);
238+
const data = {
239+
files: [bundlePath],
240+
hasOnlyHotUpdateFiles: false,
241+
hmrData: {},
242+
platform: platformData.platformNameLowerCase,
243+
};
244+
this.emit(BUNDLER_COMPILATION_COMPLETE, data);
245+
resolve(1);
246+
return;
247+
}
248+
249+
await this.$cleanupService.removeKillProcess(
250+
childProcess.pid.toString(),
251+
);
235252
const error: any = new Error(
236253
`Executing ${projectData.bundler} failed with exit code ${exitCode}.`,
237254
);
@@ -341,12 +358,15 @@ export class BundlerCompilerService
341358
projectData,
342359
prepareData,
343360
);
344-
const envParams = await this.buildEnvCommandLineParams(
345-
envData,
346-
platformData,
347-
projectData,
348-
prepareData,
349-
);
361+
const isVite = this.getBundler() === "vite";
362+
const envParams = isVite
363+
? [`--mode=${platformData.platformNameLowerCase}`]
364+
: await this.buildEnvCommandLineParams(
365+
envData,
366+
platformData,
367+
projectData,
368+
prepareData,
369+
);
350370
const additionalNodeArgs =
351371
semver.major(process.version) <= 8 ? ["--harmony"] : [];
352372

@@ -366,8 +386,10 @@ export class BundlerCompilerService
366386
...envParams,
367387
].filter(Boolean);
368388

369-
if (prepareData.watch) {
370-
args.push("--watch");
389+
if (!isVite) {
390+
if (prepareData.watch) {
391+
args.push("--watch");
392+
}
371393
}
372394

373395
const stdio = prepareData.watch ? ["ipc"] : "inherit";
@@ -391,6 +413,8 @@ export class BundlerCompilerService
391413
});
392414
}
393415

416+
console.log("args:", args);
417+
394418
const childProcess = this.$childProcess.spawn(
395419
process.execPath,
396420
args,
@@ -670,7 +694,15 @@ export class BundlerCompilerService
670694
private getBundlerExecutablePath(projectData: IProjectData): string {
671695
const bundler = this.getBundler();
672696

673-
if (this.isModernBundler(projectData)) {
697+
if (bundler === "vite") {
698+
const packagePath = resolvePackagePath(`vite`, {
699+
paths: [projectData.projectDir],
700+
});
701+
702+
if (packagePath) {
703+
return path.resolve(packagePath, "bin", "vite.js");
704+
}
705+
} else if (this.isModernBundler(projectData)) {
674706
const packagePath = resolvePackagePath(`@nativescript/${bundler}`, {
675707
paths: [projectData.projectDir],
676708
});

0 commit comments

Comments
 (0)