Skip to content

Commit

Permalink
more build fixes + .ignore files
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyrenth authored Sep 23, 2024
1 parent 4c58058 commit 63c8c0b
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
.env
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
.env
134 changes: 134 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# Core Dump
core
core.*
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
.env
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"type": "module",
"scripts": {
"lint": "tsc",
"build": "esbuild ./src/**/*.ts --outdir=dist --format=esm --packages=external --platform=node --sourcemap",
"build": "node scripts/build.js",
"start": "node dist/index.js",
"prod": "node scripts/run.js --sync lint build start",
"prod": "node scripts/run.js --sync cleanup lint build start",
"dev:lint": "tsc --watch --preserveWatchOutput",
"dev:start": "node --watch dist/index.js",
"dev:build": "esbuild ./src/**/*.ts --outdir=dist --format=esm --packages=external --platform=node --sourcemap --watch",
"dev": "node scripts/run.js dev:lint dev:start dev:build",
"dev:build": "node scripts/build.js --watch",
"dev:pre": "node scripts/run.js --sync cleanup lint build",
"dev:scripts": "node scripts/run.js dev:lint dev:start dev:build",
"dev": "node scripts/run.js --sync dev:pre dev:scripts",
"cleanup": "node scripts/cleanup.js dist",
"prepare": "husky",
"setup": "node scripts/setup.js"
Expand Down
84 changes: 84 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { readdirSync, statSync, watch as watchDir } from 'node:fs';
import { extname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import esbuild from 'esbuild';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

let args = process.argv.slice(2);
const watch = args.includes('--watch');
if (watch) args = args.filter(script => script !== '--watch');

async function findFiles(dir, ext) {
let results = [];

try {
const list = readdirSync(dir);

for (const file of list) {
const filePath = join(dir, file);
const fileStat = statSync(filePath);

if (fileStat.isDirectory()) {
results = results.concat(await findFiles(filePath, ext));
} else if (extname(file) === ext) {
results.push(filePath);
}
}
} catch (err) {
// Slient error, skip.
}

return results;
}

async function build(files, exit = true) {
console.log('Building...');

const start = Date.now();
const basePath = join(__dirname, '..');

try {
await esbuild.build({
entryPoints: files,
bundle: false,
outdir: join(__dirname, '..', 'dist'),
platform: 'node',
target: 'esnext',
format: 'esm',
sourcemap: true,
packages: 'external',
});
} catch (err) {
console.error('[ESBuild]', err);
if (exit) process.exit(1);
}

console.log([
'',
`Built ${files.length} files:`,
files.map(file => ` - ${file.replace(basePath, '')}`).join('\n'),
''
].join('\n'));

console.log(`Build completed in ${Date.now() - start}ms.`);
}

const srcDir = join(__dirname, '..', args[0] ?? 'src');

const files = await findFiles(srcDir, args[1] ?? '.ts');

if (watch) {
let lastBuild = Date.now();

watchDir(srcDir, { recursive: true }, async (_, filename) => {
if (Date.now() - lastBuild < 1000) return;
lastBuild = Date.now();

console.log(`File changed: ${filename}`);

const files = await findFiles(srcDir, args[1] ?? '.ts');
await build(files, false);
});
} else build(files);

0 comments on commit 63c8c0b

Please sign in to comment.