您可以使用postcss-inline-base64
这是一个基于此汇总启动器的工作示例:
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import path from 'path'
import postcss from 'rollup-plugin-postcss'
import base64Inliner from 'postcss-inline-base64'
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;
const __dirname = path.resolve();
const baseDir = path.join(__dirname, 'public')
export default {
input: 'src/main.js',
output: {
file: 'public/bundle.js',
format: 'iife', // immediately-invoked function expression — suitable for <script> tags
sourcemap: true
},
plugins: [
postcss({
extensions: ['.css'],
extract: path.resolve('public/styles.css'),
plugins: [base64Inliner({ baseDir })]
}),
...
]
};
然后我在 src 文件夹中创建了一个 css 文件:
body {
background: url('b64---./images/test.png---');
}
在 index.js 中:
import "./styles.css";
在 index.html 中
....
<title>rollup-starter-app</title>
<link href="styles.css" rel="stylesheet" />
....
我能够得到: