Skip to content

Commit

Permalink
Inline scripts and data for the CLI analysis report file (#745)
Browse files Browse the repository at this point in the history
* Do not create out.json file as it is not being used

* Use inject true for cli dashboard file

* Use HtmlInlineScriptPlugin for inlining script in index.html

* Conditionally inject script for dev mode
  • Loading branch information
mohdsayed authored Jul 14, 2024
1 parent e931780 commit 629aed9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
12 changes: 11 additions & 1 deletion dashboard.webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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: [
{
Expand Down
40 changes: 19 additions & 21 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -51,6 +51,7 @@ import {

events.EventEmitter.defaultMaxListeners = 15;

const isProduction = process.env.NODE_ENV === 'production';
const DELAY_TIME = 20000;
const program = new Command();

Expand Down Expand Up @@ -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[],
Expand All @@ -106,6 +99,8 @@ const saveResultsAsHTML = async (
let htmlText = '';
let reportHTML = '';

await ensureDir(outDir);

if (
existsSync(
path.resolve(
Expand All @@ -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'),
Expand All @@ -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();
Expand Down Expand Up @@ -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.');
Expand Down

0 comments on commit 629aed9

Please sign in to comment.