Skip to content

Commit 25a081a

Browse files
committed
feat: Add defineENV option
1 parent e2e90fe commit 25a081a

File tree

11 files changed

+90
-10
lines changed

11 files changed

+90
-10
lines changed

.env.local

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VITE_OFFICE_ID=c6890c26-5bbb-40ed-a321-37f07909a2f0
2-
VITE_OFFICE_DOMAIN=https://www.contoso.com
2+
VITE_OFFICE_DOMAIN=https://contoso.com
33
VITE_OFFICE_PROVIDER_NAME=Contoso, Ltd
44
VITE_OFFICE_DISPLAY_NAME=Contoso App
55
VITE_OFFICE_DESCRIPTION=Office add-in for Contoso App

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ MS Office addin for Nuxt and Vite
2525
<!-- Highlight some of the features your module provide here -->
2626
- 🌍 Supports both Nuxt & Vite
2727
- ⚙️ ENV variables support in manifest files
28+
- 🪝 Define dynamic env variables
2829
- 🧬 Same manifest routes in dev and build
2930
- 💉 Auto inject Office.js in routes
3031

@@ -33,6 +34,8 @@ MS Office addin for Nuxt and Vite
3334
- [Nuxt](docs/nuxt-setup.md)
3435
- [Vite](docs/vite-setup.md)
3536

37+
## [Configuration](docs/config.md)
38+
3639
## Contribution
3740

3841
<details>

docs/config.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Configuration
2+
3+
### `manifests`
4+
5+
- **Type:** `ManifestEntry[]`
6+
- **Description:** An array of manifest entries that define the input manifests for the Office Add-in.
7+
8+
### `injectOfficeJS`
9+
10+
- **Type:** `(string | RegExp)[]`
11+
- **Description:** An array of strings or regular expressions that specify the routes where the `office.js` library should be injected.
12+
13+
### `defineENV`
14+
15+
- **Type:** `(env: Readonly<ImportMetaEnv & object>) => Record<EnvKey, string>`
16+
- **Description:** An optional hook function that allows you to define additional environment variables for your application. The function takes an `env` parameter, which is an object containing the environment variables loaded from your environment. The function should return an object with environment variables, where the keys must be prefixed with either `NUXT_` or `VITE_`.
17+
18+
## Usage
19+
20+
To configure the Microsoft Office Add-in in your Nuxt.js application, you can create a `nuxt.config.ts` file in the root of your project and add the following configuration:
21+
22+
```ts
23+
export default defineNuxtConfig({
24+
msOfficeAddin: {
25+
manifests: [
26+
// Add your manifest entries here
27+
],
28+
injectOfficeJS: [
29+
// Add your routes where office.js should be injected
30+
],
31+
// Optionally, define additional environment variables
32+
defineENV: env => ({
33+
VITE_VAR_DATA: 'my-value',
34+
VITE_DYNAMIC_DATA: env.VITE_VAR.replace('content', 'new content'),
35+
}),
36+
},
37+
})

manifest.xml

+9
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@
1414
<SourceLocation DefaultValue="https://www.contoso.com/search_app/Default.aspx" />
1515
</DefaultSettings>
1616
<Permissions>ReadWriteDocument</Permissions>
17+
<WebApplicationInfo>
18+
<Id>{VITE_OFFICE_ID}</Id>
19+
<Resource>api://{VITE_OFFICE_HOST}/outlook/{VITE_OFFICE_ID}</Resource>
20+
<Scopes>
21+
<Scope>openid</Scope>
22+
<Scope>profile</Scope>
23+
<Scope>User.Read</Scope>
24+
</Scopes>
25+
</WebApplicationInfo>
1726
</OfficeApp>

playground/nuxt/nuxt.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@ export default defineNuxtConfig({
2222
],
2323

2424
injectOfficeJS: ['/'],
25+
26+
defineENV: env => ({
27+
VITE_OFFICE_HOST: new URL(env.VITE_OFFICE_DOMAIN).host,
28+
}),
2529
},
2630
})

playground/vite/vite.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export default defineConfig({
1616

1717
/** Office.js will be injected to index.html page */
1818
injectOfficeJS: ['/outlook.html'],
19+
20+
defineENV: env => ({
21+
VITE_OFFICE_HOST: new URL(env.VITE_OFFICE_DOMAIN).host,
22+
}),
1923
}),
2024
],
2125

src/core/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
type EnvKey = `VITE_${string}` | `NUXT_${string}`
2+
13
export interface ManifestEntry {
24
src: string
35

@@ -18,6 +20,13 @@ export interface MSOfficeAddinConfig {
1820

1921
/** Routes where office.js will be injected */
2022
injectOfficeJS: (string | RegExp)[]
23+
24+
/**
25+
* Use this hook to define additional env variables.
26+
* Variables must be prefixed with `NUXT_` or `VITE_`
27+
* @param env ENV variables loaded from your environment
28+
*/
29+
defineENV?: (env: Readonly<ImportMetaEnv & object>) => Record<EnvKey, string>
2130
}
2231

2332
export interface OfficeAddinVirtualContext {

src/core/utils.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import fs from 'node:fs'
22
import { resolve } from 'node:path'
3-
import { loadEnv, normalizePath } from 'vite'
4-
import type { GeneratedManifest, ManifestEntry } from './types'
3+
import { loadEnv } from 'vite'
4+
import type { GeneratedManifest, MSOfficeAddinConfig, ManifestEntry } from './types'
55

66
export const OFFICE_JS_URL = 'https://appsforoffice.microsoft.com/lib/1.1/hosted/office.js'
77
export const OFFICE_JS_LOADER_SNIPPET = `<script src="${OFFICE_JS_URL}" async defer></script>`
88

9-
export function getPath(paths: string[]) {
10-
return normalizePath(resolve(...paths))
11-
}
12-
139
export function transformManifests(params: {
1410
inputs: ManifestEntry[]
1511
mode: string
1612
envDir: string
13+
defineENV?: MSOfficeAddinConfig['defineENV']
1714
}) {
1815
const entries = <GeneratedManifest[]>[]
1916
const env = loadEnv(params.mode, params.envDir)
2017

18+
// Configure env variables
19+
if (params.defineENV) {
20+
const data = params.defineENV({ ...env } as any)
21+
Object.assign(env, data)
22+
}
23+
2124
for (const { src, route } of params.inputs) {
2225
const absoluteSrc = resolve(src)
2326

src/module.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default defineNuxtModule<MSOfficeAddinConfig>({
1616
mode: vite.mode!,
1717
envDir: vite.envDir ?? process.cwd(),
1818
inputs: options.manifests,
19+
defineENV: options.defineENV,
1920
})
2021

2122
const contextImportKey = '#office-addin-content'

src/vite.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export default function MSOfficeAddin(params: MSOfficeAddinConfig): Plugin {
99
function generate() {
1010
return transformManifests({
1111
mode,
12-
inputs: params.manifests,
1312
envDir: viteConfig.envDir ?? process.cwd(),
13+
inputs: params.manifests,
14+
defineENV: params.defineENV,
1415
})
1516
}
1617

test/__snapshots__/basic.test.ts.snap

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ exports[`ssr > renders office manifest 1`] = `
99
<DefaultLocale>en-US</DefaultLocale>
1010
<DisplayName DefaultValue="Contoso App" />
1111
<Description DefaultValue="Office add-in for Contoso App"/>
12-
<SupportUrl DefaultValue="https://www.contoso.com/support" />
12+
<SupportUrl DefaultValue="https://contoso.com/support" />
1313
<AppDomains>
14-
<AppDomain>https://www.contoso.com</AppDomain>
14+
<AppDomain>https://contoso.com</AppDomain>
1515
</AppDomains>
1616
<DefaultSettings>
1717
<SourceLocation DefaultValue="https://www.contoso.com/search_app/Default.aspx" />
1818
</DefaultSettings>
1919
<Permissions>ReadWriteDocument</Permissions>
20+
<WebApplicationInfo>
21+
<Id>c6890c26-5bbb-40ed-a321-37f07909a2f0</Id>
22+
<Resource>api://contoso.com/outlook/c6890c26-5bbb-40ed-a321-37f07909a2f0</Resource>
23+
<Scopes>
24+
<Scope>openid</Scope>
25+
<Scope>profile</Scope>
26+
<Scope>User.Read</Scope>
27+
</Scopes>
28+
</WebApplicationInfo>
2029
</OfficeApp>"
2130
`;

0 commit comments

Comments
 (0)