msoffice-addin
Version:
Microsoft office addin module for Nuxt abd vite
82 lines (76 loc) • 2.14 kB
JavaScript
;
const process = require('node:process');
const index = require('./shared/msoffice-addin.68b5cf3d.cjs');
require('node:fs');
require('node:path');
require('vite');
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
const process__default = /*#__PURE__*/_interopDefaultCompat(process);
function MSOfficeAddin(params) {
let mode;
let viteConfig;
function generate() {
return index.transformManifests({
mode,
envDir: viteConfig.envDir ?? process__default.cwd(),
inputs: params.manifests,
defineENV: params.defineENV
});
}
function getManifest(route) {
const outputs = generate();
return outputs.find((entry) => entry.route === route);
}
return {
name: "msoffice-addin-vite",
enforce: "post",
async config(config, env) {
config.server = {
...config.server,
headers: {
"Access-Control-Allow-Origin": "*"
}
};
mode = env.mode;
return viteConfig = config;
},
transformIndexHtml(html, ctx) {
const path = ctx.path;
const matches = params.injectOfficeJS.some(
(entry) => entry instanceof RegExp ? entry.test(path) : entry === path
);
if (!matches)
return html;
return html.replace(
/<\/head>/,
`${index.OFFICE_JS_LOADER_SNIPPET}
</head>`
);
},
configureServer(server) {
const paths = params.manifests.flatMap((i) => i.route);
server.middlewares.use((req, res, next) => {
const { url } = req;
if (url && paths.includes(url)) {
const manifest = getManifest(url);
if (manifest) {
res.setHeader("content-type", "text/xml");
return res.end(manifest.content);
}
}
next();
});
},
generateBundle() {
const manifests = generate();
for (const entry of manifests) {
this.emitFile({
type: "asset",
fileName: entry.route.slice(1),
source: entry.content
});
}
}
};
}
module.exports = MSOfficeAddin;