rollup-plugin-serve 配置跨域

在Rollup配置中,通过引入express和cors中间件解决跨域问题。示例展示了如何在rollup-plugin-serve中添加before函数,设置Access-Control-Allow-*头信息,允许本地开发时的跨域请求。

如果您要在 `rollup-plugin-serve` 中配置跨域请求,需要在配置中添加中间件来解决。在这个例子中,我们将使用 `express` 中间件来配置跨域请求。以下是一个带有 `express` 中间件的 Rollup 配置示例:

```js

// rollup.config.js

import serve from 'rollup-plugin-serve';

import express from 'express';

import cors from 'cors';

const app = express();

app.use(cors());

app.use(express.static('public'));

export default {

input: 'src/main.js',

output: {

file: 'public/bundle.js',

format: 'cjs'

},

plugins: [

serve({

open: true,

contentBase: ['public'],

port: 3000,

before: function(app) {

app.use(function(req, res, next) {

res.header('Access-Control-Allow-Origin', '*');

res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');

res.header('Access-Control-Allow-Headers', 'Content-Type');

next();

})

}

})

]

};

```

在这里,我们添加了一个自定义的 `before` 函数,用于向 `express` 应用程序添加跨域请求中间件。通过添加中间件,我们可以解决跨域问题。在这个例子中,我们使用了 `cors` 中间件来解决跨域问题,同时指定跨域的方法和头部。

以上示例中,`before` 函数被传递给 `serve` 插件,用于在启动服务器之前将其添加到应用程序中。这样一来,我们就能够在本地开发过程中使用跨域请求。

总之,通过添加中间件到 `rollup-plugin-serve` 中,我们可以在本地开发过程中解决跨域请求问题。这样一来,我们可以在本地,自由的进行调试和开发。

import path from 'path'; import * as fs from 'fs'; import * as https from 'https'; import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; import eslint from 'vite-plugin-eslint'; import { visualizer } from 'rollup-plugin-visualizer'; import * as ssl from 'ssl-root-cas'; import { commonAssetsPrefix } from './src/constants/config-env'; const { ANALYZE } = process.env; const rootCas = ssl.create(); rootCas.addFile(path.resolve(__dirname, 'node_modules/@ffe/fcore-build-utils/.certs/cert.pem')); https.globalAgent.options.ca = rootCas; function getHttpsConfig() { return { cert: fs.readFileSync(path.resolve(__dirname, 'node_modules/@ffe/fcore-build-utils/.certs/cert.pem')), key: fs.readFileSync(path.resolve(__dirname, 'node_modules/@ffe/fcore-build-utils/.certs/key.pem')), }; } export default defineConfig(config => ({ base: config.mode === 'production' ? commonAssetsPrefix : '/', resolve: { alias: { '@': path.resolve(__dirname, 'src'), '@aurora/theme': '@aurora/theme-saas', }, }, css: { preprocessorOptions: { scss: { additionalData: '@import "./theme-saas/styles.scss";', }, }, }, plugins: [ vue(), eslint({ // 检查出“警告”级别的问题时,输出警告到控制台 emitWarning: true, // 检查出“错误”级别的问题时,不输出为 error(不抛出 error,不影响渲染,只输出 错误信息) emitError: false, // 检查出 warning 时,不让打包/启动失败 failOnWarning: false, }), ...(ANALYZE === 'true' ? [ visualizer({ open: false, gzipSize: true, brotliSize: true, filename: 'dist/stats.html', }), ] : []), ], build: { target: 'esnext', }, define: { VERSION_PLACEHOLDER: JSON.stringify('1.0.0'), __VUE_OPTIONS_API__: true, __VUE_PROD_DEVTOOLS__: false, 'process.env': Object.assign({}, process.env), }, server: { hmr: true, port: 4173, open: true, host: 'localhost.huawei.com', cors: true, proxy: { '/ffe': { target: 'https://kwesit.huawei.com', changeOrigin: true, rewrite: pathItem => pathItem.replace(/^\/ffe/, ''), }, }, https: getHttpsConfig(), }, })); 以上是我的vite设置,打包后部署完打开index。html白屏,无任何报错,帮忙看看可能是什么原因
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值