Provide the functionality to auto-reload the website on build

This commit is contained in:
Manuel Thalmann 2019-10-07 20:08:35 +00:00
parent 2b44c6ac22
commit 6b8c085976
5 changed files with 1347 additions and 8 deletions

View file

@ -2,7 +2,7 @@ MYSQL_DATABASE=SilverStripe
MYSQL_ROOT_PASSWORD=root MYSQL_ROOT_PASSWORD=root
SS_ENVIRONMENT_TYPE=dev SS_ENVIRONMENT_TYPE=dev
SS_BASE_URL=http://localhost:8080 SS_BASE_URL=http://localhost
SS_DATABASE_CLASS=MySQLPDODatabase SS_DATABASE_CLASS=MySQLPDODatabase
SS_DATABASE_SERVER=db SS_DATABASE_SERVER=db

View file

@ -6,6 +6,8 @@ services:
env_file: devcontainer.env env_file: devcontainer.env
ports: ports:
- 8080:80 - 8080:80
- 3000:3000
- 3001:3001
volumes: volumes:
- ..:/vscode/src/mantra - ..:/vscode/src/mantra
- ../test/website:/var/www/html - ../test/website:/var/www/html

View file

@ -1,3 +1,4 @@
import browserSync = require("browser-sync");
import browserify = require("browserify"); import browserify = require("browserify");
import log = require("fancy-log"); import log = require("fancy-log");
import FileSystem = require("fs-extra"); import FileSystem = require("fs-extra");
@ -24,6 +25,11 @@ import "./.gulp/TaskFunction";
*/ */
const watchConnectorPort = 50958; const watchConnectorPort = 50958;
/**
* An object for syncing browsers.
*/
let syncer = browserSync.create();
/** /**
* The message that is printed when starting the compilation in watch mode. * The message that is printed when starting the compilation in watch mode.
*/ */
@ -129,6 +135,7 @@ export let Watch: TaskFunction = (done) =>
if (options["target"] === settings.Target) if (options["target"] === settings.Target)
{ {
syncer.exit();
server.close(); server.close();
done(); done();
process.exit(); process.exit();
@ -142,6 +149,28 @@ export let Watch: TaskFunction = (done) =>
Watch.description = "Builds the project in watched mode."; Watch.description = "Builds the project in watched mode.";
/**
* Reloads all browsers using `browser-sync`.
*/
function BrowserSync(filePath?: string): TaskFunction
{
let BrowserSync: TaskFunction = (done) =>
{
if (filePath)
{
syncer.reload(filePath);
}
else
{
syncer.reload();
}
done();
};
return BrowserSync;
}
/** /**
* Builds the project. * Builds the project.
*/ */
@ -150,8 +179,17 @@ export async function Build()
if (settings.Watch) if (settings.Watch)
{ {
log.info(watchStartMessage); log.info(watchStartMessage);
gulp.watch(settings.ThemeSource("**"), Theme);
gulp.watch(settings.TemplateSource("**"), Templates); syncer.init({
open: false,
proxy: "http://localhost",
port: 3000,
ghostMode: false,
online: false
});
gulp.watch(settings.ThemeSource("**"), gulp.series(Theme, BrowserSync("*.css")));
gulp.watch(settings.TemplateSource("**"), gulp.series(Templates, BrowserSync()));
} }
await Promise.all( await Promise.all(
@ -243,6 +281,11 @@ export async function Library()
{ {
if (settings.Watch && ((queue.getQueueLength() + queue.getPendingLength()) === 1)) if (settings.Watch && ((queue.getQueueLength() + queue.getPendingLength()) === 1))
{ {
if (errorMessages.length === 0)
{
syncer.reload("*.js");
}
log.info(watchFinishMessage(errorMessages.length)); log.info(watchFinishMessage(errorMessages.length));
} }
@ -310,11 +353,11 @@ export async function Theme()
gulp.dest( gulp.dest(
settings.StylePath(), settings.StylePath(),
settings.Debug ? settings.Debug ?
{ {
sourcemaps: true sourcemaps: true
} }
: :
undefined) undefined)
); );
} }

1292
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@
"@manuth/tsconfig": "^1.2.2", "@manuth/tsconfig": "^1.2.2",
"@manuth/tslint-presets": "^1.0.4", "@manuth/tslint-presets": "^1.0.4",
"@types/bootstrap": "^4.3.1", "@types/bootstrap": "^4.3.1",
"@types/browser-sync": "^2.26.1",
"@types/browserify": "^12.0.36", "@types/browserify": "^12.0.36",
"@types/fancy-log": "^1.3.1", "@types/fancy-log": "^1.3.1",
"@types/fs-extra": "^8.0.0", "@types/fs-extra": "^8.0.0",
@ -25,6 +26,7 @@
"@types/vinyl-source-stream": "0.0.30", "@types/vinyl-source-stream": "0.0.30",
"@types/watchify": "^3.7.4", "@types/watchify": "^3.7.4",
"bootstrap": "^4.3.1", "bootstrap": "^4.3.1",
"browser-sync": "^2.26.7",
"browserify": "^16.5.0", "browserify": "^16.5.0",
"create-symlink": "^1.0.0", "create-symlink": "^1.0.0",
"fancy-log": "^1.3.3", "fancy-log": "^1.3.3",