问题背景:我正在使用katex在页面上渲染一些数学运算。然后,我想为该页面的一部分创建PDF版本,因此我创建了一个HTML文档,其中包含要导出的部分,该部分内联所有CSS并将其传递给渲染器。渲染器无法访问节点资源,这就是内联所有内容的原因。除字体外,它运行完美。
我尝试了url-loader和bas64-inline-loader,但是生成的字体没有内联。我在调试器中检查了生成的CSS,但旧的URL仍然存在,没有字体的data-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/.*'
})
]
};
答案 0 :(得分:3)
最好的方法是使用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