2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-08-05 18:59:16 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
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 09:14:20 +02:00
|
|
|
import cheerio from "cheerio";
|
|
|
|
import fsRoot from "fs";
|
|
|
|
const fs = fsRoot.promises;
|
|
|
|
import path from "path";
|
2020-08-27 20:02:20 +02:00
|
|
|
import xxhash from 'xxhashjs';
|
2020-08-19 11:27:35 +02:00
|
|
|
import { rollup } from 'rollup';
|
2019-09-15 09:14:20 +02:00
|
|
|
import postcss from "postcss";
|
|
|
|
import postcssImport from "postcss-import";
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
import { dirname } from 'path';
|
2020-08-13 18:59:31 +02:00
|
|
|
import commander from "commander";
|
2020-08-05 18:34:41 +02:00
|
|
|
// 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 16:50:18 +02:00
|
|
|
// multi-entry plugin so we can add polyfill file to main
|
|
|
|
import multi from '@rollup/plugin-multi-entry';
|
2020-08-18 11:47:20 +02:00
|
|
|
import removeJsComments from 'rollup-plugin-cleanup';
|
2020-08-14 10:45:14 +02:00
|
|
|
// replace urls of asset names with content hashed version
|
|
|
|
import postcssUrl from "postcss-url";
|
2019-09-15 09:14:20 +02:00
|
|
|
|
2020-08-07 19:15:53 +02:00
|
|
|
import cssvariables from "postcss-css-variables";
|
|
|
|
import flexbugsFixes from "postcss-flexbugs-fixes";
|
|
|
|
|
2020-08-05 18:34:41 +02:00
|
|
|
const PROJECT_ID = "hydrogen";
|
|
|
|
const PROJECT_SHORT_NAME = "Hydrogen";
|
|
|
|
const PROJECT_NAME = "Hydrogen Chat";
|
2019-09-15 09:14:20 +02:00
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
const __dirname = dirname(__filename);
|
|
|
|
const projectDir = path.join(__dirname, "../");
|
2020-08-13 18:59:31 +02:00
|
|
|
const cssSrcDir = path.join(projectDir, "src/ui/web/css/");
|
|
|
|
const targetDir = path.join(projectDir, "target/");
|
2019-09-15 09:14:20 +02:00
|
|
|
|
2020-08-13 18:59:31 +02:00
|
|
|
const program = new commander.Command();
|
|
|
|
program
|
|
|
|
.option("--no-offline", "make a build without a service worker or appcache manifest")
|
|
|
|
program.parse(process.argv);
|
2020-08-18 11:42:13 +02:00
|
|
|
const {debug, noOffline} = program;
|
2020-08-05 18:34:41 +02:00
|
|
|
const offline = !noOffline;
|
2019-09-15 12:22:43 +02:00
|
|
|
|
2019-09-15 09:14:20 +02:00
|
|
|
async function build() {
|
2020-08-18 11:42:13 +02:00
|
|
|
// only used for CSS for now, using legacy for all targets for now
|
|
|
|
const legacy = true;
|
2019-09-15 12:22:43 +02:00
|
|
|
// get version number
|
|
|
|
const version = JSON.parse(await fs.readFile(path.join(projectDir, "package.json"), "utf8")).version;
|
2020-08-12 16:35:48 +02:00
|
|
|
|
|
|
|
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 18:59:31 +02:00
|
|
|
// clear target dir
|
|
|
|
await removeDirIfExists(targetDir);
|
|
|
|
await createDirs(targetDir, themes);
|
2020-08-12 16:37:55 +02:00
|
|
|
// also creates the directories where the theme css bundles are placed in,
|
|
|
|
// so do it first
|
|
|
|
const themeAssets = await copyThemeAssets(themes, legacy);
|
2020-08-18 11:42:13 +02:00
|
|
|
const jsBundlePath = await buildJs();
|
|
|
|
const jsLegacyBundlePath = await buildJsLegacy();
|
2020-08-14 10:45:14 +02:00
|
|
|
const cssBundlePaths = await buildCssBundles(legacy ? buildCssLegacy : buildCss, themes, themeAssets);
|
2020-08-18 11:42:13 +02:00
|
|
|
const assetPaths = createAssetPaths(jsBundlePath, jsLegacyBundlePath, cssBundlePaths, themeAssets);
|
2020-08-12 16:37:55 +02:00
|
|
|
|
2020-08-14 11:06:39 +02:00
|
|
|
let manifestPath;
|
2019-09-28 09:45:01 +02:00
|
|
|
if (offline) {
|
2020-08-14 11:06:39 +02:00
|
|
|
manifestPath = await buildOffline(version, assetPaths);
|
2019-09-28 09:45:01 +02:00
|
|
|
}
|
2020-08-14 11:06:39 +02:00
|
|
|
await buildHtml(doc, version, assetPaths, manifestPath);
|
2019-09-15 14:32:12 +02:00
|
|
|
|
2020-08-18 11:42:13 +02:00
|
|
|
console.log(`built ${PROJECT_ID} ${version} successfully`);
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:42:13 +02:00
|
|
|
function createAssetPaths(jsBundlePath, jsLegacyBundlePath, cssBundlePaths, themeAssets) {
|
2020-08-13 18:59:31 +02:00
|
|
|
function trim(path) {
|
|
|
|
if (!path.startsWith(targetDir)) {
|
|
|
|
throw new Error("invalid target path: " + targetDir);
|
|
|
|
}
|
|
|
|
return path.substr(targetDir.length);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
jsBundle: () => trim(jsBundlePath),
|
2020-08-18 11:42:13 +02:00
|
|
|
jsLegacyBundle: () => trim(jsLegacyBundlePath),
|
2020-08-13 18:59:31 +02:00
|
|
|
cssMainBundle: () => trim(cssBundlePaths.main),
|
|
|
|
cssThemeBundle: themeName => trim(cssBundlePaths.themes[themeName]),
|
|
|
|
cssThemeBundles: () => Object.values(cssBundlePaths.themes).map(a => trim(a)),
|
2020-08-14 10:45:14 +02:00
|
|
|
otherAssets: () => Object.values(themeAssets).map(a => trim(a))
|
2020-08-13 18:59:31 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:35:48 +02:00
|
|
|
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 18:59:31 +02:00
|
|
|
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-08-12 16:37:55 +02:00
|
|
|
async function copyThemeAssets(themes, legacy) {
|
2020-08-14 10:45:14 +02:00
|
|
|
const assets = {};
|
2020-08-12 16:37:55 +02:00
|
|
|
for (const theme of themes) {
|
|
|
|
const themeDstFolder = path.join(targetDir, `themes/${theme}`);
|
2020-08-13 18:59:31 +02:00
|
|
|
const themeSrcFolder = path.join(cssSrcDir, `themes/${theme}`);
|
|
|
|
const themeAssets = await copyFolder(themeSrcFolder, themeDstFolder, file => {
|
2020-08-12 16:37:55 +02:00
|
|
|
const isUnneededFont = legacy ? file.endsWith(".woff2") : file.endsWith(".woff");
|
2020-08-13 18:59:31 +02:00
|
|
|
return !file.endsWith(".css") && !isUnneededFont;
|
2020-08-12 16:37:55 +02:00
|
|
|
});
|
2020-08-14 10:45:14 +02:00
|
|
|
Object.assign(assets, themeAssets);
|
2020-08-12 16:37:55 +02:00
|
|
|
}
|
|
|
|
return assets;
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:06:39 +02:00
|
|
|
async function buildHtml(doc, version, assetPaths, manifestPath) {
|
2019-09-15 09:14:20 +02:00
|
|
|
// transform html file
|
2020-08-12 16:35:48 +02:00
|
|
|
// change path to main.css to css bundle
|
2020-08-13 18:59:31 +02:00
|
|
|
doc("link[rel=stylesheet]:not([title])").attr("href", assetPaths.cssMainBundle());
|
2020-08-12 16:35:48 +02:00
|
|
|
// change paths to all theme stylesheets
|
|
|
|
findThemes(doc, (themeName, theme) => {
|
2020-08-13 18:59:31 +02:00
|
|
|
theme.attr("href", assetPaths.cssThemeBundle(themeName));
|
2020-08-12 16:35:48 +02:00
|
|
|
});
|
2019-09-15 14:32:12 +02:00
|
|
|
doc("script#main").replaceWith(
|
2020-08-18 11:42:13 +02:00
|
|
|
`<script type="module">import {main} from "./${assetPaths.jsBundle()}"; main(document.body);</script>` +
|
|
|
|
`<script type="text/javascript" nomodule src="${assetPaths.jsLegacyBundle()}"></script>` +
|
|
|
|
`<script type="text/javascript" nomodule>${PROJECT_ID}Bundle.main(document.body);</script>`);
|
2019-10-12 20:23:37 +02:00
|
|
|
removeOrEnableScript(doc("script#service-worker"), offline);
|
2020-03-23 22:46:31 +01:00
|
|
|
|
|
|
|
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 09:45:01 +02:00
|
|
|
if (offline) {
|
|
|
|
doc("html").attr("manifest", "manifest.appcache");
|
2020-08-14 11:06:39 +02:00
|
|
|
doc("head").append(`<link rel="manifest" href="${manifestPath.substr(targetDir.length)}">`);
|
2019-09-28 09:45:01 +02:00
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
await fs.writeFile(path.join(targetDir, "index.html"), doc.html(), "utf8");
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:59:31 +02:00
|
|
|
async function buildJs() {
|
2019-09-15 09:14:20 +02:00
|
|
|
// create js bundle
|
2020-08-19 11:27:35 +02:00
|
|
|
const bundle = await rollup({
|
2020-08-18 11:47:20 +02:00
|
|
|
input: 'src/main.js',
|
|
|
|
plugins: [removeJsComments({comments: "none"})]
|
|
|
|
});
|
2020-08-13 18:59:31 +02:00
|
|
|
const {output} = await bundle.generate({
|
2020-08-18 11:42:13 +02:00
|
|
|
format: 'es',
|
2020-08-07 16:50:18 +02:00
|
|
|
name: `${PROJECT_ID}Bundle`
|
2020-08-07 10:08:08 +02:00
|
|
|
});
|
2020-08-13 18:59:31 +02:00
|
|
|
const code = output[0].code;
|
2020-08-18 12:16:51 +02:00
|
|
|
const bundlePath = resource(`${PROJECT_ID}.js`, code);
|
2020-08-13 18:59:31 +02:00
|
|
|
await fs.writeFile(bundlePath, code, "utf8");
|
|
|
|
return bundlePath;
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:59:31 +02:00
|
|
|
async function buildJsLegacy() {
|
2020-08-05 18:34:41 +02:00
|
|
|
// compile down to whatever IE 11 needs
|
|
|
|
const babelPlugin = babel.babel({
|
|
|
|
babelHelpers: 'bundled',
|
2020-08-07 10:08:31 +02:00
|
|
|
exclude: 'node_modules/**',
|
2020-08-05 18:34:41 +02:00
|
|
|
presets: [
|
|
|
|
[
|
|
|
|
"@babel/preset-env",
|
|
|
|
{
|
|
|
|
useBuiltIns: "entry",
|
|
|
|
corejs: "3",
|
|
|
|
targets: "IE 11"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
});
|
|
|
|
// create js bundle
|
|
|
|
const rollupConfig = {
|
2020-08-07 16:50:18 +02:00
|
|
|
input: ['src/legacy-polyfill.js', 'src/main.js'],
|
2020-08-18 11:47:20 +02:00
|
|
|
plugins: [multi(), commonjs(), nodeResolve(), babelPlugin, removeJsComments({comments: "none"})]
|
2020-08-05 18:34:41 +02:00
|
|
|
};
|
2020-08-19 11:27:35 +02:00
|
|
|
const bundle = await rollup(rollupConfig);
|
2020-08-13 18:59:31 +02:00
|
|
|
const {output} = await bundle.generate({
|
2020-08-07 10:08:08 +02:00
|
|
|
format: 'iife',
|
2020-08-07 16:50:18 +02:00
|
|
|
name: `${PROJECT_ID}Bundle`
|
2020-08-07 10:08:08 +02:00
|
|
|
});
|
2020-08-13 18:59:31 +02:00
|
|
|
const code = output[0].code;
|
|
|
|
const bundlePath = resource(`${PROJECT_ID}-legacy.js`, code);
|
|
|
|
await fs.writeFile(bundlePath, code, "utf8");
|
|
|
|
return bundlePath;
|
2020-08-05 18:34:41 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:59:31 +02:00
|
|
|
async function buildOffline(version, assetPaths) {
|
2019-09-15 14:32:12 +02:00
|
|
|
// write offline availability
|
2020-08-13 18:59:31 +02:00
|
|
|
const offlineFiles = [
|
|
|
|
assetPaths.cssMainBundle(),
|
|
|
|
"index.html",
|
|
|
|
"icon-192.png",
|
|
|
|
].concat(assetPaths.cssThemeBundles());
|
2019-09-15 14:32:12 +02:00
|
|
|
|
|
|
|
// write appcache manifest
|
2020-08-18 11:42:13 +02:00
|
|
|
const appCacheLines = [
|
2019-09-15 14:32:12 +02:00
|
|
|
`CACHE MANIFEST`,
|
|
|
|
`# v${version}`,
|
|
|
|
`NETWORK`,
|
|
|
|
`"*"`,
|
|
|
|
`CACHE`,
|
|
|
|
];
|
2020-08-18 11:42:13 +02:00
|
|
|
appCacheLines.push(assetPaths.jsLegacyBundle(), ...offlineFiles);
|
|
|
|
const swOfflineFiles = [assetPaths.jsBundle(), ...offlineFiles];
|
|
|
|
const appCacheManifest = appCacheLines.join("\n") + "\n";
|
|
|
|
await fs.writeFile(path.join(targetDir, "manifest.appcache"), appCacheManifest, "utf8");
|
2019-09-15 14:32:12 +02:00
|
|
|
// write service worker
|
|
|
|
let swSource = await fs.readFile(path.join(projectDir, "src/service-worker.template.js"), "utf8");
|
|
|
|
swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`);
|
2020-08-18 11:42:13 +02:00
|
|
|
swSource = swSource.replace(`"%%OFFLINE_FILES%%"`, JSON.stringify(swOfflineFiles));
|
2020-08-13 18:59:31 +02:00
|
|
|
swSource = swSource.replace(`"%%CACHE_FILES%%"`, JSON.stringify(assetPaths.otherAssets()));
|
2019-09-15 14:32:12 +02:00
|
|
|
await fs.writeFile(path.join(targetDir, "sw.js"), swSource, "utf8");
|
|
|
|
// write web manifest
|
|
|
|
const webManifest = {
|
2020-08-05 18:34:41 +02:00
|
|
|
name:PROJECT_NAME,
|
|
|
|
short_name: PROJECT_SHORT_NAME,
|
2020-03-13 22:54:11 +01:00
|
|
|
display: "fullscreen",
|
2019-09-15 14:32:12 +02:00
|
|
|
start_url: "index.html",
|
|
|
|
icons: [{"src": "icon-192.png", "sizes": "192x192", "type": "image/png"}],
|
|
|
|
};
|
2020-08-14 11:06:39 +02:00
|
|
|
const manifestJson = JSON.stringify(webManifest);
|
|
|
|
const manifestPath = resource("manifest.json", manifestJson);
|
|
|
|
await fs.writeFile(manifestPath, manifestJson, "utf8");
|
2019-09-15 14:32:12 +02:00
|
|
|
// copy icon
|
2020-08-13 18:59:31 +02:00
|
|
|
// should this icon have a content hash as well?
|
2019-09-15 14:32:12 +02:00
|
|
|
let icon = await fs.readFile(path.join(projectDir, "icon.png"));
|
|
|
|
await fs.writeFile(path.join(targetDir, "icon-192.png"), icon);
|
2020-08-14 11:06:39 +02:00
|
|
|
return manifestPath;
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:45:14 +02:00
|
|
|
async function buildCssBundles(buildFn, themes, themeAssets) {
|
2020-08-13 18:59:31 +02:00
|
|
|
const bundleCss = await buildFn(path.join(cssSrcDir, "main.css"));
|
|
|
|
const mainDstPath = resource(`${PROJECT_ID}.css`, bundleCss);
|
|
|
|
await fs.writeFile(mainDstPath, bundleCss, "utf8");
|
|
|
|
const bundlePaths = {main: mainDstPath, themes: {}};
|
2020-08-12 16:39:35 +02:00
|
|
|
for (const theme of themes) {
|
2020-08-14 10:45:14 +02:00
|
|
|
const urlBase = path.join(targetDir, `themes/${theme}/`);
|
|
|
|
const assetUrlMapper = ({absolutePath}) => {
|
|
|
|
const hashedDstPath = themeAssets[absolutePath];
|
|
|
|
if (hashedDstPath && hashedDstPath.startsWith(urlBase)) {
|
|
|
|
return hashedDstPath.substr(urlBase.length);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const themeCss = await buildFn(path.join(cssSrcDir, `themes/${theme}/theme.css`), assetUrlMapper);
|
2020-08-13 18:59:31 +02:00
|
|
|
const themeDstPath = resource(`themes/${theme}/bundle.css`, themeCss);
|
|
|
|
await fs.writeFile(themeDstPath, themeCss, "utf8");
|
|
|
|
bundlePaths.themes[theme] = themeDstPath;
|
2020-08-12 16:39:35 +02:00
|
|
|
}
|
2020-08-13 18:59:31 +02:00
|
|
|
return bundlePaths;
|
2020-08-12 16:39:35 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:45:14 +02:00
|
|
|
async function buildCss(entryPath, urlMapper = null) {
|
2020-08-12 16:39:35 +02:00
|
|
|
const preCss = await fs.readFile(entryPath, "utf8");
|
2020-08-14 10:45:14 +02:00
|
|
|
const options = [postcssImport];
|
|
|
|
if (urlMapper) {
|
|
|
|
options.push(postcssUrl({url: urlMapper}));
|
|
|
|
}
|
|
|
|
const cssBundler = postcss(options);
|
2020-08-12 16:39:35 +02:00
|
|
|
const result = await cssBundler.process(preCss, {from: entryPath});
|
2020-08-13 18:59:31 +02:00
|
|
|
return result.css;
|
2019-09-15 14:32:12 +02:00
|
|
|
}
|
|
|
|
|
2020-08-14 10:45:14 +02:00
|
|
|
async function buildCssLegacy(entryPath, urlMapper = null) {
|
2020-08-12 16:39:35 +02:00
|
|
|
const preCss = await fs.readFile(entryPath, "utf8");
|
2020-08-14 10:45:14 +02:00
|
|
|
const options = [
|
|
|
|
postcssImport,
|
|
|
|
cssvariables(),
|
|
|
|
flexbugsFixes()
|
|
|
|
];
|
|
|
|
if (urlMapper) {
|
|
|
|
options.push(postcssUrl({url: urlMapper}));
|
|
|
|
}
|
|
|
|
const cssBundler = postcss(options);
|
2020-08-12 16:39:35 +02:00
|
|
|
const result = await cssBundler.process(preCss, {from: entryPath});
|
2020-08-13 18:59:31 +02:00
|
|
|
return result.css;
|
2020-08-07 19:15:53 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 14:32:12 +02:00
|
|
|
function removeOrEnableScript(scriptNode, enable) {
|
|
|
|
if (enable) {
|
|
|
|
scriptNode.attr("type", "text/javascript");
|
|
|
|
} else {
|
|
|
|
scriptNode.remove();
|
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeDirIfExists(targetDir) {
|
|
|
|
try {
|
2020-08-12 16:37:55 +02:00
|
|
|
await fs.rmdir(targetDir, {recursive: true});
|
2019-09-15 09:14:20 +02:00
|
|
|
} catch (err) {
|
2019-09-15 12:22:43 +02:00
|
|
|
if (err.code !== "ENOENT") {
|
|
|
|
throw err;
|
|
|
|
}
|
2019-09-15 09:14:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 16:37:55 +02:00
|
|
|
async function copyFolder(srcRoot, dstRoot, filter) {
|
2020-08-14 10:45:14 +02:00
|
|
|
const assetPaths = {};
|
2020-08-12 16:37:55 +02:00
|
|
|
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-08-14 10:45:14 +02:00
|
|
|
Object.assign(assetPaths, await copyFolder(srcPath, dstPath, filter));
|
2020-08-12 16:37:55 +02:00
|
|
|
} else if (dirEnt.isFile() && filter(srcPath)) {
|
2020-08-13 18:59:31 +02:00
|
|
|
const content = await fs.readFile(srcPath);
|
|
|
|
const hashedDstPath = resource(dstPath, content);
|
|
|
|
await fs.writeFile(hashedDstPath, content);
|
2020-08-14 10:45:14 +02:00
|
|
|
assetPaths[srcPath] = hashedDstPath;
|
2020-08-12 16:37:55 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-13 18:59:31 +02:00
|
|
|
return assetPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource(relPath, content) {
|
|
|
|
let fullPath = relPath;
|
|
|
|
if (!relPath.startsWith("/")) {
|
|
|
|
fullPath = path.join(targetDir, relPath);
|
|
|
|
}
|
|
|
|
const hash = contentHash(Buffer.from(content));
|
|
|
|
const dir = path.dirname(fullPath);
|
|
|
|
const extname = path.extname(fullPath);
|
|
|
|
const basename = path.basename(fullPath, extname);
|
|
|
|
return path.join(dir, `${basename}-${hash}${extname}`);
|
2020-08-12 16:37:55 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 18:59:31 +02:00
|
|
|
function contentHash(str) {
|
2020-08-27 20:02:20 +02:00
|
|
|
var hasher = new xxhash.h32(0);
|
2020-08-13 18:59:31 +02:00
|
|
|
hasher.update(str);
|
|
|
|
return hasher.digest();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-15 09:14:20 +02:00
|
|
|
build().catch(err => console.error(err));
|