Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 735edfb

Browse files
committed
fix(build): modify screenshot logic
1 parent 1bd7955 commit 735edfb

File tree

9 files changed

+48
-22
lines changed

9 files changed

+48
-22
lines changed

packages/fusuma/src/tasks/build.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ const loader = require('../cli/loader');
55
const getRemoteOriginUrl = require('../utils/getRemoteOriginUrl');
66
const { build: webpackBuild } = require('../webpack');
77
const deleteDir = require('../utils/deleteDir');
8+
const fileServer = require('../utils/fileServer');
89

910
async function createOgImage(outputDirPath) {
10-
const url = require('url');
1111
const puppeteer = require('puppeteer');
1212

13-
const browser = await puppeteer.launch();
13+
const port = 5445;
14+
const browser = await puppeteer.launch({
15+
chromeWebSecurity: false,
16+
args: ['--no-sandbox', '--disable-setuid-sandbox'],
17+
});
1418
const page = await browser.newPage();
15-
const outputPath = join(outputDirPath, 'thumbnail.png');
19+
const outputFilePath = join(outputDirPath, 'thumbnail.png');
1620

1721
// https://www.kapwing.com/resources/what-is-an-og-image-make-and-format-og-images-for-your-blog-or-webpage/
1822
await page.setViewport({
1923
width: 1200,
2024
height: 630,
2125
});
22-
await page.goto(url.pathToFileURL(join(outputDirPath, 'index.html')), {
23-
waitUntil: 'networkidle2',
26+
const app = await fileServer(outputDirPath, port);
27+
await page.goto(`http://localhost:${port}`, {
28+
waitUntil: ['load', 'networkidle2'],
2429
});
25-
await page.screenshot({ path: outputPath });
30+
await page.screenshot({ path: outputFilePath });
31+
await page.close();
2632
await browser.close();
33+
app.close();
2734

28-
return outputPath;
35+
return outputFilePath;
2936
}
3037

3138
async function build(config, isConsoleOutput = true) {
3239
const spinner = loader('Rendering components to HTML...').start();
33-
const { basePath, outputDir } = config.internal;
34-
const outputDirPath = join(basePath, outputDir);
40+
const { outputDirPath } = config.internal;
3541
const remoteOrigin = await getRemoteOriginUrl();
3642

3743
if (process.env.NODE_ENV === undefined) {

packages/fusuma/src/tasks/deploy.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict';
22

3-
const { join } = require('path');
43
const loader = require('../cli/loader');
54

65
async function deploy(config) {
76
const ghpages = require('gh-pages');
87

98
return new Promise((resolve, reject) => {
109
const spinner = loader('Publishing to gh-pages...').start();
11-
const { basePath, outputDir } = config.internal;
12-
const outputDirPath = join(basePath, outputDir);
10+
const { basePath, outputDirPath } = config.internal;
1311

1412
ghpages.publish(outputDirPath, (err) => {
1513
if (err) {

packages/fusuma/src/tasks/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ const deploy = require('./deploy');
99
const pdf = require('./pdf');
1010

1111
async function tasks({ type, options }) {
12-
const basePath = join(process.cwd(), options.dir || '');
12+
const basePath = process.cwd();
13+
const inputDirPath = join(basePath, options.inputDir || 'slides');
14+
const outputDirPath = join(basePath, options.outputDir || 'dist');
1315
let config = {};
1416

1517
try {
1618
config = await fusuma.read(basePath);
1719
} catch (e) {
1820
} finally {
1921
config = fusuma.combine(config, {
20-
internal: { ...options, basePath },
22+
internal: { ...options, basePath, inputDirPath, outputDirPath },
2123
});
2224
}
2325

packages/fusuma/src/tasks/pdf.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ async function pdf(config) {
1111
}
1212

1313
const port = 3455;
14-
const { basePath, inputDir, filename } = config.internal;
15-
const inputDirPath = join(basePath, inputDir);
14+
const { basePath, inputDirPath, filename } = config.internal;
1615
const output = join(basePath, filename);
1716
const spinner = loader('Exporting as PDF...').start();
1817

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const express = require('express');
4+
5+
async function fileServer(basePath, port = 5445) {
6+
return new Promise((resolve, reject) => {
7+
const app = express();
8+
9+
app.use(express.static(basePath));
10+
11+
const server = app.listen(port, (err) => {
12+
if (err) {
13+
return reject(err);
14+
}
15+
resolve(server);
16+
});
17+
});
18+
}
19+
20+
module.exports = fileServer;

packages/fusuma/src/webpack/outputBuildInfo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const humanSize = require('human-size');
66
function outputBuildInfo(res) {
77
const output = [];
88
const options = {
9-
border: getBorderCharacters('void')
9+
border: getBorderCharacters('void'),
1010
};
1111

1212
Object.entries(res.compilation.assets).forEach(([name, asset]) => {

packages/fusuma/src/webpack/webpack.config.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ module.exports = (
3131
const { sidebar, targetBlank, showIndex, isVertical, loop, code, chart, math } = slide;
3232
const { js: jsPath, css: cssPath, webpack: webpackPath } = fileExtends;
3333
const { ssr, useCache } = build;
34-
const { basePath, remoteOrigin, htmlBody = '', buildStage, port, outputDir } = internal;
35-
const outputPath = path.resolve(basePath, outputDir || /* for start task */ 'dist');
34+
const { basePath, remoteOrigin, htmlBody = '', buildStage, port, outputDirPath } = internal;
3635
const config = (() => {
3736
switch (type) {
3837
case 'production':
@@ -47,7 +46,7 @@ module.exports = (
4746
const common = {
4847
entry,
4948
output: {
50-
path: outputPath,
49+
path: outputDirPath,
5150
publicPath: '/',
5251
},
5352
resolveLoader: {

packages/fusuma/src/webpack/webpack.ssr.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function ssr({ clientBasePath }) {
99
devtool: false,
1010
output: {
1111
filename: 'entry.js',
12-
libraryTarget: 'commonjs'
13-
}
12+
libraryTarget: 'commonjs',
13+
},
1414
};
1515
}
1616

samples/intro/.fusumarc.yml

+2
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ slide:
1616
extends:
1717
js: index.js
1818
css: style.css
19+
build:
20+
useCache: false

0 commit comments

Comments
 (0)