Skip to content

Commit 643ab3f

Browse files
author
Ahmad Awais
committed
📦 NEW: Build Script
1 parent ea442d9 commit 643ab3f

5 files changed

Lines changed: 103 additions & 9 deletions

File tree

packages/cgb-scripts/package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cgb-scripts/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"eslint-plugin-react": "^7.5.1",
3030
"eslint-plugin-wordpress": "^0.1.0",
3131
"extract-text-webpack-plugin": "^3.0.2",
32+
"filesize": "^3.5.11",
33+
"gzip-size": "^4.1.0",
3234
"node-sass": "^4.7.2",
3335
"ora": "^1.3.0",
3436
"postcss-loader": "^2.0.10",

packages/cgb-scripts/scripts/build.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,63 @@ process.on( 'unhandledRejection', err => {
2020
throw err;
2121
} );
2222

23+
// Modules.
24+
const fs = require( 'fs' );
2325
const ora = require( 'ora' );
26+
const path = require( 'path' );
2427
const chalk = require( 'chalk' );
2528
const webpack = require( 'webpack' );
26-
const config = require( '../config/webpack.config.dev' );
29+
const fileSize = require( 'filesize' );
30+
const gzipSize = require( 'gzip-size' );
2731
const resolvePkg = require( 'resolve-pkg' );
32+
const config = require( '../config/webpack.config.prod' );
2833
const cgbDevUtilsPath = resolvePkg( 'cgb-dev-utils', { cwd: __dirname } );
2934
const clearConsole = require( cgbDevUtilsPath + '/clearConsole' );
3035
const formatWebpackMessages = require( cgbDevUtilsPath +
3136
'/formatWebpackMessages' );
3237

38+
// Build file paths.
39+
const theCWD = process.cwd();
40+
const fileBuildJS = path.resolve( theCWD, './dist/blocks.build.js' );
41+
const fileEditorCSS = path.resolve( theCWD, './dist/blocks.editor.build.css' );
42+
const fileStyleCSS = path.resolve( theCWD, './dist/blocks.style.build.css' );
43+
44+
/**
45+
* Get File Size
46+
*
47+
* Get filesizes of all the files.
48+
*
49+
* @param {string} filePath path.
50+
* @returns {string} then size result.
51+
*/
52+
const getFileSize = filePath => {
53+
return fileSize( gzipSize.sync( fs.readFileSync( filePath ) ) );
54+
};
55+
3356
clearConsole();
3457

3558
// Init the spinner.
3659
const spinner = new ora( {
3760
text: '',
3861
enabled: true,
3962
} );
40-
// Create the production build and print the deployment instructions.
63+
64+
/**
65+
* Build function
66+
*
67+
* Create the production build and print the deployment instructions.
68+
*
69+
* @param {json} webpackConfig config
70+
*/
4171
async function build( webpackConfig ) {
72+
// Start the build.
4273
spinner.start( `${ chalk.dim( 'Building and compiling blocks...' ) }` );
4374

4475
// Compiler Instance.
4576
const compiler = await webpack( webpackConfig );
4677
spinner.succeed();
4778

48-
compiler.run( {}, ( err, stats ) => {
79+
compiler.run( ( err, stats ) => {
4980
if ( err ) {
5081
return console.log( err );
5182
}
@@ -84,7 +115,29 @@ async function build( webpackConfig ) {
84115
}
85116

86117
clearConsole();
87-
return console.log( '\n✅ ', chalk.black.bgGreen( ' Built successfully! \n' ) );
118+
console.log( '\n✅ ', chalk.black.bgGreen( ' Built successfully! \n' ) );
119+
120+
console.log(
121+
'\n\n',
122+
'File sizes after gzip:',
123+
'\n\n',
124+
getFileSize( fileBuildJS ),
125+
`${ chalk.dim( '— ./dist/' ) }`,
126+
`${ chalk.green( 'blocks.build.js' ) }`,
127+
'\n',
128+
getFileSize( fileEditorCSS ),
129+
`${ chalk.dim( '— ./dist/' ) }`,
130+
`${ chalk.green( 'blocks.editor.build.css' ) }`,
131+
132+
'\n',
133+
getFileSize( fileStyleCSS ),
134+
`${ chalk.dim( '— ./dist/' ) }`,
135+
`${ chalk.green( 'blocks.style.build.css' ) }`,
136+
137+
'\n\n'
138+
);
139+
140+
return true;
88141
} );
89142
}
90143

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1+
console.clear();
12
console.log( 'MANUAL TEST!' );
2-
3-
const resolvePkg = require( 'resolve-pkg' );
4-
const cgbDevUtils = resolvePkg( 'cgb-dev-utils', { cwd: __dirname } );
5-
6-
console.log( cgbDevUtils );

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,10 @@ filename-regex@^2.0.0:
20862086
version "2.0.1"
20872087
resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26"
20882088

2089+
filesize@^3.5.11:
2090+
version "3.5.11"
2091+
resolved "https://registry.yarnpkg.com/filesize/-/filesize-3.5.11.tgz#1919326749433bb3cf77368bd158caabcc19e9ee"
2092+
20892093
fill-range@^2.1.0:
20902094
version "2.2.3"
20912095
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-2.2.3.tgz#50b77dfd7e469bc7492470963699fe7a8485a723"
@@ -2409,6 +2413,13 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6:
24092413
version "4.1.11"
24102414
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
24112415

2416+
gzip-size@^4.1.0:
2417+
version "4.1.0"
2418+
resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-4.1.0.tgz#8ae096257eabe7d69c45be2b67c448124ffb517c"
2419+
dependencies:
2420+
duplexer "^0.1.1"
2421+
pify "^3.0.0"
2422+
24122423
handlebars@^4.0.2:
24132424
version "4.0.11"
24142425
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.0.11.tgz#630a35dfe0294bc281edae6ffc5d329fc7982dcc"

0 commit comments

Comments
 (0)