99 lines
3.1 KiB
JavaScript
Raw Normal View History

const path = require('path');
const fs = require('fs-extra');
2021-07-15 23:42:56 -04:00
const Parcel = require('parcel-bundler');
2021-07-12 23:20:42 -04:00
const destSVGPath = path.resolve(__dirname, '..', 'svg');
2022-06-30 00:17:25 -04:00
const distBasePath = path.resolve(__dirname, '..', 'dist');
2021-07-12 23:20:42 -04:00
const srcPath = path.resolve(__dirname, '..', 'src');
2021-11-30 23:38:46 -05:00
/** Create icons cache. */
async function consolidateSVGFiles() {
2021-07-12 23:20:42 -04:00
console.log('[1/2] Generate icon cache for extension.');
await fs
2021-11-30 23:38:46 -05:00
.copy(path.resolve(srcPath, 'custom'), destSVGPath)
.then(() => fs.readdir(destSVGPath))
.then((files) => Object.fromEntries(files.map((filename) => [filename, filename])))
.then((iconsDict) => fs.writeJSON(path.resolve(srcPath, 'icon-list.json'), iconsDict));
2021-07-12 23:20:42 -04:00
}
2022-07-18 01:16:31 -04:00
function bundleJS(outDir, entryFile) {
const parcelOptions = {
2021-07-15 23:42:56 -04:00
watch: false,
minify: true,
sourceMaps: false,
2022-07-18 01:16:31 -04:00
outDir,
};
2021-07-15 23:42:56 -04:00
const bundler = new Parcel(entryFile, parcelOptions);
2022-07-18 01:16:31 -04:00
return bundler.bundle();
}
function src(distPath) {
console.log('[2/2] Bundle extension manifest, images and main script.');
2021-07-13 09:23:06 +01:00
2021-12-01 00:38:14 -05:00
const copyIcons = fs.copy(destSVGPath, distPath);
2021-11-30 23:38:46 -05:00
2022-07-18 01:16:31 -04:00
const bundleMainScript = () => bundleJS(distPath, path.resolve(srcPath, 'main.js'));
const bundlePopupScript = () =>
bundleJS(distPath, path.resolve(srcPath, 'ui', 'popup', 'settings-popup.js'));
2022-07-20 01:01:02 -04:00
const bundleOptionsScript = () =>
bundleJS(distPath, path.resolve(srcPath, 'ui', 'options', 'options.js'));
const bundleBackgroundScript = () =>
bundleJS(distPath, path.resolve(srcPath, 'background', 'background.js'));
const bundleAll = bundleMainScript()
.then(bundlePopupScript)
.then(bundleOptionsScript)
.then(bundleBackgroundScript);
2022-07-18 01:16:31 -04:00
2022-07-06 14:47:09 -04:00
const copyPopup = Promise.all(
2022-07-19 01:28:44 -04:00
['settings-popup.html', 'settings-popup.css', 'settings-popup.github-logo.svg'].map((file) =>
fs.copy(path.resolve(srcPath, 'ui', 'popup', file), path.resolve(distPath, file))
2022-07-06 14:47:09 -04:00
)
);
2022-07-20 01:01:02 -04:00
const copyOptions = Promise.all(
['options.html', 'options.css'].map((file) =>
fs.copy(path.resolve(srcPath, 'ui', 'options', file), path.resolve(distPath, file))
)
);
2022-07-06 14:47:09 -04:00
const copyStyles = fs.copy(
path.resolve(srcPath, 'injected-styles.css'),
path.resolve(distPath, 'injected-styles.css')
);
2022-07-06 14:27:20 -04:00
2022-07-17 00:12:03 -04:00
const copyExtensionLogos = fs.copy(path.resolve(srcPath, 'extensionIcons'), distPath);
return Promise.all([
copyExtensionLogos,
copyOptions,
copyPopup,
copyStyles,
copyIcons,
bundleAll,
]);
2022-06-30 00:17:25 -04:00
}
function buildManifest(distPath, manifestName) {
return Promise.all([
fs.readJson(path.resolve(srcPath, 'manifests', 'base.json')),
fs.readJson(path.resolve(srcPath, 'manifests', manifestName)),
])
.then(([base, custom]) => ({ ...base, ...custom }))
.then((manifest) =>
fs.writeJson(path.resolve(distPath, 'manifest.json'), manifest, { spaces: 2 })
);
}
2022-06-30 00:17:25 -04:00
function buildDist(name, manifestName) {
const distPath = path.resolve(distBasePath, name);
return fs
.ensureDir(distPath)
2022-06-30 00:17:25 -04:00
.then(consolidateSVGFiles)
.then(() => src(distPath))
.then(() => buildManifest(distPath, manifestName))
.catch(console.error);
}
buildDist('firefox', 'firefox.json').then(() => buildDist('chrome-edge', 'chrome-edge.json'));