Skip to content

Commit

Permalink
Make hot-reload/hot-restart on-save work for web + css/html
Browse files Browse the repository at this point in the history
  • Loading branch information
DanTup committed Nov 21, 2019
1 parent a64b86b commit 6e55b8b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -1209,13 +1209,13 @@
"dart.flutterHotReloadOnSave": {
"type": "boolean",
"default": true,
"description": "Whether to automatically send a Hot Reload request during a Flutter debug session when saving files.",
"description": "Whether to automatically send a Hot Reload request during debug session when saving files.",
"scope": "window"
},
"dart.flutterHotRestartOnSave": {
"type": "boolean",
"default": false,
"description": "Whether to automatically send a Hot Restart request during a Flutter debug session when saving files if Hot Reload is not available.",
"default": true,
"description": "Whether to automatically send a Hot Restart request during a debug session when saving files if Hot Reload is not available but Hot Restart is.",
"scope": "window"
},
"dart.flutterCreateOrganization": {
Expand Down
5 changes: 3 additions & 2 deletions src/extension/flutter/hot_reload_save_handler.ts
Expand Up @@ -6,7 +6,7 @@ import { IAmDisposable } from "../../shared/interfaces";
import { fsPath } from "../../shared/vscode/utils";
import { DebugCommands } from "../commands/debug";
import { config } from "../config";
import { isAnalyzableAndInWorkspace } from "../utils";
import { isWithinWorkspace, shouldHotReloadFor } from "../utils";

export class HotReloadOnSaveHandler implements IAmDisposable {
private disposables: IAmDisposable[] = [];
Expand All @@ -24,6 +24,7 @@ export class HotReloadOnSaveHandler implements IAmDisposable {
}));

// FS-watcher version.
// TODO: Make this support everything that shouldHotReloadFor() does.
const watcher = workspace.createFileSystemWatcher("**/*.dart");
this.disposables.push(watcher);
watcher.onDidChange(this.handleFileSystemChange, this);
Expand Down Expand Up @@ -60,7 +61,7 @@ export class HotReloadOnSaveHandler implements IAmDisposable {
const commandToRun = shouldHotReload ? "flutter.hotReload" : "flutter.hotRestart";

// Bail out if we're in an external file, or not Dart.
if (!isAnalyzableAndInWorkspace(file) || path.extname(fsPath(file.uri)) !== ".dart")
if (!isWithinWorkspace(fsPath(file.uri)) || !shouldHotReloadFor(file))
return;

// Don't do if we have errors for the saved file.
Expand Down
12 changes: 12 additions & 0 deletions src/extension/utils.ts
Expand Up @@ -101,6 +101,18 @@ export function isAnalyzable(file: { uri: Uri, isUntitled?: boolean, languageId?
|| (extension !== undefined && analyzableFileExtensions.includes(extension));
}

export function shouldHotReloadFor(file: { uri: Uri, isUntitled?: boolean, languageId?: string }): boolean {
if (file.isUntitled || !fsPath(file.uri) || file.uri.scheme !== "file")
return false;

const reloadableFileExtensions = ["dart", "htm", "html", "css"];

const extName = path.extname(fsPath(file.uri));
const extension = extName ? extName.substr(1) : undefined;

return extension !== undefined && reloadableFileExtensions.includes(extension);
}

export function isAnalyzableAndInWorkspace(file: { uri: Uri, isUntitled?: boolean, languageId?: string }): boolean {
return isAnalyzable(file) && isWithinWorkspace(fsPath(file.uri));
}
Expand Down

0 comments on commit 6e55b8b

Please sign in to comment.