Skip to content

Restore --live-reload option #1778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ tool/test_all.dart
# Ignore extra files from dart2js that we don't want
build_runner/lib/src/server/graph_viz_main.dart.js.deps
build_runner/lib/src/server/graph_viz_main.dart.js.tar.gz
build_runner/lib/src/server/hot_reload_client/client.dart.js.deps
build_runner/lib/src/server/hot_reload_client/client.dart.js.tar.gz
build_runner/lib/src/server/build_updates_client/hot_reload_client.dart.js.deps
build_runner/lib/src/server/build_updates_client/hot_reload_client.dart.js.tar.gz

# Ignore dazel generated
.dazel
Expand Down
5 changes: 2 additions & 3 deletions build_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## 0.10.2

- `--live-reload` cli option is replaced with `--hot-reload` one with appropriate
functionality. See [hot-module-reloading](../docs/hot_module_reloading.md) for more
info.
- Added `--hot-reload` cli option and appropriate functionality.
See [hot-module-reloading](../docs/hot_module_reloading.md) for more info.

## 0.10.1+1

Expand Down
5 changes: 3 additions & 2 deletions build_runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ Some commands also have additional options:
##### serve

- `--hostname`: The host to run the server on.
- `--hot-reload`: Enables automatic module reloading on rebuilds.
See [hot-module-reloading](../docs/hot_module_reloading.md) for more info.
- `--live-reload`: Enables automatic page reloading on rebuilds.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you provide both which one takes precedence? Could potentially model this as an enum to prevent that issue.

- `--hot-reload`: Enables automatic reloading of changed modules.
See [hot-module-reloading](../docs/hot_module_reloading.md) for more info.

Trailing args of the form `<directory>:<port>` are supported to customize what
directories are served, and on what ports.
Expand Down
4 changes: 4 additions & 0 deletions build_runner/lib/src/entrypoint/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:path/path.dart' as p;
const assumeTtyOption = 'assume-tty';
const defineOption = 'define';
const deleteFilesByDefaultOption = 'delete-conflicting-outputs';
const liveReloadOption = 'live-reload';
const hotReloadOption = 'hot-reload';
const logPerformanceOption = 'log-performance';
const logRequestsOption = 'log-requests';
Expand Down Expand Up @@ -131,12 +132,14 @@ class SharedOptions {
/// Options specific to the `serve` command.
class ServeOptions extends SharedOptions {
final String hostName;
final bool liveReload;
final bool hotReload;
final bool logRequests;
final List<ServeTarget> serveTargets;

ServeOptions._({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some validation here that both hotReload and liveReload are not true.

@required this.hostName,
@required this.liveReload,
@required this.hotReload,
@required this.logRequests,
@required this.serveTargets,
Expand Down Expand Up @@ -203,6 +206,7 @@ class ServeOptions extends SharedOptions {

return ServeOptions._(
hostName: argResults[hostnameOption] as String,
liveReload: argResults[liveReloadOption] as bool,
hotReload: argResults[hotReloadOption] as bool,
logRequests: argResults[logRequestsOption] as bool,
serveTargets: serveTargets,
Expand Down
10 changes: 8 additions & 2 deletions build_runner/lib/src/entrypoint/serve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class ServeCommand extends WatchCommand {
defaultsTo: false,
negatable: false,
help: 'Enables logging for each request to the server.')
..addFlag(liveReloadOption,
defaultsTo: false,
negatable: false,
help: 'Enables automatic page reloading on rebuilds.')
..addFlag(hotReloadOption,
defaultsTo: false,
negatable: false,
help: 'Enables automatic page reloading on rebuilds.');
help: 'Enables automatic reloading of changed modules.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ... on rebuilds.

}

@override
Expand Down Expand Up @@ -90,7 +94,9 @@ class ServeCommand extends WatchCommand {
serveRequests(
server,
handler.handlerFor(target.dir,
logRequests: options.logRequests, hotReload: options.hotReload));
logRequests: options.logRequests,
liveReload: options.liveReload,
hotReload: options.hotReload));
});

_ensureBuildWebCompilersDependency(packageGraph, logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import 'reload_handler.dart';
import 'reloading_manager.dart';

final _assetsDigestPath = r'$assetDigests';
final _buildUpdatesProtocol = r'$hotreload';
final _buildUpdatesProtocol = r'$buildUpdates';

@anonymous
@JS()
Expand Down

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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var _buildUpdatesProtocol = '$buildUpdates';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, wrap this all in an anonymous function

(function() {
...
})()


var ws = new WebSocket('ws://' + location.host, [_buildUpdatesProtocol]);
ws.onmessage = function (event) {
location.reload();
};
58 changes: 35 additions & 23 deletions build_runner/lib/src/server/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import 'path_to_asset_id.dart';
const _performancePath = r'$perf';
final _graphPath = r'$graph';
final _assetsDigestPath = r'$assetDigests';
final _buildUpdatesProtocol = r'$hotreload';
final _buildUpdatesProtocol = r'$buildUpdates';
final entrypointExtensionMarker = '/* ENTRYPOINT_EXTENTION_MARKER */';

final _logger = Logger('Serve');
Expand Down Expand Up @@ -65,7 +65,9 @@ class ServeHandler implements BuildState {
@override
Stream<BuildResult> get buildResults => _state.buildResults;

shelf.Handler handlerFor(String rootDir, {bool logRequests, bool hotReload}) {
shelf.Handler handlerFor(String rootDir,
{bool logRequests, bool liveReload, bool hotReload}) {
liveReload ??= false;
hotReload ??= false;
logRequests ??= false;
if (p.url.split(rootDir).length != 1) {
Expand All @@ -74,7 +76,7 @@ class ServeHandler implements BuildState {
}
_state.currentBuild.then((_) => _warnForEmptyDirectory(rootDir));
var cascade = shelf.Cascade();
if (hotReload) {
if (liveReload || hotReload) {
cascade = cascade.add(_webSocketHandler.createHandlerByRootDir(rootDir));
}
cascade =
Expand All @@ -97,8 +99,10 @@ class ServeHandler implements BuildState {
if (logRequests) {
pipeline = pipeline.addMiddleware(_logRequests);
}
if (hotReload) {
pipeline = pipeline.addMiddleware(_injectBuildUpdatesClientCode);
if (liveReload) {
pipeline = pipeline.addMiddleware(_injectLiveReloadClientCode);
} else if (hotReload) {
pipeline = pipeline.addMiddleware(_injectHotReloadClientCode);
}
return pipeline.addHandler(cascade.handler);
}
Expand Down Expand Up @@ -210,27 +214,35 @@ class BuildUpdatesWebSocketHandler {
}
}

shelf.Handler _injectBuildUpdatesClientCode(shelf.Handler innerHandler) {
return (shelf.Request request) async {
if (!request.url.path.endsWith('.js')) {
return innerHandler(request);
}
var response = await innerHandler(request);
// TODO: Find a way how to check and/or modify body without reading it whole
var body = await response.readAsString();
if (body.startsWith(entrypointExtensionMarker)) {
body += _buildUpdatesInjectedJS;
}
return response.change(body: body);
};
}
shelf.Handler Function(shelf.Handler) _injectBuildUpdatesClientCode(
String scriptName) =>
(innerHandler) {
return (shelf.Request request) async {
if (!request.url.path.endsWith('.js')) {
return innerHandler(request);
}
var response = await innerHandler(request);
// TODO: Find a way how to check and/or modify body without reading it whole
var body = await response.readAsString();
if (body.startsWith(entrypointExtensionMarker)) {
body += _buildUpdatesInjectedJS(scriptName);
}
return response.change(body: body);
};
};

final _injectHotReloadClientCode =
_injectBuildUpdatesClientCode('hot_reload_client.dart');

final _injectLiveReloadClientCode =
_injectBuildUpdatesClientCode('live_reload_client');

/// Hot-reload config
/// Hot-/live- reload config
///
/// Listen WebSocket for updates in build results
final _buildUpdatesInjectedJS = '''\n
// Injected by build_runner for hot-reload support
window.\$dartLoader.forceLoadModule('packages/build_runner/src/server/hot_reload_client/client.dart')
String _buildUpdatesInjectedJS(String scriptName) => '''\n
// Injected by build_runner for build updates support
window.\$dartLoader.forceLoadModule('packages/build_runner/src/server/build_updates_client/$scriptName');
''';

class AssetHandler {
Expand Down
Loading