File size: 3,977 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* eslint-disable import/no-extraneous-dependencies */
import { readFile, mkdir, writeFile } from 'fs/promises'
import { rollup, watch, InputOptions, OutputOptions } from 'rollup'
import isUndefined from 'lodash/isUndefined.js'
import ora from 'ora'

import { ADMIN_JS_TMP_DIR, NODE_ENV } from './constants.js'

export class AssetBundler {
  static DEFAULT_EXTENSIONS = ['.mjs', '.cjs', '.js', '.jsx', '.json', '.ts', '.tsx', '.scss']

  static DEFAULT_GLOBALS = {
    react: 'React',
    redux: 'Redux',
    'react-feather': 'FeatherIcons',
    '@adminjs/design-system/styled-components': 'styled',
    'prop-types': 'PropTypes',
    'react-dom': 'ReactDOM',
    'react-redux': 'ReactRedux',
    'react-router': 'ReactRouter',
    'react-router-dom': 'ReactRouterDOM',
    adminjs: 'AdminJS',
    '@adminjs/design-system': 'AdminJSDesignSystem',
  }

  static DEFAULT_EXTERNALS = [
    'prop-types',
    'react',
    'react-dom',
    'redux',
    'react-redux',
    'react-router',
    'react-router-dom',
    '@adminjs/design-system/styled-components',
    'adminjs',
    '@adminjs/design-system',
    'react-feather',
  ]

  protected inputOptions: InputOptions = {}

  protected outputOptions: OutputOptions = {}

  constructor(input: InputOptions, output: OutputOptions) {
    this.createConfiguration(input, output)
  }

  public async build() {
    const bundle = await rollup(this.inputOptions)

    await bundle.write(this.outputOptions)
    await bundle.generate(this.outputOptions)
  }

  public async watch() {
    const bundle = await rollup(this.inputOptions)

    const spinner = ora(`Bundling files in watchmode: ${JSON.stringify(this.inputOptions)}`)

    const watcher = watch({
      ...this.inputOptions,
      output: this.outputOptions,
    })

    watcher.on('event', (event) => {
      if (event.code === 'START') {
        spinner.start('Bundling files...')
      }
      if (event.code === 'ERROR') {
        spinner.fail('Bundle fail:')
        // eslint-disable-next-line no-console
        console.log(event)
      }

      if (event.code === 'END') {
        spinner.succeed(`Finish bundling: ${bundle.watchFiles.length} files`)
      }
    })
  }

  public async createEntry({
    dir = ADMIN_JS_TMP_DIR,
    content,
  }: {
    write?: boolean
    dir?: string
    content: string
  }) {
    try {
      await mkdir(dir, { recursive: true })
    } catch (error) {
      if (error.code !== 'EEXIST') {
        throw error
      }
    }
    await writeFile(this.inputOptions.input as string, content)
  }

  public async getOutput(): Promise<string | null> {
    try {
      return await readFile(this.outputOptions.file as string, 'utf-8')
    } catch (error) {
      if (error.code !== 'ENOENT') {
        throw error
      }
    }

    return null
  }

  public createConfiguration(input: InputOptions, output: OutputOptions): void {
    this.inputOptions = input
    this.outputOptions = output

    if (isUndefined(this.inputOptions.input)) {
      throw new Error('InputOptions#input must be defined')
    }

    if (typeof this.inputOptions.input !== 'string') {
      throw new Error('InputOptions#input must be a "string"')
    }

    if (isUndefined(this.outputOptions.file)) {
      throw new Error('OutputOptions#file must be defined')
    }

    if (isUndefined(this.outputOptions.name)) {
      throw new Error('OutputOptions#name must be defined')
    }

    if (isUndefined(this.outputOptions.format)) {
      this.outputOptions.format = 'iife'
    }

    if (isUndefined(this.outputOptions.interop)) {
      this.outputOptions.interop = 'auto'
    }

    if (isUndefined(this.outputOptions.sourcemap)) {
      this.outputOptions.sourcemap = NODE_ENV === 'production' ? false : 'inline'
    }
  }

  public setInputOption<T = any>(key: string, value: T) {
    this.inputOptions[key] = value

    return this
  }

  public setOutputOption<T = any>(key: string, value: T) {
    this.outputOptions[key] = value

    return this
  }
}