Skip to content

Commit 16c46da

Browse files
authoredSep 16, 2020
feat(framework): make assets path configurable (#2214)
1 parent 2ddc4e9 commit 16c46da

File tree

17 files changed

+151
-24
lines changed

17 files changed

+151
-24
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ before_install:
1515
- gem install jekyll-seo-tag
1616

1717
script:
18-
- yarn build
18+
- DEPLOY_PUBLIC_PATH=/resources/ yarn build
1919
- yarn test
2020

2121
deploy:

‎README.md

+7
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ yarn build # to build the project
168168
Afterwards, you can find the build output in the `dist` folder of the corresponding package folder.
169169
For example, to find the Button component (that belongs to the `main` package), look inside the `packages/main/dist` folder.
170170

171+
**Note:** Before building the project you can also set the `DEPLOY_PUBLIC_PATH` environment variable to specify the path where non-bundled assets will be fetched from, for example:
172+
173+
```
174+
DEPLOY_PUBLIC_PATH=/my/resources/ yarn build
175+
```
176+
(for Windows: `DEPLOY_PUBLIC_PATH="\/my\/resources\/" yarn build`)
177+
171178
## Limitations
172179
None as of 1.0.0-rc.8
173180

‎docs/Configuration.md

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ language | en, de, es, etc... | en |
1414
calendarType | Gregorian, Islamic, Buddhist, Japanese, Persian | Gregorian | Default calendar type for date-related web components
1515
[noConflict](#noConflict) | true, false | false | When set to true, all events will be fired with a "ui5-" prefix only
1616
[formatSettings](#formatSettings)| See the [Format settings](#formatSettings) section below | Empty object | Allows to override locale-specific configuration
17+
[assetsPath](#assetsPath)| See the [Assets path](#assetsPath) section below | Empty string | Allows to set the assets path at runtime
1718

1819
### Content Density
1920

@@ -101,6 +102,26 @@ For example, to force the first day of week to Sunday, no matter the locale:
101102
------------ | ----------------------------------------------- | ------------- | -------------------------------------------------------------
102103
firstDayOfWeek | 0 (Sunday) through 6 (Saturday) | *Depends on locale* | When set, overrides the locale's default value
103104

105+
<a name="assetsPath"></a>
106+
### Assets path
107+
108+
This configuration setting allows to set the path where asset files (most commonly `.json` ) that are to be fetched at runtime, are located. These are:
109+
- Icon collections
110+
- `i18n` message bundles
111+
- `CLDR` files
112+
- Additional themes
113+
114+
For some scenarios the same bundle will be reused from different directories, or the directory structure is unknown in advance. Therefore it's
115+
necessary to be able to pass the right directory at runtime, most commonly inside the configuration script directly:
116+
117+
Example:
118+
```html
119+
<script data-ui5-config type="application/json">
120+
{
121+
"assetsPath": "/my/custom/assets/path"
122+
}
123+
</script>
124+
```
104125

105126
## Configuration script
106127

‎packages/base/src/InitialConfiguration.js

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let initialConfig = {
1313
noConflict: false, // no URL
1414
formatSettings: {},
1515
useDefaultLanguage: false,
16+
assetsPath: "",
1617
};
1718

1819
/* General settings */
@@ -61,6 +62,11 @@ const getFormatSettings = () => {
6162
return initialConfig.formatSettings;
6263
};
6364

65+
const getAssetsPath = () => {
66+
initConfiguration();
67+
return initialConfig.assetsPath;
68+
};
69+
6470
const booleanMapping = new Map();
6571
booleanMapping.set("true", true);
6672
booleanMapping.set("false", false);
@@ -140,4 +146,5 @@ export {
140146
getNoConflict,
141147
getCalendarType,
142148
getFormatSettings,
149+
getAssetsPath,
143150
};

‎packages/base/src/asset-registries/Icons.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { registerIcon, registerCollectionPromise } from "../SVGIconRegistry.js";
22
import { fetchJsonOnce } from "../util/FetchHelper.js";
3+
import getEffectiveAssetPath from "../util/getEffectiveAssetPath.js";
34

45
const registerIconBundle = async (collectionName, bundleData) => {
56
let resolveFn;
@@ -9,7 +10,7 @@ const registerIconBundle = async (collectionName, bundleData) => {
910
registerCollectionPromise(collectionName, collectionFetched);
1011

1112
if (typeof bundleData !== "object") { // not inlined from build -> fetch it
12-
bundleData = await fetchJsonOnce(bundleData);
13+
bundleData = await fetchJsonOnce(getEffectiveAssetPath(bundleData));
1314
}
1415
fillRegistry(bundleData);
1516
resolveFn();

‎packages/base/src/asset-registries/LocaleData.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fetchJsonOnce } from "../util/FetchHelper.js";
22
import { getFeature } from "../FeaturesRegistry.js";
33
import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from "../generated/AssetParameters.js";
4+
import getEffectiveAssetPath from "../util/getEffectiveAssetPath.js";
45

56
const resources = new Map();
67
const cldrData = {};
@@ -94,7 +95,7 @@ const fetchCldr = async (language, region, script) => {
9495
registerModuleContent(`sap/ui/core/cldr/${localeId}.json`, cldrObj);
9596
} else if (url) {
9697
// fetch it
97-
const cldrContent = await fetchJsonOnce(url);
98+
const cldrContent = await fetchJsonOnce(getEffectiveAssetPath(url));
9899
registerModuleContent(`sap/ui/core/cldr/${localeId}.json`, cldrContent);
99100
}
100101
};

‎packages/base/src/asset-registries/Themes.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fetchJsonOnce, fetchTextOnce } from "../util/FetchHelper.js";
22
import { DEFAULT_THEME } from "../generated/AssetParameters.js";
33
import getFileExtension from "../util/getFileExtension.js";
4+
import getEffectiveAssetPath from "../util/getEffectiveAssetPath.js";
45

56
const themeURLs = new Map();
67
const themeStyles = new Map();
@@ -68,7 +69,7 @@ const fetchThemeProperties = async (packageName, themeName) => {
6869
throw new Error(`You have to import the ${packageName}/dist/Assets.js module to switch to additional themes`);
6970
}
7071

71-
return getFileExtension(url) === ".css" ? fetchTextOnce(url) : fetchJsonOnce(url);
72+
return getFileExtension(url) === ".css" ? fetchTextOnce(url) : fetchJsonOnce(getEffectiveAssetPath(url));
7273
};
7374

7475
const getRegisteredPackages = () => {

‎packages/base/src/asset-registries/i18n.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fetchTextOnce } from "../util/FetchHelper.js";
55
import normalizeLocale from "../locale/normalizeLocale.js";
66
import nextFallbackLocale from "../locale/nextFallbackLocale.js";
77
import { DEFAULT_LANGUAGE } from "../generated/AssetParameters.js";
8+
import getEffectiveAssetPath from "../util/getEffectiveAssetPath.js";
89
import { getUseDefaultLanguage } from "../config/Language.js";
910

1011
const bundleData = new Map();
@@ -78,7 +79,7 @@ const fetchI18nBundle = async packageName => {
7879
return;
7980
}
8081

81-
const content = await fetchTextOnce(bundleURL);
82+
const content = await fetchTextOnce(getEffectiveAssetPath(bundleURL));
8283
let parser;
8384
if (content.startsWith("{")) {
8485
parser = JSON.parse;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getAssetsPath as getConfiguredAssetsPath } from "../InitialConfiguration.js";
2+
3+
let assetsPath;
4+
5+
const getAssetsPath = () => {
6+
if (assetsPath === undefined) {
7+
assetsPath = getConfiguredAssetsPath();
8+
}
9+
10+
return assetsPath;
11+
};
12+
13+
const setAssetsPath = newAssetsPath => {
14+
assetsPath = newAssetsPath;
15+
};
16+
17+
export { getAssetsPath, setAssetsPath }; // eslint-disable-line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getAssetsPath } from "../config/AssetsPath.js";
2+
3+
const getEffectiveAssetPath = asset => {
4+
const assetsPath = getAssetsPath();
5+
if (assetsPath && typeof asset === "string") {
6+
return `${assetsPath}${asset}`;
7+
}
8+
9+
return asset;
10+
};
11+
12+
export default getEffectiveAssetPath;

‎packages/main/bundle.esm.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getAssetsPath, setAssetsPath } from "@ui5/webcomponents-base/dist/config/AssetsPath.js";
2+
// setAssetsPath("/my-resources/");
3+
14
// OpenUI5 integration
25
import "@ui5/webcomponents-base/dist/features/OpenUI5Support.js";
36

@@ -109,6 +112,8 @@ const testAssets = {
109112
setNoConflict,
110113
getRTL,
111114
getFirstDayOfWeek,
115+
getAssetsPath,
116+
setAssetsPath
112117
},
113118
getLocaleData,
114119
applyDirection,

‎packages/main/test/pages/DatePicker.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ <h3>Test ariaLabel and ariaLabelledBy</h3>
119119
<ui5-label id="infoText">info text</ui5-label>
120120
<ui5-date-picker id="dpAriaLabelledBy" aria-labelledby="infoText"></ui5-date-picker>
121121
</section>
122-
122+
123123
<section class="ui5-content-density-compact">
124124
<h3>DatePicker in Compact</h3>
125125
<div>

‎packages/main/test/pages/Kitchen.html

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
<meta http-equiv="X-UA-Compatible" content="IE=edge">
88
<meta charset="utf-8">
99

10+
<script data-ui5-config type="application/json">
11+
{
12+
"assetsPath": ""
13+
}
14+
</script>
15+
1016
<link rel="stylesheet" type="text/css" href="./kitchen-styles.css">
1117

1218
<style>

‎packages/tools/components-package/nps.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const getScripts = (options) => {
4848
default: 'concurrently "nps watch.templates" "nps watch.samples" "nps watch.test" "nps watch.src" "nps watch.bundle" "nps watch.styles"',
4949
src: 'nps "copy.src --watch --safe --skip-initial-copy"',
5050
test: 'nps "copy.test --watch --safe --skip-initial-copy"',
51-
bundle: "rollup --config config/rollup.config.js -w --environment ES5_BUILD,DEV",
51+
bundle: "rollup --config config/rollup.config.js -w --environment ES5_BUILD,DEV,DEPLOY_PUBLIC_PATH:/resources/",
5252
styles: {
5353
default: 'concurrently "nps watch.styles.themes" "nps watch.styles.components"',
5454
themes: 'nps "build.styles.themes -w"',

‎packages/tools/components-package/rollup.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const babel = require("rollup-plugin-babel");
22
const process = require("process");
33
const resolve = require("rollup-plugin-node-resolve");
4-
const url = require("rollup-plugin-url");
4+
const url = require("@rollup/plugin-url");
55
const { terser } = require("rollup-plugin-terser");
66
const notify = require('rollup-plugin-notify');
77
const filesize = require('rollup-plugin-filesize');
@@ -26,7 +26,7 @@ function ui5DevImportCheckerPlugin() {
2626

2727
const getPlugins = ({ transpile }) => {
2828
const plugins = [];
29-
let publicPath = DEPLOY_PUBLIC_PATH || "/resources/";
29+
let publicPath = DEPLOY_PUBLIC_PATH;
3030

3131
if (!process.env.DEV) {
3232
plugins.push(filesize({

‎packages/tools/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@babel/core": "^7.1.2",
2727
"@babel/plugin-proposal-object-rest-spread": "^7.3.2",
2828
"@babel/preset-env": "^7.1.0",
29+
"@rollup/plugin-url": "^5.0.1",
2930
"@wdio/cli": "^6.1.3",
3031
"@wdio/dot-reporter": "^6.0.16",
3132
"@wdio/local-runner": "^6.1.3",
@@ -69,7 +70,6 @@
6970
"rollup-plugin-notify": "^1.1.0",
7071
"rollup-plugin-string": "^2.0.2",
7172
"rollup-plugin-terser": "^5.1.3",
72-
"rollup-plugin-url": "^2.2.0",
7373
"rollup-plugin-visualizer": "^0.9.2",
7474
"serve": "^10.1.1",
7575
"wdio-chromedriver-service": "^6.0.2"

0 commit comments

Comments
 (0)
Please sign in to comment.