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');
|
2020-07-03 02:34:48 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createTask, composeSeries } = require('./task');
|
2021-09-28 18:13:26 +02:00
|
|
|
const { BuildTypes } = 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,
|
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 = {
|
|
|
|
[BuildTypes.beta]: [
|
|
|
|
{
|
|
|
|
src: './app/build-types/beta/',
|
|
|
|
dest: `images`,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
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`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// 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
|
|
|
{
|
|
|
|
src: `./app/phishing.html`,
|
|
|
|
dest: `phishing.html`,
|
|
|
|
},
|
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
|
|
|
}
|