2021-03-24 13:04:30 +00:00
|
|
|
const EventEmitter = require('events')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
const rollup = require('rollup')
|
2022-10-03 15:35:35 +00:00
|
|
|
const loadAndParseConfigFile = require('rollup/dist/loadConfigFile')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-08-01 02:04:00 +00:00
|
|
|
const Builder = require('./Builder.cjs')
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
const rootDir = path.join(__dirname, '../../../../')
|
2022-10-03 15:35:35 +00:00
|
|
|
|
2021-03-24 13:04:30 +00:00
|
|
|
const pkgJson = require(path.join(rootDir, 'package.json'))
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
const mochaTsRelativeDir = pkgJson.directories['mocha-ts']
|
|
|
|
const mochaTsDir = path.join(rootDir, mochaTsRelativeDir)
|
|
|
|
|
|
|
|
class RollupBuilder extends Builder {
|
|
|
|
constructor ({ name, configPath, tempDir }) {
|
2021-03-24 13:04:30 +00:00
|
|
|
super(path.join(tempDir, 'semaphore'), name)
|
2022-10-03 15:35:35 +00:00
|
|
|
this.tempDir = tempDir
|
2021-03-24 13:04:30 +00:00
|
|
|
this.configPath = configPath
|
2022-10-03 15:35:35 +00:00
|
|
|
this.firstBuild = true
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
async start ({ watch = false, commonjs = false }) {
|
2021-03-24 13:04:30 +00:00
|
|
|
await super.start()
|
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
this.watch = watch
|
|
|
|
this.commonjs = commonjs
|
|
|
|
this.watchedModule = commonjs ? pkgJson.exports['.'].node.require : pkgJson.exports['.'].node.import
|
|
|
|
|
2021-03-24 13:04:30 +00:00
|
|
|
const { options } = await loadAndParseConfigFile(this.configPath)
|
2022-10-03 15:35:35 +00:00
|
|
|
|
|
|
|
// Instead of compiling all the outputs let us just take the one we are using with mocha (either cjs or esm)
|
2021-03-24 13:04:30 +00:00
|
|
|
const rollupOptions = options.filter(bundle => {
|
|
|
|
const file = (bundle.output[0].dir !== undefined)
|
|
|
|
? path.join(bundle.output[0].dir, bundle.output[0].entryFileNames)
|
|
|
|
: bundle.output[0].file
|
2022-10-03 15:35:35 +00:00
|
|
|
return file === path.join(rootDir, this.watchedModule)
|
2021-03-24 13:04:30 +00:00
|
|
|
})[0]
|
2022-10-03 15:35:35 +00:00
|
|
|
if (rollupOptions.output.length > 1) {
|
|
|
|
rollupOptions.output = rollupOptions.output[0]
|
|
|
|
}
|
2021-03-24 13:04:30 +00:00
|
|
|
|
2022-10-03 15:35:35 +00:00
|
|
|
this.builder = new RollupBundler({ rollupOptions, watch: this.watch, watchedModule: this.watchedModule })
|
2021-03-24 13:04:30 +00:00
|
|
|
|
|
|
|
this.builder.on('event', event => {
|
2022-10-03 15:35:35 +00:00
|
|
|
let updateSemaphore = true
|
2021-03-24 13:04:30 +00:00
|
|
|
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()
|
2022-10-03 15:35:35 +00:00
|
|
|
|
|
|
|
// fs.mkdirSync(path.join(this.tempDir, path.dirname(this.watchedModule)), { recursive: true })
|
|
|
|
// // console.log(path.join(this.tempDir, path.dirname(this.watchedModule)))
|
|
|
|
// fs.copyFileSync(this.watchedModule, path.join(this.tempDir, this.watchedModule))
|
|
|
|
|
|
|
|
if (this.firstBuild) {
|
|
|
|
this.firstBuild = false
|
|
|
|
updateSemaphore = false
|
|
|
|
}
|
|
|
|
this.emit('ready', updateSemaphore)
|
2021-03-24 13:04:30 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'ERROR':
|
|
|
|
if (event.result) event.result.close()
|
|
|
|
this.emit('error', event.error)
|
2022-10-03 15:35:35 +00:00
|
|
|
fs.writeFileSync(path.join(rootDir, this.watchedModule), '', 'utf8')
|
|
|
|
// fs.writeFileSync(path.join(this.tempDir, this.watchedModule), '', 'utf8')
|
2021-03-24 13:04:30 +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 {
|
2022-10-03 15:35:35 +00:00
|
|
|
constructor ({ rollupOptions, watchedModule, watch = false }) {
|
2021-03-24 13:04:30 +00:00
|
|
|
super()
|
|
|
|
this.rollupOptions = rollupOptions
|
|
|
|
this.watch = watch
|
2022-10-03 15:35:35 +00:00
|
|
|
this.watchedModule = watchedModule
|
2021-03-24 13:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async start () {
|
|
|
|
if (this.watch === true) {
|
|
|
|
this.watcher = rollup.watch(this.rollupOptions)
|
|
|
|
|
|
|
|
this.watcher.on('event', event => {
|
|
|
|
this.emit('event', event)
|
|
|
|
})
|
|
|
|
} else {
|
2022-10-03 15:35:35 +00:00
|
|
|
if (!fs.existsSync(path.join(rootDir, this.watchedModule))) {
|
2021-03-24 13:04:30 +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 {
|
|
|
|
const bundle = await rollup.rollup(optionsObj)
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 15:35:35 +00:00
|
|
|
|
|
|
|
exports.RollupBuilder = RollupBuilder
|
|
|
|
exports.rollupBuilder = new RollupBuilder({
|
|
|
|
name: 'rollup',
|
|
|
|
configPath: path.join(rootDir, pkgJson.directories.build, 'rollup.config.js'),
|
|
|
|
tempDir: mochaTsDir
|
|
|
|
})
|