0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00
penpot/frontend/gulpfile.js

382 lines
9.4 KiB
JavaScript
Raw Normal View History

2020-01-11 10:28:54 -05:00
const fs = require("fs");
const l = require("lodash");
2020-11-20 18:14:59 -05:00
const path = require("path");
const gulp = require("gulp");
2020-11-20 18:14:59 -05:00
const gulpConcat = require("gulp-concat");
const gulpGzip = require("gulp-gzip");
const gulpMustache = require("gulp-mustache");
2020-11-20 18:14:59 -05:00
const gulpPostcss = require("gulp-postcss");
const gulpRename = require("gulp-rename");
2021-07-13 06:24:42 -05:00
const gulpSass = require("gulp-sass")(require("sass"));
const svgSprite = require("gulp-svg-sprite");
2023-11-24 06:24:47 -05:00
const rename = require("gulp-rename");
2023-11-24 06:24:47 -05:00
const autoprefixer = require("autoprefixer");
const modules = require("postcss-modules");
2020-11-20 18:14:59 -05:00
const clean = require("postcss-clean");
2023-11-24 06:24:47 -05:00
const { mkdirp } = require("mkdirp");
const { rimraf } = require("rimraf");
const sass = require("sass");
const gettext = require("gettext-parser");
const marked = require("marked");
2023-11-07 11:29:17 -05:00
const cache = require("gulp-cached");
2016-06-11 13:43:57 -05:00
const mapStream = require("map-stream");
2016-06-11 13:43:57 -05:00
const paths = {};
paths.resources = "./resources/";
2016-02-21 09:42:35 -05:00
paths.output = "./resources/public/";
paths.dist = "./target/dist/";
2023-11-27 11:41:25 -05:00
const touchSourceOnStyleChange = false;
2023-11-10 10:05:47 -05:00
/***********************************************
* Marked Extensions
***********************************************/
const renderer = {
link(href, title, text) {
2021-11-11 06:21:21 -05:00
return `<a href="${href}" target="_blank">${text}</a>`;
2023-11-24 06:24:47 -05:00
},
};
2023-11-24 06:24:47 -05:00
marked.use({ renderer });
2015-06-18 12:35:50 -05:00
2016-06-11 13:43:57 -05:00
/***********************************************
* Helpers
2016-06-11 13:43:57 -05:00
***********************************************/
// Templates
2016-04-03 05:19:11 -05:00
2020-01-11 10:28:54 -05:00
function readLocales() {
2023-11-24 06:24:47 -05:00
const langs = [
"ar",
"ca",
"de",
"el",
"en",
"eu",
"it",
"es",
"fa",
"fr",
"he",
"nb_NO",
"pl",
"pt_BR",
"ro",
"id",
"ru",
"tr",
"zh_CN",
"zh_Hant",
"hr",
"gl",
"pt_PT",
"cs",
"fo",
"ko",
"lv",
"nl",
// this happens when file does not matches correct
// iso code for the language.
["ja_jp", "jpn_JP"],
// ["fi", "fin_FI"],
["uk", "ukr_UA"],
];
const result = {};
for (let lang of langs) {
let filename = `${lang}.po`;
if (l.isArray(lang)) {
filename = `${lang[1]}.po`;
2023-11-24 06:24:47 -05:00
lang = lang[0];
}
2023-11-24 06:24:47 -05:00
const content = fs.readFileSync(`./translations/${filename}`, { encoding: "utf-8" });
lang = lang.toLowerCase();
const data = gettext.po.parse(content, "utf-8");
const trdata = data.translations[""];
for (let key of Object.keys(trdata)) {
if (key === "") continue;
const comments = trdata[key].comments || {};
if (l.isNil(result[key])) {
result[key] = {};
}
const isMarkdown = l.includes(comments.flag, "markdown");
const msgs = trdata[key].msgstr;
if (msgs.length === 1) {
let message = msgs[0];
if (isMarkdown) {
message = marked.parseInline(message);
}
result[key][lang] = message;
} else {
result[key][lang] = msgs.map((item) => {
if (isMarkdown) {
return marked.parseInline(item);
} else {
return item;
}
});
}
// if (key === "modals.delete-font.title") {
// console.dir(trdata[key], {depth:10});
// console.dir(result[key], {depth:10});
// }
2020-01-11 10:28:54 -05:00
}
}
return JSON.stringify(result);
}
function readManifest() {
try {
const path = __dirname + "/resources/public/js/manifest.json";
2023-11-24 06:24:47 -05:00
const content = JSON.parse(fs.readFileSync(path, { encoding: "utf8" }));
const index = {
2023-11-24 06:24:47 -05:00
config: "js/config.js?ts=" + Date.now(),
polyfills: "js/polyfills.js?ts=" + Date.now(),
};
for (let item of content) {
index[item.name] = "js/" + item["output-name"];
2023-11-24 06:24:47 -05:00
}
return index;
} catch (e) {
console.error("Error on reading manifest, using default.");
return {
2023-11-24 06:24:47 -05:00
config: "js/config.js",
polyfills: "js/polyfills.js",
main: "js/main.js",
shared: "js/shared.js",
worker: "js/worker.js",
rasterizer: "js/rasterizer.js",
};
}
}
function touch() {
2023-11-24 06:24:47 -05:00
return mapStream(function (file, cb) {
if (file.isNull()) {
return cb(null, file);
}
// Update file modification and access time
return fs.utimes(file.path, new Date(), new Date(), () => {
2023-11-24 06:24:47 -05:00
cb(null, file);
});
});
}
function templatePipeline(options) {
2023-11-24 06:24:47 -05:00
return function () {
const input = options.input;
const output = options.output;
2022-03-22 02:12:31 -05:00
const name = options.name;
const ts = Math.floor(new Date());
const th = process.env.APP_THEME || "default";
const themes = ["default"];
2020-01-11 10:28:54 -05:00
const locales = readLocales();
const manifest = readManifest();
const tmpl = gulpMustache({
2020-01-11 10:28:54 -05:00
ts: ts,
// th: th,
manifest: manifest,
translations: JSON.stringify(locales),
2020-04-08 03:57:29 -05:00
themes: JSON.stringify(themes),
});
2016-06-11 13:43:57 -05:00
2023-11-24 06:24:47 -05:00
return gulp.src(input).pipe(tmpl).pipe(gulpRename(name)).pipe(gulp.dest(output)).pipe(touch());
};
}
/***********************************************
* Generic
***********************************************/
gulpSass.compiler = sass;
2020-04-08 03:57:29 -05:00
2023-11-24 06:24:47 -05:00
gulp.task("scss:modules", function () {
return gulp
.src(["src/**/**.scss"])
2023-11-07 11:29:17 -05:00
.pipe(cache("sass"))
2023-11-24 06:24:47 -05:00
.pipe(
gulpSass
.sync({ includePaths: ["./resources/styles/common/", "./resources/styles/"] })
.on("error", gulpSass.logError),
)
2023-11-07 11:29:17 -05:00
.pipe(cache("modules"))
2023-11-24 06:24:47 -05:00
.pipe(
gulpPostcss([
modules({
generateScopedName: "[folder]_[name]_[local]_[hash:base64:5]",
}),
autoprefixer(),
]),
)
.pipe(gulp.dest(paths.output + "css/"));
});
2023-11-24 06:24:47 -05:00
gulp.task("scss:main", function () {
return gulp
.src(paths.resources + "styles/main-default.scss")
2023-11-27 08:31:05 -05:00
.pipe(gulpSass.sync({
includePaths: [
"./node_modules/animate.css"
]
}))
2023-11-24 06:24:47 -05:00
.pipe(gulpPostcss([autoprefixer]))
.pipe(gulp.dest(paths.output + "css/"));
});
2023-11-24 06:24:47 -05:00
gulp.task("scss:concat", function () {
return gulp
.src([paths.output + "css/main-default.css", paths.output + "css/app/**/*.css"])
.pipe(gulpConcat("main.tmp.css"), { rebaseUrls: false })
.pipe(gulp.dest(paths.output + ".tmp/"));
});
gulp.task("scss:touch", function () {
return gulp
.src([paths.output + ".tmp/main.tmp.css"])
.pipe(rename("main.css"))
.pipe(gulp.dest(paths.output + "css/"))
.pipe(touch());
});
2023-11-24 06:24:47 -05:00
gulp.task("scss", gulp.series("scss:main", "scss:modules", "scss:concat", "scss:touch"));
2023-11-24 06:24:47 -05:00
gulp.task("svg:sprite:icons", function () {
return gulp
.src(paths.resources + "images/icons/*.svg")
.pipe(gulpRename({ prefix: "icon-" }))
.pipe(svgSprite({ mode: { symbol: { inline: true, sprite: "icons.svg" } } }))
.pipe(gulp.dest(paths.output + "images/sprites/"));
});
2023-11-24 06:24:47 -05:00
gulp.task("svg:sprite:cursors", function () {
return gulp
.src(paths.resources + "images/cursors/*.svg")
.pipe(gulpRename({ prefix: "cursor-" }))
.pipe(svgSprite({ mode: { symbol: { inline: true, sprite: "cursors.svg" } } }))
.pipe(gulp.dest(paths.output + "images/sprites/"));
});
2023-11-24 06:24:47 -05:00
gulp.task(
"template:main",
templatePipeline({
name: "index.html",
input: paths.resources + "templates/index.mustache",
output: paths.output,
}),
);
gulp.task(
"template:storybook",
templatePipeline({
name: "preview-body.html",
input: paths.resources + "templates/preview-body.mustache",
output: "./.storybook/",
}),
);
2023-11-24 06:24:47 -05:00
gulp.task(
"template:render",
templatePipeline({
name: "render.html",
input: paths.resources + "templates/render.mustache",
output: paths.output,
}),
);
gulp.task(
"template:rasterizer",
templatePipeline({
name: "rasterizer.html",
input: paths.resources + "templates/rasterizer.mustache",
output: paths.output,
}),
);
gulp.task(
"templates",
gulp.series(
"svg:sprite:icons",
"svg:sprite:cursors",
"template:main",
"template:storybook",
2023-11-24 06:24:47 -05:00
"template:render",
"template:rasterizer",
),
);
gulp.task("polyfills", function () {
return gulp
.src(paths.resources + "polyfills/*.js")
2020-11-20 18:14:59 -05:00
.pipe(gulpConcat("polyfills.js"))
.pipe(gulp.dest(paths.output + "js/"));
});
2023-11-24 06:24:47 -05:00
gulp.task("copy:assets:images", function () {
return gulp.src(paths.resources + "images/**/*").pipe(gulp.dest(paths.output + "images/"));
});
2016-06-11 13:43:57 -05:00
2023-11-24 06:24:47 -05:00
gulp.task("copy:assets:fonts", function () {
return gulp.src(paths.resources + "fonts/**/*").pipe(gulp.dest(paths.output + "fonts/"));
});
2016-06-11 13:43:57 -05:00
2020-04-18 13:34:34 -05:00
gulp.task("copy:assets", gulp.parallel("copy:assets:images", "copy:assets:fonts"));
2023-11-24 06:24:47 -05:00
gulp.task("dev:dirs", async function (next) {
await mkdirp("./resources/public/css/");
await mkdirp("./resources/public/js/");
next();
});
2023-11-24 06:24:47 -05:00
gulp.task("watch:main", function () {
2023-11-10 10:05:47 -05:00
const watchTask = gulp.watch("src/**/**.scss", gulp.series("scss"));
if (touchSourceOnStyleChange) {
2023-11-24 06:24:47 -05:00
watchTask.on("change", function (path) {
// Replace ".scss" for ".cljs" to refresh the file
2023-11-10 10:05:47 -05:00
gulp.src(path.replace(".scss", ".cljs")).pipe(touch());
});
2023-11-10 10:05:47 -05:00
}
gulp.watch(paths.resources + "styles/**/**.scss", gulp.series("scss"));
gulp.watch(paths.resources + "images/**/*", gulp.series("copy:assets:images"));
2023-11-24 06:24:47 -05:00
gulp.watch([paths.resources + "templates/*.mustache", "translations/*.po"], gulp.series("templates"));
});
2023-11-24 06:24:47 -05:00
gulp.task("clean:output", function (next) {
rimraf(paths.output).finally(next);
});
2023-11-24 06:24:47 -05:00
gulp.task("clean:dist", function (next) {
rimraf(paths.dist).finally(next);
});
gulp.task("build:styles", gulp.parallel("scss"));
gulp.task("build:assets", gulp.parallel("polyfills", "templates", "copy:assets"));
gulp.task("watch", gulp.series("dev:dirs", "build:styles", "build:assets", "watch:main"));
2023-11-24 06:24:47 -05:00
gulp.task("build:copy", function () {
return gulp.src(paths.output + "**/*").pipe(gulp.dest(paths.dist));
2016-02-21 11:01:27 -05:00
});