This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathbuild.js
140 lines (119 loc) · 3.69 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Build
*
* The create-guten-block CLI builds here.
*/
'use strict';
// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'production';
process.env.NODE_ENV = 'production';
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on( 'unhandledRejection', err => {
throw err;
} );
// Modules.
const fs = require( 'fs' );
const ora = require( 'ora' );
const path = require( 'path' );
const chalk = require( 'chalk' );
const webpack = require( 'webpack' );
const fileSize = require( 'filesize' );
const gzipSize = require( 'gzip-size' );
const resolvePkg = require( 'resolve-pkg' );
const config = require( '../config/webpack.config.prod' );
const cgbDevUtilsPath = resolvePkg( 'cgb-dev-utils', { cwd: __dirname } );
const clearConsole = require( cgbDevUtilsPath + '/clearConsole' );
const formatWebpackMessages = require( cgbDevUtilsPath +
'/formatWebpackMessages' );
// Build file paths.
const theCWD = process.cwd();
const fileBuildJS = path.resolve( theCWD, './dist/blocks.build.js' );
const fileEditorCSS = path.resolve( theCWD, './dist/blocks.editor.build.css' );
const fileStyleCSS = path.resolve( theCWD, './dist/blocks.style.build.css' );
/**
* Get File Size
*
* Get filesizes of all the files.
*
* @param {string} filePath path.
* @returns {string} then size result.
*/
const getFileSize = filePath => {
return fileSize( gzipSize.sync( fs.readFileSync( filePath ) ) );
};
clearConsole();
// Init the spinner.
const spinner = new ora( { text: '' } );
/**
* Build function
*
* Create the production build and print the deployment instructions.
*
* @param {json} webpackConfig config
*/
async function build( webpackConfig ) {
// Compiler Instance.
const compiler = await webpack( webpackConfig );
// Run the compiler.
compiler.run( ( err, stats ) => {
clearConsole();
if ( err ) {
return console.log( err );
}
// Get the messages formatted.
const messages = formatWebpackMessages( stats.toJson( {}, true ) );
// If there are errors just show the errors.
if ( messages.errors.length ) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if ( messages.errors.length > 1 ) {
messages.errors.length = 1;
}
// Formatted errors.
clearConsole();
console.log( '\n❌ ', chalk.black.bgRed( ' Failed to compile build. \n' ) );
console.log( '\n👉 ', messages.errors.join( '\n\n' ) );
// Don't go beyond this point at this time.
return;
}
// CI.
if (
process.env.CI &&
( typeof process.env.CI !== 'string' ||
process.env.CI.toLowerCase() !== 'false' ) &&
messages.warnings.length
) {
console.log(
chalk.yellow(
'\nTreating warnings as errors because process.env.CI = true.\n' +
'Most CI servers set it automatically.\n'
)
);
console.log( messages.warnings.join( '\n\n' ) );
}
// Start the build.
console.log( `\n ${ chalk.dim( 'Let\'s build and compile the files...' ) }` );
console.log( '\n✅ ', chalk.black.bgGreen( ' Built successfully! \n' ) );
console.log(
'\n\n',
'File sizes after gzip:',
'\n\n',
getFileSize( fileBuildJS ),
`${ chalk.dim( '— ./dist/' ) }`,
`${ chalk.green( 'blocks.build.js' ) }`,
'\n',
getFileSize( fileEditorCSS ),
`${ chalk.dim( '— ./dist/' ) }`,
`${ chalk.green( 'blocks.editor.build.css' ) }`,
'\n',
getFileSize( fileStyleCSS ),
`${ chalk.dim( '— ./dist/' ) }`,
`${ chalk.green( 'blocks.style.build.css' ) }`,
'\n\n'
);
return true;
} );
}
build( config );