Skip to content

Commit a8a2644

Browse files
fix(vite): changing config chrashes vite
1 parent 165fd22 commit a8a2644

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

vite-plugin/vite-plugin-polywarp.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ const execAsync = promisify(exec);
1515
const normalizePath = (path: string) => path.replaceAll("\\", "/");
1616

1717
export const polywarp = (): Plugin => {
18+
let laravel_polywarp_config_path = "";
19+
1820
let content_paths = new Array<string>();
1921
let translation_paths = new Array<string>();
2022
let outfile = "";
2123

24+
// instead of having to spread or merge the content/translation paths we save them in a single array
25+
// this way we can just check if the file changed is in any of the content or translation paths
26+
// this is useful for the hotUpdate method which may be called quite often
2227
let paths = new Array<string>();
2328

2429
const runCommand = async () => {
@@ -28,7 +33,7 @@ export const polywarp = (): Plugin => {
2833
return {
2934
name: "@itiden/vite-plugin-polywarp",
3035
enforce: "pre",
31-
async config() {
36+
async config(conf) {
3237
const { stderr, stdout } = await execAsync("php artisan polywarp:config");
3338

3439
if (stderr) {
@@ -41,21 +46,31 @@ export const polywarp = (): Plugin => {
4146
translation_paths = config.translation_directories.map(normalizePath);
4247
outfile = normalizePath(config.output_path);
4348

49+
laravel_polywarp_config_path = resolve(
50+
conf.root ?? process.cwd(),
51+
"config/polywarp.php"
52+
);
53+
4454
paths = [...content_paths, ...translation_paths];
4555
},
4656
async buildStart() {
47-
return runCommand().then(() =>
48-
this.environment.logger.info("generated polywarp files")
49-
);
57+
return runCommand();
5058
},
5159
async hotUpdate({ file, server }) {
52-
if (file === resolve(this.environment.config.root, "config/polywarp.php"))
53-
return server.restart();
60+
if (file === laravel_polywarp_config_path) {
61+
this.environment.logger.clearScreen("info");
62+
this.environment.logger.info(
63+
"config/polywarp.php config file changed, restarting server",
64+
{
65+
timestamp: true,
66+
}
67+
);
68+
server.restart();
69+
return;
70+
}
5471

5572
if (shouldRun(paths, { file, server }, outfile)) {
56-
await runCommand().then(() =>
57-
this.environment.logger.info("generated polywarp files")
58-
);
73+
await runCommand();
5974
}
6075
},
6176
};

0 commit comments

Comments
 (0)