问题背景:我正在使用katex在页面上呈现一些数学。然后,我想创建该页面的一部分的PDF版本,因此我创建了一个HTML文档,其中包含内联所有CSS的要导出的部分,并将其传递给渲染器。渲染器不能访问节点资源,这就是为什么一切都是内联的原因。它工作得很好,除了字体。
我尝试了url-loader和bas64-inline-loader,但是生成的字体不是内联的。我在调试器中检查了生成的CSS,旧的URL仍然存在,没有字体的数据URL。
这是我当前的webpack.config.js:
const path = require('path');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
entry: {
"editor": './src/editor.js',
"editor.worker": 'monaco-editor/esm/vs/editor/editor.worker.js',
"json.worker": 'monaco-editor/esm/vs/language/json/json.worker',
"css.worker": 'monaco-editor/esm/vs/language/css/css.worker',
"html.worker": 'monaco-editor/esm/vs/language/html/html.worker',
"ts.worker": 'monaco-editor/esm/vs/language/typescript/ts.worker',
},
output: {
globalObject: 'self',
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.(woff|woff2|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: ['url-loader']
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
filename: 'editor_text.html',
template: 'src/editor_text.html'
}),
new HtmlWebpackPlugin({
filename: 'editor_markdown.html',
template: 'src/editor_markdown.html',
inlineSource: '/katex/.*'
})
]
};发布于 2019-11-08 22:18:50
最好的方法是使用postcss-cli和postcss-inline-base64
webpack:
{
test: /\.(css|sass|scss)$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 2,
sourceMap: true
},
},
{
loader: 'postcss-loader', // important
options: {
sourceMap: true,
config: {
path: './config/',
},
},
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
}, {
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: [{
loader: 'file-loader',
}]
},创建配置文件夹宽度postcss.config.js
module.exports = {
plugins: {
'postcss-inline-base64': {
baseDir: './sources/'
},
},
};baseDir是字体的路径。在scss文件中,我以这种方式添加了一个字体:
@font-face {
font-family: 'Lato-Light';
src: url('b64---../fonts/Lato-Light.ttf---') format('truetype');
font-weight: normal;
font-style: normal;
}作为工作的结果,我们可以很好地将字体转换为base64 @font-face{font-family:Lato-Light;src:url("data:font/ttf;charset=utf-8;base64,...
更新:我准备了一个小示例postcss-inline-base64
发布于 2022-02-23 07:50:06
有一个名为base64-inline-loader的npm包,对于我的问题,这似乎是一个很好的选择。
在我的vue项目中,您可以参考我的配置。
首先,添加包
yarn add -D base64-inline-loader然后,处理“vue.config.js”
chainWebpack(config) {
const fontsRule = config.module.rule('fonts')
fontsRule.uses.clear()
config.module
.rule('fonts')
.test(/\.(ttf|otf|eot|woff|woff2)$/)
.use('base64-inline-loader')
.loader('base64-inline-loader')
.tap((options) => {
// modify the options...
return options
})
.end()
}经过测试和验证

https://stackoverflow.com/questions/58715958
复制相似问题