aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/rollup.config.js
blob: 547260bcbb9be8b5da1e732daa78ce8165be70ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import node from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import terser from '@rollup/plugin-terser'
import { existsSync } from 'fs'
import { join } from 'path'
import { bundleStats } from 'rollup-plugin-bundle-stats'

const prod = process.env.PRODUCTION
const addon = process.env.ADDON

function resolve (file, origin) {
  // Your way to resolve local include path
}

function pathResolve (options) {
  return {
    resolveId: function (file, origin) {
      // Your local include path must either starts with `./` or `../`
      if (file.startsWith('./') || file.startsWith('../')) {
        // Return an absolute include path
        return resolve(file, origin)
      }
      return null // Continue to the next plugins!
    },
  }
}

const general = {
  output: [
    {
      dir: './dist',
      format: 'esm',
      entryFileNames: '[name].mjs',
    },
  ],
  watch: {
    clearScreen: false,
    include: ['src/**', 'node_modules/mapclay/dist/mapclay.mjs'],
  },
  context: 'window',
  plugins: [
    {
      name: 'watch-mapclay',
      buildStart () {
        const mapclayPath = join(process.cwd(), 'node_modules', 'mapclay', 'dist', 'mapclay.mjs')
        if (existsSync(mapclayPath)) {
          this.addWatchFile(mapclayPath)
        } else {
          console.warn('mapclay.mjs not found at:', mapclayPath)
        }
      },
    },
    {
      name: 'leader-line',
      transform (code, id) {
        if (id.includes('node_modules/leader-line/')) {
          return `${code}\nexport default LeaderLine;`
        }
        return null
      },
    },
    pathResolve(),
    node(),
    commonjs(),
    prod && terser({
      keep_fnames: true,
    }),
    prod && bundleStats(),
  ],
}

export default [
  {
    input: 'src/editor.mjs',
  },
  {
    input: 'src/dumbymap.mjs',
  },
]
  .map(config => ({ ...general, ...config }))
  .filter(config => {
    if (addon) return config.input.match(/dumbymap/)
    if (!prod) return config.input.match(/editor/)
    return true
  })
  .map(config => {
    if (!addon) return config

    config.output.forEach(o => { o.dir = './addon' })
    config.plugins.push({
      name: 'remove-exports',
      transform (code, id) {
        if (id.includes(config.input)) {
          // remove export keyword for addon
          const transformedCode = code.replace(/\n(\s*)export\s*/g, '$1')
          return {
            code: [
              transformedCode,
              'globalThis.generateMaps = generateMaps',
              'globalThis.mapclay = mapclay',
            ].join('\n'),
          }
        }
        return null
      },
    })

    return config
  })