@@ -36,9 +36,11 @@ const config = {
36
36
styles_layout : false ,
37
37
styles_variables : false ,
38
38
styles_stories : false ,
39
+ styles_theme : false ,
39
40
js_drupal : false ,
40
41
js_storybook : false ,
41
- sdc : false ,
42
+ sdc_base : false ,
43
+ sdc_components : false ,
42
44
assets : false ,
43
45
constants : false ,
44
46
base : false ,
@@ -56,13 +58,13 @@ if (['cli', 'lintex'].indexOf(flags[0]) >= 0) {
56
58
config . build = buildWatchFlagCount === 0 || flags . indexOf ( 'build' ) >= 0
57
59
config . watch = flags . indexOf ( 'watch' ) >= 0
58
60
config . combine = true
59
- config . styles = true
60
61
config . styles_storybook = true
61
62
config . styles_editor = true
62
63
config . styles_variables = true
63
64
config . styles_stories = true
64
- config . js_drupal = true
65
- config . js_storybook = true
65
+ config . styles_theme = true
66
+ config . sdc_base = true
67
+ config . sdc_components = true
66
68
config . assets = true
67
69
config . constants = true
68
70
} else {
@@ -107,6 +109,7 @@ const STYLE_VARIABLE_FILE_OUT = `${DIR_OUT}/${STYLE_NAME}.variables.css`
107
109
const STYLE_ADMIN_FILE_OUT = `${ DIR_OUT } /${ STYLE_NAME } .admin.css`
108
110
const STYLE_LAYOUT_FILE_OUT = `${ DIR_OUT } /${ STYLE_NAME } .layout.css`
109
111
const STYLE_STORIES_FILE_OUT = `${ DIR_OUT } /${ STYLE_NAME } .stories.css`
112
+ const STYLE_THEME_FILE_OUT = `${ DIR_OUT } /${ STYLE_NAME } .theme.css`
110
113
111
114
const DIR_CT_ASSETS = `/themes/${ DRUPAL_THEME_FOLDER } /${ THEME_NAME } /dist/assets/`
112
115
const DIR_SB_ASSETS = `/assets/`
@@ -129,6 +132,12 @@ const CONSTANTS_BACKGROUND_UTIL = `${COMPONENT_DIR}/00-base/background/backgroun
129
132
const CONSTANTS_ICON_UTIL = `${ COMPONENT_DIR } /00-base/icon/icon.utils.js`
130
133
const CONSTANTS_LOGO_UTIL = `${ COMPONENT_DIR } /02-molecules/logo/logo.utils.js`
131
134
135
+ const SPLIT_CSS_HEADER = '/**\n * This file was automatically generated. Please run `npm run dist` to update.\n */\n\n' ;
136
+ const SPLIT_CSS_BASE_FILE = `${ DIR_OUT } /${ STYLE_NAME } .base.css` ;
137
+ const SPLIT_JS_BASE_FILE = `${ DIR_OUT } /${ SCRIPT_NAME } .drupal.base.js`
138
+ const SPLIT_JS_BASE_STORYBOOK_FILE = `${ DIR_OUT } /${ SCRIPT_NAME } .base.js`
139
+ const SPLIT_JS_BASE = `${ COMPONENT_DIR } /00-base/**/!(*.stories|*.test|*.data|*.stories.data|*.utils).js`
140
+
132
141
if ( config . build ) {
133
142
build ( )
134
143
}
@@ -336,17 +345,34 @@ async function build() {
336
345
buildStylesLayout ( )
337
346
buildStylesVariables ( )
338
347
buildStylesStories ( )
348
+ buildStylesTheme ( )
349
+ buildStylesSdcBase ( )
350
+ await buildStylesSdcComponents ( )
351
+ buildStylesSdcCopyBack ( )
339
352
buildJavascript ( )
353
+ buildJavascriptSdcBase ( )
340
354
buildAssetsDirectory ( )
341
355
await buildConstants ( )
342
- await splitCssBuild ( )
343
356
} catch ( error ) {
344
357
errorReporter ( error ) ;
345
358
}
346
359
347
360
console . log ( `Time taken: ${ time ( true ) } ` )
348
361
}
349
362
363
+ function buildStylesTheme ( ) {
364
+ if ( config . styles_theme ) {
365
+ const themecss = [
366
+ VAR_CT_ASSETS_DIRECTORY ,
367
+ getImportsFromGlob ( STYLE_THEME_FILE_IN , PATH ) ,
368
+ ] . join ( '\n' )
369
+
370
+ const compiled = sass . compileString ( themecss , { loadPaths : [ COMPONENT_DIR , PATH ] } )
371
+ fs . writeFileSync ( STYLE_THEME_FILE_OUT , sortCssLines ( compiled . css ) , 'utf-8' )
372
+ successReporter ( `Saved: Component styles (theme) ${ time ( ) } ` )
373
+ }
374
+ }
375
+
350
376
function watch ( ) {
351
377
console . log ( `Watching: ${ path . relative ( PATH , DIR_COMPONENTS_IN ) } /` )
352
378
const supportedExtensions = [ 'scss' , 'js' , 'twig' ]
@@ -385,35 +411,33 @@ function lintExclusions() {
385
411
} )
386
412
}
387
413
388
- async function splitCssBuild ( ) {
389
- if ( config . sdc ) {
390
- // ------------------------------------------------------------------------- SPLIT CSS
391
- // Output back into components directory.
392
- const SPLIT_CSS_HEADER = '/**\n * This file was automatically generated. Please run `npm run dist` to update.\n */\n\n' ;
393
-
394
- // Generate the base.css file.
414
+ function buildStylesSdcBase ( ) {
415
+ if ( config . sdc_base ) {
395
416
const baseCss = [
396
417
VAR_CT_ASSETS_DIRECTORY ,
397
418
`@import '00-base/variables';` ,
398
- getImportsFromGlob ( `00-base/**/!(*.stories|variables|_variables.*).scss` , DIR_COMPONENTS_IN ) ,
419
+ getImportsFromGlob ( `00-base/**/!(*.stories|variables|_variables.*).scss` , COMPONENT_DIR ) ,
399
420
] . join ( '\n' ) ;
400
- const baseResult = sass . compileString ( baseCss , { loadPaths : [ DIR_COMPONENTS_IN ] } ) ;
401
- const compiledImportAtTop = sortCssLines ( baseResult . css )
402
- fs . writeFileSync ( `${ DIR_OUT } /civictheme.base.css` , SPLIT_CSS_HEADER + compiledImportAtTop ) ;
403
- successReporter ( `Saved: Split styles (base) ${ time ( ) } ` )
421
+ const compiled = sass . compileString ( baseCss , { loadPaths : [ COMPONENT_DIR ] } ) ;
422
+ fs . writeFileSync ( SPLIT_CSS_BASE_FILE , SPLIT_CSS_HEADER + sortCssLines ( compiled . css ) ) ;
423
+ successReporter ( `Saved: SDC styles (base) ${ time ( ) } ` )
424
+ }
425
+ }
404
426
405
- // Generate the individual component css files.
427
+ async function buildStylesSdcComponents ( ) {
428
+ if ( config . sdc_components ) {
429
+ // TODO - we need to also output the style to the components out folder.
406
430
const fileList = [
407
- ...globSync ( `01-atoms/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
408
- ...globSync ( `02-molecules/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
409
- ...globSync ( `03-organisms/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
410
- ...globSync ( `04-templates/**/*.scss` , { cwd : DIR_COMPONENTS_IN } )
431
+ ...globSync ( `01-atoms/**/*.scss` , { cwd : COMPONENT_DIR } ) ,
432
+ ...globSync ( `02-molecules/**/*.scss` , { cwd : COMPONENT_DIR } ) ,
433
+ ...globSync ( `03-organisms/**/*.scss` , { cwd : COMPONENT_DIR } ) ,
434
+ ...globSync ( `04-templates/**/*.scss` , { cwd : COMPONENT_DIR } )
411
435
] ;
412
436
413
437
const componentDependencies = [
414
438
VAR_CT_ASSETS_DIRECTORY ,
415
439
`@import '00-base/variables';` ,
416
- getImportsFromGlob ( `00-base/mixins/**/*.scss` , DIR_COMPONENTS_IN )
440
+ getImportsFromGlob ( `00-base/mixins/**/*.scss` , COMPONENT_DIR )
417
441
] . join ( '\n' )
418
442
419
443
await Promise . all ( fileList . map ( async filePath => {
@@ -425,42 +449,61 @@ async function splitCssBuild() {
425
449
const styleData = `${ componentDependencies } \n@import '${ filePath } ';` ;
426
450
427
451
// Compile SCSS asynchronously.
428
- const result = await sass . compileStringAsync ( styleData , { loadPaths : [ DIR_COMPONENTS_IN ] } ) ;
452
+ const result = await sass . compileStringAsync ( styleData , { loadPaths : [ COMPONENT_DIR ] } ) ;
429
453
430
454
// Write to directory.
431
- fs . writeFileSync ( `${ DIR_COMPONENTS_IN } /${ styleDir } /${ styleName } .css` , SPLIT_CSS_HEADER + result . css ) ;
455
+ fs . writeFileSync ( `${ COMPONENT_DIR } /${ styleDir } /${ styleName } .css` , SPLIT_CSS_HEADER + result . css ) ;
432
456
} ) ) ;
433
- successReporter ( `Saved: Split styles (components) ${ time ( ) } ` )
457
+ successReporter ( `Saved: SDC styles (components) ${ time ( ) } ` )
458
+ }
459
+ }
460
+
461
+ function buildStylesSdcCopyBack ( ) {
462
+ if ( config . sdc_components && ! config . base ) {
463
+ // Copy back the css files to the components directory.
464
+ const originFileList = [
465
+ ...globSync ( `01-atoms/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
466
+ ...globSync ( `02-molecules/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
467
+ ...globSync ( `03-organisms/**/*.scss` , { cwd : DIR_COMPONENTS_IN } ) ,
468
+ ...globSync ( `04-templates/**/*.scss` , { cwd : DIR_COMPONENTS_IN } )
469
+ ] . map ( i => {
470
+ const cssFileName = `${ i . substring ( 0 , i . lastIndexOf ( '.' ) ) } .css`
471
+ return {
472
+ from : `${ COMPONENT_DIR } /${ cssFileName } ` ,
473
+ to : `${ DIR_COMPONENTS_IN } /${ cssFileName } `
474
+ }
475
+ } ) . forEach ( file => fs . copyFileSync ( file . from , file . to ) )
476
+ }
477
+ successReporter ( `Saved: Copied SDC files to components ${ time ( ) } ` )
478
+ }
434
479
435
- // ------------------------------------------------------------------------- SPLIT JS
436
- // Create Base JS
437
- // Load libraries and assets
480
+ function buildJavascriptSdcBase ( ) {
481
+ if ( config . sdc_base ) {
438
482
const libJs = [
439
483
...JS_LIB_IMPORTS ,
440
484
...globSync ( JS_ASSET_IMPORTS )
441
485
] . map ( filename => stripJS ( fs . readFileSync ( filename , 'utf-8' ) ) ) . join ( '\n' )
442
486
443
487
// Load base components after DOM is ready
444
- const SPLIT_JS_BASE = `${ DIR_COMPONENTS_IN } /00-base/**/!(*.stories|*.test|*.data|*.stories.data|*.utils).js`
445
488
const baseComponentJs = globSync ( SPLIT_JS_BASE ) . map ( filename => stripJS ( fs . readFileSync ( filename , 'utf-8' ) ) ) . join ( '\n' )
446
489
447
490
// Write drupal js base
448
491
const newDrupalBaseJs = [
449
492
JS_LINT_EXCLUSION_HEADER ,
450
493
libJs ,
451
- `Drupal.behaviors.civictheme_base = {attach: function (context, settings) {\n${ baseComponentJs } \n}};`
494
+ `Drupal.behaviors.${ THEME_NAME } = {attach: function (context, settings) {\n${ baseComponentJs } \n}};`
452
495
] . join ( '\n' )
453
- fs . writeFileSync ( ` ${ DIR_OUT } /civictheme.drupal.base.js` , newDrupalBaseJs , 'utf-8' )
454
- successReporter ( `Saved: Compiled split javascript base (drupal) ${ time ( ) } ` )
496
+ fs . writeFileSync ( SPLIT_JS_BASE_FILE , newDrupalBaseJs , 'utf-8' )
497
+ successReporter ( `Saved: SDC javascript base (drupal) ${ time ( ) } ` )
455
498
456
499
// Write vanilla js base
457
500
const newBaseJs = [
458
501
JS_LINT_EXCLUSION_HEADER ,
459
502
libJs ,
460
503
`document.addEventListener('DOMContentLoaded', () => {\n${ baseComponentJs } \n});`
461
504
] . join ( '\n' )
462
- fs . writeFileSync ( ` ${ DIR_OUT } /civictheme.base.js` , newBaseJs , 'utf-8' )
463
- successReporter ( `Saved: Compiled split javascript base (storybook) ${ time ( ) } ` )
505
+ fs . writeFileSync ( SPLIT_JS_BASE_STORYBOOK_FILE , newBaseJs , 'utf-8' )
506
+ successReporter ( `Saved: SDC javascript base (storybook) ${ time ( ) } ` )
464
507
}
465
508
}
466
509
0 commit comments