2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const watch = require('gulp-watch');
|
|
|
|
const glob = require('fast-glob');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const locales = require('../../app/_locales/index.json');
|
2022-03-10 17:01:50 +01:00
|
|
|
const { BuildType } = require('../lib/build-type');
|
2020-07-03 02:34:48 +02:00
|
|
|
|
2022-06-21 22:07:05 +02:00
|
|
|
const { TASKS } = require('./constants');
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createTask, composeSeries } = require('./task');
|
2022-11-30 16:19:45 +01:00
|
|
|
const { getPathInsideNodeModules } = require('./utils');
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-08-31 01:49:39 +02:00
|
|
|
const EMPTY_JS_FILE = './development/empty.js';
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-08-31 01:49:39 +02:00
|
|
|
module.exports = function createStaticAssetTasks({
|
|
|
|
livereload,
|
|
|
|
browserPlatforms,
|
|
|
|
shouldIncludeLockdown = true,
|
2022-11-24 01:36:19 +01:00
|
|
|
shouldIncludeSnow = true,
|
2021-09-28 18:13:26 +02:00
|
|
|
buildType,
|
2021-08-31 01:49:39 +02:00
|
|
|
}) {
|
2022-11-24 01:36:19 +01:00
|
|
|
const copyTargetsProds = {};
|
|
|
|
const copyTargetsDevs = {};
|
|
|
|
|
|
|
|
browserPlatforms.forEach((browser) => {
|
|
|
|
const [copyTargetsProd, copyTargetsDev] = getCopyTargets(
|
|
|
|
shouldIncludeLockdown,
|
|
|
|
// Snow currently only works on Chromium based browsers
|
|
|
|
shouldIncludeSnow && browser === 'chrome',
|
|
|
|
);
|
|
|
|
copyTargetsProds[browser] = copyTargetsProd;
|
|
|
|
copyTargetsDevs[browser] = copyTargetsDev;
|
|
|
|
});
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-09-28 23:45:00 +02:00
|
|
|
const additionalBuildTargets = {
|
2021-10-25 18:57:30 +02:00
|
|
|
[BuildType.beta]: [
|
2021-09-28 23:45:00 +02:00
|
|
|
{
|
2021-11-11 00:03:59 +01:00
|
|
|
src: './app/build-types/beta/images/',
|
2021-09-28 23:45:00 +02:00
|
|
|
dest: `images`,
|
|
|
|
},
|
|
|
|
],
|
2021-10-29 03:35:58 +02:00
|
|
|
[BuildType.flask]: [
|
|
|
|
{
|
2021-11-11 00:03:59 +01:00
|
|
|
src: './app/build-types/flask/images/',
|
2021-10-29 03:35:58 +02:00
|
|
|
dest: `images`,
|
|
|
|
},
|
|
|
|
],
|
2021-09-28 23:45:00 +02:00
|
|
|
};
|
2021-09-08 22:08:23 +02:00
|
|
|
|
2021-09-28 23:45:00 +02:00
|
|
|
if (Object.keys(additionalBuildTargets).includes(buildType)) {
|
2022-11-24 01:36:19 +01:00
|
|
|
Object.entries(copyTargetsProds).forEach(([_, copyTargetsProd]) =>
|
|
|
|
copyTargetsProd.push(...additionalBuildTargets[buildType]),
|
|
|
|
);
|
|
|
|
Object.entries(copyTargetsDevs).forEach(([_, copyTargetsDev]) =>
|
|
|
|
copyTargetsDev.push(...additionalBuildTargets[buildType]),
|
|
|
|
);
|
2021-09-28 23:45:00 +02:00
|
|
|
}
|
2021-09-08 22:08:23 +02:00
|
|
|
|
2022-11-24 01:36:19 +01:00
|
|
|
const prodTasks = [];
|
|
|
|
Object.entries(copyTargetsProds).forEach(([browser, copyTargetsProd]) => {
|
|
|
|
copyTargetsProd.forEach((target) => {
|
|
|
|
prodTasks.push(async function copyStaticAssets() {
|
|
|
|
await performCopy(target, browser);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const devTasks = [];
|
|
|
|
Object.entries(copyTargetsDevs).forEach(([browser, copyTargetsDev]) => {
|
|
|
|
copyTargetsDev.forEach((target) => {
|
|
|
|
devTasks.push(async function copyStaticAssets() {
|
|
|
|
await setupLiveCopy(target, browser);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const prod = createTask(TASKS.STATIC_PROD, composeSeries(...prodTasks));
|
|
|
|
const dev = createTask(TASKS.STATIC_DEV, composeSeries(...devTasks));
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { dev, prod };
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2022-11-24 01:36:19 +01:00
|
|
|
async function setupLiveCopy(target, browser) {
|
2023-01-18 13:35:37 +01:00
|
|
|
const pattern = target.pattern === undefined ? '/**/*' : target.pattern;
|
2020-03-09 01:55:02 +01:00
|
|
|
watch(target.src + pattern, (event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
livereload.changed(event.path);
|
2022-11-24 01:36:19 +01:00
|
|
|
performCopy(target, browser);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2022-11-24 01:36:19 +01:00
|
|
|
await performCopy(target, browser);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-24 01:36:19 +01:00
|
|
|
async function performCopy(target, browser) {
|
2023-01-18 13:35:37 +01:00
|
|
|
if (target.pattern === undefined) {
|
2022-11-24 01:36:19 +01:00
|
|
|
await copyGlob(
|
|
|
|
target.src,
|
2023-01-18 13:35:37 +01:00
|
|
|
`${target.src}`,
|
2022-11-24 01:36:19 +01:00
|
|
|
`./dist/${browser}/${target.dest}`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
await copyGlob(
|
|
|
|
target.src,
|
2023-01-18 13:35:37 +01:00
|
|
|
`${target.src}${target.pattern}`,
|
2022-11-24 01:36:19 +01:00
|
|
|
`./dist/${browser}/${target.dest}`,
|
|
|
|
);
|
|
|
|
}
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function copyGlob(baseDir, srcGlob, dest) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const sources = await glob(srcGlob, { onlyFiles: false });
|
2020-11-03 00:41:28 +01:00
|
|
|
await Promise.all(
|
|
|
|
sources.map(async (src) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const relativePath = path.relative(baseDir, src);
|
|
|
|
await fs.copy(src, `${dest}${relativePath}`);
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
2021-08-31 01:49:39 +02:00
|
|
|
};
|
|
|
|
|
2022-11-24 01:36:19 +01:00
|
|
|
function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) {
|
2021-08-31 01:49:39 +02:00
|
|
|
const allCopyTargets = [
|
|
|
|
{
|
|
|
|
src: `./app/_locales/`,
|
|
|
|
dest: `_locales`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/images/`,
|
|
|
|
dest: `images`,
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules('@metamask/contract-metadata', 'images/'),
|
2021-08-31 01:49:39 +02:00
|
|
|
dest: `images/contract`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/fonts/`,
|
|
|
|
dest: `fonts`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/vendor/`,
|
|
|
|
dest: `vendor`,
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules(
|
|
|
|
'@fortawesome/fontawesome-free',
|
|
|
|
'webfonts/',
|
|
|
|
),
|
2021-08-31 01:49:39 +02:00
|
|
|
dest: `fonts/fontawesome`,
|
|
|
|
},
|
2021-10-13 16:22:51 +02:00
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules('react-responsive-carousel', 'lib/styles/'),
|
2021-10-13 16:22:51 +02:00
|
|
|
dest: 'react-gallery/',
|
|
|
|
},
|
2021-08-31 01:49:39 +02:00
|
|
|
{
|
|
|
|
src: `./ui/css/output/`,
|
|
|
|
pattern: `*.css`,
|
|
|
|
dest: ``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/loading.html`,
|
|
|
|
dest: `loading.html`,
|
|
|
|
},
|
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules('globalthis', 'dist/browser.js'),
|
2021-08-31 01:49:39 +02:00
|
|
|
dest: `globalthis.js`,
|
|
|
|
},
|
2022-11-24 01:36:19 +01:00
|
|
|
{
|
|
|
|
src: shouldIncludeSnow
|
|
|
|
? `./node_modules/@lavamoat/snow/snow.prod.js`
|
|
|
|
: EMPTY_JS_FILE,
|
|
|
|
dest: `snow.js`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: shouldIncludeSnow ? `./app/scripts/use-snow.js` : EMPTY_JS_FILE,
|
|
|
|
dest: `use-snow.js`,
|
|
|
|
},
|
2021-08-31 01:49:39 +02:00
|
|
|
{
|
|
|
|
src: shouldIncludeLockdown
|
2022-11-30 16:19:45 +01:00
|
|
|
? getPathInsideNodeModules('ses', 'dist/lockdown.umd.min.js')
|
2021-08-31 01:49:39 +02:00
|
|
|
: EMPTY_JS_FILE,
|
|
|
|
dest: `lockdown-install.js`,
|
|
|
|
},
|
2022-06-18 09:10:30 +02:00
|
|
|
{
|
|
|
|
src: './app/scripts/init-globals.js',
|
|
|
|
dest: 'init-globals.js',
|
|
|
|
},
|
2021-08-31 01:49:39 +02:00
|
|
|
{
|
|
|
|
src: shouldIncludeLockdown
|
|
|
|
? `./app/scripts/lockdown-run.js`
|
|
|
|
: EMPTY_JS_FILE,
|
|
|
|
dest: `lockdown-run.js`,
|
|
|
|
},
|
2021-10-01 20:53:12 +02:00
|
|
|
{
|
|
|
|
src: shouldIncludeLockdown
|
|
|
|
? `./app/scripts/lockdown-more.js`
|
|
|
|
: EMPTY_JS_FILE,
|
|
|
|
dest: `lockdown-more.js`,
|
|
|
|
},
|
2021-08-31 01:49:39 +02:00
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime-cjs.js'),
|
2021-08-31 01:49:39 +02:00
|
|
|
dest: `runtime-cjs.js`,
|
2023-01-18 13:35:37 +01:00
|
|
|
pattern: '',
|
2021-08-31 01:49:39 +02:00
|
|
|
},
|
2021-08-31 05:58:50 +02:00
|
|
|
{
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime.js'),
|
2021-10-06 00:06:31 +02:00
|
|
|
dest: `runtime-lavamoat.js`,
|
2023-01-18 13:35:37 +01:00
|
|
|
pattern: '',
|
2021-10-06 00:06:31 +02:00
|
|
|
},
|
2021-08-31 01:49:39 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
const languageTags = new Set();
|
|
|
|
for (const locale of locales) {
|
|
|
|
const { code } = locale;
|
|
|
|
const tag = code.split('_')[0];
|
|
|
|
languageTags.add(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const tag of languageTags) {
|
|
|
|
allCopyTargets.push({
|
2022-11-30 16:19:45 +01:00
|
|
|
src: getPathInsideNodeModules(
|
|
|
|
'@formatjs/intl-relativetimeformat',
|
|
|
|
`dist/locale-data/${tag}.json`,
|
|
|
|
),
|
2021-08-31 01:49:39 +02:00
|
|
|
dest: `intl/${tag}/relative-time-format-data.json`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const copyTargetsDev = [
|
|
|
|
...allCopyTargets,
|
|
|
|
{
|
|
|
|
src: './development',
|
|
|
|
pattern: '/chromereload.js',
|
|
|
|
dest: ``,
|
|
|
|
},
|
|
|
|
// empty files to suppress missing file errors
|
|
|
|
{
|
|
|
|
src: EMPTY_JS_FILE,
|
|
|
|
dest: `bg-libs.js`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: EMPTY_JS_FILE,
|
|
|
|
dest: `ui-libs.js`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
const copyTargetsProd = [
|
|
|
|
...allCopyTargets,
|
|
|
|
// empty files to suppress missing file errors
|
|
|
|
{
|
|
|
|
src: EMPTY_JS_FILE,
|
|
|
|
dest: `chromereload.js`,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
return [copyTargetsProd, copyTargetsDev];
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|