rollup.config.ts 3.6 KB

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