diff --git a/build/lib/copyAndWatch.js b/build/lib/copyAndWatch.js index ca42dcbd..626a0abb 100644 --- a/build/lib/copyAndWatch.js +++ b/build/lib/copyAndWatch.js @@ -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(); + } + }); } } diff --git a/build/lib/readdir.js b/build/lib/readdir.js index 06f2f31d..4ce688fa 100644 --- a/build/lib/readdir.js +++ b/build/lib/readdir.js @@ -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}`); } diff --git a/build/tools/build.js b/build/tools/build.js index 7d4dbb1c..2996824e 100644 --- a/build/tools/build.js +++ b/build/tools/build.js @@ -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', }); - diff --git a/build/tools/copy.js b/build/tools/copy.js index 4c4371ad..456f7e90 100644 --- a/build/tools/copy.js +++ b/build/tools/copy.js @@ -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 } +); diff --git a/build/tools/serve.js b/build/tools/serve.js index c0b007dd..fa6cb45f 100644 --- a/build/tools/serve.js +++ b/build/tools/serve.js @@ -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', }); - diff --git a/package.json b/package.json index d7255a20..738f4d69 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/rollup.config.js b/rollup.config.js index bb70df3d..3f367a64 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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'; @@ -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, ];