Axe*_*xel 10 javascript css data-uri webpack
问题背景:我正在使用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/.*'
})
]
};
Run Code Online (Sandbox Code Playgroud)
最好的方法是使用 postcss-cli 和 postcss-inline-base64
网页包:
{
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',
}]
},
Run Code Online (Sandbox Code Playgroud)
创建配置文件夹宽度 postcss.config.js
module.exports = {
plugins: {
'postcss-inline-base64': {
baseDir: './sources/'
},
},
};
Run Code Online (Sandbox Code Playgroud)
baseDir 是字体的路径。在 scss 文件中,我以这种方式添加字体:
@font-face {
font-family: 'Lato-Light';
src: url('b64---../fonts/Lato-Light.ttf---') format('truetype');
font-weight: normal;
font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)
通过这项工作,我们得到了一个很好的将字体转换为 Base64 的结果@font-face{font-family:Lato-Light;src:url("data:font/ttf;charset=utf-8;base64,...
更新: 我准备了一个小例子postcss-inline-base64
| 归档时间: |
|
| 查看次数: |
229 次 |
| 最近记录: |