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

[DRAFT] add build, separate lib and website #57

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/node_modules/
/disabled_sf2s/
/.idea
.idea
.directory
/src/spessasynth_lib/synthetizer/freeverb_unused/
/src/website/server/config.json
/src/website/server/config.json
/src/website/minified/
/src/spessasynth_lib/minified/
/src/website/lib/
16 changes: 16 additions & 0 deletions build/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { context } from 'esbuild'
import { getConfig } from './esbuild.config.js'

(async () => {
const watch = process.argv.includes('--watch');
const config = getConfig(process.argv);
const ctx = await context(config);

if (watch) {
await ctx.watch();
} else {
await ctx.rebuild();
await ctx.dispose();
console.log('[BUILD] done.');
}
})();
4 changes: 4 additions & 0 deletions build/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { copyFiles } from './copy.plugin.js'
import { LIB_FILES } from './esbuild.config.js'

copyFiles(LIB_FILES.from, LIB_FILES.to);
68 changes: 68 additions & 0 deletions build/copy.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fs from 'fs';
import path from 'path';

const copyFile = (src, destFile) => {
fs.copyFile(src, destFile, (err) => {
if (err) {
console.error(`Error copying file: ${err.message}`);
} else {
console.log(`File copied to: ${destFile}`, performance.now());
}
});
}

function copyToFileOrDir(src, dest) {
const destFile = path.extname(dest) ? dest : path.join(dest, path.basename(src));
const destDir = path.dirname(destFile);
fs.stat(destDir, (err, stats) => {
if (!err) {
copyFile(src, destFile);
return;
}
if (err.code === 'ENOENT') {
fs.mkdir(destDir, { recursive: true }, (err) => {
if (err) {
console.error(`Error creating directory: ${err.message}`);
} else {
copyFile(src, destFile);
}
})
} else {
console.error(`Error checking directory: ${err.message}`);
}
});
}

const findValidArrItem = (arr, i) => {
if (arr[i]) {
return arr[i];
}
if (i > 0) {
return findValidArrItem(arr, i - 1);
}
}

const config = (from, to) => {
return from.map((file, index) => {
return {
from: file,
to: findValidArrItem(to, index),
};
});
}

export const copyFiles = (from, to) => {
const files = config(from, to);
files.forEach(({ from, to }) => {
copyToFileOrDir(from, to);
});
}

export const copyFilesPlugin = ({ from, to }) => ({
name: 'copy-files-plugin',
setup(build) {
build.onDispose(async () => {
copyFiles(from, to);
});
},
});
57 changes: 57 additions & 0 deletions build/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { copyFilesPlugin } from './copy.plugin.js'

export const LIB_FILES = {
from: [
'./src/spessasynth_lib/minified/worklet_processor_main.min.js',
'./src/spessasynth_lib/synthetizer/audio_effects/impulse_response_2.flac'
],
to: ['./src/website/lib'],
};

export const getConfig = (params) =>{
const watch = params.includes('--watch');
const sourcemap = params.includes('--sourcemap');
const js = params.includes('--js');
const css = params.includes('--css');
const minify = !params.includes('--skipminify');
const skipCopy = params.includes('--skipcopy');
const isLib = params.includes('--lib');

const mainDir = isLib ? './src/spessasynth_lib' : './src/website';
const cssFiles = `${mainDir}/**/*_main.css`;
const jsFiles = `${mainDir}/**/*_main.js`;
const outdir = `${mainDir}/minified`;


const entryPoints = [];
const plugins = [];
if (js) {
entryPoints.push(jsFiles);
}

if (css) {
entryPoints.push(cssFiles);
}

if (!js && !css) {
entryPoints.push(jsFiles, cssFiles);
}

if (!skipCopy && !css && !isLib) {
plugins.push(copyFilesPlugin(LIB_FILES));
}

console.log('[BUILD] start.', { watch, sourcemap, minify, skipCopy, entryPoints });
return {
entryPoints,
outdir,
entryNames: '[name].min',
bundle: true,
format: 'esm',
platform: 'browser',
minify,
logLevel: 'info',
sourcemap: sourcemap || watch,
plugins,
};
};
Loading