logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

vite.config.js (6276B)


  1. import { fileURLToPath } from 'node:url'
  2. import { dirname, resolve } from 'node:path'
  3. import { defineConfig } from 'vite'
  4. import vue from '@vitejs/plugin-vue'
  5. import vueJsx from '@vitejs/plugin-vue-jsx'
  6. import stylelint from 'vite-plugin-stylelint'
  7. import eslint from 'vite-plugin-eslint2'
  8. import { devSwPlugin, buildSwPlugin, swMessagesPlugin } from './build/sw_plugin.js'
  9. import copyPlugin from './build/copy_plugin.js'
  10. import { getCommitHash } from './build/commit_hash.js'
  11. const localConfigPath = '<projectRoot>/config/local.json'
  12. const getLocalDevSettings = async () => {
  13. try {
  14. const settings = (await import('./config/local.json')).default
  15. if (settings.target && settings.target.endsWith('/')) {
  16. // replacing trailing slash since it can conflict with some apis
  17. // and that's how actual BE reports its url
  18. settings.target = settings.target.replace(/\/$/, '')
  19. }
  20. console.info(`Using local dev server settings (${localConfigPath}):`)
  21. console.info(JSON.stringify(settings, null, 2))
  22. return settings
  23. } catch (e) {
  24. console.info(`Local dev server settings not found (${localConfigPath}), using default`, e)
  25. return {}
  26. }
  27. }
  28. const projectRoot = dirname(fileURLToPath(import.meta.url))
  29. const getTransformSWSettings = (settings) => {
  30. if ('transformSW' in settings) {
  31. return settings.transformSW
  32. } else {
  33. console.info(
  34. '`transformSW` is not present in your local settings.\n' +
  35. 'This option controls whether the service worker should be bundled and transformed into iife (immediately-invoked function expression) during development.\n' +
  36. 'If set to false, the service worker will be served as-is, as an ES Module.\n' +
  37. 'Some browsers (e.g. Firefox) does not support ESM service workers.\n' +
  38. 'To avoid surprises, it is defaulted to true, but this can be slow.\n' +
  39. 'If you are using a browser that supports ESM service workers, you can set this option to false.\n' +
  40. `No matter your choice, you can set the transformSW option in ${localConfigPath} in to disable this message.`
  41. )
  42. return true
  43. }
  44. }
  45. export default defineConfig(async ({ mode, command }) => {
  46. const settings = await getLocalDevSettings()
  47. const target = settings.target || 'http://localhost:4000/'
  48. const transformSW = getTransformSWSettings(settings)
  49. const proxy = {
  50. '/api': {
  51. target,
  52. changeOrigin: true,
  53. cookieDomainRewrite: 'localhost',
  54. ws: true
  55. },
  56. '/nodeinfo': {
  57. target,
  58. changeOrigin: true,
  59. cookieDomainRewrite: 'localhost'
  60. },
  61. '/socket': {
  62. target,
  63. changeOrigin: true,
  64. cookieDomainRewrite: 'localhost',
  65. ws: true,
  66. headers: {
  67. 'Origin': target
  68. }
  69. },
  70. '/oauth': {
  71. target,
  72. changeOrigin: true,
  73. cookieDomainRewrite: 'localhost'
  74. }
  75. }
  76. const swSrc = 'src/sw.js'
  77. const swDest = 'sw-pleroma.js'
  78. const alias = {
  79. src: '/src',
  80. components: '/src/components',
  81. ...(mode === 'test' ? { vue: 'vue/dist/vue.esm-bundler.js' } : {})
  82. }
  83. return {
  84. plugins: [
  85. vue({
  86. template: {
  87. compilerOptions: {
  88. isCustomElement(tag) {
  89. if (tag === 'pinch-zoom') {
  90. return true
  91. }
  92. return false
  93. }
  94. }
  95. }
  96. }),
  97. vueJsx(),
  98. devSwPlugin({ swSrc, swDest, transformSW, alias }),
  99. buildSwPlugin({ swSrc, swDest }),
  100. swMessagesPlugin(),
  101. copyPlugin({
  102. inUrl: '/static/ruffle',
  103. inFs: resolve(projectRoot, 'node_modules/@ruffle-rs/ruffle')
  104. }),
  105. eslint({
  106. lintInWorker: true,
  107. lintOnStart: true,
  108. cacheLocation: resolve(projectRoot, 'node_modules/.cache/eslintcache')
  109. }),
  110. stylelint({
  111. lintInWorker: true,
  112. lintOnStart: true,
  113. cacheLocation: resolve(projectRoot, 'node_modules/.cache/stylelintcache')
  114. })
  115. ],
  116. optimizeDeps: {
  117. // For unknown reasons, during vitest, vite will re-optimize the following
  118. // deps, causing the test to reload, so add them here so that it will not
  119. // reload during tests
  120. include: [
  121. 'custom-event-polyfill',
  122. 'vue-i18n',
  123. '@ungap/event-target',
  124. 'lodash.merge',
  125. 'body-scroll-lock',
  126. '@kazvmoe-infra/pinch-zoom-element'
  127. ]
  128. },
  129. css: {
  130. devSourcemap: true
  131. },
  132. resolve: {
  133. alias
  134. },
  135. define: {
  136. 'process.env': JSON.stringify({
  137. NODE_ENV: mode === 'test' ? 'testing' : command === 'serve' ? 'development' : 'production',
  138. HAS_MODULE_SERVICE_WORKER: command === 'serve' && !transformSW
  139. }),
  140. 'COMMIT_HASH': JSON.stringify(command === 'serve' ? 'DEV' : getCommitHash()),
  141. 'DEV_OVERRIDES': JSON.stringify(command === 'serve' ? settings : undefined),
  142. '__VUE_OPTIONS_API__': true,
  143. '__VUE_PROD_DEVTOOLS__': false,
  144. '__VUE_PROD_HYDRATION_MISMATCH_DETAILS__': false
  145. },
  146. build: {
  147. sourcemap: true,
  148. rollupOptions: {
  149. input: {
  150. main: 'index.html'
  151. },
  152. output: {
  153. inlineDynamicImports: false,
  154. entryFileNames (chunkInfo) {
  155. const id = chunkInfo.facadeModuleId
  156. if (id.endsWith(swSrc)) {
  157. return swDest
  158. } else {
  159. return 'static/js/[name].[hash].js'
  160. }
  161. },
  162. chunkFileNames () {
  163. return 'static/js/[name].[hash].js'
  164. },
  165. assetFileNames (assetInfo) {
  166. const name = assetInfo.names?.[0] || ''
  167. if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(name)) {
  168. return 'static/img/[name].[hash][extname]'
  169. } else if (/\.css$/.test(name)) {
  170. return 'static/css/[name].[hash][extname]'
  171. } else {
  172. return 'static/misc/[name].[hash][extname]'
  173. }
  174. }
  175. }
  176. },
  177. },
  178. server: {
  179. ...(mode === 'test' ? {} : { proxy }),
  180. port: Number(process.env.PORT) || 8080
  181. },
  182. preview: {
  183. proxy
  184. },
  185. test: {
  186. globals: true,
  187. browser: {
  188. enabled: true,
  189. provider: 'playwright',
  190. instances: [
  191. { browser: 'firefox' }
  192. ]
  193. }
  194. }
  195. }
  196. })