Skip to content

Commit

Permalink
lint more
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Mar 6, 2024
1 parent 18e30d7 commit 36892f9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 85 deletions.
27 changes: 17 additions & 10 deletions build/lib/copyAndWatch.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@

import chokidar from 'chokidar';
import fs from 'fs';
import path from 'path';

const debug = console.log; //() => {};
const removeLeadingSlash = s => s.replace(/^\//, '');
const removeLeadingSlash = (s) => s.replace(/^\//, '');

/**
* Recursively copies files and watches for changes.
*
*
* Example:
*
* copyAndWatch([
* {src: "src\/**\/*.js", srcPrefix: "src", dst: "out"}, // would copy src/bar/moo.js -> out/bar/moo.js
* {src: "index.html", dst: "out"}, // copies index.html -> out/index.html
* ]);
*
*
* @param {*} paths [{src: glob, srcPrefix: string, dst: string }]
* @param {*} options { watch: true/false } // watch: false = just copy and exit.
*/
export function copyAndWatch(paths, { watch } = { watch: true }) {
for (const {src, srcPrefix, dst} of paths) {
for (const { src, srcPrefix, dst } of paths) {
const watcher = chokidar.watch(src, {
ignored: /(^|[\/\\])\../, // ignore dot files
persistent: watch,
});

const makeDstPath = (path, dst) => `${dst}/${removeLeadingSlash(path.startsWith(srcPrefix) ? path.substring(srcPrefix.length) : path)}`;
const makeDstPath = (path, dst) =>
`${dst}/${removeLeadingSlash(
path.startsWith(srcPrefix) ? path.substring(srcPrefix.length) : path
)}`;

watcher
.on('addDir', (srcPath, stats) => {
.on('addDir', (srcPath) => {
const dstPath = makeDstPath(srcPath, dst);
debug('addDir:', srcPath, dstPath);
fs.mkdirSync(dstPath, { recursive: true });
})
.on('add', (srcPath, stats) => {
.on('add', (srcPath) => {
const dstPath = makeDstPath(srcPath, dst);
const dir = path.dirname(dstPath);
fs.mkdirSync(dir, { recursive: true });
debug('add:', srcPath, dstPath);
fs.copyFileSync(srcPath, dstPath);
})
.on('change', (srcPath, stats) => {
.on('change', (srcPath) => {
const dstPath = makeDstPath(srcPath, dst);
debug('change:', srcPath, dstPath);
fs.copyFileSync(srcPath, dstPath);
})
.on('unlink', (srcPath, stats) => {
.on('unlink', (srcPath) => {
const dstPath = makeDstPath(srcPath, dst);
debug('unlink:', srcPath, dstPath);
fs.unlinkSync(dstPath);
})
.on('ready', () => {
if (!watch) {
watcher.close();
}
});
}
}
12 changes: 8 additions & 4 deletions build/lib/readdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import path from 'path';

// not needed in node v20+
export function readDirSyncRecursive(dir) {
const basename = path.basename(dir);
const entries = fs.readdirSync(dir, { withFileTypes: true });
return entries.map(entry => entry.isDirectory()
const basename = path.basename(dir);
const entries = fs.readdirSync(dir, { withFileTypes: true });
return entries
.map((entry) =>
entry.isDirectory()
? readDirSyncRecursive(`${dir}/${entry.name}`)
: entry.name
).flat().map(name => `${basename}/${name}`);
)
.flat()
.map((name) => `${basename}/${name}`);
}
13 changes: 4 additions & 9 deletions build/tools/build.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {spawn} from 'child_process';
import {mkdirSync} from 'fs';
import { spawn } from 'child_process';
import { mkdirSync } from 'fs';

mkdirSync('out', { recursive: true });

spawn('node', [
'build/tools/copy.js',
], {
spawn('node', ['build/tools/copy.js'], {
shell: true,
stdio: 'inherit',
});

spawn('./node_modules/.bin/rollup', [
"-c"
], {
spawn('./node_modules/.bin/rollup', ['-c'], {
shell: true,
stdio: 'inherit',
});

19 changes: 11 additions & 8 deletions build/tools/copy.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {copyAndWatch} from '../lib/copyAndWatch.js';
import { copyAndWatch } from '../lib/copyAndWatch.js';

const watch = !!process.argv[2];

copyAndWatch([
{src: "public/**/*", srcPrefix: "public", dst: "out"},
{src: "meshes/**/*", dst: "out"},
{src: "sample/**/*", dst: "out"},
{src: "shaders/**/*", dst: "out"},
{src: "index.html", dst: "out"},
], { watch });
copyAndWatch(
[
{ src: 'public/**/*', srcPrefix: 'public', dst: 'out' },
{ src: 'meshes/**/*', dst: 'out' },
{ src: 'sample/**/*', dst: 'out' },
{ src: 'shaders/**/*', dst: 'out' },
{ src: 'index.html', dst: 'out' },
],
{ watch }
);
19 changes: 5 additions & 14 deletions build/tools/serve.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
import {spawn} from 'child_process';
import {mkdirSync} from 'fs';
import { spawn } from 'child_process';
import { mkdirSync } from 'fs';

mkdirSync('out', { recursive: true });

spawn('npm', [
'run',
'watch',
], {
spawn('npm', ['run', 'watch'], {
shell: true,
stdio: 'inherit',
});

spawn('node', [
'build/tools/copy.js',
'1',
], {
spawn('node', ['build/tools/copy.js', '1'], {
shell: true,
stdio: 'inherit',
});

spawn('./node_modules/.bin/servez', [
"out"
], {
spawn('./node_modules/.bin/servez', ['out'], {
shell: true,
stdio: 'inherit',
});

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"url": "https://github.com/webgpu/webgpu-samples.git"
},
"scripts": {
"lint": "eslint --ext .ts,.tsx src/ sample/",
"fix": "eslint --fix --ext .ts,.tsx src/ sample/",
"lint": "eslint --ext .ts,.js,.html src/ sample/ build/",
"fix": "eslint --fix --ext .ts,.js,.html src/ sample/ build/",
"build": "node build/tools/build.js",
"start": "node build/tools/serve.js",
"serve": "node build/tools/serve.js",
Expand Down
73 changes: 35 additions & 38 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import { readDirSyncRecursive } from './build/lib/readdir.js';
Expand All @@ -10,54 +10,51 @@ function wgslPlugin() {
if (id.endsWith('.wgsl')) {
return {
code: `export default \`${code}\`;`,
map: { mappings: '' }
map: { mappings: '' },
};
}
}
},
};
}

const samplePlugins = [
wgslPlugin(),
nodeResolve(),
commonjs(),
typescript({ tsconfig: './sample/tsconfig.json' }),
wgslPlugin(),
nodeResolve(),
commonjs(),
typescript({ tsconfig: './sample/tsconfig.json' }),
];

// add a rollup rule for each sample
const samples = readDirSyncRecursive('sample')
.filter(n => n.endsWith('/main.ts') || n.endsWith('/worker.ts'))
.map(filename => {
return {
input: filename,
output: [
{
file: `out/${filename.replace(/\.ts$/, '.js')}`,
format: 'esm',
sourcemap: true
}
],
plugins: samplePlugins,
};
});
.filter((n) => n.endsWith('/main.ts') || n.endsWith('/worker.ts'))
.map((filename) => {
return {
input: filename,
output: [
{
file: `out/${filename.replace(/\.ts$/, '.js')}`,
format: 'esm',
sourcemap: true,
},
],
plugins: samplePlugins,
};
});

export default [
{
input: 'src/main.ts',
output: [
{
file: `out/main.js`,
format: 'esm',
sourcemap: true,
},
],
plugins: [
nodeResolve(),
typescript({ tsconfig: './src/tsconfig.json' }),
],
watch: {
clearScreen: false,
},
{
input: 'src/main.ts',
output: [
{
file: `out/main.js`,
format: 'esm',
sourcemap: true,
},
],
plugins: [nodeResolve(), typescript({ tsconfig: './src/tsconfig.json' })],
watch: {
clearScreen: false,
},
...samples,
},
...samples,
];

0 comments on commit 36892f9

Please sign in to comment.