-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile-publish.js
162 lines (131 loc) · 3.97 KB
/
gulpfile-publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
process.env.NODE_ENV = "production";
import { createRequire } from "module"
const require = createRequire(import.meta.url);
import gulp from 'gulp';
import through2 from 'through2';
import React from 'react';
import ReactDomServer from 'react-dom/server';
import appConfig from "./app/app-config.cjs";
import htmlmin from 'gulp-htmlmin';
import del from 'del';
import { promisify } from 'util';
import fs from 'fs';
import projectLoader from './app/request-handlers/project-loader.js';
import { rollup } from 'rollup';
import rollupConfig from './rollup-config.js';
import path from "path";
import { include } from './app/gulpfile.js'
import * as vm from "vm";
// Register dynamic build tasks
const appBuild = include({ production: true });
const promiseReadFile = (filePath) => promisify(fs.readFile)(filePath, 'utf8');
const promiseStream = (gulpStream) => new Promise((resolve, reject) => gulpStream.on('end', resolve).on('error', reject));
// Static build tasks
function jsxToHtml(options) {
return through2.obj(function (file, enc, cb) {
require('node-jsx').install({extension: '.jsx'});
let component = {};
if (file.contents) {
const js = file.contents.toString();
const script = new vm.Script(`
((module, require) => {
${js}
return module.exports;
});
`);
component = script.runInThisContext()(require('node:module'), require);
}
if (!component) {
component = file.contents || require(file.path);
component = component.default || component;
}
const markup = '<!doctype html>' + ReactDomServer.renderToStaticMarkup(React.createElement(component, options));
file.contents = Buffer.from(markup);
file.path = file.path.replace(path.extname(file.path), '.html');
this.push(file);
cb();
});
}
const cleanBuild = () => del(['build', 'staging']);
function copyDynamicBuild() {
return gulp.src(['./app/public/**/*']).pipe(gulp.dest('./build/public'));
}
function bundleJs() {
return through2.obj(async function (file, enc, cb) {
try {
const bundle = await rollup(Object.assign(
rollupConfig,
{
input: file.path,
}));
const { output } = await bundle.generate({format: 'cjs'});
file.contents = Buffer.from(output[0].code);
file.path = file.path.replace(path.extname(file.path), '.cjs');
this.push(file);
cb();
} catch (e) {
cb(e);
}
});
}
function buildServerJs() {
return gulp.src('./app/app-release.js')
.pipe(bundleJs())
.pipe(gulp.dest('./build'));
}
async function buildStaticResume() {
const rawMarkdown = await promiseReadFile(appConfig.resumeLocation);
await promiseStream(gulp
.src('./app/views/resume/resume.js')
.pipe(bundleJs())
.pipe(jsxToHtml({resume: rawMarkdown}))
.pipe(htmlmin())
.pipe(gulp.dest('./build/public/html')));
}
async function buildStaticIndex() {
const rawMarkdown = await promiseReadFile(appConfig.bio.path);
await promiseStream(gulp
.src('./app/views/index/index.js')
.pipe(bundleJs())
.pipe(jsxToHtml({bio: rawMarkdown}))
.pipe(htmlmin())
.pipe(gulp.dest('./build/public/html')));
}
async function buildStaticProjects() {
const portfolios = await projectLoader();
await promiseStream(gulp
.src('./app/views/project/project-list.js')
.pipe(bundleJs())
.pipe(jsxToHtml({projects: portfolios}))
.pipe(htmlmin())
.pipe(gulp.dest('./build/public/html')));
}
const copyNodeProjectData = () => gulp.src(['./app/start-server.sh']).pipe(gulp.dest('./build'));
const buildStatic = gulp.series(
cleanBuild,
appBuild.build,
gulp.parallel(
appBuild.copyPublicFonts,
copyDynamicBuild,
copyNodeProjectData,
buildServerJs,
buildStaticIndex,
buildStaticResume,
buildStaticProjects,
)
);
const buildResume = gulp.series(
cleanBuild,
appBuild.buildResumePdf,
copyDynamicBuild,
buildStaticResume);
const buildPortfolio = gulp.series(
cleanBuild,
appBuild.buildProjectImages,
copyDynamicBuild,
buildStaticProjects);
export {
buildStatic,
buildResume,
buildPortfolio,
};