Automatically refresh page when Dart source files change

How can you get Dartium to automatically restart your web client application whenever changes to the source files occur?

Related: How do I automatically update Firefox when changing a file?

+5
source share
2 answers

EDIT: You can also skip all this and just press CTRL + R in the editor. The script below may still be useful if you use tools outside the Dart editor (but still rely on it for the build process) or want to view the code and the preview without switching focus to the Dartium window.

!

dart.build "" HTML , , LivePage, .

  • Dartium LivePage. ( | | | LivePage www.fullondesign.co.uk | )

  • . Dartium LivePage. "Live". , LivePage html (, css) .

  • , html . Dartium .

  • build.dart , pubspec.yaml. Dart , (, .dart).

  • build.dart. 'web/yourpage.html', HTML, LivePage.

  • .dart, , .

: ▶ Dart build.dart ▶ html ▶ LivePage Dartium

import 'dart:io';

// This number needs to be high enough to prevent the Dart Editor from going
// into an "infinite compile" loop.  If that happens, simply comment out the
// call to touch() below and save this file.
const int MIN_INTERVAL_MS = 5000;
const String HTML_FILE = 'web/yourpage.html';

void main() { 
  build(new Options().arguments, [HTML_FILE]);
  touch(HTML_FILE, new Duration(milliseconds:MIN_INTERVAL_MS));
}

/// Save a small, trivial change to the contents of [filename], unless
/// its already been modified within the last [interval].
void touch(String filename, [Duration interval]) {
  const int SPACE = 32;
  var file = new File(filename);
  if (?interval &&
      new Date.now()
      .difference(file.lastModifiedSync())
      .inMilliseconds < interval.inMilliseconds) return;
  RandomAccessFile f = file.openSync(FileMode.APPEND);
  try {
    // If the file doesn't end with a space, append one, otherwise remove it    
    int length = f.lengthSync();
    f.setPositionSync(length - 1);
    if (f.readByteSync() == SPACE) {
      f.truncateSync(length - 1);
    } else {
      f.writeByteSync(SPACE);
    }
  } finally {
    f.closeSync();
  }
}

, dart build.dart .

touch() . . LivePage , , , - .

Dart , , build.dart, ", " build.dart... ... , , script , , , MIN_INTERVAL_MS.

LivePage , " " CSS Javascript , . , (.. html ) .

web_ui, , , web_ui build.dart.

+6

kragerer . script Dart 1.0. , ( ) .

build.dart

import 'dart:io';

// This number needs to be high enough to prevent the Dart Editor from going
// into an "infinite compile" loop.  If that happens, simply comment out the
// call to touch() below and save this file.
const int MIN_INTERVAL_MS = 5000;
const String HTML_FILE = 'web/index.html';

void main() {
  touch(HTML_FILE, new Duration(milliseconds:MIN_INTERVAL_MS));
}

/// Save a small, trivial change to the contents of [filename], unless
/// its already been modified within the last [interval].
void touch(String filename, [Duration interval]) {
  const int SPACE = 32;
  var file = new File(filename);
  if (interval != null && new DateTime.now().difference(file.lastModifiedSync()).inMilliseconds < interval.inMilliseconds) return;
  RandomAccessFile f = file.openSync(mode:FileMode.APPEND);
  try {
    // If the file doesn't end with a space, append one, otherwise remove it
    int length = f.lengthSync();
    f.setPositionSync(length - 1);
    if (f.readByteSync() == SPACE) {
      f.truncateSync(length - 1);
    } else {
      f.writeByteSync(SPACE);
    }
  } finally {
    f.closeSync();
  }
}
+2

All Articles