tauri-config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { existsSync, readFileSync } from 'fs-extra'
  2. import { TauriConfig } from 'types'
  3. import merge from 'webpack-merge'
  4. import logger from '../helpers/logger'
  5. import * as appPaths from './app-paths'
  6. import nonWebpackRequire from '../helpers/non-webpack-require'
  7. import chalk from 'chalk'
  8. import { isTauriConfig, ajv } from '../types/config.validator'
  9. const error = logger('ERROR:', chalk.red)
  10. const getTauriConfig = (cfg: Partial<TauriConfig>): TauriConfig => {
  11. const pkgPath = appPaths.resolve.app('package.json')
  12. const tauriConfPath = appPaths.resolve.tauri('tauri.conf.json')
  13. if (!existsSync(pkgPath)) {
  14. error("Could not find a package.json in your app's directory.")
  15. process.exit(1)
  16. }
  17. if (!existsSync(tauriConfPath)) {
  18. error(
  19. "Could not find a tauri config (tauri.conf.json) in your app's directory."
  20. )
  21. process.exit(1)
  22. }
  23. const tauriConf = JSON.parse(readFileSync(tauriConfPath).toString()) as TauriConfig
  24. const pkg = nonWebpackRequire(pkgPath) as { productName: string }
  25. const config = merge(
  26. {
  27. build: {},
  28. ctx: {},
  29. tauri: {
  30. embeddedServer: {
  31. active: true,
  32. port: "3000"
  33. },
  34. bundle: {
  35. active: true,
  36. icon: [],
  37. resources: [],
  38. externalBin: [],
  39. deb: {
  40. depends: []
  41. },
  42. osx: {
  43. frameworks: []
  44. }
  45. },
  46. whitelist: {
  47. all: false
  48. },
  49. window: {
  50. title: pkg.productName
  51. },
  52. security: {
  53. csp: "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'"
  54. },
  55. edge: {
  56. active: true
  57. },
  58. inliner: {
  59. active: true
  60. }
  61. }
  62. } as any,
  63. tauriConf as any,
  64. cfg as any
  65. ) as TauriConfig
  66. if (!isTauriConfig(config)) {
  67. const messages = ajv.errorsText(
  68. isTauriConfig.errors?.filter(e => e.keyword !== 'if').map(e => {
  69. e.dataPath = e.dataPath.replace(/\./g, ' > ')
  70. if (e.keyword === 'additionalProperties' && typeof e.message === 'string' && 'additionalProperty' in e.params) {
  71. e.message = `has unknown property ${e.params.additionalProperty}`
  72. }
  73. return e
  74. }), { dataVar: 'tauri.conf.json', separator: '\n' }
  75. ).split('\n')
  76. for (const message of messages) {
  77. error(message)
  78. }
  79. process.exit(1)
  80. }
  81. const runningDevServer = config.build.devPath?.startsWith('http')
  82. if (!runningDevServer) {
  83. config.build.devPath = appPaths.resolve.tauri(config.build.devPath)
  84. process.env.TAURI_DIST_DIR = config.build.devPath
  85. }
  86. if (config.build.distDir) {
  87. config.build.distDir = appPaths.resolve.tauri(config.build.distDir)
  88. process.env.TAURI_DIST_DIR = config.build.distDir
  89. }
  90. // OSX bundle config
  91. if (config.tauri.bundle.osx) {
  92. const license = config.tauri.bundle.osx.license
  93. if (typeof license === 'string') {
  94. config.tauri.bundle.osx.license = appPaths.resolve.tauri(license)
  95. } else if (license !== null) {
  96. const licensePath = appPaths.resolve.app('LICENSE')
  97. if (existsSync(licensePath)) {
  98. config.tauri.bundle.osx.license = licensePath
  99. }
  100. }
  101. }
  102. // bundle targets
  103. if (Array.isArray(config.tauri.bundle.targets)) {
  104. if (process.platform !== 'win32') {
  105. config.tauri.bundle.targets = config.tauri.bundle.targets.filter(t => t !== 'msi')
  106. }
  107. }
  108. process.env.TAURI_DIR = appPaths.tauriDir
  109. process.env.TAURI_CONFIG = JSON.stringify(config)
  110. return config
  111. }
  112. export default getTauriConfig