Skip to content

Commit

Permalink
Restart dev server on package.json changes (withastro#5412)
Browse files Browse the repository at this point in the history
* Restart dev server on package.json changes

* Adding a changeset

* update vitefu

* lockfile updated
  • Loading branch information
matthewp authored Nov 16, 2022
1 parent 7191ed1 commit a278c7a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-walls-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Restart dev server on package.json changes
4 changes: 2 additions & 2 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@
"typescript": "*",
"unist-util-visit": "^4.1.0",
"vfile": "^5.3.2",
"vite": "~3.2.1",
"vitefu": "^0.2.0",
"vite": "~3.2.4",
"vitefu": "^0.2.1",
"yargs-parser": "^21.0.1",
"zod": "^3.17.3"
},
Expand Down
11 changes: 9 additions & 2 deletions packages/astro/src/core/config/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AstroConfig, AstroSettings, AstroUserConfig } from '../../@types/astro';
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './../constants.js';

import { fileURLToPath } from 'url';
import { fileURLToPath, pathToFileURL } from 'url';
import jsxRenderer from '../../jsx/renderer.js';
import { createDefaultDevConfig } from './config.js';
import { loadTSConfig } from './tsconfig.js';
Expand All @@ -24,9 +24,16 @@ export function createBaseSettings(config: AstroConfig): AstroSettings {
export function createSettings(config: AstroConfig, cwd?: string): AstroSettings {
const tsconfig = loadTSConfig(cwd);
const settings = createBaseSettings(config);

const watchFiles = tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : [];

if(cwd) {
watchFiles.push(fileURLToPath(new URL('./package.json', pathToFileURL(cwd))));
}

settings.tsConfig = tsconfig?.config;
settings.tsConfigPath = tsconfig?.path;
settings.watchFiles = tsconfig?.exists ? [tsconfig.path, ...tsconfig.extendedPaths] : [];
settings.watchFiles = watchFiles;
return settings;
}

Expand Down
33 changes: 33 additions & 0 deletions packages/astro/test/units/dev/restart.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { fileURLToPath } from 'url';

import {
createContainerWithAutomaticRestart,
Expand Down Expand Up @@ -147,4 +148,36 @@ describe('dev container restarts', () => {
await restart.container.close();
}
});

it('Is able to restart project on package.json changes', async () => {
const fs = createFs(
{
'/src/pages/index.astro': ``,
},
root
);

const { astroConfig } = await openConfig({
cwd: root,
flags: {},
cmd: 'dev',
logging: defaultLogging,
});
const settings = createSettings(astroConfig, fileURLToPath(root));

let restart = await createContainerWithAutomaticRestart({
params: { fs, root, settings },
});
await startContainer(restart.container);
expect(isStarted(restart.container)).to.equal(true);

try {
let restartComplete = restart.restarted();
fs.writeFileSync('/package.json', `{}`);
triggerFSEvent(restart.container, fs, '/package.json', 'change');
await restartComplete;
} finally {
await restart.container.close();
}
});
});
2 changes: 1 addition & 1 deletion packages/integrations/solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"dependencies": {
"babel-preset-solid": "^1.4.2",
"vitefu": "^0.2.0"
"vitefu": "^0.2.1"
},
"devDependencies": {
"astro": "workspace:*",
Expand Down
27 changes: 19 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a278c7a

Please sign in to comment.