diff --git a/config/helpers/vite_plugin_images.mjs b/config/helpers/vite_plugin_images.mjs new file mode 100644 index 0000000000000000000000000000000000000000..993612c6e89a701e2d310965095666e62b8b45b4 --- /dev/null +++ b/config/helpers/vite_plugin_images.mjs @@ -0,0 +1,56 @@ +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, + }, + }; + }, + }; +} diff --git a/vite.config.js b/vite.config.js index 73d1d6e7038d274cd6db913e036a977ac56b9438..010478b558472f08416823d447473d45790d7d2c 100644 --- a/vite.config.js +++ b/vite.config.js @@ -21,6 +21,7 @@ import { PageEntrypointsPlugin } from './config/helpers/vite_plugin_page_entrypo import { FixedRubyPlugin } from './config/helpers/vite_plugin_ruby_fixed.mjs'; import { StylePlugin } from './config/helpers/vite_plugin_style.mjs'; import { IconsPlugin } from './config/helpers/vite_plugin_icons.mjs'; +import { ImagesPlugin } from './config/helpers/vite_plugin_images.mjs'; /* eslint-enable import/extensions */ let viteGDKConfig; @@ -86,6 +87,7 @@ export default defineConfig({ plugins: [ PageEntrypointsPlugin(), IconsPlugin(), + ImagesPlugin(), StylePlugin({ shouldWatch: viteGDKConfig.hmr !== null }), viteTailwindCompilerPlugin({ shouldWatch: viteGDKConfig.hmr !== null }), CopyPlugin({