Skip to content

build: switch to browser-sync #14295

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 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@google-cloud/storage": "^1.1.1",
"@octokit/rest": "^15.9.4",
"@schematics/angular": "^7.0.6",
"@types/browser-sync": "^0.0.42",
"@types/chalk": "^0.4.31",
"@types/fs-extra": "^4.0.3",
"@types/glob": "^5.0.33",
Expand All @@ -76,6 +77,7 @@
"@types/run-sequence": "^0.0.29",
"autoprefixer": "^6.7.6",
"axe-webdriverjs": "^1.1.1",
"browser-sync": "^2.26.3",
"chalk": "^1.1.3",
"codelyzer": "^4.5.0",
"conventional-changelog": "^3.0.5",
Expand All @@ -90,7 +92,6 @@
"gulp-clean": "^0.3.2",
"gulp-clean-css": "^3.3.1",
"gulp-cli": "^2.0.1",
"gulp-connect": "^5.0.0",
"gulp-dom": "^0.9.17",
"gulp-flatten": "^0.3.1",
"gulp-highlight-files": "^0.0.5",
Expand Down
25 changes: 13 additions & 12 deletions tools/gulp/tasks/development.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
copyFiles,
inlineResourcesForDirectory,
sequenceTask,
watchFiles,
} from 'material2-build-tools';
import {
cdkPackage,
Expand All @@ -17,6 +16,7 @@ import {
momentAdapterPackage,
examplesPackage,
} from '../packages';
import {watchFilesAndReload} from '../util/watch-files-reload';

// These imports don't have any typings provided.
const firebaseTools = require('firebase-tools');
Expand Down Expand Up @@ -56,7 +56,7 @@ task(':build:devapp:assets', copyTask(assetsGlob, outDir));
task(':build:devapp:scss', () => buildScssPipeline(appDir).pipe(dest(outDir)));
task(':build:devapp:inline-resources', () => inlineResourcesForDirectory(outDir));

task(':serve:devapp', serverTask(outDir, true));
task(':serve:devapp', serverTask(outDir));

task('build:devapp', sequenceTask(
'cdk:build-no-bundles',
Expand Down Expand Up @@ -123,40 +123,41 @@ task('deploy:devapp', ['stage-deploy:devapp'], () => {
*/

task(':watch:devapp', () => {
watchFiles(join(appDir, '**/*.ts'), [':build:devapp:ts']);
watchFiles(join(appDir, '**/*.scss'), [':watch:devapp:rebuild-scss']);
watchFiles(join(appDir, '**/*.html'), [':watch:devapp:rebuild-html']);
watchFilesAndReload(join(appDir, '**/*.ts'), [':build:devapp:ts']);
watchFilesAndReload(join(appDir, '**/*.scss'), [':watch:devapp:rebuild-scss']);
watchFilesAndReload(join(appDir, '**/*.html'), [':watch:devapp:rebuild-html']);

// Custom watchers for all packages that are used inside of the dev-app. This is necessary
// because we only want to build the changed package (using the build-no-bundles task).

// CDK package watchers.
watchFiles(join(cdkPackage.sourceDir, '**/*'), ['cdk:build-no-bundles']);
watchFilesAndReload(join(cdkPackage.sourceDir, '**/*'), ['cdk:build-no-bundles']);

const materialCoreThemingGlob = join(materialPackage.sourceDir, '**/core/theming/**/*.scss');

// Material package watchers.
watchFiles([
watchFilesAndReload([
join(materialPackage.sourceDir, '**/!(*-theme.scss)'), `!${materialCoreThemingGlob}`
], ['material:build-no-bundles']);
watchFiles([
watchFilesAndReload([
join(materialPackage.sourceDir, '**/*-theme.scss'), materialCoreThemingGlob
], [':build:devapp:scss']);

// Moment adapter package watchers
watchFiles(join(momentAdapterPackage.sourceDir, '**/*'),
watchFilesAndReload(join(momentAdapterPackage.sourceDir, '**/*'),
['material-moment-adapter:build-no-bundles']);

// Material experimental package watchers
watchFiles(join(materialExperimentalPackage.sourceDir, '**/*'),
watchFilesAndReload(join(materialExperimentalPackage.sourceDir, '**/*'),
['material-experimental:build-no-bundles']);

// CDK experimental package watchers
watchFiles(join(cdkExperimentalPackage.sourceDir, '**/*'),
watchFilesAndReload(join(cdkExperimentalPackage.sourceDir, '**/*'),
['cdk-experimental:build-no-bundles']);

// Example package watchers.
watchFiles(join(examplesPackage.sourceDir, '**/*'), ['material-examples:build-no-bundles']);
watchFilesAndReload(join(examplesPackage.sourceDir, '**/*'),
['material-examples:build-no-bundles']);
});

// Note that we need to rebuild the TS here, because the resource inlining
Expand Down
25 changes: 14 additions & 11 deletions tools/gulp/tasks/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {task} from 'gulp';
import {buildConfig, sequenceTask, triggerLivereload, watchFiles} from 'material2-build-tools';
import {buildConfig, sequenceTask, watchFiles} from 'material2-build-tools';
import {join} from 'path';
import {copyTask, execNodeTask, ngcBuildTask, serverTask} from '../util/task_helpers';

// There are no type definitions available for these imports.
const gulpConnect = require('gulp-connect');
import {
copyTask,
execNodeTask,
getActiveBrowserSyncInstance,
ngcBuildTask,
serverTask
} from '../util/task_helpers';

const {outputDir, packagesDir, projectDir} = buildConfig;

Expand Down Expand Up @@ -39,7 +42,7 @@ task('e2e:watch', sequenceTask(
/** Watches the e2e app and tests for changes and triggers a test rerun on change. */
task(':e2e:watch', () => {
watchFiles([join(appDir, '**/*.+(html|ts|css)'), join(e2eTestDir, '**/*.+(html|ts)')],
[':e2e:rerun'], false);
[':e2e:rerun']);
});

/** Updates the e2e app and runs the protractor tests. */
Expand All @@ -52,7 +55,7 @@ task(':e2e:rerun', sequenceTask(

/** Triggers a reload of the e2e app. */
task(':e2e:reload', () => {
return triggerLivereload();
return getActiveBrowserSyncInstance().reload();
});

/** Task that builds the e2e-app in AOT mode. */
Expand All @@ -76,8 +79,8 @@ task('e2e-app:copy-index-html', copyTask(join(appDir, 'index.html'), outDir));
task('e2e-app:build-ts', ngcBuildTask(tsconfigPath));

task(':watch:e2eapp', () => {
watchFiles(join(appDir, '**/*.ts'), ['e2e-app:build'], false);
watchFiles(join(appDir, '**/*.html'), ['e2e-app:copy-assets'], false);
watchFiles(join(appDir, '**/*.ts'), ['e2e-app:build']);
watchFiles(join(appDir, '**/*.html'), ['e2e-app:copy-assets']);
});

/** Ensures that protractor and webdriver are set up to run. */
Expand All @@ -90,7 +93,7 @@ task(':test:protractor:setup', execNodeTask(
task(':test:protractor', execNodeTask('protractor', [PROTRACTOR_CONFIG_PATH]));

/** Starts up the e2e app server and rewrites the HTTP requests to properly work with AOT. */
task(':serve:e2eapp', serverTask(outDir, false, [
task(':serve:e2eapp', serverTask(outDir, [
// Rewrite each request for .ngfactory files which are outside of the e2e-app to the **actual**
// path. This is necessary because NGC cannot generate factory files for the node modules
// and release output. If we work around it by adding multiple root directories, the directory
Expand All @@ -108,7 +111,7 @@ task(':serve:e2eapp', serverTask(outDir, false, [
]));

/** Terminates the e2e app server */
task(':serve:e2eapp:stop', gulpConnect.serverClose);
task(':serve:e2eapp:stop', () => getActiveBrowserSyncInstance().exit());

/** Builds and serves the e2e app. */
task('serve:e2eapp', sequenceTask('e2e-app:build', ':serve:e2eapp'));
Expand Down
37 changes: 24 additions & 13 deletions tools/gulp/util/task_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import {BrowserSyncInstance, create as createBrowserSyncInstance} from 'browser-sync';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as gulp from 'gulp';
import * as path from 'path';
import {buildConfig} from 'material2-build-tools';
import * as path from 'path';

// Those imports lack typings.
// This import lacks type definitions.
const gulpClean = require('gulp-clean');
const gulpConnect = require('gulp-connect');

// There are no type definitions available for these imports.
const resolveBin = require('resolve-bin');
const httpRewrite = require('http-rewrite-middleware');

const {projectDir} = buildConfig;

/** Currently active browsersync instance. */
let activeBrowserSyncInstance: BrowserSyncInstance;

/** If the string passed in is a glob, returns it, otherwise append '**\/*' to it. */
function _globify(maybeGlob: string, suffix = '**/*') {
if (maybeGlob.indexOf('*') > -1) {
Expand Down Expand Up @@ -125,9 +128,7 @@ export function cleanTask(glob: string) {
* Create a task that serves a given directory in the project.
* The server rewrites all node_module/ or dist/ requests to the correct directory.
*/
export function serverTask(packagePath: string, livereload = true,
rewrites?: {from: string, to: string}[]) {

export function serverTask(packagePath: string, rewrites?: {from: string, to: string}[]) {
// The http-rewrite-middleware only supports relative paths as rewrite destinations.
const relativePath = path.relative(projectDir, packagePath);
const defaultHttpRewrites = [
Expand All @@ -143,14 +144,24 @@ export function serverTask(packagePath: string, livereload = true,
];

return () => {
gulpConnect.server({
root: projectDir,
livereload: livereload,
if (activeBrowserSyncInstance) {
throw new Error('Cannot setup BrowserSync because there is already an instance running.');
}

activeBrowserSyncInstance = createBrowserSyncInstance();
activeBrowserSyncInstance.init({
server: projectDir,
port: 4200,
host: '0.0.0.0',
Copy link
Member

Choose a reason for hiding this comment

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

can we add host: '0.0.0.0' back in?

Copy link
Member Author

Choose a reason for hiding this comment

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

Browserstack is actually smarter about this and does this automatically. It also prints out your external host address in the terminal.

middleware: () => {
return [httpRewrite.getMiddleware(rewrites || defaultHttpRewrites)];
}
middleware: httpRewrite.getMiddleware(rewrites || defaultHttpRewrites),
});
};
}

/** Gets the currently active browsersync instance */
export function getActiveBrowserSyncInstance(): BrowserSyncInstance {
if (!activeBrowserSyncInstance) {
throw new Error('Cannot return Browsersync instance because there is no instance running.');
}

return activeBrowserSyncInstance;
}
10 changes: 10 additions & 0 deletions tools/gulp/util/watch-files-reload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {watchFiles} from 'material2-build-tools';
import {getActiveBrowserSyncInstance} from './task_helpers';

/**
* Function that watches a set of file globs and runs the specified tasks if a file
* changed. Additionally BrowserSync will reload all browsers on file change.
*/
export function watchFilesAndReload(fileGlob: string | string[], tasks: string[]) {
watchFiles(fileGlob, [...tasks, () => getActiveBrowserSyncInstance().reload()]);
}
11 changes: 0 additions & 11 deletions tools/package-tools/gulp/trigger-livereload.ts

This file was deleted.

14 changes: 5 additions & 9 deletions tools/package-tools/gulp/watch-files.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import {watch} from 'gulp';
import {triggerLivereload} from './trigger-livereload';
import {watch, WatchCallback} from 'gulp';

/**
* Function that watches a set of file globs and runs given Gulp tasks if a given file changes.
* By default the livereload server will be also called on file change.
*/
export function watchFiles(fileGlob: string | string[], tasks: string[], livereload = true,
debounceDelay = 700) {
watch(fileGlob, {debounceDelay}, [...tasks, () => livereload && triggerLivereload()]);
/** Function that watches a set of file globs and runs given Gulp tasks if a given file changes. */
export function watchFiles(fileGlob: string | string[], tasks: (string|WatchCallback)[],
debounceDelay = 700) {
watch(fileGlob, {debounceDelay}, tasks);
}
1 change: 0 additions & 1 deletion tools/package-tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export * from './inline-resources';
export * from './gulp/build-tasks-gulp';
export * from './gulp/build-scss-pipeline';
export * from './gulp/sequence-task';
export * from './gulp/trigger-livereload';
export * from './gulp/watch-files';
Loading