Skip to content

Commit

Permalink
ensure build dir is clean when building assets (#3356)
Browse files Browse the repository at this point in the history
Ensure build dir is clean when building assets.

---------

Co-authored-by: Travis Ravert <travert@osc.edu>
  • Loading branch information
johrstrom and Oglopf authored Feb 26, 2024
1 parent 86d15ac commit 40987c5
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions apps/dashboard/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const esbuild = require('esbuild');
const fs = require('fs');

// FIXME probably a better way to do this? build below already recognizes the filetypes.
const faDir = 'node_modules/@fortawesome/fontawesome-free/webfonts/';
fs.readdir(faDir, (err, files) => {
if(err) throw `${faDir} has to exist for to compile. Did you run 'bin/setup'?`;

files.forEach(file => {
fs.copyFile(`${faDir}/${file}`, `app/assets/builds/${file}`, () => {});
});
});

// could just glob and pass this in the cli, but glob support is shell dependant
entryPoints = filesFromDir('app/javascript');
const entryPoints = filesFromDir('app/javascript');
const buildDir = 'app/assets/builds';

function filesFromDir(dir) {
return fs.readdirSync(dir).map((file) => {
Expand All @@ -24,13 +17,34 @@ function filesFromDir(dir) {
}).flat(); // only works for 1 subdirectory
}

const prepPlugin = {
name: 'prep-build-dir',
setup(build) {
build.onStart(() => {
fs.rmSync(buildDir, { recursive: true, force: true });
fs.mkdirSync(buildDir);
fs.copyFileSync('tmp/.keep', `${buildDir}/.keep`);

// FIXME probably a better way to do this? build below already recognizes the filetypes.
fs.readdir(faDir, (err, files) => {
if(err) throw `${faDir} has to exist for assets to compile. Did you run 'bin/setup'?`;

files.forEach(file => {
fs.copyFileSync(`${faDir}/${file}`, `${buildDir}/${file}`);
});
});
})
},
}

esbuild.build({
entryPoints: entryPoints,
bundle: true,
sourcemap: true,
format: 'esm',
outdir: 'app/assets/builds',
outdir: buildDir,
external: ['fs'],
plugins: [prepPlugin],
minify: process.env.RAILS_ENV == 'production' ? true : false,
}).catch((e) => console.error(e.message));

0 comments on commit 40987c5

Please sign in to comment.