11#!/usr/bin/env node
2- "use strict" ;
32import * as esbuild from 'esbuild'
43import { lessLoader } from 'esbuild-plugin-less' ;
54import { sassPlugin } from 'esbuild-sass-plugin' ;
65import { writeFile , opendir , unlink } from 'node:fs/promises' ;
76import path from 'node:path' ;
87import parseArgs from 'minimist' ;
98
10- const assets_dir = 'root/assets' ;
11-
129const config = {
1310 entryPoints : [
1411 'root/static/js/main.mjs' ,
@@ -18,10 +15,9 @@ const config = {
1815 assetNames : '[name]-[hash]' ,
1916 entryNames : '[name]-[hash]' ,
2017 format : 'esm' ,
21- outdir : assets_dir ,
18+ outdir : 'root/assets' ,
2219 bundle : true ,
2320 sourcemap : true ,
24- metafile : true ,
2521 inject : [ 'root/static/js/inject.js' ] ,
2622 loader : {
2723 '.eot' : 'file' ,
@@ -37,21 +33,23 @@ const config = {
3733 name = 'metacpan-build' ;
3834
3935 setup ( build ) {
40- build . onStart ( ( ) => {
41- console . log ( 'building assets...' )
42- } ) ;
4336 build . onResolve (
4437 { filter : / ^ \/ / } ,
4538 args => ( { external : true } ) ,
4639 ) ;
40+
41+ build . initialOptions . metafile = true ;
42+ build . onStart ( ( ) => {
43+ console . log ( 'building assets...' )
44+ } ) ;
4745 build . onEnd ( async result => {
48- const metafile = result . metafile ;
49- if ( metafile && metafile . outputs ) {
50- const files = Object . keys ( metafile . outputs ) . sort ( )
51- . map ( file => path . relative ( assets_dir , file ) ) ;
46+ const outputs = result ? .metafile ?. outputs ;
47+ if ( outputs ) {
48+ const files = Object . keys ( outputs ) . sort ( )
49+ . map ( file => path . relative ( build . initialOptions . outdir , file ) ) ;
5250 try {
5351 await writeFile (
54- path . join ( assets_dir , 'assets.json' ) ,
52+ path . join ( build . initialOptions . outdir , 'assets.json' ) ,
5553 JSON . stringify ( files ) ,
5654 'utf8' ,
5755 ) ;
@@ -78,8 +76,7 @@ if (args.minify) {
7876 config . minify = true ;
7977}
8078if ( args . clean ) {
81-
82- for await ( const file of await opendir ( assets_dir , { withFileTypes : true } ) ) {
79+ for await ( const file of await opendir ( config . outdir , { withFileTypes : true } ) ) {
8380 const filePath = path . join ( file . parentPath , file . name ) ;
8481 if ( file . name . match ( / ^ \. / ) ) {
8582 // ignore these
0 commit comments