Skip to content

Commit b8e3cef

Browse files
committed
Add GCDS loader for co-existance tested version
1 parent 7d1e6b8 commit b8e3cef

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Gruntfile.coffee

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ module.exports = (grunt) ->
220220
"copy:fonts"
221221
"copy:wetboew"
222222
"copy:depsJS_custom"
223+
"copy:gcdsLoader"
224+
"usebanner:gcdsLoader"
223225
]
224226
)
225227

@@ -583,6 +585,7 @@ module.exports = (grunt) ->
583585
"!{sites,common,components,templates,design-patterns,wet-boew}/**/assets"
584586
"!{sites,common,components,templates,design-patterns,wet-boew}/**/demo"
585587
"!{sites,common,components,templates,design-patterns,wet-boew}/**/demos"
588+
"!sites/gcdsloader.js"
586589
]
587590
dest: "<%= themeDist %>/js/theme.js"
588591
common:
@@ -666,6 +669,22 @@ module.exports = (grunt) ->
666669
"<%= themeDist %>/css/*.*",
667670
"<%= themeDist %>/méli-mélo/*.css"
668671
]
672+
673+
gcdsLoader:
674+
options:
675+
position: "replace"
676+
props:
677+
ver: "<%= pkg.peerDependencies[ '@cdssnc/gcds-components' ] %>"
678+
srijs: "<%= pkg[ 'io.github.wet-boew' ].gcdsSriJs %>"
679+
sricss: "<%= pkg[ 'io.github.wet-boew' ].gcdsSriCss %>"
680+
replace: (fileContents, newBanner, insertPositionMarker, src, options) ->
681+
# Replace GCDS version and SRIs in the source file
682+
return fileContents
683+
.replace( "@gcdsComponentVersion@", options.props.ver )
684+
.replace( "@gcdsSriJs@", options.props.srijs )
685+
.replace( "@gcdsSriCss@", options.props.sricss )
686+
src: "<%= themeDist %>/js/gcdsloader.js"
687+
669688
#
670689
# Use the name in the package.json as packageName in the theme
671690
# used to build the URL and to ease the reuse of this build script for derivative themes
@@ -839,6 +858,11 @@ module.exports = (grunt) ->
839858
cwd: "<%= themeDist %>/deps-js"
840859
src: "**/*.*"
841860
dest: "dist/wet-boew/js/deps"
861+
gcdsLoader:
862+
expand: true
863+
flatten: true
864+
src: "sites/gcdsloader.js"
865+
dest: "<%= themeDist %>/js"
842866

843867
wetboew_demos:
844868
expand: true

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
"bootstrap-sass": "3.4.3",
1616
"wet-boew": "github:wet-boew/wet-boew#v4.0.88.1"
1717
},
18+
"peerDependencies": {
19+
"@cdssnc/gcds-components": "0.36.0"
20+
},
21+
"io.github.wet-boew": {
22+
"gcdsSriJs": "sha384-dyE8IpLmFMtdU/ftC4nFeuLMvr2vMI8s1UY2OXl9pbzizebJeCfWp9iLhiVXdXtA",
23+
"gcdsSriCss": "sha384-0CRuUE9X0B5RfCdDx1BDT6Ru1MajB7Ngy0CmoORlBF+ey/W8T75AKwLehkl+QaGR"
24+
},
25+
"peerDependenciesMeta": {
26+
"@cdssnc/gcds-components": {
27+
"optional": true
28+
}
29+
},
1830
"browserslist": [
1931
"last 2 versions",
2032
"Firefox ESR",

sites/gcdsloader.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @title GCDS loader
3+
* @overview Quick script to load latest version of GCDS verison that tested by GCWeb
4+
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
5+
* @author @duboisp
6+
*/
7+
( function( wnd, doc, create, append, escape ) {
8+
"use strict";
9+
10+
11+
//
12+
// Configuration updated during the build process with info in package.json
13+
//
14+
const gcdsVersion = "@gcdsComponentVersion@", // pkg.peerDependencies[ "@cdssnc/gcds-components" ]
15+
gcdsSriJs = "@gcdsSriJs@", // pkg[ "io.github.wet-boew" ].gcdsSriJs
16+
gcdsSriCss = "@gcdsSriCss@"; // pkg[ "io.github.wet-boew" ].gcdsSriCss
17+
18+
19+
//
20+
// General configuration used to detect and load GCSD
21+
//
22+
const gcdsBasePath = "https://cdn.design-system.alpha.canada.ca/@cdssnc/gcds-components@",
23+
gcdsSuffixJs = "/dist/gcds/gcds.esm.js",
24+
gcdsSuffixCss = "/dist/gcds/gcds.css",
25+
txtScript = "script",
26+
txtLink = "link",
27+
blockingState = "render";
28+
29+
30+
//
31+
// Check if GCDS lib are already defined in the header
32+
//
33+
let traceFound = doc.querySelector( txtLink + "[href$=" + escape( gcdsSuffixCss ) + "]," + txtScript + "[src$=" + escape( gcdsSuffixJs ) + "]" );
34+
35+
36+
//
37+
// Import the GCDS libs
38+
//
39+
if ( !traceFound && gcdsSriJs && gcdsSriCss ) {
40+
let scrGcds = create( txtScript );
41+
scrGcds.blocking = blockingState;
42+
scrGcds.type = "module";
43+
scrGcds.integrity = gcdsSriJs;
44+
scrGcds.src = gcdsBasePath + gcdsVersion + gcdsSuffixJs;
45+
append( scrGcds );
46+
47+
let lnkGcds = create( txtLink );
48+
lnkGcds.blocking = blockingState;
49+
lnkGcds.rel = "stylesheet";
50+
lnkGcds.crossOrigin = "anonymous";
51+
lnkGcds.integrity = gcdsSriCss;
52+
lnkGcds.href = gcdsBasePath + gcdsVersion + gcdsSuffixCss;
53+
append( lnkGcds );
54+
55+
56+
//
57+
// Set GCDS version globally once loaded
58+
//
59+
wnd.addEventListener( "appload", ( event ) => {
60+
if ( event.detail.namespace === "gcds" ) {
61+
wnd.wbgcds = gcdsVersion;
62+
}
63+
} );
64+
}
65+
66+
} )( window, document, document.createElement, document.head.appendChild, CSS.escape );

0 commit comments

Comments
 (0)