rollup.config.ts 3.2 KB

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