-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
210 lines (172 loc) · 5.49 KB
/
gulpfile.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { createRequire } from "module"
const require = createRequire(import.meta.url);
import 'dotenv/config'
import path, {dirname} from 'path';
import React from 'react';
import ReactDomServer from 'react-dom/server';
import htmlmin from 'gulp-htmlmin';
import del from 'del';
import rename from 'gulp-rename';
import through2 from 'through2';
import gulp from 'gulp';
import {fileURLToPath} from "url";
import projectLoader from "./app/project-loader.js";
import {promisify} from "util";
import fs from "fs";
import clientRollupConfig from "./client-rollup-config.js";
import cleanCss from "gulp-clean-css";
import {rollup} from "rollup";
const os = require('os');
const parallel = require('concurrent-transform');
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const sass = require('sass');
const gulpSass = require('gulp-sass')(sass);
const Jimp = require("jimp");
const projectsLocation = process.env.PROJECTS_LOCATION;
const numberOfCpus = os.cpus().length;
const nodeModuleDir = path.join(__dirname, './node_modules');
const promiseMkDir = promisify(fs.mkdir);
const promiseCopyFile = promisify(fs.copyFile);
const promiseStream = (gulpStream) => new Promise((resolve, reject) => gulpStream.on('end', resolve).on('error', reject));
let outputDir = path.join(__dirname, './build');
const getOutputDir = (relativeDir) => path.join(outputDir, relativeDir || '');
const getInputDir = (relativeDir) => path.join(__dirname, relativeDir || '');
function jsxToHtml(options) {
return through2.obj(async function (file, enc, next) {
try {
let component = await import(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');
next(null, file);
} catch (e) {
next(e);
}
});
}
const cleanBuild = () => del(['build']);
const npmSassAliases = {};
/**
* Will look for .scss|sass files inside the node_modules folder
*/
function npmSassResolver(url, file, done) {
// check if the path was already found and cached
if(npmSassAliases[url]) {
return done({ file: npmSassAliases[url] });
}
// look for modules installed through npm
try {
const newPath = require.resolve(url);
npmSassAliases[url] = newPath; // cache this request
return done({ file: newPath });
} catch(e) {
// if your module could not be found, just return the original url
npmSassAliases[url] = url;
return done({ file: url });
}
}
function deSassify() {
return gulpSass(
{
importer: npmSassResolver
}).on('error', gulpSass.logError);
}
// copy slick carousel blobs
function collectSlickBlobs() {
return gulp
.src([`${nodeModuleDir}/slick-carousel/slick/**/*.{woff,tff,gif,jpg,png}`])
.pipe(gulp.dest(getOutputDir('public')));
}
// Bundle SASS
function transformSass() {
return gulp.src(getInputDir('app/index/index.scss'))
.pipe(deSassify())
.pipe(cleanCss())
.pipe(gulp.dest(getOutputDir('public')));
}
const buildCss = gulp.parallel(collectSlickBlobs, transformSass);
function copySvg() {
return gulp
.src('./app/index/*.svg')
.pipe(gulp.dest(getOutputDir('public')));
}
async function buildProjectImages() {
const projects = await projectLoader(projectsLocation);
const destDir = getOutputDir('public/imgs/projects');
await Promise.all(projects
.flatMap(p => [p.image?.url, ...p.examples.map(i => i.url)])
.filter(uri => uri)
.map(async uri => {
if (!uri || uri.startsWith("http://") || uri.startsWith("https://")) return;
const destination = path.join(destDir, path.relative(projectsLocation, uri));
if (path.extname(destination) === ".svg") {
const directory = path.dirname(destination);
console.log(`Making directory ${directory}.`);
await promiseMkDir(directory, { recursive: true })
await promiseCopyFile(uri, destination)
return;
}
const image = await Jimp.read(uri);
const resizedImage = image.resize(Jimp.AUTO, 300);
await resizedImage.write(destination);
}));
}
function buildClientJs() {
const destDir = getOutputDir('public/js');
return gulp.src(getInputDir('app/**/*.client.{js,jsx}'))
.pipe(parallel(
through2.obj(async (file, enc, next) => {
try {
const bundle = await rollup(Object.assign(
clientRollupConfig,
{
input: file.path,
}));
const {output} = await bundle.generate({format: 'iife'});
file.contents = Buffer.from(output[0].code);
next(null, file);
} catch (e) {
next(e);
}
}),
numberOfCpus))
.pipe(rename({
dirname: '',
extname: '.js'
}))
.pipe(gulp.dest(destDir));
}
async function buildServerHtml() {
const portfolios = await projectLoader(projectsLocation);
const imgDir = 'imgs/projects';
for (const portfolio of portfolios) {
const {image, examples} = portfolio;
if (image) {
image.url = path.join(imgDir, path.relative(projectsLocation, image.url));
}
for (const example of examples) {
example.url = path.join(imgDir, path.relative(projectsLocation, example.url));
}
}
await promiseStream(gulp
.src('./app/index/index.js')
.pipe(jsxToHtml( {projects: portfolios}))
.pipe(htmlmin())
.pipe(gulp.dest('./build/public')));
}
const build = gulp.series(
cleanBuild,
gulp.parallel(
buildServerHtml,
buildProjectImages,
buildCss,
copySvg,
buildClientJs,
),
)
// gulp.task('watch', ['build'], () => {
// gulp.watch('./app/**/*.{jsx,css,svg}', ['build-static']);
// });
export { build };