rollup.config.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, 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 chunkInfo.name.replace('node_modules', 'external') + '.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 chunkInfo.name.replace('node_modules', 'external') + '.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: '../../core/tauri/scripts/bundle.global.js'
  61. },
  62. plugins: [typescript(), terser()],
  63. onwarn
  64. }
  65. ])
  66. function onwarn(warning: RollupLog) {
  67. // deny warnings by default
  68. throw Object.assign(new Error(), warning)
  69. }
  70. function makeFlatPackageInDist(): Plugin {
  71. return {
  72. name: 'makeFlatPackageInDist',
  73. writeBundle() {
  74. // copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
  75. fg.sync('(LICENSE*|*.md|package.json)').forEach((f) =>
  76. copyFileSync(f, `dist/${f}`)
  77. )
  78. }
  79. }
  80. }
  81. function cleanDir(path: string) {
  82. let dir: Dir
  83. try {
  84. dir = opendirSync(path)
  85. } catch (err: any) {
  86. switch (err.code) {
  87. case 'ENOENT':
  88. return // Noop when directory don't exists.
  89. case 'ENOTDIR':
  90. throw new Error(`'${path}' is not a directory.`)
  91. default:
  92. throw err
  93. }
  94. }
  95. let file = dir.readSync()
  96. while (file) {
  97. const filePath = join(path, file.name)
  98. rmSync(filePath, { recursive: true })
  99. file = dir.readSync()
  100. }
  101. dir.closeSync()
  102. }