index.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { load as loadYaml } from 'js-yaml'
  2. import fs from 'node:fs'
  3. import path from 'node:path'
  4. import { PluginOption } from 'vite'
  5. // we need to use relative import from atlas-meta-server because of an issue in Vite: https://github.com/vitejs/vite/issues/5370
  6. import { generateCommonMetaTags, generateMetaHtml } from '../../atlas-meta-server/src/tags'
  7. import { configSchema } from '../src/config/configSchema'
  8. // read config file - we cannot use `@/config` since it relies on YAML plugin being already loaded and that's not done in this context
  9. const rawConfigPath = path.resolve(__dirname, '..', 'atlas.config.yml')
  10. const rawConfigText = fs.readFileSync(rawConfigPath, 'utf-8')
  11. const rawConfig = loadYaml(rawConfigText)
  12. const parsedConfig = configSchema.parse(rawConfig)
  13. // This plugin fixes https://github.com/Joystream/atlas/issues/3005
  14. // By default vite was transpiling `import.meta.url` (that you can find in `node_modules/@polkadot/api/packageInfo.js`)
  15. // to the code which uses `document.baseURI`. `Document` is not available in workers and in the result we got reference errors.
  16. // This plugin replace `document.baseURI` with `self.location.href` which should be available in the worker
  17. export const PolkadotWorkerMetaFixPlugin: PluginOption = {
  18. name: 'resolve-import-meta-polkadot',
  19. resolveImportMeta(_, { chunkId }) {
  20. if (chunkId.includes('polkadot-worker')) {
  21. return 'self.location.href'
  22. }
  23. },
  24. }
  25. // This plugin overrides the name property in manifest.webmanifest file
  26. export const AtlasWebmanifestPlugin: PluginOption = {
  27. name: 'atlas-webmanifest',
  28. buildStart() {
  29. const inputManifestPath = path.resolve('src/public/manifest.webmanifest')
  30. const manifestData = JSON.parse(fs.readFileSync(inputManifestPath, `utf-8`))
  31. Object.assign(manifestData, {
  32. name: parsedConfig.general.appName,
  33. })
  34. try {
  35. this.emitFile({
  36. type: 'asset',
  37. source: JSON.stringify(manifestData, null, 2),
  38. fileName: path.normalize('manifest.webmanifest'),
  39. })
  40. } catch (err) {
  41. throw new Error('Failed to emit asset file, possibly a naming conflict?')
  42. }
  43. },
  44. }
  45. // This plugin replaces <meta-tags /> in index.html with the actual meta tags
  46. export const AtlasHtmlMetaTagsPlugin: PluginOption = {
  47. name: 'atlas-html-meta-tags',
  48. transformIndexHtml: {
  49. enforce: 'pre',
  50. transform: (html: string) => {
  51. const metaTags = generateCommonMetaTags(
  52. parsedConfig.general.appName,
  53. parsedConfig.general.appUrl,
  54. parsedConfig.general.appName,
  55. parsedConfig.general.appDescription,
  56. parsedConfig.general.appOgImgPath,
  57. parsedConfig.general.appTwitterId
  58. )
  59. const titleHtml = `<title>${parsedConfig.general.appName}</title>`
  60. const metaHtml = generateMetaHtml(metaTags, true)
  61. const finalMetaHtml = [titleHtml, metaHtml].join('\n')
  62. return html.replace('<meta-tags />', finalMetaHtml)
  63. },
  64. },
  65. }
  66. // This plugin enables /embedded path in development mode.
  67. // Without this, dev server will always try to serve main index.html file
  68. export const EmbeddedFallbackPlugin: PluginOption = {
  69. name: 'embedded-fallback',
  70. configureServer(server) {
  71. server.middlewares.use('/embedded', (req, res, next) => {
  72. if (req.url?.includes('.')) {
  73. next()
  74. return
  75. }
  76. req.url = '/index.html'
  77. req.originalUrl = '/embedded/index.html'
  78. next()
  79. })
  80. },
  81. }