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
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createTask, composeSeries } = require('./task');
|
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,
|
2021-09-28 18:13:26 +02:00
|
|
|
buildType,
|
2021-08-31 01:49:39 +02:00
|
|
|
}) {
|
|
|
|
const [copyTargetsProd, copyTargetsDev] = getCopyTargets(
|
|
|
|
shouldIncludeLockdown,
|
|
|
|
);
|
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)) {
|
|
|
|
copyTargetsProd.push(...additionalBuildTargets[buildType]);
|
|
|
|
copyTargetsDev.push(...additionalBuildTargets[buildType]);
|
|
|
|
}
|
2021-09-08 22:08:23 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const prod = createTask(
|
|
|
|
'static:prod',
|
|
|
|
composeSeries(
|
2021-09-28 23:45:00 +02:00
|
|
|
...copyTargetsProd.map((target) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
return async function copyStaticAssets() {
|
2021-02-04 19:15:23 +01:00
|
|
|
await performCopy(target);
|
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
}),
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const dev = createTask(
|
|
|
|
'static:dev',
|
|
|
|
composeSeries(
|
|
|
|
...copyTargetsDev.map((target) => {
|
|
|
|
return async function copyStaticAssets() {
|
2021-02-04 19:15:23 +01:00
|
|
|
await setupLiveCopy(target);
|
|
|
|
};
|
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-02-04 19:15:23 +01:00
|
|
|
return { dev, prod };
|
2020-03-09 01:55:02 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function setupLiveCopy(target) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const pattern = 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);
|
|
|
|
performCopy(target);
|
|
|
|
});
|
|
|
|
await performCopy(target);
|
2020-03-09 01:55:02 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function performCopy(target) {
|
|
|
|
await Promise.all(
|
|
|
|
browserPlatforms.map(async (platform) => {
|
|
|
|
if (target.pattern) {
|
|
|
|
await copyGlob(
|
|
|
|
target.src,
|
|
|
|
`${target.src}${target.pattern}`,
|
|
|
|
`./dist/${platform}/${target.dest}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
} else {
|
|
|
|
await copyGlob(
|
|
|
|
target.src,
|
|
|
|
`${target.src}`,
|
|
|
|
`./dist/${platform}/${target.dest}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
function getCopyTargets(shouldIncludeLockdown) {
|
|
|
|
const allCopyTargets = [
|
|
|
|
{
|
|
|
|
src: `./app/_locales/`,
|
|
|
|
dest: `_locales`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/images/`,
|
|
|
|
dest: `images`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./node_modules/@metamask/contract-metadata/images/`,
|
|
|
|
dest: `images/contract`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/fonts/`,
|
|
|
|
dest: `fonts`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./app/vendor/`,
|
|
|
|
dest: `vendor`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./node_modules/@fortawesome/fontawesome-free/webfonts/`,
|
|
|
|
dest: `fonts/fontawesome`,
|
|
|
|
},
|
2021-10-13 16:22:51 +02:00
|
|
|
{
|
|
|
|
src: `./node_modules/react-responsive-carousel/lib/styles`,
|
|
|
|
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`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: `./node_modules/globalthis/dist/browser.js`,
|
|
|
|
dest: `globalthis.js`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: shouldIncludeLockdown
|
|
|
|
? `./node_modules/ses/dist/lockdown.umd.min.js`
|
|
|
|
: EMPTY_JS_FILE,
|
|
|
|
dest: `lockdown-install.js`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
// eslint-disable-next-line node/no-extraneous-require
|
|
|
|
src: require.resolve('@lavamoat/lavapack/src/runtime-cjs.js'),
|
|
|
|
dest: `runtime-cjs.js`,
|
|
|
|
},
|
2021-08-31 05:58:50 +02:00
|
|
|
{
|
2021-10-06 00:06:31 +02:00
|
|
|
// eslint-disable-next-line node/no-extraneous-require
|
|
|
|
src: require.resolve('@lavamoat/lavapack/src/runtime.js'),
|
|
|
|
dest: `runtime-lavamoat.js`,
|
|
|
|
},
|
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({
|
|
|
|
src: `./node_modules/@formatjs/intl-relativetimeformat/dist/locale-data/${tag}.json`,
|
|
|
|
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
|
|
|
}
|