rollup.config.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2019-2023 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 {
  10. writeFileSync,
  11. copyFileSync,
  12. opendirSync,
  13. rmSync,
  14. Dir,
  15. readFileSync
  16. } from 'fs'
  17. import { fileURLToPath } from 'url'
  18. // cleanup dist dir
  19. const __dirname = fileURLToPath(new URL('.', import.meta.url))
  20. cleanDir(join(__dirname, './dist'))
  21. const modules = fg.sync(['./src/*.ts'])
  22. export default defineConfig([
  23. {
  24. input: Object.fromEntries(modules.map((p) => [basename(p, '.ts'), p])),
  25. output: [
  26. {
  27. format: 'esm',
  28. dir: './dist',
  29. preserveModules: true,
  30. preserveModulesRoot: 'src',
  31. entryFileNames: '[name].js'
  32. },
  33. {
  34. format: 'cjs',
  35. dir: './dist',
  36. preserveModules: true,
  37. preserveModulesRoot: 'src',
  38. entryFileNames: '[name].cjs'
  39. }
  40. ],
  41. plugins: [
  42. typescript({
  43. declaration: true,
  44. declarationDir: './dist/types',
  45. rootDir: 'src'
  46. }),
  47. makeFlatPackageInDist()
  48. ],
  49. onwarn
  50. },
  51. {
  52. input: 'src/index.ts',
  53. output: {
  54. format: 'iife',
  55. name: '__TAURI_IIFE__',
  56. footer: 'window.__TAURI__ = __TAURI_IIFE__',
  57. file: '../../core/tauri/scripts/bundle.global.js'
  58. },
  59. plugins: [typescript(), terser()],
  60. onwarn
  61. }
  62. ])
  63. function onwarn(warning: RollupLog) {
  64. // deny warnings by default
  65. throw Object.assign(new Error(), warning)
  66. }
  67. function makeFlatPackageInDist(): Plugin {
  68. return {
  69. name: 'makeFlatPackageInDist',
  70. writeBundle() {
  71. // append our api modules to `exports` in `package.json` then write it to `./dist`
  72. const pkg = JSON.parse(readFileSync('package.json', 'utf8'))
  73. const mods = modules.map((p) => basename(p).split('.')[0])
  74. const outputPkg = {
  75. ...pkg,
  76. devDependencies: {},
  77. exports: Object.assign(
  78. {},
  79. ...mods.map((mod) => {
  80. const exports: Record<
  81. string,
  82. { types: string; import: string; require: string }
  83. > = {}
  84. const key = mod === 'index' ? '.' : `./${mod}`
  85. exports[key] = {
  86. types: `./types/${mod}.d.ts`,
  87. import: `./${mod}.js`,
  88. require: `./${mod}.cjs`
  89. }
  90. return exports
  91. }),
  92. // if for some reason in the future we manually add something in the `exports` field
  93. // this will ensure it doesn't get overwritten by the logic above
  94. { ...(pkg.exports || {}) }
  95. )
  96. }
  97. writeFileSync(
  98. 'dist/package.json',
  99. JSON.stringify(outputPkg, undefined, 2)
  100. )
  101. // copy necessary files like `CHANGELOG.md` , `README.md` and Licenses to `./dist`
  102. fg.sync('(LICENSE*|*.md)').forEach((f) => copyFileSync(f, `dist/${f}`))
  103. }
  104. }
  105. }
  106. function cleanDir(path: string) {
  107. let dir: Dir
  108. try {
  109. dir = opendirSync(path)
  110. } catch (err: any) {
  111. switch (err.code) {
  112. case 'ENOENT':
  113. return // Noop when directory don't exists.
  114. case 'ENOTDIR':
  115. throw new Error(`'${path}' is not a directory.`)
  116. default:
  117. throw err
  118. }
  119. }
  120. let file = dir.readSync()
  121. while (file) {
  122. const filePath = join(path, file.name)
  123. rmSync(filePath, { recursive: true })
  124. file = dir.readSync()
  125. }
  126. dir.closeSync()
  127. }