-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159300 from microsoft/joh/swc
SWC experiments
- Loading branch information
Showing
12 changed files
with
382 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/swcrc", | ||
"exclude": "\\.js$", | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": false, | ||
"decorators": true | ||
}, | ||
"target": "es2020", | ||
"loose": false, | ||
"minify": { | ||
"compress": false, | ||
"mangle": false | ||
} | ||
}, | ||
"module": { | ||
"type": "amd", | ||
"noInterop": true | ||
}, | ||
"minify": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/swcrc", | ||
"exclude": "\\.js$", | ||
"jsc": { | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": false, | ||
"decorators": true | ||
}, | ||
"target": "es2020", | ||
"loose": false, | ||
"minify": { | ||
"compress": false, | ||
"mangle": false | ||
} | ||
}, | ||
"isModule": false, | ||
"module": { | ||
"type": "es6" | ||
}, | ||
"minify": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use strict"; | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createSwcClientStream = void 0; | ||
const child_process_1 = require("child_process"); | ||
const stream_1 = require("stream"); | ||
const path_1 = require("path"); | ||
const util = require("util"); | ||
const gulp = require("gulp"); | ||
/** | ||
* SWC transpile stream. Can be used as stream but `exec` is the prefered way because under the | ||
* hood this simply shells out to swc-cli. There is room for improvement but this already works. | ||
* Ideas | ||
* * use API, not swc-cli | ||
* * invoke binaries directly, don't go through swc-cli | ||
* * understand how to configure both setups in one (https://github.com/swc-project/swc/issues/4989) | ||
*/ | ||
function createSwcClientStream() { | ||
const execAsync = util.promisify(child_process_1.exec); | ||
const cwd = (0, path_1.join)(__dirname, '../../../'); | ||
const srcDir = (0, path_1.join)(__dirname, '../../../src'); | ||
const outDir = (0, path_1.join)(__dirname, '../../../out'); | ||
const pathConfigAmd = (0, path_1.join)(__dirname, '.swcrc-amd'); | ||
const pathConfigNoModule = (0, path_1.join)(__dirname, '.swcrc-no-mod'); | ||
return new class extends stream_1.Readable { | ||
constructor() { | ||
super({ objectMode: true, highWaterMark: Number.MAX_SAFE_INTEGER }); | ||
this._isStarted = false; | ||
} | ||
async exec(print) { | ||
const t1 = Date.now(); | ||
const errors = []; | ||
try { | ||
const data1 = await execAsync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); | ||
errors.push(data1.stderr); | ||
const data2 = await execAsync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); | ||
errors.push(data2.stderr); | ||
return true; | ||
} | ||
catch (error) { | ||
console.error(errors); | ||
console.error(error); | ||
this.destroy(error); | ||
return false; | ||
} | ||
finally { | ||
if (print) { | ||
console.log(`DONE with SWC after ${Date.now() - t1}ms`); | ||
} | ||
} | ||
} | ||
async _read(_size) { | ||
if (this._isStarted) { | ||
return; | ||
} | ||
this._isStarted = true; | ||
if (!this.exec()) { | ||
this.push(null); | ||
return; | ||
} | ||
for await (const file of gulp.src(`${outDir}/**/*.js`, { base: outDir })) { | ||
this.push(file); | ||
} | ||
this.push(null); | ||
} | ||
}; | ||
} | ||
exports.createSwcClientStream = createSwcClientStream; | ||
if (process.argv[1] === __filename) { | ||
createSwcClientStream().exec(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { exec } from 'child_process'; | ||
import { Readable } from 'stream'; | ||
import { join } from 'path'; | ||
import * as util from 'util'; | ||
import * as gulp from 'gulp'; | ||
|
||
/** | ||
* SWC transpile stream. Can be used as stream but `exec` is the prefered way because under the | ||
* hood this simply shells out to swc-cli. There is room for improvement but this already works. | ||
* Ideas | ||
* * use API, not swc-cli | ||
* * invoke binaries directly, don't go through swc-cli | ||
* * understand how to configure both setups in one (https://github.com/swc-project/swc/issues/4989) | ||
*/ | ||
export function createSwcClientStream(): Readable & { exec(print?: boolean): Promise<boolean> } { | ||
|
||
const execAsync = util.promisify(exec); | ||
|
||
const cwd = join(__dirname, '../../../'); | ||
const srcDir = join(__dirname, '../../../src'); | ||
const outDir = join(__dirname, '../../../out'); | ||
|
||
const pathConfigAmd = join(__dirname, '.swcrc-amd'); | ||
const pathConfigNoModule = join(__dirname, '.swcrc-no-mod'); | ||
|
||
return new class extends Readable { | ||
|
||
private _isStarted = false; | ||
|
||
constructor() { | ||
super({ objectMode: true, highWaterMark: Number.MAX_SAFE_INTEGER }); | ||
} | ||
|
||
async exec(print?: boolean) { | ||
const t1 = Date.now(); | ||
const errors: string[] = []; | ||
try { | ||
const data1 = await execAsync(`npx swc --config-file ${pathConfigAmd} ${srcDir}/ --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); | ||
errors.push(data1.stderr); | ||
|
||
const data2 = await execAsync(`npx swc --config-file ${pathConfigNoModule} ${srcDir}/vs/base/worker/workerMain.ts --out-dir ${outDir}`, { encoding: 'utf-8', cwd }); | ||
errors.push(data2.stderr); | ||
return true; | ||
} catch (error) { | ||
console.error(errors); | ||
console.error(error); | ||
this.destroy(error); | ||
return false; | ||
} finally { | ||
if (print) { | ||
console.log(`DONE with SWC after ${Date.now() - t1}ms`); | ||
} | ||
} | ||
} | ||
|
||
async _read(_size: number): Promise<void> { | ||
if (this._isStarted) { | ||
return; | ||
} | ||
this._isStarted = true; | ||
if (!this.exec()) { | ||
this.push(null); | ||
return; | ||
} | ||
for await (const file of gulp.src(`${outDir}/**/*.js`, { base: outDir })) { | ||
this.push(file); | ||
} | ||
this.push(null); | ||
} | ||
}; | ||
} | ||
|
||
if (process.argv[1] === __filename) { | ||
createSwcClientStream().exec(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.