bigint-crypto-utils/build/testing/mocha/builders/RollupBuilder.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-08-01 17:10:00 +00:00
import EventEmitter from 'events'
import { writeFileSync, existsSync } from 'fs'
import { join, resolve } from 'path'
import { fileURLToPath } from 'url'
2021-03-25 12:40:04 +00:00
2022-08-01 17:10:00 +00:00
import { watch as _watch, rollup as _rollup } from 'rollup'
import loadAndParseConfigFile from 'rollup/dist/loadConfigFile.js'
2021-03-25 12:40:04 +00:00
2022-08-01 17:10:00 +00:00
import Builder from './Builder.js'
2021-03-25 12:40:04 +00:00
2022-08-01 17:10:00 +00:00
const __dirname = resolve(fileURLToPath(import.meta.url), '../')
2021-03-25 12:40:04 +00:00
2022-08-01 17:10:00 +00:00
const rootDir = join(__dirname, '../../../../')
const pkgJson = (await import(join(rootDir, 'package.json'), {
assert: {
type: "json",
}
})).default
export default class RollupBuilder extends Builder {
constructor ({ name = 'rollup', configPath = join(rootDir, 'rollup.config.js'), tempDir = join(rootDir, '.mocha-ts'), watch = false }) {
super(join(tempDir, 'semaphore'), name)
2021-03-25 12:40:04 +00:00
this.configPath = configPath
this.watch = watch
}
async start () {
await super.start()
const { options } = await loadAndParseConfigFile(this.configPath)
2022-08-01 17:10:00 +00:00
// Watch only the Node ESM module, that is the one we are going to use with mocha
2021-03-25 12:40:04 +00:00
const rollupOptions = options.filter(bundle => {
const file = (bundle.output[0].dir !== undefined)
2022-08-01 17:10:00 +00:00
? join(bundle.output[0].dir, bundle.output[0].entryFileNames)
2021-03-25 12:40:04 +00:00
: bundle.output[0].file
2022-08-01 17:10:00 +00:00
return file === join(rootDir, pkgJson.exports['.'].node.import)
2021-03-25 12:40:04 +00:00
})[0]
2022-08-01 17:10:00 +00:00
if (rollupOptions.output.length > 1) {
rollupOptions.output = rollupOptions.output[0]
}
2021-03-25 12:40:04 +00:00
this.builder = new RollupBundler(rollupOptions, this.watch)
this.builder.on('event', event => {
switch (event.code) {
case 'START':
this.emit('busy')
if (this.firstBuild === true) {
this.emit('message', 'building your module...')
} else {
this.emit('message', 'file changes detected. Rebuilding module files...')
}
break
case 'BUNDLE_END':
if (event.result) event.result.close()
break
case 'END':
if (event.result) event.result.close()
this.emit('ready')
break
case 'ERROR':
if (event.result) event.result.close()
this.emit('error', event.error)
2022-08-01 17:10:00 +00:00
writeFileSync(join(rootDir, pkgJson.exports['.'].node.import), '', 'utf8')
2021-03-25 12:40:04 +00:00
this.emit('ready')
break
default:
this.emit('busy')
break
}
})
this.builder.start()
return await this.ready()
}
async close () {
await super.close()
this.builder.close()
}
}
class RollupBundler extends EventEmitter {
constructor (rollupOptions, watch = false) {
super()
this.rollupOptions = rollupOptions
this.watch = watch
}
async start () {
if (this.watch === true) {
2022-08-01 17:10:00 +00:00
this.watcher = _watch(this.rollupOptions)
2021-03-25 12:40:04 +00:00
this.watcher.on('event', event => {
this.emit('event', event)
})
} else {
2022-08-01 17:10:00 +00:00
if (existsSync(join(rootDir, pkgJson.exports['.'].node.import)) === false) {
2021-03-25 12:40:04 +00:00
await this._bundle()
} else {
this.emit('event', { code: 'END', noBuild: true })
}
}
}
async _bundle () {
this.emit('event', { code: 'START' })
for (const optionsObj of [].concat(this.rollupOptions)) {
try {
2022-08-01 17:10:00 +00:00
const bundle = await _rollup(optionsObj)
2021-03-25 12:40:04 +00:00
try {
await Promise.all(optionsObj.output.map(bundle.write))
this.emit('event', { code: 'BUNDLE_END' })
} catch (error) {
this.emit('event', { code: 'ERROR', error })
}
} catch (error) {
this.emit('event', { code: 'ERROR', error })
}
}
this.emit('event', { code: 'END' })
}
close () {
if (this.watcher !== undefined) this.watcher.close()
}
}