|
| 1 | +# Branding |
| 2 | + |
| 3 | +The UI supports static branding at build time. Dynamically switching brands is not |
| 4 | +possible with the current implementation. |
| 5 | + |
| 6 | +## Summary |
| 7 | + |
| 8 | +Each of the project modules need to do some branding enablement. |
| 9 | + |
| 10 | +- `@konveyor-ui/common` pulls in the branding assets and packages the configuration, |
| 11 | + strings and assets within the common package. The other modules pull branding |
| 12 | + from the common module. |
| 13 | + |
| 14 | +- `@konveyor-ui/client` uses branding from the common package: |
| 15 | + |
| 16 | + - The location of `favicon.ico`, `manifest.json` and any other branding |
| 17 | + assets that may be referenced in the `brandingStrings` are sourced from the |
| 18 | + common package. |
| 19 | + |
| 20 | + - The `brandingStrings` are used by the dev-server runtime, to fill out the |
| 21 | + `index.html` template. |
| 22 | + |
| 23 | + - The about modal and application masthead components use the branding strings |
| 24 | + provided by the common module to display brand appropriate logos, titles and |
| 25 | + about information. Since the common module provides all the information, it |
| 26 | + is packaged directly into the app at build time. |
| 27 | + |
| 28 | +- `@konveyor-ui/server` uses the `brandingStrings` from the common package to fill |
| 29 | + out the `index.html` template. |
| 30 | + |
| 31 | +## Providing alternate branding |
| 32 | + |
| 33 | +To provide an alternate branding to the build, specify the path to the branding assets |
| 34 | +with the `BRANDING` environment variable. Relative paths in `BRANDING` are computed |
| 35 | +from the project source root. |
| 36 | + |
| 37 | +Each brand requires the presence of at least the following files: |
| 38 | + |
| 39 | +- `strings.json` |
| 40 | +- `favicon.ico` |
| 41 | +- `manifest.json` |
| 42 | + |
| 43 | +With a file path of `/alt/custom-branding`, a build that uses an alternate branding |
| 44 | +is run as: |
| 45 | + |
| 46 | +```sh |
| 47 | +> BRANDING=/alt/custom-branding npm run build |
| 48 | +``` |
| 49 | + |
| 50 | +The dev server can also be run this way. Since file watching of the branding assets |
| 51 | +is not implemented in the common module's build watch mode, it may be necessary to |
| 52 | +manually build the common module before running the dev server. When working on a |
| 53 | +brand, it is useful to run the dev server like this: |
| 54 | + |
| 55 | +```sh |
| 56 | +> export BRANDING=/alt/custom-branding |
| 57 | +> npm run build -w common |
| 58 | +> npm run start:dev |
| 59 | +> unset BRANDING # when you don't want to use the custom branding path anymore |
| 60 | +``` |
| 61 | + |
| 62 | +### File details |
| 63 | + |
| 64 | +#### strings.json |
| 65 | + |
| 66 | +The expected shape of `strings.json` is defined in [branding.ts](./common/src/branding.ts). |
| 67 | + |
| 68 | +The default version of the file is [branding/strings.json](./branding/strings.json). |
| 69 | + |
| 70 | +A minimal viable example of the file is: |
| 71 | + |
| 72 | +```json |
| 73 | +{ |
| 74 | + "application": { |
| 75 | + "title": "Konveyor" |
| 76 | + }, |
| 77 | + "about": { |
| 78 | + "displayName": "Konveyor" |
| 79 | + }, |
| 80 | + "masthead": {} |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +At build time, the json file is processed as an [ejs](https://ejs.co/) template. The |
| 85 | +variable `brandingRoot` is provided as the relative root of the branding |
| 86 | +assets within the build of the common module. Consider the location of `strings.json` |
| 87 | +in your branding directory as the base `brandingRoot` when creating a new brand. |
| 88 | + |
| 89 | +For example, to properly reference a logo within this branding structure: |
| 90 | + |
| 91 | +``` |
| 92 | + special-brand/ |
| 93 | + images/ |
| 94 | + masthead-logo.svg |
| 95 | + about-logo.svg |
| 96 | + strings.json |
| 97 | +``` |
| 98 | + |
| 99 | +Use a url string like this: |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "about": { |
| 104 | + "imageSrc": "<%= brandingRoot %>/images/about-logo.svg" |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +and in the output of `BRANDING=special-brand npm run build -w common`, the `imageSrc` |
| 110 | +will be `branding/images/about-logo.svg` with all of the files in `special-branding/*` |
| 111 | +copied to and available to the client and server modules from |
| 112 | +`@konveyor-ui/common/branding/*`. |
| 113 | + |
| 114 | +#### favicon.ico |
| 115 | + |
| 116 | +A standard favorite icon file `favicon.ico` is required to be in the same directory |
| 117 | +as `strings.json` |
| 118 | + |
| 119 | +#### manifest.json |
| 120 | + |
| 121 | +A standard [web app manifest](https://developer.mozilla.org/en-US/docs/Web/Manifest) |
| 122 | +file `manifest.json` is required to be in the same directory as `strings.json`. |
| 123 | + |
| 124 | +## Technical details |
| 125 | + |
| 126 | +All branding strings and assets are pulled in to the common module. The client and |
| 127 | +server modules access the branding from the common module build. |
| 128 | + |
| 129 | +The `common` module relies on rollup packaging to embed all of the brand for easy |
| 130 | +use. The use of branding strings in `client` and `server` modules is straight forward. |
| 131 | +Pulling in `strings.json` and providing the base path to the brand assets is a |
| 132 | +more complicated. |
| 133 | + |
| 134 | +The `common` module provides the `brandingAssetPath()` function to let the build time |
| 135 | +code find the root path to all brand assets. Webpack configuration files use this |
| 136 | +function to source the favicon.ico, manifest.json and other brand assets to be copied |
| 137 | +to the application bundle. |
| 138 | + |
| 139 | +The `brandingStrings` is typed and sourced from a json file. To pass typescript builds, |
| 140 | +a stub json file needs to be available at transpile time. By using a typescript paths |
| 141 | +of `@branding/strings.json`, the stub json is found at transpile time. The generated |
| 142 | +javascript will still import the path alias. The |
| 143 | +[virtual rollup plugin](https://github.com/rollup/plugins/tree/master/packages/virtual) |
| 144 | +further transform the javascript output by replacing the `@branding/strings.json` import |
| 145 | +with a dynamically built module containing the contents of the brand's `strings.json`. |
| 146 | +The brand json becomes a virtual module embedded in the common module. |
| 147 | + |
| 148 | +A build for a custom brand will fail (1) if the expected files cannot be read, or (2) |
| 149 | +if `strings.json` is not a valid JSON file. **Note:** The context of `stings.json` is |
| 150 | +not currently validated. If something is missing or a url is malformed, it will only |
| 151 | +be visible as a runtime error. |
0 commit comments