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";
|
2020-08-27 23:32:20 +05:30
|
|
|
import xxhash from 'xxhashjs';
|
2020-08-19 14:57:35 +05:30
|
|
|
import { rollup } from 'rollup';
|
2019-09-15 12:44:20 +05:30
|
|
|
import postcss from "postcss";
|
|
|
|
import postcssImport from "postcss-import";
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
2020-08-13 22:29:31 +05:30
|
|
|
import commander from "commander";
|
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';
|
2020-08-18 15:17:20 +05:30
|
|
|
import removeJsComments from 'rollup-plugin-cleanup';
|
2020-08-14 14:15:14 +05:30
|
|
|
// replace urls of asset names with content hashed version
|
|
|
|
import postcssUrl from "postcss-url";
|
2019-09-15 12:44:20 +05:30
|
|
|
|
2020-08-07 22:45:53 +05:30
|
|
|
import cssvariables from "postcss-css-variables";
|
2020-10-08 17:47:43 +05:30
|
|
|
import autoprefixer from "autoprefixer";
|
2020-08-07 22:45:53 +05:30
|
|
|
import flexbugsFixes from "postcss-flexbugs-fixes";
|
|
|
|
|
2019-09-15 12:44:20 +05:30
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
2020-10-26 20:14:11 +05:30
|
|
|
const cssSrcDir = path.join(projectDir, "src/platform/web/ui/css/");
|
2019-09-15 12:44:20 +05:30
|
|
|
|
2020-10-26 20:14:11 +05:30
|
|
|
const parameters = new commander.Command();
|
|
|
|
parameters
|
2020-10-02 23:24:24 +05:30
|
|
|
.option("--modern-only", "don't make a legacy build")
|
2021-03-25 22:38:47 +05:30
|
|
|
.option("--override-imports <json file>", "pass in a file to override import paths, see doc/SKINNING.md")
|
2021-03-25 23:24:10 +05:30
|
|
|
.option("--override-css <main css file>", "pass in an alternative main css file")
|
2020-10-26 20:14:11 +05:30
|
|
|
parameters.parse(process.argv);
|
2019-09-15 15:52:43 +05:30
|
|
|
|
2021-03-25 23:24:10 +05:30
|
|
|
async function build({modernOnly, overrideImports, overrideCss}) {
|
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;
|
2021-03-25 22:38:47 +05:30
|
|
|
let importOverridesMap;
|
|
|
|
if (overrideImports) {
|
|
|
|
importOverridesMap = await readImportOverrides(overrideImports);
|
|
|
|
}
|
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-13 22:29:31 +05:30
|
|
|
// clear target dir
|
2020-10-02 13:03:57 +05:30
|
|
|
const targetDir = path.join(projectDir, "target/");
|
2020-08-13 22:29:31 +05:30
|
|
|
await removeDirIfExists(targetDir);
|
|
|
|
await createDirs(targetDir, themes);
|
2020-10-01 22:51:27 +05:30
|
|
|
const assets = new AssetMap(targetDir);
|
|
|
|
// copy olm assets
|
|
|
|
const olmAssets = await copyFolder(path.join(projectDir, "lib/olm/"), assets.directory);
|
|
|
|
assets.addSubMap(olmAssets);
|
2021-03-25 22:38:47 +05:30
|
|
|
await assets.write(`hydrogen.js`, await buildJs("src/main.js", ["src/platform/web/Platform.js"], importOverridesMap));
|
2020-10-02 23:24:24 +05:30
|
|
|
if (!modernOnly) {
|
2020-10-26 20:14:11 +05:30
|
|
|
await assets.write(`hydrogen-legacy.js`, await buildJsLegacy("src/main.js", [
|
|
|
|
'src/platform/web/legacy-polyfill.js',
|
|
|
|
'src/platform/web/LegacyPlatform.js'
|
2021-03-25 22:38:47 +05:30
|
|
|
], importOverridesMap));
|
2020-10-26 20:14:11 +05:30
|
|
|
await assets.write(`worker.js`, await buildJsLegacy("src/platform/web/worker/main.js", ['src/platform/web/worker/polyfill.js']));
|
2020-10-02 23:24:24 +05:30
|
|
|
}
|
2020-11-10 21:53:23 +05:30
|
|
|
// copy over non-theme assets
|
2021-03-22 23:49:44 +05:30
|
|
|
const baseConfig = JSON.parse(await fs.readFile(path.join(projectDir, "assets/config.json"), {encoding: "utf8"}));
|
2020-11-10 21:53:23 +05:30
|
|
|
const downloadSandbox = "download-sandbox.html";
|
|
|
|
let downloadSandboxHtml = await fs.readFile(path.join(projectDir, `assets/${downloadSandbox}`));
|
|
|
|
await assets.write(downloadSandbox, downloadSandboxHtml);
|
2020-10-01 22:51:27 +05:30
|
|
|
// creates the directories where the theme css bundles are placed in,
|
|
|
|
// and writes to assets, so the build bundles can translate them, so do it first
|
|
|
|
await copyThemeAssets(themes, assets);
|
2021-03-25 23:24:10 +05:30
|
|
|
await buildCssBundles(buildCssLegacy, themes, assets, overrideCss);
|
2020-10-02 23:24:24 +05:30
|
|
|
await buildManifest(assets);
|
2020-10-15 19:28:29 +05:30
|
|
|
// all assets have been added, create a hash from all assets name to cache unhashed files like index.html
|
|
|
|
assets.addToHashForAll("index.html", devHtml);
|
2021-03-23 19:50:33 +05:30
|
|
|
let swSource = await fs.readFile(path.join(projectDir, "src/platform/web/service-worker.js"), "utf8");
|
2020-10-15 19:28:29 +05:30
|
|
|
assets.addToHashForAll("sw.js", swSource);
|
|
|
|
|
|
|
|
const globalHash = assets.hashForAll();
|
|
|
|
|
2020-10-15 22:42:16 +05:30
|
|
|
await buildServiceWorker(swSource, version, globalHash, assets);
|
2021-03-22 23:49:44 +05:30
|
|
|
await buildHtml(doc, version, baseConfig, globalHash, modernOnly, assets);
|
2020-10-02 23:24:24 +05:30
|
|
|
console.log(`built hydrogen ${version} (${globalHash}) successfully with ${assets.size} files`);
|
2020-08-13 22:29:31 +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-13 22:29:31 +05:30
|
|
|
async function createDirs(targetDir, themes) {
|
|
|
|
await fs.mkdir(targetDir);
|
|
|
|
const themeDir = path.join(targetDir, "themes");
|
|
|
|
await fs.mkdir(themeDir);
|
|
|
|
for (const theme of themes) {
|
|
|
|
await fs.mkdir(path.join(themeDir, theme));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:51:27 +05:30
|
|
|
async function copyThemeAssets(themes, assets) {
|
2020-08-12 20:07:55 +05:30
|
|
|
for (const theme of themes) {
|
2020-10-02 13:03:57 +05:30
|
|
|
const themeDstFolder = path.join(assets.directory, `themes/${theme}`);
|
2020-08-13 22:29:31 +05:30
|
|
|
const themeSrcFolder = path.join(cssSrcDir, `themes/${theme}`);
|
|
|
|
const themeAssets = await copyFolder(themeSrcFolder, themeDstFolder, file => {
|
2020-10-01 22:51:27 +05:30
|
|
|
return !file.endsWith(".css");
|
2020-08-12 20:07:55 +05:30
|
|
|
});
|
2020-10-01 22:51:27 +05:30
|
|
|
assets.addSubMap(themeAssets);
|
2020-08-12 20:07:55 +05:30
|
|
|
}
|
|
|
|
return assets;
|
|
|
|
}
|
|
|
|
|
2021-03-22 23:49:44 +05:30
|
|
|
async function buildHtml(doc, version, baseConfig, globalHash, modernOnly, assets) {
|
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
|
2020-10-01 22:51:27 +05:30
|
|
|
doc("link[rel=stylesheet]:not([title])").attr("href", assets.resolve(`hydrogen.css`));
|
2020-10-15 14:47:33 +05:30
|
|
|
// adjust file name of icon on iOS
|
|
|
|
doc("link[rel=apple-touch-icon]").attr("href", assets.resolve(`icon-maskable.png`));
|
2020-08-12 20:05:48 +05:30
|
|
|
// change paths to all theme stylesheets
|
|
|
|
findThemes(doc, (themeName, theme) => {
|
2020-10-01 22:51:27 +05:30
|
|
|
theme.attr("href", assets.resolve(`themes/${themeName}/bundle.css`));
|
2020-08-12 20:05:48 +05:30
|
|
|
});
|
2021-03-22 23:49:44 +05:30
|
|
|
const configJSON = JSON.stringify(Object.assign({}, baseConfig, {
|
2020-10-02 23:24:24 +05:30
|
|
|
worker: assets.has("worker.js") ? assets.resolve(`worker.js`) : null,
|
2020-11-10 21:53:23 +05:30
|
|
|
downloadSandbox: assets.resolve("download-sandbox.html"),
|
2020-10-16 16:19:42 +05:30
|
|
|
serviceWorker: "sw.js",
|
2020-10-01 22:51:27 +05:30
|
|
|
olm: {
|
|
|
|
wasm: assets.resolve("olm.wasm"),
|
|
|
|
legacyBundle: assets.resolve("olm_legacy.js"),
|
|
|
|
wasmBundle: assets.resolve("olm.js"),
|
|
|
|
}
|
2021-03-22 23:49:44 +05:30
|
|
|
}));
|
2021-03-27 18:41:19 +05:30
|
|
|
const modernScript = `import {main, Platform} from "./${assets.resolve(`hydrogen.js`)}"; main(new Platform(document.body, ${configJSON}));`;
|
2020-10-02 23:24:24 +05:30
|
|
|
const mainScripts = [
|
2021-03-27 18:41:19 +05:30
|
|
|
`<script type="module">${wrapWithLicenseComments(modernScript)}</script>`
|
2020-10-02 23:24:24 +05:30
|
|
|
];
|
|
|
|
if (!modernOnly) {
|
2021-03-27 18:41:19 +05:30
|
|
|
const legacyScript = `hydrogen.main(new hydrogen.Platform(document.body, ${configJSON}));`;
|
2020-10-02 23:24:24 +05:30
|
|
|
mainScripts.push(
|
|
|
|
`<script type="text/javascript" nomodule src="${assets.resolve(`hydrogen-legacy.js`)}"></script>`,
|
2021-03-27 18:41:19 +05:30
|
|
|
`<script type="text/javascript" nomodule>${wrapWithLicenseComments(legacyScript)}</script>`
|
2020-10-02 23:24:24 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
doc("script#main").replaceWith(mainScripts.join(""));
|
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}"`);
|
2020-10-02 23:24:24 +05:30
|
|
|
vSource = vSource.replace(`"%%GLOBAL_HASH%%"`, `"${globalHash}"`);
|
2021-03-27 18:41:19 +05:30
|
|
|
versionScript.text(wrapWithLicenseComments(vSource));
|
2020-10-02 23:24:24 +05:30
|
|
|
doc("head").append(`<link rel="manifest" href="${assets.resolve("manifest.json")}">`);
|
2020-10-02 13:03:35 +05:30
|
|
|
await assets.writeUnhashed("index.html", doc.html());
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2021-03-25 22:38:47 +05:30
|
|
|
async function buildJs(mainFile, extraFiles, importOverrides) {
|
2019-09-15 12:44:20 +05:30
|
|
|
// create js bundle
|
2021-03-25 22:38:47 +05:30
|
|
|
const plugins = [multi(), removeJsComments({comments: "none"})];
|
|
|
|
if (importOverrides) {
|
|
|
|
plugins.push(overridesAsRollupPlugin(importOverrides));
|
|
|
|
}
|
2020-08-19 14:57:35 +05:30
|
|
|
const bundle = await rollup({
|
2020-10-26 20:14:11 +05:30
|
|
|
input: extraFiles.concat(mainFile),
|
2021-03-25 22:38:47 +05:30
|
|
|
plugins
|
2020-08-18 15:17:20 +05:30
|
|
|
});
|
2020-08-13 22:29:31 +05:30
|
|
|
const {output} = await bundle.generate({
|
2020-08-18 15:12:13 +05:30
|
|
|
format: 'es',
|
2020-09-10 16:30:11 +05:30
|
|
|
// TODO: can remove this?
|
2020-10-26 20:14:11 +05:30
|
|
|
name: `hydrogen`
|
2020-08-07 13:38:08 +05:30
|
|
|
});
|
2020-08-13 22:29:31 +05:30
|
|
|
const code = output[0].code;
|
2021-03-27 18:41:19 +05:30
|
|
|
return wrapWithLicenseComments(code);
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2021-03-25 22:38:47 +05:30
|
|
|
async function buildJsLegacy(mainFile, extraFiles, importOverrides) {
|
2020-08-05 22:04:41 +05:30
|
|
|
// 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",
|
2020-09-28 18:58:22 +05:30
|
|
|
targets: "IE 11",
|
|
|
|
// we provide our own promise polyfill (es6-promise)
|
|
|
|
// with support for synchronous flushing of
|
|
|
|
// the queue for idb where needed
|
|
|
|
exclude: ["es.promise", "es.promise.all-settled", "es.promise.finally"]
|
2020-08-05 22:04:41 +05:30
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
});
|
2021-03-25 22:38:47 +05:30
|
|
|
const plugins = [multi(), commonjs()];
|
|
|
|
if (importOverrides) {
|
|
|
|
plugins.push(overridesAsRollupPlugin(importOverrides));
|
|
|
|
}
|
|
|
|
plugins.push(nodeResolve(), babelPlugin);
|
2020-08-05 22:04:41 +05:30
|
|
|
// create js bundle
|
|
|
|
const rollupConfig = {
|
2020-10-06 16:26:20 +05:30
|
|
|
// important the extraFiles come first,
|
|
|
|
// so polyfills are available in the global scope
|
|
|
|
// if needed for the mainfile
|
|
|
|
input: extraFiles.concat(mainFile),
|
2021-03-25 22:38:47 +05:30
|
|
|
plugins
|
2020-08-05 22:04:41 +05:30
|
|
|
};
|
2020-08-19 14:57:35 +05:30
|
|
|
const bundle = await rollup(rollupConfig);
|
2020-08-13 22:29:31 +05:30
|
|
|
const {output} = await bundle.generate({
|
2020-08-07 13:38:08 +05:30
|
|
|
format: 'iife',
|
2020-10-26 20:14:11 +05:30
|
|
|
name: `hydrogen`
|
2020-08-07 13:38:08 +05:30
|
|
|
});
|
2020-08-13 22:29:31 +05:30
|
|
|
const code = output[0].code;
|
2021-03-27 18:41:19 +05:30
|
|
|
return wrapWithLicenseComments(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapWithLicenseComments(code) {
|
|
|
|
// Add proper license comments to make GNU LibreJS accept the file
|
|
|
|
const start = '// @license magnet:?xt=urn:btih:8e4f440f4c65981c5bf93c76d35135ba5064d8b7&dn=apache-2.0.txt Apache-2.0';
|
|
|
|
const end = '// @license-end';
|
|
|
|
return `${start}\n${code}\n${end}`;
|
2020-08-05 22:04:41 +05:30
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:09 +05:30
|
|
|
const NON_PRECACHED_JS = [
|
2020-10-02 23:24:24 +05:30
|
|
|
"hydrogen-legacy.js",
|
|
|
|
"olm_legacy.js",
|
2020-10-15 19:48:09 +05:30
|
|
|
"worker.js"
|
2020-10-02 23:24:24 +05:30
|
|
|
];
|
|
|
|
|
|
|
|
function isPreCached(asset) {
|
|
|
|
return asset.endsWith(".svg") ||
|
|
|
|
asset.endsWith(".png") ||
|
|
|
|
asset.endsWith(".css") ||
|
|
|
|
asset.endsWith(".wasm") ||
|
2020-11-10 21:53:23 +05:30
|
|
|
asset.endsWith(".html") ||
|
2020-10-02 23:24:24 +05:30
|
|
|
// most environments don't need the worker
|
2020-10-15 19:48:09 +05:30
|
|
|
asset.endsWith(".js") && !NON_PRECACHED_JS.includes(asset);
|
2020-10-02 23:24:24 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
async function buildManifest(assets) {
|
2020-09-24 14:45:15 +05:30
|
|
|
const webManifest = JSON.parse(await fs.readFile(path.join(projectDir, "assets/manifest.json"), "utf8"));
|
2020-10-01 22:51:27 +05:30
|
|
|
// copy manifest icons
|
2020-09-24 14:45:15 +05:30
|
|
|
for (const icon of webManifest.icons) {
|
|
|
|
let iconData = await fs.readFile(path.join(projectDir, icon.src));
|
2020-10-01 22:51:27 +05:30
|
|
|
const iconTargetPath = path.basename(icon.src);
|
2020-10-02 13:03:07 +05:30
|
|
|
icon.src = await assets.write(iconTargetPath, iconData);
|
2020-09-24 14:45:15 +05:30
|
|
|
}
|
2020-10-02 23:24:24 +05:30
|
|
|
await assets.write("manifest.json", JSON.stringify(webManifest));
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:42:16 +05:30
|
|
|
async function buildServiceWorker(swSource, version, globalHash, assets) {
|
2020-10-02 23:24:24 +05:30
|
|
|
const unhashedPreCachedAssets = ["index.html"];
|
|
|
|
const hashedPreCachedAssets = [];
|
|
|
|
const hashedCachedOnRequestAssets = [];
|
|
|
|
|
|
|
|
for (const [unresolved, resolved] of assets) {
|
2020-10-15 19:48:09 +05:30
|
|
|
if (unresolved === resolved) {
|
2020-10-02 23:24:24 +05:30
|
|
|
unhashedPreCachedAssets.push(resolved);
|
|
|
|
} else if (isPreCached(unresolved)) {
|
|
|
|
hashedPreCachedAssets.push(resolved);
|
|
|
|
} else {
|
|
|
|
hashedCachedOnRequestAssets.push(resolved);
|
|
|
|
}
|
|
|
|
}
|
2021-03-23 19:48:07 +05:30
|
|
|
|
|
|
|
const replaceArrayInSource = (name, value) => {
|
|
|
|
const newSource = swSource.replace(`${name} = []`, `${name} = ${JSON.stringify(value)}`);
|
|
|
|
if (newSource === swSource) {
|
|
|
|
throw new Error(`${name} was not found in the service worker source`);
|
|
|
|
}
|
|
|
|
return newSource;
|
|
|
|
};
|
2021-03-25 04:42:57 +05:30
|
|
|
const replaceStringInSource = (name, value) => {
|
|
|
|
const newSource = swSource.replace(new RegExp(`${name}\\s=\\s"[^"]*"`), `${name} = ${JSON.stringify(value)}`);
|
|
|
|
if (newSource === swSource) {
|
|
|
|
throw new Error(`${name} was not found in the service worker source`);
|
|
|
|
}
|
|
|
|
return newSource;
|
|
|
|
};
|
2021-03-23 19:48:07 +05:30
|
|
|
|
2019-09-15 18:02:12 +05:30
|
|
|
// write service worker
|
2020-10-15 22:42:16 +05:30
|
|
|
swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
2020-10-02 23:24:24 +05:30
|
|
|
swSource = swSource.replace(`"%%GLOBAL_HASH%%"`, `"${globalHash}"`);
|
2021-03-23 19:48:07 +05:30
|
|
|
swSource = replaceArrayInSource("UNHASHED_PRECACHED_ASSETS", unhashedPreCachedAssets);
|
|
|
|
swSource = replaceArrayInSource("HASHED_PRECACHED_ASSETS", hashedPreCachedAssets);
|
|
|
|
swSource = replaceArrayInSource("HASHED_CACHED_ON_REQUEST_ASSETS", hashedCachedOnRequestAssets);
|
2021-03-25 04:42:57 +05:30
|
|
|
swSource = replaceStringInSource("NOTIFICATION_BADGE_ICON", assets.resolve("icon.png"));
|
|
|
|
|
2020-10-01 22:51:27 +05:30
|
|
|
// service worker should not have a hashed name as it is polled by the browser for updates
|
2020-10-02 13:03:35 +05:30
|
|
|
await assets.writeUnhashed("sw.js", swSource);
|
2019-09-15 18:02:12 +05:30
|
|
|
}
|
|
|
|
|
2021-03-25 23:24:10 +05:30
|
|
|
async function buildCssBundles(buildFn, themes, assets, mainCssFile = null) {
|
|
|
|
if (!mainCssFile) {
|
|
|
|
mainCssFile = path.join(cssSrcDir, "main.css");
|
|
|
|
}
|
|
|
|
const bundleCss = await buildFn(mainCssFile);
|
2020-10-02 13:03:07 +05:30
|
|
|
await assets.write(`hydrogen.css`, bundleCss);
|
2020-08-12 20:09:35 +05:30
|
|
|
for (const theme of themes) {
|
2020-10-01 22:51:27 +05:30
|
|
|
const themeRelPath = `themes/${theme}/`;
|
|
|
|
const themeRoot = path.join(cssSrcDir, themeRelPath);
|
2020-08-14 14:15:14 +05:30
|
|
|
const assetUrlMapper = ({absolutePath}) => {
|
2020-10-01 22:51:27 +05:30
|
|
|
if (!absolutePath.startsWith(themeRoot)) {
|
|
|
|
throw new Error("resource is out of theme directory: " + absolutePath);
|
|
|
|
}
|
|
|
|
const relPath = absolutePath.substr(themeRoot.length);
|
|
|
|
const hashedDstPath = assets.resolve(path.join(themeRelPath, relPath));
|
|
|
|
if (hashedDstPath) {
|
|
|
|
return hashedDstPath.substr(themeRelPath.length);
|
2020-08-14 14:15:14 +05:30
|
|
|
}
|
|
|
|
};
|
2020-10-01 22:51:27 +05:30
|
|
|
const themeCss = await buildFn(path.join(themeRoot, `theme.css`), assetUrlMapper);
|
2020-10-02 13:03:07 +05:30
|
|
|
await assets.write(path.join(themeRelPath, `bundle.css`), themeCss);
|
2020-08-12 20:09:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:51:27 +05:30
|
|
|
// async function buildCss(entryPath, urlMapper = null) {
|
|
|
|
// const preCss = await fs.readFile(entryPath, "utf8");
|
|
|
|
// const options = [postcssImport];
|
|
|
|
// if (urlMapper) {
|
|
|
|
// options.push(postcssUrl({url: urlMapper}));
|
|
|
|
// }
|
|
|
|
// const cssBundler = postcss(options);
|
|
|
|
// const result = await cssBundler.process(preCss, {from: entryPath});
|
|
|
|
// return result.css;
|
|
|
|
// }
|
2019-09-15 18:02:12 +05:30
|
|
|
|
2020-08-14 14:15:14 +05:30
|
|
|
async function buildCssLegacy(entryPath, urlMapper = null) {
|
2020-08-12 20:09:35 +05:30
|
|
|
const preCss = await fs.readFile(entryPath, "utf8");
|
2020-08-14 14:15:14 +05:30
|
|
|
const options = [
|
|
|
|
postcssImport,
|
2021-03-17 00:04:34 +05:30
|
|
|
cssvariables({
|
|
|
|
preserve: (declaration) => {
|
|
|
|
return declaration.value.indexOf("var(--ios-") == 0;
|
|
|
|
}
|
|
|
|
}),
|
2020-10-08 17:47:43 +05:30
|
|
|
autoprefixer({overrideBrowserslist: ["IE 11"], grid: "no-autoplace"}),
|
2020-08-14 14:15:14 +05:30
|
|
|
flexbugsFixes()
|
|
|
|
];
|
|
|
|
if (urlMapper) {
|
|
|
|
options.push(postcssUrl({url: urlMapper}));
|
|
|
|
}
|
|
|
|
const cssBundler = postcss(options);
|
2020-08-12 20:09:35 +05:30
|
|
|
const result = await cssBundler.process(preCss, {from: entryPath});
|
2020-08-13 22:29:31 +05:30
|
|
|
return result.css;
|
2020-08-07 22:45:53 +05:30
|
|
|
}
|
|
|
|
|
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-10-01 22:51:27 +05:30
|
|
|
async function copyFolder(srcRoot, dstRoot, filter, assets = null) {
|
|
|
|
assets = assets || new AssetMap(dstRoot);
|
2020-08-12 20:07:55 +05:30
|
|
|
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);
|
2020-10-01 22:51:27 +05:30
|
|
|
await copyFolder(srcPath, dstPath, filter, assets);
|
2020-08-27 16:54:04 +05:30
|
|
|
} else if ((dirEnt.isFile() || dirEnt.isSymbolicLink()) && (!filter || filter(srcPath))) {
|
2020-08-13 22:29:31 +05:30
|
|
|
const content = await fs.readFile(srcPath);
|
2020-10-02 13:03:07 +05:30
|
|
|
await assets.write(dstPath, content);
|
2020-08-12 20:07:55 +05:30
|
|
|
}
|
|
|
|
}
|
2020-10-01 22:51:27 +05:30
|
|
|
return assets;
|
2020-08-12 20:07:55 +05:30
|
|
|
}
|
|
|
|
|
2020-08-13 22:29:31 +05:30
|
|
|
function contentHash(str) {
|
2020-08-27 23:32:20 +05:30
|
|
|
var hasher = new xxhash.h32(0);
|
2020-08-13 22:29:31 +05:30
|
|
|
hasher.update(str);
|
|
|
|
return hasher.digest();
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:51:27 +05:30
|
|
|
class AssetMap {
|
|
|
|
constructor(targetDir) {
|
|
|
|
// remove last / if any, so substr in create works well
|
|
|
|
this._targetDir = path.resolve(targetDir);
|
|
|
|
this._assets = new Map();
|
2020-10-15 19:28:29 +05:30
|
|
|
// hashes for unhashed resources so changes in these resources also contribute to the hashForAll
|
|
|
|
this._unhashedHashes = [];
|
2020-10-01 22:51:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
_toRelPath(resourcePath) {
|
|
|
|
let relPath = resourcePath;
|
|
|
|
if (path.isAbsolute(resourcePath)) {
|
|
|
|
if (!resourcePath.startsWith(this._targetDir)) {
|
|
|
|
throw new Error(`absolute path ${resourcePath} that is not within target dir ${this._targetDir}`);
|
|
|
|
}
|
|
|
|
relPath = resourcePath.substr(this._targetDir.length + 1); // + 1 for the /
|
|
|
|
}
|
|
|
|
return relPath;
|
|
|
|
}
|
|
|
|
|
2020-10-02 13:03:07 +05:30
|
|
|
_create(resourcePath, content) {
|
2020-10-01 22:51:27 +05:30
|
|
|
const relPath = this._toRelPath(resourcePath);
|
|
|
|
const hash = contentHash(Buffer.from(content));
|
|
|
|
const dir = path.dirname(relPath);
|
|
|
|
const extname = path.extname(relPath);
|
|
|
|
const basename = path.basename(relPath, extname);
|
|
|
|
const dstRelPath = path.join(dir, `${basename}-${hash}${extname}`);
|
|
|
|
this._assets.set(relPath, dstRelPath);
|
2020-10-02 13:03:07 +05:30
|
|
|
return dstRelPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async write(resourcePath, content) {
|
|
|
|
const relPath = this._create(resourcePath, content);
|
|
|
|
const fullPath = path.join(this.directory, relPath);
|
|
|
|
if (typeof content === "string") {
|
|
|
|
await fs.writeFile(fullPath, content, "utf8");
|
|
|
|
} else {
|
|
|
|
await fs.writeFile(fullPath, content);
|
|
|
|
}
|
|
|
|
return relPath;
|
|
|
|
}
|
2020-10-02 13:03:35 +05:30
|
|
|
|
|
|
|
async writeUnhashed(resourcePath, content) {
|
|
|
|
const relPath = this._toRelPath(resourcePath);
|
|
|
|
this._assets.set(relPath, relPath);
|
|
|
|
const fullPath = path.join(this.directory, relPath);
|
|
|
|
if (typeof content === "string") {
|
|
|
|
await fs.writeFile(fullPath, content, "utf8");
|
|
|
|
} else {
|
|
|
|
await fs.writeFile(fullPath, content);
|
|
|
|
}
|
|
|
|
return relPath;
|
2020-10-01 22:51:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get directory() {
|
|
|
|
return this._targetDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(resourcePath) {
|
|
|
|
const relPath = this._toRelPath(resourcePath);
|
|
|
|
const result = this._assets.get(relPath);
|
|
|
|
if (!result) {
|
|
|
|
throw new Error(`unknown path: ${relPath}, only know ${Array.from(this._assets.keys()).join(", ")}`);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
addSubMap(assetMap) {
|
|
|
|
if (!assetMap.directory.startsWith(this.directory)) {
|
|
|
|
throw new Error(`map directory doesn't start with this directory: ${assetMap.directory} ${this.directory}`);
|
|
|
|
}
|
|
|
|
const relSubRoot = assetMap.directory.substr(this.directory.length + 1);
|
|
|
|
for (const [key, value] of assetMap._assets.entries()) {
|
|
|
|
this._assets.set(path.join(relSubRoot, key), path.join(relSubRoot, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 23:24:24 +05:30
|
|
|
[Symbol.iterator]() {
|
|
|
|
return this._assets.entries();
|
2020-10-01 22:51:27 +05:30
|
|
|
}
|
|
|
|
|
2020-10-02 23:24:24 +05:30
|
|
|
isUnhashed(relPath) {
|
|
|
|
const resolvedPath = this._assets.get(relPath);
|
|
|
|
if (!resolvedPath) {
|
|
|
|
throw new Error("Unknown asset: " + relPath);
|
|
|
|
}
|
|
|
|
return relPath === resolvedPath;
|
2020-10-01 22:51:27 +05:30
|
|
|
}
|
|
|
|
|
2020-10-02 23:24:24 +05:30
|
|
|
get size() {
|
|
|
|
return this._assets.size;
|
|
|
|
}
|
2020-10-01 22:51:27 +05:30
|
|
|
|
2020-10-02 23:24:24 +05:30
|
|
|
has(relPath) {
|
|
|
|
return this._assets.has(relPath);
|
|
|
|
}
|
2020-10-15 19:28:29 +05:30
|
|
|
|
|
|
|
hashForAll() {
|
|
|
|
const globalHashAssets = Array.from(this).map(([, resolved]) => resolved);
|
|
|
|
globalHashAssets.push(...this._unhashedHashes);
|
|
|
|
globalHashAssets.sort();
|
|
|
|
return contentHash(globalHashAssets.join(","));
|
|
|
|
}
|
|
|
|
|
|
|
|
addToHashForAll(resourcePath, content) {
|
|
|
|
this._unhashedHashes.push(`${resourcePath}-${contentHash(Buffer.from(content))}`);
|
|
|
|
}
|
2020-10-02 23:24:24 +05:30
|
|
|
}
|
2020-08-13 22:29:31 +05:30
|
|
|
|
2021-03-25 22:38:47 +05:30
|
|
|
async function readImportOverrides(filename) {
|
|
|
|
const json = await fs.readFile(filename, "utf8");
|
|
|
|
const mapping = new Map(Object.entries(JSON.parse(json)));
|
|
|
|
return {
|
|
|
|
basedir: path.dirname(path.resolve(filename))+path.sep,
|
|
|
|
mapping
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function overridesAsRollupPlugin(importOverrides) {
|
|
|
|
const {mapping, basedir} = importOverrides;
|
|
|
|
return {
|
|
|
|
name: "rewrite-imports",
|
|
|
|
resolveId (source, importer) {
|
|
|
|
let file;
|
|
|
|
if (source.startsWith(path.sep)) {
|
|
|
|
file = source;
|
|
|
|
} else {
|
|
|
|
file = path.join(path.dirname(importer), source);
|
|
|
|
}
|
|
|
|
if (file.startsWith(basedir)) {
|
|
|
|
const searchPath = file.substr(basedir.length);
|
|
|
|
const replacingPath = mapping.get(searchPath);
|
|
|
|
if (replacingPath) {
|
|
|
|
console.info(`replacing ${searchPath} with ${replacingPath}`);
|
|
|
|
return path.join(basedir, replacingPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-26 20:14:11 +05:30
|
|
|
build(parameters).catch(err => console.error(err));
|