Skip to content

Commit 7ec1ebd

Browse files
committed
* tweak createSwcClientStream to have async exec
* add `transpileClientSWC` and corresponding gulp task
1 parent bbf0bd9 commit 7ec1ebd

File tree

5 files changed

+98
-19
lines changed

5 files changed

+98
-19
lines changed

build/gulpfile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ require('events').EventEmitter.defaultMaxListeners = 100;
1111
const gulp = require('gulp');
1212
const util = require('./lib/util');
1313
const task = require('./lib/task');
14-
const { transpileTask, compileTask, watchTask, compileApiProposalNamesTask, watchApiProposalNamesTask } = require('./lib/compilation');
14+
const { transpileClientSWC, transpileTask, compileTask, watchTask, compileApiProposalNamesTask, watchApiProposalNamesTask } = require('./lib/compilation');
1515
const { monacoTypecheckTask/* , monacoTypecheckWatchTask */ } = require('./gulpfile.editor');
1616
const { compileExtensionsTask, watchExtensionsTask, compileExtensionMediaTask } = require('./gulpfile.extensions');
1717

1818
// API proposal names
1919
gulp.task(compileApiProposalNamesTask);
2020
gulp.task(watchApiProposalNamesTask);
2121

22+
// SWC Client Transpile
23+
const transpileClientSWCTask = task.define('transpile-client-swc', task.series(util.rimraf('out'), transpileClientSWC('src', 'out')));
24+
gulp.task(transpileClientSWCTask);
25+
2226
// Transpile only
2327
const transpileClientTask = task.define('transpile-client', task.series(util.rimraf('out'), util.buildWebNodePaths('out'), transpileTask('src', 'out')));
2428
gulp.task(transpileClientTask);

build/lib/compilation.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66
Object.defineProperty(exports, "__esModule", { value: true });
7-
exports.watchApiProposalNamesTask = exports.compileApiProposalNamesTask = exports.watchTask = exports.compileTask = exports.transpileTask = void 0;
7+
exports.watchApiProposalNamesTask = exports.compileApiProposalNamesTask = exports.watchTask = exports.compileTask = exports.transpileTask = exports.transpileClientSWC = void 0;
88
const es = require("event-stream");
99
const fs = require("fs");
1010
const gulp = require("gulp");
@@ -18,7 +18,29 @@ const ansiColors = require("ansi-colors");
1818
const os = require("os");
1919
const File = require("vinyl");
2020
const task = require("./task");
21+
const swc_1 = require("./swc");
2122
const watch = require('./watch');
23+
// --- SWC: transpile -------------------------------------
24+
function transpileClientSWC(src, out) {
25+
return function () {
26+
// run SWC sync and put files straight onto the disk
27+
const swcPromise = (0, swc_1.createSwcClientStream)().exec();
28+
// copy none TS resources, like CSS, images, onto the disk
29+
const bom = require('gulp-bom');
30+
const utf8Filter = util.filter(data => /(\/|\\)test(\/|\\).*utf8/.test(data.path));
31+
const tsFilter = util.filter(data => !/\.ts$/.test(data.path));
32+
const srcStream = gulp.src(`${src}/**`, { base: `${src}` });
33+
const copyStream = srcStream
34+
.pipe(utf8Filter)
35+
.pipe(bom()) // this is required to preserve BOM in test files that loose it otherwise
36+
.pipe(utf8Filter.restore)
37+
.pipe(tsFilter);
38+
const copyPromise = util.streamToPromise(copyStream.pipe(gulp.dest(out)));
39+
return Promise.all([swcPromise, copyPromise]);
40+
};
41+
}
42+
exports.transpileClientSWC = transpileClientSWC;
43+
// --- gulp-tsb: compile and transpile --------------------------------
2244
const reporter = (0, reporter_1.createReporter)();
2345
function getTypeScriptCompilerOptions(src) {
2446
const rootDir = path.join(__dirname, `../../${src}`);

build/lib/compilation.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,40 @@ import * as os from 'os';
1717
import ts = require('typescript');
1818
import * as File from 'vinyl';
1919
import * as task from './task';
20-
20+
import { createSwcClientStream } from './swc';
2121
const watch = require('./watch');
2222

23+
24+
// --- SWC: transpile -------------------------------------
25+
26+
export function transpileClientSWC(src: string, out: string) {
27+
28+
return function () {
29+
30+
// run SWC sync and put files straight onto the disk
31+
const swcPromise = createSwcClientStream().exec();
32+
33+
// copy none TS resources, like CSS, images, onto the disk
34+
const bom = require('gulp-bom') as typeof import('gulp-bom');
35+
const utf8Filter = util.filter(data => /(\/|\\)test(\/|\\).*utf8/.test(data.path));
36+
const tsFilter = util.filter(data => !/\.ts$/.test(data.path));
37+
const srcStream = gulp.src(`${src}/**`, { base: `${src}` });
38+
39+
const copyStream = srcStream
40+
.pipe(utf8Filter)
41+
.pipe(bom()) // this is required to preserve BOM in test files that loose it otherwise
42+
.pipe(utf8Filter.restore)
43+
.pipe(tsFilter);
44+
45+
const copyPromise = util.streamToPromise(copyStream.pipe(gulp.dest(out)));
46+
47+
return Promise.all([swcPromise, copyPromise]);
48+
};
49+
50+
}
51+
52+
// --- gulp-tsb: compile and transpile --------------------------------
53+
2354
const reporter = createReporter();
2455

2556
function getTypeScriptCompilerOptions(src: string): ts.CompilerOptions {

build/lib/swc/index.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ exports.createSwcClientStream = void 0;
88
const child_process_1 = require("child_process");
99
const stream_1 = require("stream");
1010
const path_1 = require("path");
11+
const util = require("util");
1112
const gulp = require("gulp");
1213
function createSwcClientStream() {
14+
const execAsync = util.promisify(child_process_1.exec);
15+
const cwd = (0, path_1.join)(__dirname, '../../../');
1316
const srcDir = (0, path_1.join)(__dirname, '../../../src');
1417
const outDir = (0, path_1.join)(__dirname, '../../../out');
1518
const pathConfigAmd = (0, path_1.join)(__dirname, '.swcrc-amd');
@@ -19,19 +22,27 @@ function createSwcClientStream() {
1922
super({ objectMode: true, highWaterMark: Number.MAX_SAFE_INTEGER });
2023
this._isStarted = false;
2124
}
22-
exec() {
25+
async exec(print) {
26+
const t1 = Date.now();
27+
const errors = [];
2328
try {
24-
const out1 = (0, child_process_1.execSync)(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8' });
25-
console.log(out1);
26-
const out2 = (0, child_process_1.execSync)(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8' });
27-
console.log(out2);
29+
const data1 = await execAsync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8', cwd });
30+
errors.push(data1.stderr);
31+
const data2 = await execAsync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8', cwd });
32+
errors.push(data2.stderr);
2833
return true;
2934
}
3035
catch (error) {
31-
console.error();
36+
console.error(errors);
37+
console.error(error);
3238
this.destroy(error);
3339
return false;
3440
}
41+
finally {
42+
if (print) {
43+
console.log(`DONE with SWC after ${Date.now() - t1}ms`);
44+
}
45+
}
3546
}
3647
async _read(_size) {
3748
if (this._isStarted) {
@@ -51,5 +62,5 @@ function createSwcClientStream() {
5162
}
5263
exports.createSwcClientStream = createSwcClientStream;
5364
if (process.argv[1] === __filename) {
54-
createSwcClientStream().exec();
65+
createSwcClientStream().exec(true);
5566
}

build/lib/swc/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { execSync } from 'child_process';
6+
import { exec } from 'child_process';
77
import { Readable } from 'stream';
88
import { join } from 'path';
9+
import * as util from 'util';
910
import * as gulp from 'gulp';
1011

11-
export function createSwcClientStream() {
12+
export function createSwcClientStream(): Readable & { exec(print?: boolean): Promise<boolean> } {
1213

14+
const execAsync = util.promisify(exec);
15+
16+
const cwd = join(__dirname, '../../../');
1317
const srcDir = join(__dirname, '../../../src');
1418
const outDir = join(__dirname, '../../../out');
1519

@@ -24,18 +28,25 @@ export function createSwcClientStream() {
2428
super({ objectMode: true, highWaterMark: Number.MAX_SAFE_INTEGER });
2529
}
2630

27-
exec() {
31+
async exec(print?: boolean) {
32+
const t1 = Date.now();
33+
const errors: string[] = [];
2834
try {
29-
const out1 = execSync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8' });
30-
console.log(out1);
35+
const data1 = await execAsync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8', cwd });
36+
errors.push(data1.stderr);
3137

32-
const out2 = execSync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8' });
33-
console.log(out2);
38+
const data2 = await execAsync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8', cwd });
39+
errors.push(data2.stderr);
3440
return true;
3541
} catch (error) {
36-
console.error();
42+
console.error(errors);
43+
console.error(error);
3744
this.destroy(error);
3845
return false;
46+
} finally {
47+
if (print) {
48+
console.log(`DONE with SWC after ${Date.now() - t1}ms`);
49+
}
3950
}
4051
}
4152

@@ -57,5 +68,5 @@ export function createSwcClientStream() {
5768
}
5869

5970
if (process.argv[1] === __filename) {
60-
createSwcClientStream().exec();
71+
createSwcClientStream().exec(true);
6172
}

0 commit comments

Comments
 (0)