import path from 'path' ; import fs from 'fs' ; import fse from 'fs-extra' ; import request from 'supertest' ; import webpack from 'webpack' ; import { CleanWebpackPlugin } from 'clean-webpack-plugin' ; import CopyWebpackPlugin from 'copy-webpack-plugin' ; import pawConfig from '../config' ; import directories from '../webpack/utils/directories' ; import wHandler from '../webpack' ; import ExtractEmittedAssets from '../webpack/plugins/extract-emitted-assets' ; import { pawExistsSync } from '../globals' ; const isVerbose = process . env . PAW_VERBOSE === 'true' ; const stats = { all : undefined , assets : true , assetsSort : 'field' , builtAt : true , cached : isVerbose , cachedAssets : isVerbose , children : true , chunks : isVerbose , chunkModules : isVerbose , chunkOrigins : isVerbose , chunksSort : 'field' , context : directories . src , colors : true , depth : isVerbose , entrypoints : isVerbose , env : true , errors : true , errorDetails : true , hash : true , maxModules : 0 , modules : isVerbose , modulesSort : 'field' , moduleTrace : isVerbose , performance : true , providedExports : false , publicPath : true , reasons : isVerbose , source : false , timings : true , usedExports : false , version : isVerbose , warnings : true , } ; console . log ( `
===================================================
  Building application...
  This may take time depending on the application size.
  Thank you for your patience.
===================================================
` ) ; let cleanDistFolder = false ; let copyPublicFolder = ! fs . existsSync ( path . join ( directories . src , 'public' ) ) ; wHandler . hooks . beforeConfig . tap ( 'AddSyncedFilesPlugin' , ( wEnv , wType , wConfigs ) => { if ( wType === 'web' ) { wConfigs . forEach ( ( c ) => { const wConfig = c ; if ( ! cleanDistFolder ) { if ( ! wConfig . plugins ) wConfig . plugins = [ ] ; /**
                 * Check if plugins array exists and if it does
                 * check if CleanWebpackPlugin exists inside the plugin array
                 */ const hasCleanWebpackPlugin = wConfig . plugins . some ( ( p ) => p instanceof CleanWebpackPlugin ) ; if ( ! hasCleanWebpackPlugin ) { /**
                     * Push CleanWebpackPlugin to plugin list and ask it to clean
                     * the dist folder
                     */ wConfig . plugins . push ( new CleanWebpackPlugin ( { cleanOnceBeforeBuildPatterns : [ directories . dist , ] , } ) ) ; cleanDistFolder = true ; } } if ( ! copyPublicFolder ) { if ( ! wConfig . plugins ) wConfig . plugins = [ ] ; /**
                 * We need to make sure that everything inside the public folder
                 * is copied as it is to the build/public folder
                 */ wConfig . plugins . push ( new CopyWebpackPlugin ( { patterns : [ { from : path . join ( directories . src , 'public' ) , to : directories . build , } , ] , } ) ) ; copyPublicFolder = true ; } const hasExtractEmittedAssets = wConfig . plugins && wConfig . plugins . some ( ( p ) => p instanceof ExtractEmittedAssets ) ; if ( ! hasExtractEmittedAssets ) { if ( typeof wConfig . plugins === 'undefined' ) wConfig . plugins = [ ] ; wConfig . plugins . push ( new ExtractEmittedAssets ( { outputPath : directories . dist , } ) ) ; } } ) ; } if ( wType === 'server' ) { wConfigs . forEach ( ( c ) => { const wConfig = c ; if ( wConfig . entry === pawExistsSync ( path . join ( process . env . LIB_ROOT || '' , './src/server/server' ) ) ) { wConfig . entry = pawExistsSync ( path . join ( process . env . LIB_ROOT || '' , './src/server/build' ) ) ; } if ( ! wConfig . externals ) wConfig . externals = { } ; if ( typeof wConfig . externals [ 'pwa-assets' ] === 'undefined' ) { wConfig . externals [ 'pwa-assets' ] = './assets.json' ; } } ) ; } } ) ; try { const serverConfig = wHandler . getConfig ( process . env . PAW_ENV , 'server' ) ; const webConfig = wHandler . getConfig ( process . env . PAW_ENV , 'web' ) ; webpack ( webConfig , ( webErr ? , webStats ? ) => { if ( webErr || webStats ?. hasErrors ( ) ) { console . log ( webErr ) ; webStats ?. toJson && console . log ( webStats . toJson ( ) ) ; console . log ( 'Web compiler error occurred. Please handle error here' ) ; return ; } console . log ( webStats ?. toString ( stats ) ) ; webpack ( serverConfig , async ( serverErr , serverStats ) => { if ( serverErr || serverStats ?. hasErrors ( ) ) { if ( serverErr ) console . log ( serverErr ) ; console . log ( serverStats ?. toString ?. ( stats ) ) ; return ; } try { if ( fs . existsSync ( path . resolve ( directories . dist , 'images' ) ) ) { fse . moveSync ( path . resolve ( directories . dist , 'images' ) , path . resolve ( directories . build , 'images' ) , { overwrite : true } ) ; } } catch ( ex ) { console . log ( ex ) ; } try { if ( fs . existsSync ( path . resolve ( directories . dist , 'assets' ) ) ) { fse . moveSync ( path . resolve ( directories . dist , 'assets' ) , path . resolve ( directories . build , 'assets' ) , { overwrite : true } ) ; } } catch ( ex ) { console . log ( ex ) ; } console . log ( serverStats ?. toString ( stats ) ) ; if ( pawConfig . singlePageApplication ) { console . log ( 'Creating static files...' ) ; const outputConfig = serverConfig [ 0 ] . output ; let server = require ( pawExistsSync ( path . join ( outputConfig . path , outputConfig . filename ) ) ) ; server = server . default ? server . default : server ; console . log ( 'Generating index.html & manifest.json' ) ; const [ indexResponse , manifestResponse ] = await Promise . all ( [ request ( server ) . get ( '/' ) , request ( server ) . get ( '/manifest.json' ) , ] ) ; fs . writeFileSync ( path . join ( directories . build , 'index.html' ) , indexResponse . text , 'utf-8' ) ; fs . writeFileSync ( path . join ( directories . build , 'manifest.json' ) , manifestResponse . text , 'utf-8' ) ; console . log ( `Successfully created: ${ path . join ( directories . build , 'index.html' ) }` ) ; console . log ( `Successfully created: ${ path . join ( directories . build , 'manifest.json' ) }` ) ; console . log ( '\n\nRe-organizing files...\n' ) ; try { const tempPawJSBuildPath = path . join ( directories . root , 'pawjs-temp-build' ) ; if ( fse . existsSync ( tempPawJSBuildPath ) ) { fse . removeSync ( tempPawJSBuildPath ) ; } fse . moveSync ( directories . build , tempPawJSBuildPath ) ; fse . removeSync ( directories . dist ) ; fse . moveSync ( tempPawJSBuildPath , directories . dist ) ; console . log ( 'Static site generated successfully.' ) ; } catch ( ex ) { console . log ( ex ) ; } } } ) ; } ) ; } catch ( ex ) { console . log ( ex ) ; }