|
| 1 | +"use strict"; |
| 2 | +/*--------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 7 | +exports.createSwcClientStream = void 0; |
| 8 | +const child_process_1 = require("child_process"); |
| 9 | +const stream_1 = require("stream"); |
| 10 | +const path_1 = require("path"); |
| 11 | +const util = require("util"); |
| 12 | +const gulp = require("gulp"); |
| 13 | +/** |
| 14 | + * SWC transpile stream. Can be used as stream but `exec` is the prefered way because under the |
| 15 | + * hood this simply shells out to swc-cli. There is room for improvement but this already works. |
| 16 | + * Ideas |
| 17 | + * * use API, not swc-cli |
| 18 | + * * invoke binaries directly, don't go through swc-cli |
| 19 | + * * understand how to configure both setups in one (https://github.com/swc-project/swc/issues/4989) |
| 20 | + */ |
| 21 | +function createSwcClientStream() { |
| 22 | + const execAsync = util.promisify(child_process_1.exec); |
| 23 | + const cwd = (0, path_1.join)(__dirname, '../../../'); |
| 24 | + const srcDir = (0, path_1.join)(__dirname, '../../../src'); |
| 25 | + const outDir = (0, path_1.join)(__dirname, '../../../out'); |
| 26 | + const pathConfigAmd = (0, path_1.join)(__dirname, '.swcrc-amd'); |
| 27 | + const pathConfigNoModule = (0, path_1.join)(__dirname, '.swcrc-no-mod'); |
| 28 | + return new class extends stream_1.Readable { |
| 29 | + constructor() { |
| 30 | + super({ objectMode: true, highWaterMark: Number.MAX_SAFE_INTEGER }); |
| 31 | + this._isStarted = false; |
| 32 | + } |
| 33 | + async exec(print) { |
| 34 | + const t1 = Date.now(); |
| 35 | + const errors = []; |
| 36 | + try { |
| 37 | + const data1 = await execAsync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); |
| 38 | + errors.push(data1.stderr); |
| 39 | + const data2 = await execAsync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); |
| 40 | + errors.push(data2.stderr); |
| 41 | + return true; |
| 42 | + } |
| 43 | + catch (error) { |
| 44 | + console.error(errors); |
| 45 | + console.error(error); |
| 46 | + this.destroy(error); |
| 47 | + return false; |
| 48 | + } |
| 49 | + finally { |
| 50 | + if (print) { |
| 51 | + console.log(`DONE with SWC after ${Date.now() - t1}ms`); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + async _read(_size) { |
| 56 | + if (this._isStarted) { |
| 57 | + return; |
| 58 | + } |
| 59 | + this._isStarted = true; |
| 60 | + if (!this.exec()) { |
| 61 | + this.push(null); |
| 62 | + return; |
| 63 | + } |
| 64 | + for await (const file of gulp.src(`${outDir}/**/*.js`, { base: outDir })) { |
| 65 | + this.push(file); |
| 66 | + } |
| 67 | + this.push(null); |
| 68 | + } |
| 69 | + }; |
| 70 | +} |
| 71 | +exports.createSwcClientStream = createSwcClientStream; |
| 72 | +if (process.argv[1] === __filename) { |
| 73 | + createSwcClientStream().exec(true); |
| 74 | +} |
0 commit comments