build(frontend): fix webpack config

This commit is contained in:
Sarah Vaupel 2024-09-03 02:44:26 +02:00
parent f4ee39c717
commit c7d5cbfb49
2 changed files with 23 additions and 23 deletions

View File

@ -27,4 +27,4 @@ RUN make node_modules
RUN make well-known
ENV FRADRIVE_MAKE_TARGET=frontend-watch
ENTRYPOINT make -- ${FRADRIVE_MAKE_TARGET} CHROME_BIN=${CHROME_BIN}
ENTRYPOINT make -- ${FRADRIVE_MAKE_TARGET} CHROME_BIN=${CHROME_BIN}

View File

@ -3,12 +3,12 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import webpack from 'webpack';
import path from 'path';
import { resolve, join, relative } from 'node:path';
import { execSync } from 'node:child_process';
import tmp from 'tmp';
tmp.setGracefulCleanup();
import fs from 'fs-extra';
import { glob, globSync } from 'glob';
import execSync from 'child_process';
import axios from 'axios';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
@ -34,8 +34,8 @@ const packageVersion = packageJson.version;
import faviconJson from './config/favicon.json' assert { type: 'json' };
async function webpackConfig() {
const wellKnownCacheDir = path.resolve('.cache/well-known');
const assetsDirectory = path.resolve('assets');
const wellKnownCacheDir = resolve('.cache/well-known');
const assetsDirectory = resolve('assets');
let faviconApiVersion = undefined;
if (!fs.existsSync(wellKnownCacheDir)) {
@ -111,8 +111,8 @@ async function webpackConfig() {
},
entry: {
main: [ path.resolve('frontend/src/polyfill.js'),
path.resolve('frontend/src/main.js'),
main: [ resolve('frontend/src/polyfill.js'),
resolve('frontend/src/main.js'),
]
},
@ -125,14 +125,14 @@ async function webpackConfig() {
ignoreOrder: false, // Enable to remove warnings about conflicting order
}),
new WebpackManifestPlugin({
fileName: path.resolve('config/webpack.yml'),
fileName: resolve('config/webpack.yml'),
publicPath: `wp-${webpackVersion}`,
generate: (seed, files, entrypoints) => Object.keys(entrypoints).reduce((acc, fs) => ({...acc, [fs]: files.filter(file => entrypoints[fs].filter(basename => !(/\.map$/.test(basename))).some(basename => file.path.endsWith(basename))).filter(file => file.isInitial).map(file => file.path)}), {}),
serialize: yaml.dump
}),
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [ path.resolve('static'),
path.resolve('well-known'),
cleanOnceBeforeBuildPatterns: [ resolve('static'),
resolve('well-known'),
]
}),
new webpack.IgnorePlugin({
@ -180,8 +180,8 @@ async function webpackConfig() {
cacheHash.update(JSON.stringify(langJsons));
const cacheFiles = new Set([
...(Array.from(langs).map(lang => path.resolve(langJsons[lang].masterPicture))),
path.resolve('config/robots.txt'),
...(Array.from(langs).map(lang => resolve(langJsons[lang].masterPicture))),
resolve('config/robots.txt'),
]);
for (const cacheFile of cacheFiles) {
@ -192,7 +192,7 @@ async function webpackConfig() {
let cachedVersion = undefined;
const versionFile = path.resolve('.well-known-cache', `${cacheDigest}.version`);
const versionFile = resolve('.well-known-cache', `${cacheDigest}.version`);
try {
if (fs.existsSync(versionFile)) {
cachedVersion = fs.readFileSync(versionFile, 'utf8');
@ -209,14 +209,14 @@ async function webpackConfig() {
return Array.from(langs).map(lang => {
const faviconConfig = { versioning: { param_name: 'v', param_value: versionDigest.substr(0,10) }, ...langJsons[lang] };
const cacheDirectory = path.resolve('.well-known-cache', `${cacheDigest}-${lang}`);
const cacheDirectory = resolve('.well-known-cache', `${cacheDigest}-${lang}`);
if (fs.existsSync(wellKnownCacheDir)) {
console.log("Using favicons generated by nix");
return [
new CopyPlugin({
patterns: [
{ from: path.resolve(wellKnownCacheDir, lang), to: path.resolve('well-known', lang) }
{ from: resolve(wellKnownCacheDir, lang), to: resolve('well-known', lang) }
]
})
];
@ -225,7 +225,7 @@ async function webpackConfig() {
return [
new CopyPlugin({
patterns: [
{ from: cacheDirectory, to: path.resolve('well-known', lang) }
{ from: cacheDirectory, to: resolve('well-known', lang) }
]
})
];
@ -236,24 +236,24 @@ async function webpackConfig() {
return [
new RealFaviconPlugin({
faviconJson: path.relative(".", tmpobj.name),
outputPath: path.resolve('well-known', lang),
faviconJson: relative(".", tmpobj.name),
outputPath: resolve('well-known', lang),
inject: false
}),
new CopyPlugin({
patterns: [
{ from: 'config/robots.txt', to: path.resolve('well-known', lang, 'robots.txt') },
{ from: 'config/robots.txt', to: resolve('well-known', lang, 'robots.txt') },
]
}),
{ apply: compiler => compiler.hooks.afterEmit.tap('AfterEmitPlugin', compilation => {
const imgFiles = globSync(path.resolve('well-known', lang) + '/*.@(png)');
const imgFiles = globSync(resolve('well-known', lang) + '/*.@(png)');
const imgFilesArgs = Array.from(imgFiles).join(" ");
execSync(`exiftool -overwrite_original -all= ${imgFilesArgs}`, { stdio: 'inherit' });
})
},
{ apply: compiler => compiler.hooks.afterEmit.tap('AfterEmitPlugin', compilation => {
fs.ensureDirSync('.well-known-cache');
fs.copySync(path.resolve('well-known', lang), cacheDirectory);
fs.copySync(resolve('well-known', lang), cacheDirectory);
if (!fs.existsSync(versionFile)) {
fs.writeFileSync(versionFile, faviconApiVersion, { encoding: 'utf8' });
}
@ -268,7 +268,7 @@ async function webpackConfig() {
output: {
chunkFilename: '[chunkhash].js',
filename: '[chunkhash].js',
path: path.resolve('static', `wp-${webpackVersion}`),
path: resolve('static', `wp-${webpackVersion}`),
publicPath: `/static/res/wp-${webpackVersion}/`,
hashFunction: 'shake256',
hashDigestLength: 36
@ -293,7 +293,7 @@ async function webpackConfig() {
mode: 'production',
recordsPath: path.join(path.resolve('records.json')),
recordsPath: join(resolve('records.json')),
performance: {
assetFilter: (assetFilename) => !(/\.(map|svg|ttf|eot)$/.test(assetFilename))