-
Notifications
You must be signed in to change notification settings - Fork 53
feat(ARCH-349): add assets api #3692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
951ad6c
7e5903b
7022f7e
a77c39f
43804c1
42bd422
53e8c15
64edb08
6049a3a
7cd06c5
838130b
af78073
3e19f32
15d9c31
0a4fe8e
ed92fd9
1c85f25
2f06926
4690bda
d4a5d08
2aaa5f3
5b3f588
0773bbc
e6101b7
fb5e77d
854cfbd
dc3ad06
cb60a46
b357c9c
95dee6d
b72975d
5ad8e11
75d2546
0ef170b
e630f70
12ae362
96244b6
c77d94a
658fb55
1a27e0e
ad85545
dd5a536
3191d86
c4170cd
609754d
333c45e
7b65e86
421a123
2b94ced
bd3264f
e13a1f4
ce20b55
7c0f2f7
489b959
4944d86
bd955db
5075db0
4a3a86d
732ace5
578ef21
ceb4f4d
5a8d8f2
ae1f754
4dee206
965c5d5
6498986
0585939
6912efb
6bbec14
5268584
54df8e6
b8921cd
48716ea
9603827
e50f1b7
f3d1504
bffeabf
2f54c2e
6c72be6
496c983
c094532
ac12fcd
ff17618
331acbd
25afd56
ec8f355
28c6d4f
029f86a
9e25b37
f432805
0d8f1a3
1b529b8
fb3b0f2
2a8c931
2b220eb
0fddf7d
3df8f0d
672bf34
7e0d83e
c92979f
6aa87cc
099caa5
927ad9d
e60244e
0d44297
99e6e8f
3b4ac4a
332cfcd
a9037bc
6c970a2
514982f
0ae09f3
9ea303f
6f7b29c
2558f84
38ad87e
ead22ac
d76933f
d76665f
9fef15c
d30cbce
f2a47cd
3746d84
41c155e
1d9b322
24bb10d
a2e3dd8
f023dee
477802d
908208f
3d014f0
2042b87
c48ade9
a70041a
8eb0969
0440ad4
01992ce
4b72f53
a26c39b
5a35295
610c4e0
51547e4
45ef1ab
a305133
5097e3e
f70ae3a
1aa9d06
708b3c2
dc348c5
c6b94c0
aa7b44f
a977d43
d5ed3b4
b25eb57
7a2e7a7
14968ea
cd989dc
54664bc
6d8fadc
170fc47
0546e93
e99cda2
d9dfa77
9d42821
7b37d45
35cccea
2adb4c9
837a4a7
bdf31f3
9136236
27f7d93
92a2425
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@talend/react-datagrid': minor | ||
| --- | ||
|
|
||
| feat: use @talend/assets-api to load datagrid umds on demand |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@talend/scripts-config-react-webpack': minor | ||
| --- | ||
|
|
||
| feat: add CDN_URL to /cdn by default |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@talend/design-system': minor | ||
| --- | ||
|
|
||
| feat: use @talend/assets-api to load icons | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@talend/react-forms': minor | ||
| --- | ||
|
|
||
| Use @talend/assets-api to load aceeditor |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@talend/react-dataviz': minor | ||
| --- | ||
|
|
||
| feat: Use @talend/assets-api to load topologies. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ | |
| build | ||
| dist | ||
| node_modules | ||
| __fixtures__ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # @talend/assets-api | ||
|
|
||
| Assets are files from a npm package which can be needed at some point by a web application. It can be a stylesheet, a SVG icon, a js script, a json file, etc... | ||
|
|
||
| At Talend our web applications may rely on a CDN to load libraries like lodash, d3 but also @talend/design-system using UMD distribution format. An application rely on a CDN depending of the execution context; We are following 12 factors principals. So the same code base must work in both case so an asset url may change from `/cdn/my-package/1.2.3/dist/my-assets.svg` to `https://mycdn.talend.com/my-package/1.2.3/dist/my-assets.svg`. | ||
|
|
||
| This is made possible using custom configuration of `@talend/dynamic-cdn-webpack-plugin`. Now we also need to be able to load translations, icons and more **assets** from the code. | ||
|
|
||
| This package exposes a simple and friendly API to let developers access assets without the complexity of computing URL depending on the context (CDN or not). | ||
|
|
||
| ## Learn with examples | ||
|
|
||
| First you need to ensure you have setup the [babel plugin](https://npmjs.com/package/@talend/babel-plugin-assets-api) or you use @talend/scripts-core to build your lib / project. | ||
|
|
||
| Then we can start with this global example: | ||
|
|
||
| ```javascript | ||
| import assetsAPI from '@talend/assets-api'; | ||
|
|
||
| // The lowest level API is the ability to get the URL of anything. Here, a CSS file. | ||
| const href = assetsApi.getURL('/dist/styles/ag-grid.css', 'ag-grid-community'); | ||
| // babel will add all missing arguments and at runtime you will have | ||
| console.log(href); | ||
| // -> 'https://unpkg.com/ag-grid-community@25.0.0/dist/styles/ag-grid.css'; | ||
|
|
||
| // Higher level APIs enable users to get a JSON file for locales, timezones, etc... | ||
| async function getTopology(file) { | ||
| const locales = await assetsAPI.getJSON( | ||
| `/dist/assets/maps/${file}.topo.json`, | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| // We can also lazy load a component from a UMD. | ||
| // This one is a bit more complex. You have to know that React.lazy wants a default esModule from a Promise. This is what getUMD + toDefaultModule give you. | ||
| const AgGridReact = React.lazy(() => | ||
| assetsApi | ||
| .getUMD('ag-grid-community') | ||
| .then(() => assetsApi.getUMD('ag-grid-react')) | ||
| .then(mod => assetsApi.toDefaultModule(mod.AgGridReact)) | ||
| ); | ||
|
|
||
|
|
||
| // Finally, this is how we load styles from a lazy loaded component. | ||
| export default function DataGrid(props) { | ||
| //... | ||
| useEffect(() => { | ||
| const href = assetsApi.getURL('/dist/styles/ag-grid.css', 'ag-grid-community'); | ||
| assetsApi.addStyle({ href }); | ||
| }, []); | ||
| //... | ||
| return <React.Suspense fallback={}>AgGridReact</React.Suspense> | ||
| } | ||
| ``` | ||
|
|
||
| ## Requirements | ||
|
|
||
| - devs do not have to write the version: it is injected at build time thanks to [babel plugin](https://npmjs.com/package/@talend/babel-plugin-assets-api) | ||
| - the assets' version is implicitly specified by the consumer webapp (`talend-scripts` adds global data that is then read by the API at runtime) | ||
| - the inject.js script will be able to control this version (it should update meta value) | ||
| - `sessionStorage` is used to let anyone override a version locally | ||
| - the API is compatible with `React.Suspense` / lazy React APIs | ||
| - for UMDs, the path is computed form `module-to-cdn` and injected at build time thanks to the babel plugin | ||
| - for relative paths in a package, the name of the package is optional | ||
|
|
||
| ## How to configure the CDN to use ? | ||
|
|
||
| The assets API uses a global function to compute the URL: `window.Talend.getCDNUrl()`. By default the package will add it for you. This version will use a global `CDN_URL` with the following shape: | ||
|
|
||
| ```javascript | ||
| `${CDN_URL}/${info.name}/${info.version}${info.path}`; | ||
| ``` | ||
|
|
||
| but fallbacks to unpkg.com public CDN if no CDN_URL is provided. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| { | ||
| "name": "@talend/assets-api", | ||
| "description": "A set of API designed to access assets using CDN", | ||
| "types": "lib/index.d.ts", | ||
| "main": "lib/index.js", | ||
| "mainSrc": "src/index.ts", | ||
| "license": "Apache-2.0", | ||
| "scripts": { | ||
| "pre-release": "echo no pre-release for assets-api", | ||
| "build:lib": "talend-scripts build:ts:lib", | ||
| "start": "echo nothing to start", | ||
| "test": "talend-scripts test", | ||
| "test:watch": "talend-scripts test --watch", | ||
| "test:cov": "talend-scripts test --coverage", | ||
| "test:demo": "echo nothing to demo", | ||
| "lint:es": "talend-scripts lint:es --format json -o eslint-report.json", | ||
| "lint": "talend-scripts lint:es --format json -o eslint-report.json" | ||
| }, | ||
| "keywords": [ | ||
| "talend" | ||
| ], | ||
| "author": "Talend Frontend <frontend@talend.com>", | ||
| "homepage": "https://github.com/Talend/ui/tree/master/packages/assets-api#readme", | ||
| "bugs": { | ||
| "url": "https://github.com/Talend/ui/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/Talend/ui.git" | ||
| }, | ||
| "dependencies": {}, | ||
| "devDependencies": { | ||
| "@talend/scripts-core": "^11.6.0", | ||
| "@talend/scripts-preset-react-lib": "^11.0.2", | ||
| "read-pkg-up": "^7.0.1" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Quite the downgrade
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we already have read-pkg-up in multiple scripts in CJS and I prefere be aligned. It could be upgraded if we move to ESM every where. |
||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "version": "1.0.0" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import readPackageUp from 'read-pkg-up'; | ||
| import assetsApi, { Asset } from '.'; | ||
|
|
||
| const iconsInfo = readPackageUp.sync({ cwd: require.resolve('@talend/icons') }); | ||
| const currentInfo = readPackageUp.sync({ cwd: __dirname }); | ||
|
|
||
| describe('assets-api', () => { | ||
| describe('getURL', () => { | ||
| it('should return unpkg url', () => { | ||
| const assetsPath = '/dist/svg-bundles/all.svg'; | ||
| const url = assetsApi.getURL(assetsPath, '@talend/icons', iconsInfo?.packageJson.version); | ||
| expect(url).toBe( | ||
| `https://unpkg.com/@talend/icons@${iconsInfo?.packageJson.version}${assetsPath}`, | ||
| ); | ||
| }); | ||
| it('should use global getCDNUrl', () => { | ||
| const assetsPath = '/package.json'; | ||
| const original = window.Talend.getCDNUrl; | ||
| window.Talend.getCDNUrl = jest.fn( | ||
| (info: Asset) => `https://mycdn.talend.com/${info.name}/${info.version}${info.path}`, | ||
| ); | ||
| const url = assetsApi.getURL( | ||
| assetsPath, | ||
| '@talend/assets-api', | ||
| currentInfo?.packageJson.version, | ||
| ); | ||
| expect(url).toBe( | ||
| `https://mycdn.talend.com/@talend/assets-api/${currentInfo?.packageJson.version}${assetsPath}`, | ||
| ); | ||
| window.Talend.getCDNUrl = original; | ||
| }); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This maybe requires more context than the requirements.
What real-life issues is it trying to solve? Can you give examples? Is it replacing something? As a newbie I don't really understand the point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with that, and it's missing a description on top to tell what it does. The documentation starts with requirements but we don't know requirements for what.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me know if this is better now