11'use strict' ;
2- const bs = require ( 'browser-sync' ) . create ( 'PatternLab ') ;
3- const buildPatterns = require ( './build ' ) ;
2+ const chokidar = require ( 'chokidar ' ) ;
3+ const liveServer = require ( 'live-server ' ) ;
44const patternlab = require ( 'patternlab-node' ) ;
5- const htmlInjector = require ( 'bs-html-injector' ) ;
65const path = require ( 'path' ) ;
76const _ = require ( 'lodash' ) ;
7+
8+ const buildPatterns = require ( './build' ) ;
89const isValidConfig = require ( './validate-config' ) ;
910const copyWithPattern = require ( './utils' ) . copyWithPattern ;
1011const wrapAsync = require ( './utils' ) . wrapAsync ;
@@ -18,14 +19,14 @@ const error = require('./utils').error;
1819 */
1920function serve ( config , watch ) {
2021 if ( ! isValidConfig ) throw new TypeError ( 'serve: Expects config not to be empty and of type object.' ) ;
21-
22+
2223 if ( ! _ . has ( config , 'paths.public.root' ) || _ . isEmpty ( config . paths . public . root ) ) {
2324 throw new TypeError ( 'serve: config.paths.public.root is empty or does not exist. Please check your PatternLab config.' ) ;
2425 }
2526 if ( ! _ . has ( config , 'paths.source.root' ) || _ . isEmpty ( config . paths . source . root ) ) {
2627 throw new TypeError ( 'serve: config.paths.source.root is empty or does not exist. Please check your PatternLab config.' ) ;
2728 }
28-
29+
2930 try {
3031 const pl = patternlab ( ) ;
3132 const src = config . paths . source ;
@@ -34,71 +35,48 @@ function serve(config, watch) {
3435 const sourceStyleguide = path . join ( path . resolve ( src . styleguide ) , '/**/*.*' ) ;
3536 const patterns = pl . getSupportedTemplateExtensions ( ) . map ( dotExtension => path . join ( path . resolve ( src . patterns ) , `/**/*${ dotExtension } ` ) ) ;
3637
37- // The browser-sync config
38- const bsConfig = {
39- server : publicDir ,
40- snippetOptions : {
41- blacklist : [ '/index.html' , '/' , '/?*' ] // Ignore all HTML files within the templates folder
42- } ,
43- notify : {
44- styles : [
45- 'display: none' ,
46- 'padding: 15px' ,
47- 'font-family: sans-serif' ,
48- 'position: fixed' ,
49- 'font-size: 1em' ,
50- 'z-index: 9999' ,
51- 'bottom: 0px' ,
52- 'right: 0px' ,
53- 'border-top-left-radius: 5px' ,
54- 'background-color: #1B2032' ,
55- 'opacity: 0.4' ,
56- 'margin: 0' ,
57- 'color: white' ,
58- 'text-align: center'
59- ]
60- }
38+ // The liveserver config
39+ const liveServerConf = {
40+ root : publicDir ,
41+ open : true ,
42+ ignore : path . join ( publicDir ) ,
43+ file : 'index.html'
6144 } ;
62-
45+
6346 /**
6447 * @func copyAndReloadCSS
6548 */
6649 const copyAndReloadCSS = ( ) => wrapAsync ( function * ( ) {
6750 yield copyWithPattern ( path . resolve ( src . css ) , '**/*.css' , path . resolve ( config . paths . public . css ) ) ;
68- bs . reload ( '*.css' ) ;
51+ liveServer . refreshCSS ( ) ;
6952 } ) ;
70-
53+
7154 /**
7255 * @func copyAndReloadStyleguide
7356 */
7457 const copyAndReloadStyleguide = ( ) => wrapAsync ( function * ( ) {
7558 yield copyWithPattern ( path . resolve ( src . styleguide ) , '**/!(*.css)' , path . resolve ( config . paths . public . styleguide ) ) ;
7659 yield copyWithPattern ( path . resolve ( src . styleguide ) , '**/*.css' , path . resolve ( config . paths . public . styleguide ) ) ;
77- bs . reload ( '*.css' ) ;
60+ liveServer . refreshCSS ( ) ;
7861 } ) ;
79-
62+
8063 /**
8164 * @func reload
8265 * @desc Calls browser-sync's reload method to tell browsers to refresh their page
8366 */
8467 const buildAndReload = function ( ) {
8568 buildPatterns ( config ) ;
86- bs . reload ( ) ;
69+ liveServer . reload ( ) ;
8770 } ;
8871
89- // Register plugins
90- bs . use ( htmlInjector , {
91- files : [ publicDir + '/index.html' , publicDir + '../styleguide/styleguide.html' ]
92- } ) ;
93-
9472 if ( watch ) {
9573 /**
9674 * 1. Watch source css, then copy css and callreloadCSS
9775 * 2. Watch source styleguide, then copy styleguide and css and call reloadCSS
9876 * 3. Watch pattern-specific and engine-specific extensions, run build and reload
9977 */
100- bs . watch ( sourceCSS ) . on ( 'change' , copyAndReloadCSS ) ; // 1
101- bs . watch ( sourceStyleguide ) . on ( 'change' , copyAndReloadStyleguide ) ; // 2
78+ chokidar . watch ( sourceCSS ) . on ( 'change' , copyAndReloadCSS ) ; // 1
79+ chokidar . watch ( sourceStyleguide ) . on ( 'change' , copyAndReloadStyleguide ) ; // 2
10280 const patternWatches = [
10381 path . join ( path . resolve ( src . patterns ) , '**/*.json' ) ,
10482 path . join ( path . resolve ( src . patterns ) , '**/*.md' ) ,
@@ -109,11 +87,10 @@ function serve(config, watch) {
10987 path . join ( path . resolve ( src . annotations ) , '*' )
11088 ] . concat ( patterns ) ; // 3
11189
112- bs . watch ( patternWatches ) . on ( 'change' , buildAndReload ) ;
90+ chokidar . watch ( patternWatches ) . on ( 'change' , buildAndReload ) ;
11391 }
114-
11592 // Init browser-sync
116- bs . init ( bsConfig ) ;
93+ liveServer . start ( liveServerConf ) ;
11794 } catch ( err ) {
11895 error ( err ) ;
11996 }
0 commit comments