import { FabBuildStep , ProtoFab , RuntimePlugin } from '@fab/core' ; import { PrecompileArgs , PrecompileMetadata , ConfigOverrides , PluginOverrides , } from './types' ; import { _log } from '@fab/cli' ; import webpack from 'webpack' ; import fs from 'fs-extra' ; import path from 'path' ; import filenamify from 'filenamify' ; const log = _log ( '@fab/plugin-precompile' ) ; export const build = async ( args , proto_fab , config_path , skip_cache = false ) => { const config_dir = path . dirname ( path . resolve ( config_path ) ) ; const cache_dir = path . join ( config_dir , '.fab' , '.cache' , 'precompile' ) ; await fs . ensureDir ( cache_dir ) ; const output_files = [ ] ; for ( const [ file , config ] of Object . entries ( args ) ) { const webpacked_output = path . join ( cache_dir , `${ filenamify ( file , { replacement : '-' } ) }.js` ) ; const entry = path . resolve ( file ) ; const { _config : plugin_config_file , ... plugin_args } = config ; const { customise_aliases , customise_webpack } = await getPluginOverrides ( config_dir , plugin_config_file ) ; const options = customise_webpack ( { stats : 'verbose' , mode : 'production' , target : 'webworker' , entry , optimization : { minimize : false , } , output : { path : path . dirname ( webpacked_output ) , filename : path . basename ( webpacked_output ) , library : 'server' , libraryTarget : 'commonjs2' , } , resolve : { alias : customise_aliases ( { fs : require . resolve ( 'memfs' ) , } ) , extensions : [ '.js' , '.jsx' , '.ts' , '.tsx' , '.mjs' ] , } , module : { rules : [ { test : /\.(js|jsx|ts|tsx)$/ , use : { loader : '@sucrase/webpack-loader' , options : { transforms : [ 'jsx' , 'typescript' , 'imports' ] , } , } , } , ] , } , node : { global : false , } , plugins : [ new webpack . DefinePlugin ( { eval : 'HERE_NO_EVAL' , } ) , ] , } ) ; await new Promise ( ( resolve , reject ) => webpack ( options , ( err , stats ) => { if ( err || stats . hasErrors ( ) ) { console . log ( 'Build failed.' ) ; console . log ( err ) ; console . log ( stats && stats . toJson ( ) . errors . toString ( ) ) ; reject ( ) ; } resolve ( ) ; } ) ) ; output_files . push ( { runtime : webpacked_output , plugin_args , } ) ; } return output_files ; } ; async function getPluginOverrides ( config_dir , plugin_config_file ) { if ( ! plugin_config_file ) { return { customise_webpack : ( x ) => x , customise_aliases : ( x ) => x , } ; } const plugin_overrides = require ( path . resolve ( config_dir , plugin_config_file ) ) as PluginOverrides ; return { customise_webpack : plugin_overrides . webpack || ( ( x ) => x ) , customise_aliases : plugin_overrides . alias || ( ( x ) => x ) , } ; }