* feat: 🎸 Allow building and bundling of web workers in ember * refactor: 💡 Cleanup code and add comments
55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
const Plugin = require('broccoli-plugin');
|
|
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const esbuild = require('esbuild');
|
|
|
|
module.exports = class BuildWorkers extends Plugin {
|
|
build() {
|
|
this.inputPaths.forEach((inputPath) => {
|
|
this.#buildWorkers(inputPath);
|
|
});
|
|
}
|
|
|
|
#buildWorkers(inputPath) {
|
|
const workers = this.#getWorkers(inputPath);
|
|
|
|
const workerBuilder = this.#configureWorkerBuilder({
|
|
isProduction: this.isProduction,
|
|
buildDir: this.outputPath,
|
|
});
|
|
|
|
Object.entries(workers).map(workerBuilder);
|
|
}
|
|
|
|
#getWorkers(inputPath) {
|
|
let workers = {};
|
|
let dir = fs.readdirSync(inputPath);
|
|
|
|
dir.forEach((name) => {
|
|
workers[name] = path.join(inputPath, name);
|
|
});
|
|
|
|
return workers;
|
|
}
|
|
|
|
#configureWorkerBuilder({ isProduction, buildDir }) {
|
|
return ([name, entryPath]) => {
|
|
esbuild.buildSync({
|
|
loader: { '.js': 'js' },
|
|
entryPoints: [entryPath],
|
|
bundle: true,
|
|
outfile: path.join(buildDir, name),
|
|
format: 'esm',
|
|
minify: isProduction,
|
|
sourcemap: !isProduction,
|
|
});
|
|
};
|
|
}
|
|
};
|