mirror of
https://github.com/morten-olsen/morten-olsen.github.io.git
synced 2026-02-08 01:46:28 +01:00
feat: image generator
This commit is contained in:
43
config/plugins/withImageGenerator/index.js
Normal file
43
config/plugins/withImageGenerator/index.js
Normal file
@@ -0,0 +1,43 @@
|
||||
const path = require('path');
|
||||
const withLatex = (nextConfig = {}) => {
|
||||
return Object.assign({}, nextConfig, {
|
||||
webpack(config, options) {
|
||||
const {isServer, dev} = options;
|
||||
let outputPath = '';
|
||||
if (isServer && dev) {
|
||||
outputPath = "../";
|
||||
} else if (isServer) {
|
||||
outputPath = "../../";
|
||||
}
|
||||
config.module.rules.push({
|
||||
test: /\.png.yml$/,
|
||||
use: [{
|
||||
loader: 'file-loader',
|
||||
options: {
|
||||
|
||||
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath || ''}/_next/static/images/`,
|
||||
outputPath: `${outputPath}static/images/`,
|
||||
name: (name) => {
|
||||
const fileName = path.basename(name);
|
||||
const parts = fileName.split('.');
|
||||
parts.pop();
|
||||
const ext = parts.pop();
|
||||
|
||||
return `${parts.join('.')}.[hash].${ext}`;
|
||||
},
|
||||
esModule: nextConfig.esModule || false,
|
||||
},
|
||||
}, {
|
||||
loader: require.resolve('./webpack.js'),
|
||||
}],
|
||||
});
|
||||
if (typeof nextConfig.webpack === "function") {
|
||||
return nextConfig.webpack(config, options);
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = withLatex;
|
||||
16
config/plugins/withImageGenerator/webpack.js
Normal file
16
config/plugins/withImageGenerator/webpack.js
Normal file
@@ -0,0 +1,16 @@
|
||||
require('reflect-metadata');
|
||||
require('@babel/register')({
|
||||
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
|
||||
});
|
||||
const { generateImage } = require('../../../src/images');
|
||||
|
||||
module.exports = function (source) {
|
||||
var callback = this.async();
|
||||
const location = this.resourcePath;
|
||||
generateImage(source, location)
|
||||
.then((canvas) => {
|
||||
const buffer = canvas.toBuffer('image/png', {})
|
||||
callback(null, buffer);
|
||||
})
|
||||
.catch(callback);
|
||||
}
|
||||
@@ -13,7 +13,6 @@ const withLatex = (nextConfig = {}) => {
|
||||
use: [{
|
||||
loader: 'file-loader',
|
||||
options: {
|
||||
|
||||
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath || ''}/_next/static/images/`,
|
||||
outputPath: `${outputPath}static/images/`,
|
||||
name: "[name]-[hash].pdf",
|
||||
|
||||
Reference in New Issue
Block a user