auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

esbuild.cjs (2865B)


      1 #!/usr/bin/env node
      2 
      3 // require('dotenv').config();
      4 const cpy     = require('cpy').default;
      5 const fs      = require('node:fs');
      6 const esbuild = require('esbuild');
      7 // const { nodeModulesPolyfillPlugin } = require('esbuild-plugins-node-modules-polyfill');
      8 
      9 // const __dirname = dirname(fileURLToPath(import.meta.url));
     10 
     11 const entryPoints = {
     12   'main': __dirname + '/src/main.ts',
     13 };
     14 
     15 const config = {
     16   format: 'iife', // immediately invoked function expression
     17   platform: 'browser',
     18   target: ['chrome108','firefox107'],
     19   mainFields: ['browser','module','main'],
     20   bundle: true,
     21   outdir: __dirname + '/dist',
     22   entryPoints: [
     23     ...Object.values(entryPoints),
     24     // __dirname+'/src/service-worker.ts',
     25   ],
     26   minify: false,
     27   // define,
     28 
     29   jsxFactory     : 'm',
     30   jsxFragment    : '"["',
     31 
     32   loader: {
     33     '.eot'  : 'dataurl',
     34     '.html' : 'text',
     35     '.wasm' : 'dataurl',
     36     '.png'  : 'dataurl',
     37     '.svg'  : 'dataurl',
     38     '.ttf'  : 'dataurl',
     39     '.woff' : 'dataurl',
     40     '.woff2': 'dataurl',
     41   },
     42 
     43   // Kept for when a Buffer is needed.
     44   // plugins: [
     45   //   nodeModulesPolyfillPlugin({
     46   //     globals: {
     47   //       Buffer: true
     48   //     }
     49   //   })
     50   // ],
     51 
     52 };
     53 
     54 const buildList = [];
     55 const styles    = ['assets/global.css'];
     56 
     57 esbuild
     58   .build(config)
     59   .then(async () => {
     60 
     61     try { fs.mkdirSync('./dist/assets'); } catch { /* empty */ }
     62     // Already rendered at every size by util/icon.js, so only copied here.
     63     await cpy(__dirname + '/src/assets/**/*', __dirname + '/dist/assets');
     64     for(const name of Object.keys(entryPoints)) {
     65       buildList.push(`${name}.js`);
     66       try {
     67         fs.statSync(config.outdir + `/${name}.css`);
     68         styles.push(`${name}.css`);
     69       } catch(e) {
     70         // Intentionally empty
     71       }
     72     }
     73 
     74     fs.writeFileSync(config.outdir + `/index.html`, `<!DOCTYPE html>
     75 <html>
     76   <head>
     77     <meta charset="utf-8">
     78     <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
     79     <title>auth</title>
     80     ${styles.map(name => `<link rel="stylesheet" href="/${name}"/>`).join('\n    ')}
     81   </head>
     82   <body>
     83     ${buildList.map(name => `<script src="/${name}" defer></script>`).join('\n    ')}
     84   </body>
     85 </html>
     86 `);
     87 
     88     // const files = await new Promise(done => {
     89     //   exec(`find ${config.outdir} -type f`, (err,stdout, stderr) => {
     90     //     done(stdout
     91     //       .split('\n')
     92     //       .map(file => file.slice(config.outdir.length))
     93     //       .filter(file => file)
     94     //     );
     95     //   });
     96     // });
     97     // let sw = fs.readFileSync(`${config.outdir}/service-worker.js`, 'utf-8')
     98     //   .split('"--FILES--"').join(JSON.stringify(['/',...files]));
     99     // fs.writeFileSync(`${config.outdir}/service-worker.js`, sw);
    100   })
    101   // Without this a failed build still exits 0, and the image ships empty.
    102   .catch(e => { console.error(e); process.exit(1); });
    103