@equinor/fusion-framework-vite-plugin-spa 1.0.0-next.0 → 1.0.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/esm/html/bootstrap.js +57 -0
  3. package/dist/esm/html/bootstrap.js.map +1 -0
  4. package/dist/esm/html/index.html.js +51 -0
  5. package/dist/esm/html/index.html.js.map +1 -0
  6. package/dist/esm/html/index.js +2 -0
  7. package/dist/esm/html/index.js.map +1 -0
  8. package/dist/esm/html/register-service-worker.js +58 -0
  9. package/dist/esm/html/register-service-worker.js.map +1 -0
  10. package/dist/esm/html/sw.js +172 -0
  11. package/dist/esm/html/sw.js.map +1 -0
  12. package/dist/esm/index.js +3 -0
  13. package/dist/esm/index.js.map +1 -0
  14. package/dist/esm/plugin.js +70 -0
  15. package/dist/esm/plugin.js.map +1 -0
  16. package/dist/esm/types.js +2 -0
  17. package/dist/esm/types.js.map +1 -0
  18. package/dist/esm/util/load-env.js +24 -0
  19. package/dist/esm/util/load-env.js.map +1 -0
  20. package/dist/esm/util/object-to-env.js +46 -0
  21. package/dist/esm/util/object-to-env.js.map +1 -0
  22. package/dist/esm/version.js +3 -0
  23. package/dist/esm/version.js.map +1 -0
  24. package/dist/tsconfig.tsbuildinfo +1 -0
  25. package/dist/types/html/bootstrap.d.ts +1 -0
  26. package/dist/types/html/index.d.ts +1 -0
  27. package/dist/types/html/index.html.d.ts +19 -0
  28. package/dist/types/html/register-service-worker.d.ts +3 -0
  29. package/dist/types/html/sw.d.ts +1 -0
  30. package/dist/types/index.d.ts +2 -0
  31. package/dist/types/plugin.d.ts +21 -0
  32. package/dist/types/types.d.ts +45 -0
  33. package/dist/types/util/load-env.d.ts +15 -0
  34. package/dist/types/util/object-to-env.d.ts +33 -0
  35. package/dist/types/version.d.ts +1 -0
  36. package/package.json +6 -6
  37. package/src/html/bootstrap.ts +8 -4
  38. package/src/plugin.ts +7 -0
  39. package/src/version.ts +1 -1
  40. package/tsconfig.json +17 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export { registerServiceWorker } from './register-service-worker.js';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Represents an HTML template string used for generating the main structure of an SPA (Single Page Application).
3
+ *
4
+ * @see {@link https://vite.dev/guide/env-and-mode.html#html-constant-replacement}
5
+ *
6
+ * The template includes placeholders for dynamic values such as:
7
+ * - `%FUSION_SPA_TITLE%`: The title of the SPA.
8
+ * - `%MODE%`: The mode of the application (e.g., development, production).
9
+ * - `%FUSION_SPA_BOOTSTRAP%`: The path to the bootstrap script for initializing the SPA.
10
+ *
11
+ * Additionally, it includes:
12
+ * - A meta tag for the plugin version, dynamically populated using the `version` variable.
13
+ * - A link to the Equinor font stylesheet hosted on a CDN.
14
+ *
15
+ * @constant
16
+ * @type {string}
17
+ */
18
+ export declare const html = "\n <!DOCTYPE html>\n <html>\n <head>\n <title>%FUSION_SPA_TITLE%</title>\n <meta name=\"mode\" content=\"%MODE%\">\n <meta name=\"fusion-spa-plugin-version\" content=\"1.0.0-next.2\">\n <link rel=\"stylesheet\" href=\"https://cdn.eds.equinor.com/font/equinor-font.css\" />\n <script type=\"module\" src=\"%FUSION_SPA_BOOTSTRAP%\"></script>\n <script>\n // suppress console error for custom elements already defined. \n // WebComponents should be added by the portal, but not removed from application\n const _customElementsDefine = window.customElements.define;\n window.customElements.define = (name, cl, conf) => {\n if (!customElements.get(name)) {\n _customElementsDefine.call(window.customElements, name, cl, conf);\n }\n };\n </script>\n <style>\n html, body {\n margin: 0;\n padding: 0;\n height: 100%;\n font-family: 'EquinorFont', sans-serif;\n }\n </style>\n </head>\n <body></body>\n </html>\n";
19
+ export default html;
@@ -0,0 +1,3 @@
1
+ import type { ModulesInstance } from '@equinor/fusion-framework-module';
2
+ import type { MsalModule } from '@equinor/fusion-framework-module-msal';
3
+ export declare function registerServiceWorker(framework: ModulesInstance<[MsalModule]>): Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export { default, plugin as fusionSpaPlugin, type PluginOptions } from './plugin.js';
2
+ export * from './types.js';
@@ -0,0 +1,21 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { TemplateEnv, TemplateEnvFn } from './types.js';
3
+ /**
4
+ * Represents the options for configuring a plugin.
5
+ *
6
+ * @template TEnv - The type of the template environment. Defaults to `TemplateEnv`.
7
+ *
8
+ * @property {string} [template] - The path to the template file to be used.
9
+ * @property {string} [templateEnvPrefix] - A prefix to filter environment variables for the template.
10
+ * @property generateTemplateEnv -
11
+ * A function to generate the template environment. It receives the configuration environment and
12
+ * a partial template environment as arguments, and returns a modified or extended partial template environment.
13
+ */
14
+ export type PluginOptions<TEnv extends TemplateEnv = TemplateEnv> = {
15
+ template?: string;
16
+ templateEnvPrefix?: string;
17
+ generateTemplateEnv?: TemplateEnvFn<TEnv>;
18
+ logger?: Pick<Console, 'debug' | 'info' | 'warn' | 'error'>;
19
+ };
20
+ export declare const plugin: <TEnv extends TemplateEnv = TemplateEnv>(options?: PluginOptions<TEnv>) => Plugin;
21
+ export default plugin;
@@ -0,0 +1,45 @@
1
+ import type { ConfigEnv } from 'vite';
2
+ export type TemplateEnv = Record<string, unknown>;
3
+ export type ResourceConfiguration = {
4
+ url: string;
5
+ scopes?: string[];
6
+ rewrite?: string;
7
+ };
8
+ /**
9
+ * Represents the environment configuration for a template.
10
+ */
11
+ export type FusionTemplateEnv = {
12
+ /** Document title */
13
+ title: string;
14
+ /** Template bootstrap file path */
15
+ bootstrap: string;
16
+ /** Id of the portal to load */
17
+ portal: {
18
+ id: string;
19
+ tag?: string;
20
+ };
21
+ /** Service discovery configuration */
22
+ serviceDiscovery: {
23
+ url: string;
24
+ scopes: string[];
25
+ };
26
+ /** MSAL configuration */
27
+ msal: {
28
+ tenantId: string;
29
+ clientId: string;
30
+ redirectUri: string;
31
+ requiresAuth: string;
32
+ };
33
+ /** Service worker configuration */
34
+ serviceWorker: {
35
+ resources: ResourceConfiguration[];
36
+ };
37
+ };
38
+ /**
39
+ * A function type that generates a partial environment configuration for a template.
40
+ *
41
+ * @template TEnv - The type of the environment configuration. Defaults to `TemplateEnv`.
42
+ * @param configEnv - The configuration environment provided as input.
43
+ * @returns A partial environment configuration of type `TEnv`, or `undefined` if no configuration is provided.
44
+ */
45
+ export type TemplateEnvFn<TEnv extends TemplateEnv> = (configEnv: ConfigEnv) => Partial<TEnv> | undefined;
@@ -0,0 +1,15 @@
1
+ import { type ConfigEnv, type UserConfig } from 'vite';
2
+ /**
3
+ * Loads environment variables for a Vite project based on the provided configuration and environment.
4
+ *
5
+ * @see {@link http://vite.dev/guide/env-and-mode.html#env-files}
6
+ *
7
+ * @param config - The user configuration object, which includes properties such as `root` and `envDir`.
8
+ * - `root`: The root directory of the project. Defaults to the current working directory if not specified.
9
+ * - `envDir`: The directory containing environment files. If not specified, defaults to the root directory.
10
+ * @param env - The environment configuration object.
11
+ * - `mode`: The mode in which the application is running (e.g., 'development', 'production').
12
+ * @returns A record of environment variables prefixed with `FUSION_SPA_`.
13
+ */
14
+ export declare function loadEnvironment(config: UserConfig, env: ConfigEnv, namespace?: string): Record<string, string>;
15
+ export default loadEnvironment;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Converts a nested object into a flat object with keys in snake_case and uppercase.
3
+ * Nested keys are prefixed with their parent keys, separated by underscores.
4
+ * Non-object values are stringified.
5
+ *
6
+ * @param obj - The input object to be flattened and converted.
7
+ * @param prefix - An optional prefix to prepend to the keys (default is an empty string).
8
+ * @returns A flat object with snake_case, uppercase keys and stringified values.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const input = {
13
+ * someKey: "value",
14
+ * nestedObject: {
15
+ * anotherKey: 42,
16
+ * deepNested: {
17
+ * finalKey: true
18
+ * }
19
+ * }
20
+ * };
21
+ *
22
+ * const result = objectToEnv(input);
23
+ * console.log(result);
24
+ * // Output:
25
+ * // {
26
+ * // SOME_KEY: "\"value\"",
27
+ * // NESTED_OBJECT_ANOTHER_KEY: "42",
28
+ * // NESTED_OBJECT_DEEP_NESTED_FINAL_KEY: "true"
29
+ * // }
30
+ * ```
31
+ */
32
+ export declare function objectToEnv(obj: object, prefix?: string): Record<string, string>;
33
+ export default objectToEnv;
@@ -0,0 +1 @@
1
+ export declare const version = "1.0.0-next.2";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@equinor/fusion-framework-vite-plugin-spa",
3
- "version": "1.0.0-next.0",
3
+ "version": "1.0.0-next.2",
4
4
  "description": "Vite plugin for SPA development",
5
5
  "type": "module",
6
6
  "types": "dist/types/index.d.ts",
@@ -34,11 +34,11 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "lodash.mergewith": "^4.6.2",
37
- "@equinor/fusion-framework-module": "^4.4.2",
38
- "@equinor/fusion-framework-module-http": "^6.3.3-next.0",
39
- "@equinor/fusion-framework-module-msal": "^4.0.6",
40
- "@equinor/fusion-framework-module-service-discovery": "^8.0.15-next.0",
41
- "@equinor/fusion-framework-vite-plugin-api-service": "^1.0.0-next.0"
37
+ "@equinor/fusion-framework-module": "^4.4.3-next.0",
38
+ "@equinor/fusion-framework-module-msal": "^4.0.7-next.0",
39
+ "@equinor/fusion-framework-module-http": "^6.3.3-next.1",
40
+ "@equinor/fusion-framework-module-service-discovery": "^8.0.15-next.1",
41
+ "@equinor/fusion-framework-vite-plugin-api-service": "^1.0.0-next.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@types/lodash.mergewith": "^4.6.9",
@@ -12,7 +12,8 @@ import { registerServiceWorker } from './register-service-worker.js';
12
12
  type PortalManifest = {
13
13
  build: {
14
14
  config: Record<string, unknown>;
15
- entrypoint: string;
15
+ templateEntry: string;
16
+ assetPath?: string;
16
17
  };
17
18
  };
18
19
 
@@ -79,11 +80,14 @@ enableMSAL(configurator, (builder) => {
79
80
  document.body.innerHTML = '';
80
81
  document.body.appendChild(el);
81
82
 
83
+ const portalEntryPoint = [portal_manifest.build.assetPath, portal_manifest.build.templateEntry]
84
+ .filter(Boolean)
85
+ .join('/');
86
+
82
87
  // @todo: should test if the entrypoint is external or internal
83
88
  // @todo: add proper return type
84
- const { render } = await importWithoutVite<Promise<{ render: (...args: unknown[]) => void }>>(
85
- portal_manifest.build.entrypoint,
86
- );
89
+ const { render } =
90
+ await importWithoutVite<Promise<{ render: (...args: unknown[]) => void }>>(portalEntryPoint);
87
91
 
88
92
  // render the portal - this will load the portal template and render it
89
93
  render(el, { ref, manifest: portal_manifest, config: portal_config });
package/src/plugin.ts CHANGED
@@ -75,6 +75,13 @@ export const plugin = <TEnv extends TemplateEnv = TemplateEnv>(
75
75
  for (const [key, value] of Object.entries(env)) {
76
76
  config.define[`import.meta.env.${key}`] = value;
77
77
  }
78
+
79
+ config.server ??= {};
80
+ config.server.fs ??= {};
81
+ config.server.fs.allow ??= [];
82
+ // allow access to the template file
83
+ config.server.fs.allow.push(new URL('./html', import.meta.url).pathname);
84
+
78
85
  log?.info(`plugin configured for ${env.FUSION_SPA_PORTAL_ID}`);
79
86
  },
80
87
  configureServer(server) {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  // Generated by genversion.
2
- export const version = '1.0.0-next.0';
2
+ export const version = '1.0.0-next.2';
package/tsconfig.json CHANGED
@@ -7,6 +7,23 @@
7
7
  "baseUrl": ".",
8
8
  "types": ["vite/client"]
9
9
  },
10
+ "references": [
11
+ {
12
+ "path": "../../modules/module"
13
+ },
14
+ {
15
+ "path": "../../modules/http"
16
+ },
17
+ {
18
+ "path": "../../modules/msal"
19
+ },
20
+ {
21
+ "path": "../../modules/service-discovery"
22
+ },
23
+ {
24
+ "path": "../../modules/services"
25
+ }
26
+ ],
10
27
  "include": ["src/**/*"],
11
28
  "exclude": ["node_modules"]
12
29
  }