/
build.js
executable file
·276 lines (237 loc) · 6.86 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const glob = require( 'fast-glob' );
const ProgressBar = require( 'progress' );
const workerFarm = require( 'worker-farm' );
const { Readable, Transform } = require( 'stream' );
const files = process.argv.slice( 2 );
/**
* Path to packages directory.
*
* @type {string}
*/
const PACKAGES_DIR = path
.resolve( __dirname, '../../packages' )
.replace( /\\/g, '/' );
const stylesheetEntryPoints = glob.sync(
path.resolve( PACKAGES_DIR, '*/src/*.scss' )
);
/**
* Get the package name for a specified file
*
* @param {string} file File name.
*
* @return {string} Package name.
*/
function getPackageName( file ) {
return path.relative( PACKAGES_DIR, file ).split( path.sep )[ 0 ];
}
/**
* Parses all Sass import statements in a given file
*
* @param {string} file File name.
*
* @return {Array} List of Import Statements in a file.
*/
function parseImportStatements( file ) {
const fileContent = fs.readFileSync( file, 'utf8' );
return fileContent.toString().match( /@import "(.*?)"/g );
}
function isFileImportedInStyleEntry( file, importStatements ) {
const packageName = getPackageName( file );
const regex = new RegExp( `/${ packageName }/`, 'g' );
return (
importStatements &&
importStatements.find( ( importStatement ) =>
importStatement.match( regex )
)
);
}
/**
* Finds all stylesheet entry points that contain import statements
* that include the given file name
*
* @param {string} file File name.
*
* @return {Array} List of entry points that import the styles from the file.
*/
function findStyleEntriesThatImportFile( file ) {
const entriesWithImport = stylesheetEntryPoints.reduce(
( acc, entryPoint ) => {
const styleEntryImportStatements =
parseImportStatements( entryPoint );
if (
isFileImportedInStyleEntry( file, styleEntryImportStatements )
) {
acc.push( entryPoint );
}
return acc;
},
[]
);
return entriesWithImport;
}
/**
* Returns a stream transform which maps an individual stylesheet to its
* package entrypoint. Unlike JavaScript which uses an external bundler to
* efficiently manage rebuilds by entrypoints, stylesheets are rebuilt fresh
* in their entirety from the build script.
*
* @return {Transform} Stream transform instance.
*/
function createStyleEntryTransform() {
const packages = new Set();
return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
// Only stylesheets are subject to this transform.
if ( path.extname( file ) !== '.scss' ) {
this.push( file );
callback();
return;
}
// Only operate once per package, assuming entries are common.
const packageName = getPackageName( file );
if ( packages.has( packageName ) ) {
callback();
return;
}
packages.add( packageName );
const entries = await glob(
path.resolve( PACKAGES_DIR, packageName, 'src/*.scss' )
);
// Account for the specific case where block styles in
// block-library package also need rebuilding.
if (
packageName === 'block-library' &&
[ 'style.scss', 'editor.scss', 'theme.scss' ].includes(
path.basename( file )
)
) {
entries.push( file );
}
entries.forEach( ( entry ) => this.push( entry ) );
// Find other stylesheets that need to be rebuilt because
// they import the styles that are being transformed.
const styleEntries = findStyleEntriesThatImportFile( file );
// Rebuild stylesheets that import the styles being transformed.
if ( styleEntries.length ) {
styleEntries.forEach( ( entry ) => stream.push( entry ) );
}
callback();
},
} );
}
/**
* Returns a stream transform which maps an individual block.json to the
* index.js that imports it. Presently, babel resolves the import of json
* files by inlining them as a JavaScript primitive in the importing file.
* This transform ensures the importing file is rebuilt.
*
* @return {Transform} Stream transform instance.
*/
function createBlockJsonEntryTransform() {
const blocks = new Set();
return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
const matches =
/block-library[\/\\]src[\/\\](.*)[\/\\]block.json$/.exec(
file
);
const blockName = matches ? matches[ 1 ] : undefined;
// Only block.json files in the block-library folder are subject to this transform.
if ( ! blockName ) {
this.push( file );
callback();
return;
}
// Only operate once per block, assuming entries are common.
if ( blockName && blocks.has( blockName ) ) {
callback();
return;
}
blocks.add( blockName );
this.push( file.replace( 'block.json', 'index.js' ) );
callback();
},
} );
}
let onFileComplete = () => {};
let stream;
if ( files.length ) {
stream = new Readable( { encoding: 'utf8' } );
files.forEach( ( file ) => {
stream.push( file );
} );
stream.push( null );
stream = stream
.pipe( createStyleEntryTransform() )
.pipe( createBlockJsonEntryTransform() );
} else {
const bar = new ProgressBar( 'Build Progress: [:bar] :percent', {
width: 30,
incomplete: ' ',
total: 1,
} );
bar.tick( 0 );
stream = glob.stream(
[
`${ PACKAGES_DIR }/*/src/**/*.{js,ts,tsx}`,
`${ PACKAGES_DIR }/*/src/*.scss`,
`${ PACKAGES_DIR }/block-library/src/**/*.js`,
`${ PACKAGES_DIR }/block-library/src/*/style.scss`,
`${ PACKAGES_DIR }/block-library/src/*/theme.scss`,
`${ PACKAGES_DIR }/block-library/src/*/editor.scss`,
`${ PACKAGES_DIR }/block-library/src/*.scss`,
],
{
ignore: [
`**/benchmark/**`,
`**/{__mocks__,__tests__,test}/**`,
`**/{storybook,stories}/**`,
`**/e2e-test-utils-playwright/**`,
],
onlyFiles: true,
}
);
// Pause to avoid data flow which would begin on the `data` event binding,
// but should wait until worker processing below.
//
// See: https://nodejs.org/api/stream.html#stream_two_reading_modes
stream.pause().on( 'data', ( file ) => {
bar.total = files.push( file );
} );
onFileComplete = () => {
bar.tick();
};
}
const worker = workerFarm( require.resolve( './build-worker' ) );
let ended = false,
complete = 0;
stream
.on( 'data', ( file ) =>
worker( file, ( error ) => {
onFileComplete();
if ( error ) {
// If an error occurs, the process can't be ended immediately since
// other workers are likely pending. Optimally, it would end at the
// earliest opportunity (after the current round of workers has had
// the chance to complete), but this is not made directly possible
// through `worker-farm`. Instead, ensure at least that when the
// process does exit, it exits with a non-zero code to reflect the
// fact that an error had occurred.
process.exitCode = 1;
console.error( error );
}
++complete;
if ( ended && complete === files.length ) {
workerFarm.end( worker );
}
} )
)
.on( 'end', () => ( ended = true ) )
.resume();