-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
build-worker.js
199 lines (178 loc) · 4.22 KB
/
build-worker.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
/**
* External dependencies
*/
const { promisify } = require( 'util' );
const fs = require( 'fs' );
const path = require( 'path' );
const babel = require( '@babel/core' );
const makeDir = require( 'make-dir' );
const sass = require( 'sass' );
const postcss = require( 'postcss' );
/**
* Internal dependencies
*/
const getBabelConfig = require( './get-babel-config' );
/**
* Path to packages directory.
*
* @type {string}
*/
const PACKAGES_DIR = path
.resolve( __dirname, '../../packages' )
.replace( /\\/g, '/' );
/**
* Mapping of JavaScript environments to corresponding build output.
*
* @type {Object}
*/
const JS_ENVIRONMENTS = {
main: 'build',
module: 'build-module',
};
/**
* Promisified fs.readFile.
*
* @type {Function}
*/
const readFile = promisify( fs.readFile );
/**
* Promisified fs.writeFile.
*
* @type {Function}
*/
const writeFile = promisify( fs.writeFile );
/**
* Promisified sass.render.
*
* @type {Function}
*/
const renderSass = promisify( sass.render );
/**
* 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 ];
}
/**
* Get Build Path for a specified file.
*
* @param {string} file File to build.
* @param {string} buildFolder Output folder.
*
* @return {string} Build path.
*/
function getBuildPath( file, buildFolder ) {
const pkgName = getPackageName( file );
const pkgSrcPath = path.resolve( PACKAGES_DIR, pkgName, 'src' );
const pkgBuildPath = path.resolve( PACKAGES_DIR, pkgName, buildFolder );
const relativeToSrcPath = path.relative( pkgSrcPath, file );
return path.resolve( pkgBuildPath, relativeToSrcPath );
}
async function buildCSS( file ) {
const outputFile = getBuildPath(
file.replace( '.scss', '.css' ),
'build-style'
);
const outputFileRTL = getBuildPath(
file.replace( '.scss', '-rtl.css' ),
'build-style'
);
const [ , contents ] = await Promise.all( [
makeDir( path.dirname( outputFile ) ),
readFile( file, 'utf8' ),
] );
const importLists = [
'colors',
'breakpoints',
'variables',
'mixins',
'animations',
'z-index',
]
// Editor styles should be excluded from the default CSS vars output.
.concat(
file.includes( 'common.scss' ) || ! file.includes( 'block-library' )
? [ 'default-custom-properties' ]
: []
)
.map( ( imported ) => `@import "${ imported }";` )
.join( ' ' );
const builtSass = await renderSass( {
file,
includePaths: [ path.join( PACKAGES_DIR, 'base-styles' ) ],
data: ''.concat( '@use "sass:math";', importLists, contents ),
} );
const result = await postcss(
require( '@wordpress/postcss-plugins-preset' )
).process( builtSass.css, {
from: 'src/app.css',
to: 'dest/app.css',
} );
const resultRTL = await postcss( [ require( 'rtlcss' )() ] ).process(
result.css,
{
from: 'src/app.css',
to: 'dest/app.css',
}
);
await Promise.all( [
writeFile( outputFile, result.css ),
writeFile( outputFileRTL, resultRTL.css ),
] );
}
async function buildJS( file ) {
for ( const [ environment, buildDir ] of Object.entries(
JS_ENVIRONMENTS
) ) {
const destPath = getBuildPath(
file.replace( /\.tsx?$/, '.js' ),
buildDir
);
const babelOptions = getBabelConfig(
environment,
file.replace( PACKAGES_DIR, '@wordpress' )
);
const [ , transformed ] = await Promise.all( [
makeDir( path.dirname( destPath ) ),
babel.transformFileAsync( file, babelOptions ),
] );
await Promise.all( [
writeFile( destPath + '.map', JSON.stringify( transformed.map ) ),
writeFile(
destPath,
transformed.code +
'\n//# sourceMappingURL=' +
path.basename( destPath ) +
'.map'
),
] );
}
}
/**
* Object of build tasks per file extension.
*
* @type {Object<string,Function>}
*/
const BUILD_TASK_BY_EXTENSION = {
'.scss': buildCSS,
'.js': buildJS,
'.ts': buildJS,
'.tsx': buildJS,
};
module.exports = async ( file, callback ) => {
const extension = path.extname( file );
const task = BUILD_TASK_BY_EXTENSION[ extension ];
if ( ! task ) {
callback( new Error( `No handler for extension: ${ extension }` ) );
}
try {
await task( file );
callback();
} catch ( error ) {
callback( error );
}
};