2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-08-05 22:29:16 +05:30
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 22:08:55 +05:30
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-09-15 12:44:20 +05:30
|
|
|
import cheerio from "cheerio";
|
|
|
|
import fsRoot from "fs";
|
|
|
|
const fs = fsRoot.promises;
|
|
|
|
import path from "path";
|
|
|
|
import rollup from 'rollup';
|
|
|
|
import postcss from "postcss";
|
|
|
|
import postcssImport from "postcss-import";
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
2020-08-05 22:04:41 +05:30
|
|
|
// needed for legacy bundle
|
|
|
|
import babel from '@rollup/plugin-babel';
|
|
|
|
// needed to find the polyfill modules in the main-legacy.js bundle
|
|
|
|
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
|
|
// needed because some of the polyfills are written as commonjs modules
|
|
|
|
import commonjs from '@rollup/plugin-commonjs';
|
2020-08-07 20:20:18 +05:30
|
|
|
// multi-entry plugin so we can add polyfill file to main
|
|
|
|
import multi from '@rollup/plugin-multi-entry';
|
2019-09-15 12:44:20 +05:30
|
|
|
|
2020-08-07 22:45:53 +05:30
|
|
|
import cssvariables from "postcss-css-variables";
|
|
|
|
import flexbugsFixes from "postcss-flexbugs-fixes";
|
|
|
|
|
2020-08-05 22:04:41 +05:30
|
|
|
const PROJECT_ID = "hydrogen";
|
|
|
|
const PROJECT_SHORT_NAME = "Hydrogen";
|
|
|
|
const PROJECT_NAME = "Hydrogen Chat";
|
2019-09-15 12:44:20 +05:30
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
2020-08-12 20:07:55 +05:30
|
|
|
const cssDir = path.join(projectDir, "src/ui/web/css/");
|
2019-09-15 12:44:20 +05:30
|
|
|
const targetDir = path.join(projectDir, "target");
|
|
|
|
|
2020-08-05 22:04:41 +05:30
|
|
|
const {debug, noOffline, legacy} = process.argv.reduce((params, param) => {
|
|
|
|
if (param.startsWith("--")) {
|
|
|
|
params[param.substr(2)] = true;
|
|
|
|
}
|
|
|
|
return params;
|
|
|
|
}, {
|
|
|
|
debug: false,
|
|
|
|
noOffline: false,
|
|
|
|
legacy: false
|
|
|
|
});
|
|
|
|
const offline = !noOffline;
|
2019-09-15 15:52:43 +05:30
|
|
|
|
2019-09-15 12:44:20 +05:30
|
|
|
async function build() {
|
2019-09-15 15:52:43 +05:30
|
|
|
// get version number
|
|
|
|
const version = JSON.parse(await fs.readFile(path.join(projectDir, "package.json"), "utf8")).version;
|
2019-09-15 12:44:20 +05:30
|
|
|
// clear target dir
|
|
|
|
await removeDirIfExists(targetDir);
|
|
|
|
await fs.mkdir(targetDir);
|
2020-08-05 22:04:41 +05:30
|
|
|
let bundleName = `${PROJECT_ID}.js`;
|
|
|
|
if (legacy) {
|
|
|
|
bundleName = `${PROJECT_ID}-legacy.js`;
|
|
|
|
}
|
2020-08-12 20:05:48 +05:30
|
|
|
|
|
|
|
|
|
|
|
const devHtml = await fs.readFile(path.join(projectDir, "index.html"), "utf8");
|
|
|
|
const doc = cheerio.load(devHtml);
|
|
|
|
const themes = [];
|
|
|
|
findThemes(doc, themeName => {
|
|
|
|
themes.push(themeName);
|
|
|
|
});
|
|
|
|
|
2020-08-12 20:07:55 +05:30
|
|
|
// also creates the directories where the theme css bundles are placed in,
|
|
|
|
// so do it first
|
|
|
|
const themeAssets = await copyThemeAssets(themes, legacy);
|
|
|
|
|
2020-08-12 20:05:48 +05:30
|
|
|
await buildHtml(doc, version, bundleName);
|
2020-08-05 22:04:41 +05:30
|
|
|
if (legacy) {
|
|
|
|
await buildJsLegacy(bundleName);
|
|
|
|
} else {
|
|
|
|
await buildJs(bundleName);
|
|
|
|
}
|
2020-08-12 20:09:35 +05:30
|
|
|
await buildCssBundles(legacy ? buildCssLegacy : buildCss, themes);
|
2019-09-28 13:15:01 +05:30
|
|
|
if (offline) {
|
2020-08-12 20:10:28 +05:30
|
|
|
await buildOffline(version, bundleName, themeAssets);
|
2019-09-28 13:15:01 +05:30
|
|
|
}
|
2019-09-15 18:02:12 +05:30
|
|
|
|
2020-08-05 22:04:41 +05:30
|
|
|
console.log(`built ${PROJECT_ID}${legacy ? " legacy" : ""} ${version} successfully`);
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2020-08-12 20:05:48 +05:30
|
|
|
async function findThemes(doc, callback) {
|
|
|
|
doc("link[rel~=stylesheet][title]").each((i, el) => {
|
|
|
|
const theme = doc(el);
|
|
|
|
const href = theme.attr("href");
|
|
|
|
const themesPrefix = "/themes/";
|
|
|
|
const prefixIdx = href.indexOf(themesPrefix);
|
|
|
|
if (prefixIdx !== -1) {
|
|
|
|
const themeNameStart = prefixIdx + themesPrefix.length;
|
|
|
|
const themeNameEnd = href.indexOf("/", themeNameStart);
|
|
|
|
const themeName = href.substr(themeNameStart, themeNameEnd - themeNameStart);
|
|
|
|
callback(themeName, theme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:07:55 +05:30
|
|
|
async function copyThemeAssets(themes, legacy) {
|
|
|
|
const assets = [];
|
|
|
|
// create theme directories and copy assets
|
|
|
|
await fs.mkdir(path.join(targetDir, "themes"));
|
|
|
|
for (const theme of themes) {
|
|
|
|
assets.push(`themes/${theme}/bundle.css`);
|
|
|
|
const themeDstFolder = path.join(targetDir, `themes/${theme}`);
|
|
|
|
await fs.mkdir(themeDstFolder);
|
|
|
|
const themeSrcFolder = path.join(cssDir, `themes/${theme}`);
|
|
|
|
await copyFolder(themeSrcFolder, themeDstFolder, file => {
|
|
|
|
const isUnneededFont = legacy ? file.endsWith(".woff2") : file.endsWith(".woff");
|
|
|
|
if (!file.endsWith(".css") && !isUnneededFont) {
|
|
|
|
assets.push(file.substr(cssDir.length));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return assets;
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:05:48 +05:30
|
|
|
async function buildHtml(doc, version, bundleName) {
|
2019-09-15 12:44:20 +05:30
|
|
|
// transform html file
|
2020-08-12 20:05:48 +05:30
|
|
|
// change path to main.css to css bundle
|
|
|
|
doc("link[rel=stylesheet]:not([title])").attr("href", `${PROJECT_ID}.css`);
|
|
|
|
// change paths to all theme stylesheets
|
|
|
|
findThemes(doc, (themeName, theme) => {
|
|
|
|
theme.attr("href", `themes/${themeName}/bundle.css`);
|
|
|
|
});
|
2019-09-15 18:02:12 +05:30
|
|
|
doc("script#main").replaceWith(
|
2020-08-05 22:04:41 +05:30
|
|
|
`<script type="text/javascript" src="${bundleName}"></script>` +
|
2020-08-07 20:20:18 +05:30
|
|
|
`<script type="text/javascript">${PROJECT_ID}Bundle.main(document.body);</script>`);
|
2019-10-12 23:53:37 +05:30
|
|
|
removeOrEnableScript(doc("script#service-worker"), offline);
|
2020-03-24 03:16:31 +05:30
|
|
|
|
|
|
|
const versionScript = doc("script#version");
|
|
|
|
versionScript.attr("type", "text/javascript");
|
|
|
|
let vSource = versionScript.contents().text();
|
|
|
|
vSource = vSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
|
|
|
versionScript.text(vSource);
|
|
|
|
|
2019-09-28 13:15:01 +05:30
|
|
|
if (offline) {
|
|
|
|
doc("html").attr("manifest", "manifest.appcache");
|
|
|
|
doc("head").append(`<link rel="manifest" href="manifest.json">`);
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2020-08-05 22:04:41 +05:30
|
|
|
async function buildJs(bundleName) {
|
2019-09-15 12:44:20 +05:30
|
|
|
// create js bundle
|
2020-08-07 13:38:08 +05:30
|
|
|
const bundle = await rollup.rollup({input: 'src/main.js'});
|
|
|
|
await bundle.write({
|
|
|
|
file: path.join(targetDir, bundleName),
|
|
|
|
format: 'iife',
|
2020-08-07 20:20:18 +05:30
|
|
|
name: `${PROJECT_ID}Bundle`
|
2020-08-07 13:38:08 +05:30
|
|
|
});
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2020-08-05 22:04:41 +05:30
|
|
|
async function buildJsLegacy(bundleName) {
|
|
|
|
// compile down to whatever IE 11 needs
|
|
|
|
const babelPlugin = babel.babel({
|
|
|
|
babelHelpers: 'bundled',
|
2020-08-07 13:38:31 +05:30
|
|
|
exclude: 'node_modules/**',
|
2020-08-05 22:04:41 +05:30
|
|
|
presets: [
|
|
|
|
[
|
|
|
|
"@babel/preset-env",
|
|
|
|
{
|
|
|
|
useBuiltIns: "entry",
|
|
|
|
corejs: "3",
|
|
|
|
targets: "IE 11"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
// create js bundle
|
|
|
|
const rollupConfig = {
|
2020-08-07 20:20:18 +05:30
|
|
|
input: ['src/legacy-polyfill.js', 'src/main.js'],
|
|
|
|
plugins: [multi(), commonjs(), nodeResolve(), babelPlugin]
|
2020-08-05 22:04:41 +05:30
|
|
|
};
|
|
|
|
const bundle = await rollup.rollup(rollupConfig);
|
2020-08-07 13:38:08 +05:30
|
|
|
await bundle.write({
|
|
|
|
file: path.join(targetDir, bundleName),
|
|
|
|
format: 'iife',
|
2020-08-07 20:20:18 +05:30
|
|
|
name: `${PROJECT_ID}Bundle`
|
2020-08-07 13:38:08 +05:30
|
|
|
});
|
2020-08-05 22:04:41 +05:30
|
|
|
}
|
|
|
|
|
2020-08-12 20:10:28 +05:30
|
|
|
async function buildOffline(version, bundleName, themeAssets) {
|
|
|
|
const {offlineAssets, cacheAssets} = themeAssets.reduce((result, asset) => {
|
|
|
|
if (asset.endsWith(".css")) {
|
|
|
|
result.offlineAssets.push(asset);
|
|
|
|
} else {
|
|
|
|
result.cacheAssets.push(asset);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, {offlineAssets: [], cacheAssets: []});
|
2019-09-15 18:02:12 +05:30
|
|
|
// write offline availability
|
2020-08-12 20:10:28 +05:30
|
|
|
const offlineFiles = [bundleName, `${PROJECT_ID}.css`, "index.html", "icon-192.png"].concat(offlineAssets);
|
2019-09-15 18:02:12 +05:30
|
|
|
|
|
|
|
// write appcache manifest
|
|
|
|
const manifestLines = [
|
|
|
|
`CACHE MANIFEST`,
|
|
|
|
`# v${version}`,
|
|
|
|
`NETWORK`,
|
|
|
|
`"*"`,
|
|
|
|
`CACHE`,
|
|
|
|
];
|
|
|
|
manifestLines.push(...offlineFiles);
|
|
|
|
const manifest = manifestLines.join("\n") + "\n";
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.appcache"), manifest, "utf8");
|
|
|
|
// write service worker
|
|
|
|
let swSource = await fs.readFile(path.join(projectDir, "src/service-worker.template.js"), "utf8");
|
|
|
|
swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
2020-08-12 20:10:28 +05:30
|
|
|
swSource = swSource.replace(`"%%OFFLINE_FILES%%"`, JSON.stringify(offlineFiles));
|
|
|
|
swSource = swSource.replace(`"%%CACHE_FILES%%"`, JSON.stringify(cacheAssets));
|
2019-09-15 18:02:12 +05:30
|
|
|
await fs.writeFile(path.join(targetDir, "sw.js"), swSource, "utf8");
|
|
|
|
// write web manifest
|
|
|
|
const webManifest = {
|
2020-08-05 22:04:41 +05:30
|
|
|
name:PROJECT_NAME,
|
|
|
|
short_name: PROJECT_SHORT_NAME,
|
2020-03-14 03:24:11 +05:30
|
|
|
display: "fullscreen",
|
2019-09-15 18:02:12 +05:30
|
|
|
start_url: "index.html",
|
|
|
|
icons: [{"src": "icon-192.png", "sizes": "192x192", "type": "image/png"}],
|
|
|
|
};
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.json"), JSON.stringify(webManifest), "utf8");
|
|
|
|
// copy icon
|
|
|
|
let icon = await fs.readFile(path.join(projectDir, "icon.png"));
|
|
|
|
await fs.writeFile(path.join(targetDir, "icon-192.png"), icon);
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:09:35 +05:30
|
|
|
async function buildCssBundles(buildFn, themes) {
|
|
|
|
const cssMainFile = path.join(cssDir, "main.css");
|
|
|
|
await buildFn(cssMainFile, path.join(targetDir, `${PROJECT_ID}.css`));
|
|
|
|
for (const theme of themes) {
|
|
|
|
await buildFn(
|
|
|
|
path.join(cssDir, `themes/${theme}/theme.css`),
|
|
|
|
path.join(targetDir, `themes/${theme}/bundle.css`)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildCss(entryPath, bundlePath) {
|
|
|
|
const preCss = await fs.readFile(entryPath, "utf8");
|
2019-09-15 12:44:20 +05:30
|
|
|
const cssBundler = postcss([postcssImport]);
|
2020-08-12 20:09:35 +05:30
|
|
|
const result = await cssBundler.process(preCss, {from: entryPath});
|
|
|
|
await fs.writeFile(bundlePath, result.css, "utf8");
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2020-08-12 20:09:35 +05:30
|
|
|
async function buildCssLegacy(entryPath, bundlePath) {
|
|
|
|
const preCss = await fs.readFile(entryPath, "utf8");
|
2020-08-07 22:45:53 +05:30
|
|
|
const cssBundler = postcss([postcssImport, cssvariables(), flexbugsFixes()]);
|
2020-08-12 20:09:35 +05:30
|
|
|
const result = await cssBundler.process(preCss, {from: entryPath});
|
|
|
|
await fs.writeFile(bundlePath, result.css, "utf8");
|
2020-08-07 22:45:53 +05:30
|
|
|
}
|
|
|
|
|
2019-09-15 18:02:12 +05:30
|
|
|
function removeOrEnableScript(scriptNode, enable) {
|
|
|
|
if (enable) {
|
|
|
|
scriptNode.attr("type", "text/javascript");
|
|
|
|
} else {
|
|
|
|
scriptNode.remove();
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function removeDirIfExists(targetDir) {
|
|
|
|
try {
|
2020-08-12 20:07:55 +05:30
|
|
|
await fs.rmdir(targetDir, {recursive: true});
|
2019-09-15 12:44:20 +05:30
|
|
|
} catch (err) {
|
2019-09-15 15:52:43 +05:30
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-09-15 12:44:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 20:07:55 +05:30
|
|
|
async function copyFolder(srcRoot, dstRoot, filter) {
|
|
|
|
const dirEnts = await fs.readdir(srcRoot, {withFileTypes: true});
|
|
|
|
for (const dirEnt of dirEnts) {
|
|
|
|
const dstPath = path.join(dstRoot, dirEnt.name);
|
|
|
|
const srcPath = path.join(srcRoot, dirEnt.name);
|
|
|
|
if (dirEnt.isDirectory()) {
|
|
|
|
await fs.mkdir(dstPath);
|
|
|
|
await copyFolder(srcPath, dstPath, filter);
|
|
|
|
} else if (dirEnt.isFile() && filter(srcPath)) {
|
|
|
|
await fs.copyFile(srcPath, dstPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 12:44:20 +05:30
|
|
|
build().catch(err => console.error(err));
|