Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure build dir is clean when building assets #3356

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));

Loading