Skip to content

Commit 92b92e9

Browse files
authored
Add doc for esm imports (#71)
* add doc for esm imports * review * review
1 parent 3fd0cdb commit 92b92e9

File tree

8 files changed

+230
-24
lines changed

8 files changed

+230
-24
lines changed

sdk/va-report-components/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,32 @@ Access to a deployment of SAS Visual Analytics 8.4 (or later) is necessary in or
1313

1414
### NPM
1515

16-
The <a target="_blank" href="https://www.npmjs.com/package/@sassoftware/va-report-components">`@sassoftware/va-report-components`</a> library is published to NPM and can be installed by running the `npm install` command as shown below. `va-report-components` does not support ES6 imports. Therefore, the contents of the `va-report-components/dist` folder must be deployed with your page, and then loaded using a `script` tag.
16+
The <a target="_blank" href="https://www.npmjs.com/package/@sassoftware/va-report-components">`@sassoftware/va-report-components`</a> library is published to NPM and can be installed by running the `npm install` command as shown below. `va-report-components` can then be loaded with either a `script` tag or with an ES module import.
1717

1818
```bash
1919
# From the root directory of your project
2020
npm install @sassoftware/va-report-components
21+
```
22+
23+
When using a `script` tag, the contents of the `va-report-components/dist` folder must be deployed with your page.
2124

25+
```bash
2226
# Copy the contents of the package to an asset folder for deployment
2327
cp -r ./node_modules/@sassoftware/va-report-components ./sdk-assets
2428
```
2529

26-
The library can then be loaded out of the deployed assets folder using a `script` tag.
30+
The library can then be loaded out of the deployed assets folder.
2731

2832
```html
2933
<script async src="./sdk-assets/dist/umd/va-report-components.js"></script>
3034
```
3135

36+
If your site is built using a code bundler, it might be more convenient to load the library through ES module imports. See the <a target="_blank" href="https://developer.sas.com/sdk/va/docs/guides/esm/">ES module imports guide</a> for more details.
37+
38+
```js
39+
import "@sassoftware/va-report-components"
40+
```
41+
3242
### CDN (Content Delivery Network)
3343

3444
Accessing the `va-report-components` library from the SAS Developer CDN is easy. It does not require installation or

sdk/va-report-components/documentation/docs/api-reference.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ assets are loaded. The `vaReportComponents.loaded` event is dispatched once it i
3333
});
3434
</script>
3535
```
36+
37+
## Loading with imports
38+
39+
When you load the library as an ES module, APIs are available as named imports. See the [ES module imports guide](guides/esm.md) for more information about loading the library as a module.
40+
41+
```ts
42+
import { connectToServer } from '@sassoftware/va-report-components';
43+
```

sdk/va-report-components/documentation/docs/api/setFormatterLocale.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@ setFormatterLocale(locale): void
1515

1616
The locale should be specified as a language code and an optional country code. This is the same format that is used by [navigator.language](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language)
1717

18-
### Examples
18+
## Examples
19+
20+
### Script Tag
1921

2022
```javascript
21-
window.addEventListener("vaReportComponents.loaded", function () {
23+
window.addEventListener("vaReportComponents.loaded", function() {
2224
vaReportComponents.setFormatterLocale("fr");
2325
});
2426
```
2527

2628
```javascript
27-
window.addEventListener("vaReportComponents.loaded", function () {
29+
window.addEventListener("vaReportComponents.loaded", function() {
2830
vaReportComponents.setFormatterLocale("fr-CA");
2931
});
3032
```
33+
34+
### ESM
35+
36+
```javascript
37+
import { setFormatterLocale } from "@sassoftware/va-report-components";
38+
setFormatterLocale("fr");
39+
```
40+
41+
```javascript
42+
import { setFormatterLocale } from "@sassoftware/va-report-components";
43+
setFormatterLocale("fr-CA");
44+
```

sdk/va-report-components/documentation/docs/api/setLoadingTheme.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ setLoadingTheme(options): void
99

1010
`setLoadingTheme` allows the user to set the theme of the sdk before a report loads. You might want to set the theme for the initial loading state to match the website that the SAS Visual Analytic SDK is embedded in. You also might want to set the loading theme to be consistent with the report that will be loaded.
1111

12-
1312
`setLoadingTheme` only affects the initial loading and logon states. It does not affect the report theme once a report has been loaded.
1413

1514
## Arguments
@@ -18,33 +17,50 @@ setLoadingTheme(options): void
1817

1918
When you select `'light'`, `'dark'`, or `'high-contrast'`, then the loading theme is based on one of the built-in themes.
2019

21-
2220
Alternatively, you can specify theme values. The following optional properties are supported:
2321

2422
- `baseTheme: 'light' | 'dark'`: The base theme that is used for loading the theme values. The theme values are used when no defaults are provided. For example, if backgroundColor is not specified and the baseTheme is light, then the backgoundColor will be white since that is the theme's background color. **default**:`light`
2523

2624
You can further customize the loading style with additional style properties. Some examples are provided below to show you how certain options affect the theme. The following style overrides are available:
25+
2726
- `primary: string`: The logon button background and loading indicator.
2827
- `backgroundColor: string`
2928
- `textColor: string`
3029
- `textColorInverse: string` The button text color for the logon state.
3130
- `fontFamily: string`
3231

32+
## Examples
3333

34-
### Examples
34+
### Script Tag
3535

3636
```javascript
37-
window.addEventListener('vaReportComponents.loaded', function() {
38-
vaReportComponents.setLoadingTheme('dark');
39-
});
37+
window.addEventListener("vaReportComponents.loaded", function() {
38+
vaReportComponents.setLoadingTheme("dark");
39+
});
4040
```
4141

4242
```javascript
43-
window.addEventListener('vaReportComponents.loaded', function() {
44-
vaReportComponents.setLoadingTheme({
45-
baseTheme: 'dark',
46-
primary: '#FFA500',
47-
fontFamily: 'Gothic',
48-
});
43+
window.addEventListener("vaReportComponents.loaded", function() {
44+
vaReportComponents.setLoadingTheme({
45+
baseTheme: "dark",
46+
primary: "#FFA500",
47+
fontFamily: "Gothic",
4948
});
50-
```
49+
});
50+
```
51+
52+
### ESM
53+
54+
```javascript
55+
import { setLoadingTheme } from "@sassoftware/va-report-components";
56+
setLoadingTheme("dark");
57+
```
58+
59+
```javascript
60+
import { setLoadingTheme } from "@sassoftware/va-report-components";
61+
setLoadingTheme({
62+
baseTheme: "dark",
63+
primary: "#FFA500",
64+
fontFamily: "Gothic",
65+
});
66+
```

sdk/va-report-components/documentation/docs/api/setLocale.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,30 @@ setLocale(locale): void
1515

1616
The locale should be specified as a language code and an optional country code. This is the same format that is used by [navigator.language](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language)
1717

18-
### Examples
18+
## Examples
19+
20+
### Script Tag
1921

2022
```javascript
21-
window.addEventListener("vaReportComponents.loaded", function () {
23+
window.addEventListener("vaReportComponents.loaded", function() {
2224
vaReportComponents.setLocale("fr");
2325
});
2426
```
2527

2628
```javascript
27-
window.addEventListener("vaReportComponents.loaded", function () {
29+
window.addEventListener("vaReportComponents.loaded", function() {
2830
vaReportComponents.setLocale("fr-CA");
2931
});
3032
```
33+
34+
### ESM
35+
36+
```javascript
37+
import { setLocale } from "@sassoftware/va-report-components";
38+
setLocale("fr");
39+
```
40+
41+
```javascript
42+
import { setLocale } from "@sassoftware/va-report-components";
43+
setLocale("fr-CA");
44+
```

sdk/va-report-components/documentation/docs/getting-started.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,32 @@ You can embed entire reports with the `<sas-report>` custom HTML element, embed
1111

1212
### NPM
1313

14-
The <a target="_blank" href="https://www.npmjs.com/package/@sassoftware/va-report-components">`@sassoftware/va-report-components`</a> library is published to NPM and can be installed by running the `npm install` command as shown below. `va-report-components` does not support ES6 imports. Therefore, the contents of the `va-report-components/dist` folder must be deployed with your page, and then loaded using a `script` tag.
14+
The <a target="_blank" href="https://www.npmjs.com/package/@sassoftware/va-report-components">`@sassoftware/va-report-components`</a> library is published to NPM and can be installed by running the `npm install` command as shown below. `va-report-components` can then be loaded with either a `script` tag or with an ES module import.
1515

1616
```bash
1717
# From the root directory of your project
1818
npm install @sassoftware/va-report-components
19+
```
20+
21+
When using a `script` tag, the contents of the `va-report-components/dist` folder must be deployed with your page.
1922

23+
```bash
2024
# Copy the contents of the package to an asset folder for deployment
2125
cp -r ./node_modules/@sassoftware/va-report-components ./sdk-assets
2226
```
2327

24-
The library can then be loaded out of the deployed assets folder using a `script` tag.
28+
The library can then be loaded out of the deployed assets folder.
2529

2630
```html
2731
<script async src="./sdk-assets/dist/umd/va-report-components.js"></script>
2832
```
2933

34+
If your site is built using a code bundler, it might be more convenient to load the library through ES module imports. See the [ES module guide](guides/esm.md) for more details.
35+
36+
```js
37+
import "@sassoftware/va-report-components"
38+
```
39+
3040
### CDN (Content Delivery Network)
3141

3242
Accessing the `va-report-components` library from the SAS Developer CDN is easy. It does not require installation or
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: esm
3+
title: ES Module Imports
4+
---
5+
6+
Sites built using a code bundler may choose to load `@sassoftware/va-report-components` with ES module imports. When loaded this way, the APIs are available as named imports.
7+
8+
[SASReportElement](api/SASReportElement.md) and the other custom elements are defined through side effects, so care must be taken to ensure `@sassoftware/va-report-components` is included at runtime. One way to do this is by adding a side effect import in the build's entry point or in the modules where `va-report-components` is used.
9+
10+
```js
11+
import "@sassoftware/va-report-components";
12+
```
13+
14+
A side effect import might not be necessary if the code uses an imported API directly.
15+
16+
```js
17+
import { SASReportElement } from "@sassoftware/va-report-components";
18+
const report = new SASReportElement();
19+
document.body.appendChild(report);
20+
```
21+
22+
For TypeScript applications, type imports are insufficent for ensuring that `va-report-components` will be included in the build.
23+
24+
```ts
25+
// Type imports are NOT included at runtime
26+
import type { SASReportElement } from "@sassoftware/va-report-components";
27+
// This report will not load unless va-report-components is imported elsewhere
28+
const report = document.getElementById("my-report") as SASReportElement;
29+
```
30+
31+
## Usage notes
32+
33+
### Webpack
34+
35+
#### Configuration
36+
37+
`va-report-components` contains CSS files that might not work as intended when processed by webpack loaders. If your build uses loaders such as style-loader or css-loader, you should exclude `va-report-components`.
38+
39+
```js
40+
// webpack.config.js
41+
export default {
42+
module: {
43+
rules: [
44+
{
45+
test: /\.css$/,
46+
use: ["style-loader", "css-loader"],
47+
exclude: /va-report-components/,
48+
},
49+
],
50+
},
51+
};
52+
```
53+
54+
#### SAS Report Packages
55+
56+
If your site hosts [exported SAS Report Packages](guides/export-report-package.md) alongside your webpack build, they can be referenced relative to `__webpack_public_path__`.
57+
58+
```js
59+
import { SASReportElement } from "@sassoftware/va-report-components";
60+
const report = new SASReportElement();
61+
report.packageUri = `${__webpack_public_path__}reports/ExampleReport.sasreportpkg`;
62+
```
63+
64+
### Vite
65+
66+
#### Configuration
67+
68+
`va-report-components` references some assets with the `new URL("...", import.meta.url)` syntax, a feature not yet fully supported in Vite dev builds. In order to load `va-report-components` in Vite dev builds, `@sassoftware/va-report-components` must be excluded from `optimizeDeps` in the Vite config. Then, all CommonJS modules imported by the package, both directly and indirectly, must be re-included.
69+
70+
```ts
71+
// vite.config.js
72+
export default defineConfig({
73+
optimizeDeps: {
74+
exclude: ["@sassoftware/va-report-components"],
75+
include: [
76+
"@tanstack/react-query",
77+
"classnames",
78+
"handlebars",
79+
"hoist-non-react-statics",
80+
"prop-types",
81+
"react-dom",
82+
"react-fast-compare",
83+
"react-redux",
84+
"void-elements",
85+
"warning",
86+
],
87+
},
88+
});
89+
```
90+
91+
Known CommonJS dependencies are listed above, but the actual list can vary between applications. When a module needs to be added to the list, a message is logged in the browser console. Here is an example:
92+
93+
```text
94+
SyntaxError: The requested module '/node_modules/prop-types/index.js?v=4776a820' does not provide an export named 'default'
95+
```
96+
97+
Production builds are not affected by this issue.
98+
99+
#### SAS Report Packages
100+
101+
If you include [exported SAS Report Packages](guides/export-report-package.md) in your Vite project's
102+
`public` directory, the packages can be referenced relative to `import.meta.env.BASE_URL`.
103+
104+
```js
105+
import { SASReportElement } from "@sassoftware/va-report-components";
106+
const report = new SASReportElement();
107+
report.packageUri = `${import.meta.env.BASE_URL}reports/ExampleReport.sasreportpkg`;
108+
```
109+
110+
### React
111+
112+
Custom elements such as `sas-report` can be rendered in React, but the `class` prop should be used rather than `className`.
113+
114+
```jsx
115+
import '@sassoftware/va-report-components';
116+
117+
export const MyComponent(props) {
118+
return (
119+
<sas-report
120+
class="my-class"
121+
authenticationType="guest"
122+
url="http://my-viya-server.com"
123+
reportUri="/reports/reports/c3c6befb-3981-4c9e-b011-7dc11dec5e37"
124+
></sas-report>
125+
)
126+
}
127+
```
128+
129+
For more information, see [Custom HTML elements](https://react.dev/reference/react-dom/components#custom-html-elements) in the React documentation.

sdk/va-report-components/documentation/website/sidebars.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"docs": {
33
"Introduction": ["getting-started"],
4-
"Guides": ["guides/viya-setup", "guides/export-report-package", "guides/data-driven-content"],
4+
"Guides": [
5+
"guides/viya-setup",
6+
"guides/export-report-package",
7+
"guides/data-driven-content",
8+
"guides/esm"
9+
],
510
"API Reference": [
611
"api-reference",
712
"api/SASReportElement",

0 commit comments

Comments
 (0)