rollup.config.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { defineConfig, Plugin, RollupLog } from 'rollup'
  5. import typescript from '@rollup/plugin-typescript'
  6. import terser from '@rollup/plugin-terser'
  7. import fg from 'fast-glob'
  8. import { basename, dirname, join } from 'path'
  9. import { copyFileSync, opendirSync, rmSync, Dir } from 'fs'
  10. import { fileURLToPath } from 'url'
  11. // cleanup dist dir
  12. const __dirname = fileURLToPath(new URL('.', import.meta.url))
  13. cleanDir(join(__dirname, './dist'))
  14. const modules = fg.sync(['!./src/*.d.ts', './src/*.ts'])
  15. export default defineConfig([
  16. {
  17. input: Object.fromEntries(modules.map((p) => [basename(p, '.ts'), p])),
  18. output: [
  19. {
  20. format: 'esm',
  21. dir: './dist',
  22. preserveModules: true,
  23. preserveModulesRoot: 'src',
  24. entryFileNames: (chunkInfo) => {
  25. if (chunkInfo.name.includes('node_modules')) {
  26. return externalLibPath(chunkInfo.name) + '.js'
  27. }
  28. return '[name].js'
  29. }
  30. },
  31. {
  32. format: 'cjs',
  33. dir: './dist',
  34. preserveModules: true,
  35. preserveModulesRoot: 'src',
  36. entryFileNames: (chunkInfo) => {
  37. if (chunkInfo.name.includes('node_modules')) {
  38. return externalLibPath(chunkInfo.name) + '.cjs'
  39. }
  40. return '[name].cjs'
  41. }
  42. }
  43. ],
  44. plugins: [
  45. typescript({
  46. declaration: true,
  47. declarationDir: './dist',
  48. rootDir: 'src'
  49. }),
  50. makeFlatPackageInDist()
  51. ],
  52. onwarn
  53. },
  54. {
  55. input: 'src/index.ts',
  56. output: {
  57. format: 'iife',
  58. name: '__TAURI_IIFE__',
  59. footer: 'window.__TAURI__ = __TAURI_IIFE__',
  60. file: '../../crates/tauri/scripts/bundle.global.js'
  61. },
  62. plugins: [typescript(), terser()],
  63. onwarn
  64. }
  65. ])
  66. function externalLibPath(path: string) {
  67. return `external/${basename(dirname(path))}/${basename(path)}`
  68. }
  69. function onwarn(warning: RollupLog) {
  70. // deny warnings by default
  71. throw Object.assign(new Error(), warning)
  72. }
  73. function makeFlatPackageInDist(): Plugin {
  74. return {
  75. name: 'makeFlatPackageInDist',
  76. writeBundle() {
  77. // copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
  78. fg.sync('(LICENSE*|*.md|package.json)').forEach((f) =>
  79. copyFileSync(f, `dist/${f}`)
  80. )
  81. }
  82. }
  83. }
  84. function cleanDir(path: string) {
  85. let dir: Dir
  86. try {
  87. dir = opendirSync(path)
  88. } catch (err: any) {
  89. switch (err.code) {
  90. case 'ENOENT':
  91. return // Noop when directory don't exists.
  92. case 'ENOTDIR':
  93. throw new Error(`'${path}' is not a directory.`)
  94. default:
  95. throw err
  96. }
  97. }
  98. let file = dir.readSync()
  99. while (file) {
  100. const filePath = join(path, file.name)
  101. rmSync(filePath, { recursive: true })
  102. file = dir.readSync()
  103. }
  104. dir.closeSync()
  105. }