mirror of
https://gitlab.com/gitlab-org/gitlab-foss.git
synced 2025-08-20 14:11:11 +00:00
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import path from 'node:path';
|
|
import { readdir } from 'node:fs/promises';
|
|
|
|
const imagesPaths = [
|
|
path.resolve(__dirname, '../..', 'app/assets/images'),
|
|
path.resolve(__dirname, '../..', 'ee/app/assets/images'),
|
|
path.resolve(__dirname, '../..', 'jh/app/assets/images'),
|
|
];
|
|
|
|
async function getAllFiles(dir, prependPath = '') {
|
|
const result = [];
|
|
let files = []
|
|
try {
|
|
files = await readdir(dir, { withFileTypes: true });
|
|
} catch(e) {}
|
|
|
|
for (const file of files) {
|
|
const filePath = path.join(dir, file.name);
|
|
|
|
if (file.isDirectory()) {
|
|
const nestedFiles = await getAllFiles(filePath, `${prependPath}${file.name}/`);
|
|
result.push(...nestedFiles);
|
|
} else {
|
|
result.push(prependPath + file.name);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export async function ImagesPlugin() {
|
|
return {
|
|
name: 'vite-plugin-gitlab-images',
|
|
async config() {
|
|
const [CEfiles, EEfiles, JHfiles] = await Promise.all(imagesPaths.map(async imagesPath => await getAllFiles(imagesPath)));
|
|
const [CEpath, EEpath, JHpath] = imagesPaths;
|
|
const mappings = [[CEpath, CEfiles], [EEpath, EEfiles], [JHpath, JHfiles]].reduce((acc, [filesPath, filenames]) => {
|
|
filenames.forEach(filename => {
|
|
acc[filename] = path.resolve(filesPath, filename);
|
|
});
|
|
return acc;
|
|
}, {});
|
|
const alias = Object.keys(mappings).map(mapping => {
|
|
return {
|
|
find: mapping,
|
|
replacement: mappings[mapping],
|
|
}
|
|
});
|
|
return {
|
|
resolve: {
|
|
alias,
|
|
},
|
|
};
|
|
},
|
|
};
|
|
}
|