Skip to content

Commit

Permalink
disable vite watchers unless command is dev or --watch flag enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Nov 16, 2024
1 parent 8b4cae6 commit 15de589
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
11 changes: 7 additions & 4 deletions packages/core/src/cli/cli-executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ program.addCommand(ClearCacheCommand);
program.addCommand(PluginCommand);
program.addCommand(PrintEnvCommand);



// have to pass through the root program for this one so we can access all the subcommands
addDocsCommand(program);

customizeHelp(program);

initCliRunCtx();
program
.hook('preAction', (thisCommand, actionCommand) => {
// we need to know up front whether to enable the file watchers when initializing the vite server
const enableWatch = actionCommand.name() === 'dev' || actionCommand.opts().watch;
initCliRunCtx(enableWatch);
});

debug(`finish loading - begin parse ${+new Date() - startBoot}ms`);
try {
await program.parseAsync();
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/cli/commands/resolve.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ addWatchMode(program); // must be first
addCacheFlags(program);
addServiceSelection(program, { disablePrompt: isSubshell() });


program.action(async (opts: {
// these args should be handled already by the helpers
// service?: string,
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/cli/commands/run.command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import kleur from 'kleur';
import _ from 'lodash-es';
import { ExecaChildProcess, execa } from 'execa';
import which from 'which';

import { tryCatch } from '@dmno/ts-lib';
import { DmnoCommand } from '../lib/dmno-command';
import { formatError, formattedValue, getItemSummary } from '../lib/formatting';
import { addServiceSelection } from '../lib/selection-helpers';
import { getCliRunCtx } from '../lib/cli-ctx';
import { addCacheFlags } from '../lib/cache-helpers';
Expand Down Expand Up @@ -75,6 +72,7 @@ program.action(async (_command, opts: {
}
}
fullInjectedEnv.DMNO_INJECTED_ENV = JSON.stringify(service.configraphEntity.getInjectedEnvJSON());
fullInjectedEnv.DMNO_PROCESS_UUID = 'abc123';

commandProcess = execa(pathAwareCommand || rawCommand, commandArgsOnly, {
stdio: 'inherit',
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/cli/lib/cli-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ const ctxHelpers = {

export const cliRunContext = new AsyncLocalStorage<CliRunCtx>();

export function initCliRunCtx() {
export function initCliRunCtx(enableWatch = false) {
cliRunContext.enterWith({
// not sure about this...
// configLoader: new ConfigLoaderProcess(),
configLoader: new ConfigLoader(),
configLoader: new ConfigLoader(enableWatch),
...ctxHelpers,
});
}
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/config-loader/config-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ConfigLoader {
// get isReady() { return this.isReadyDeferred.promise; }
isReady: Promise<void>;

constructor() {
constructor(private enableWatch: boolean) {
this.isReady = this.finishInit();
this.startAt = new Date();
}
Expand Down Expand Up @@ -67,7 +67,11 @@ export class ConfigLoader {

// TODO: we may want to do this on demand
// so it does not slow down `dmno init` or other commands that don't need it
const { viteRunner } = await setupViteServer(this.workspaceRootPath, (ctx) => this.viteHotReloadHandler(ctx));
const { viteRunner } = await setupViteServer({
workspaceRootPath: this.workspaceRootPath,
enableWatch: this.enableWatch,
hotReloadHandler: (ctx) => this.viteHotReloadHandler(ctx),
});
this.viteRunner = viteRunner;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/config-loader/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { ViteNodeServer } from 'vite-node/server';
import { installSourcemapsSupport } from 'vite-node/source-map';
import MagicString from 'magic-string';

export async function setupViteServer(
export async function setupViteServer(opts: {
workspaceRootPath: string,
hotReloadHandler: (ctx: HmrContext) => Promise<void>,
) {
enableWatch: boolean
}) {
const customPlugin: Plugin = {
name: 'dmno-config-loader-plugin',

Expand All @@ -26,7 +27,7 @@ export async function setupViteServer(
return {
// pointing at dist/index is hard-coded...
// we could extract the main entry point from the resolution instead?
id: `${workspaceRootPath}/node_modules/dmno/dist/index.js`,
id: `${opts.workspaceRootPath}/node_modules/dmno/dist/index.js`,
external: 'absolute',
};
}
Expand Down Expand Up @@ -68,14 +69,13 @@ export async function setupViteServer(
if (m.id) viteRunner.moduleCache.deleteByModuleId(m.id);
});

await hotReloadHandler(ctx);
await opts.hotReloadHandler(ctx);
},
};


// create vite server
const server = await createServer({
root: workspaceRootPath,
root: opts.workspaceRootPath,
appType: 'custom',
clearScreen: false,
logLevel: 'warn',
Expand All @@ -95,7 +95,7 @@ export async function setupViteServer(
// },
// ssr: true,
},

...!opts.enableWatch && { server: { watch: null } },
});
// console.log(server.config);

Expand Down

0 comments on commit 15de589

Please sign in to comment.