diff --git a/dashboard.webpack.config.cjs b/dashboard.webpack.config.cjs index 156a68b07..2100c20e6 100644 --- a/dashboard.webpack.config.cjs +++ b/dashboard.webpack.config.cjs @@ -13,11 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +/** + * External dependencies. + */ const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlInlineScriptPlugin = require('html-inline-script-webpack-plugin'); const CopyPlugin = require('copy-webpack-plugin'); const WebpackBar = require('webpackbar'); + +/** + * Internal dependencies. + */ const commonConfig = require('./webpack.shared.cjs'); const report = { @@ -62,8 +69,11 @@ const dashboard = { title: 'Report', template: '../cli-dashboard/public/index.html', filename: 'index.html', - inject: false, + inject: commonConfig.mode === 'production' ? 'body' : false, }), + ...(commonConfig.mode === 'production' + ? [new HtmlInlineScriptPlugin()] + : []), new CopyPlugin({ patterns: [ { diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 59a6c29a9..7c316c39a 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -19,7 +19,7 @@ */ import { Command } from 'commander'; import events from 'events'; -import { existsSync, ensureFile, writeFile } from 'fs-extra'; +import { existsSync, ensureDir } from 'fs-extra'; // @ts-ignore Package does not support typescript. import Spinnies from 'spinnies'; import fs from 'fs'; @@ -51,6 +51,7 @@ import { events.EventEmitter.defaultMaxListeners = 15; +const isProduction = process.env.NODE_ENV === 'production'; const DELAY_TIME = 20000; const program = new Command(); @@ -90,14 +91,6 @@ program program.parse(); -const saveResultsAsJSON = async ( - outDir: string, - result: CompleteJson | CompleteJson[] -) => { - await ensureFile(outDir + '/out.json'); - await writeFile(outDir + '/out.json', JSON.stringify(result, null, 4)); -}; - const saveResultsAsHTML = async ( outDir: string, result: CompleteJson | CompleteJson[], @@ -106,6 +99,8 @@ const saveResultsAsHTML = async ( let htmlText = ''; let reportHTML = ''; + await ensureDir(outDir); + if ( existsSync( path.resolve( @@ -130,13 +125,15 @@ const saveResultsAsHTML = async ( 'base64' ); - fs.copyFileSync( - path.resolve( - __dirname + - '../../node_modules/@google-psat/cli-dashboard/dist/index.js' - ), - outDir + '/index.js' - ); + if (!isProduction) { + fs.copyFileSync( + path.resolve( + __dirname + + '../../node_modules/@google-psat/cli-dashboard/dist/index.js' + ), + outDir + '/index.js' + ); + } } else { htmlText = fs.readFileSync( path.resolve(__dirname + '../../../cli-dashboard/dist/index.html'), @@ -148,10 +145,12 @@ const saveResultsAsHTML = async ( 'base64' ); - fs.copyFileSync( - path.resolve(__dirname + '../../../cli-dashboard/dist/index.js'), - outDir + '/index.js' - ); + if (!isProduction) { + fs.copyFileSync( + path.resolve(__dirname + '../../../cli-dashboard/dist/index.js'), + outDir + '/index.js' + ); + } } const messages = I18n.getMessages(); @@ -323,7 +322,6 @@ const saveResultsAsHTML = async ( process.exit(0); } - await saveResultsAsJSON(outputDir, result); await saveResultsAsHTML(outputDir, result, isSiteMap); })().catch((error) => { console.log('Some error occured while analysing the website.');