From d6f572702129b782ae55061cbb92a043d7ea975a Mon Sep 17 00:00:00 2001 From: Johannes Date: Wed, 7 Sep 2022 11:18:53 +0200 Subject: [PATCH] also use SWC for extensions transpile --- build/gulpfile.extensions.js | 2 +- build/lib/tsb/transpiler.js | 25 +++++++++++++++++++++++-- build/lib/tsb/transpiler.ts | 28 ++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/build/gulpfile.extensions.js b/build/gulpfile.extensions.js index 04132cd4400..727b14eb267 100644 --- a/build/gulpfile.extensions.js +++ b/build/gulpfile.extensions.js @@ -110,7 +110,7 @@ const tasks = compilations.map(function (tsconfigFile) { overrideOptions.inlineSources = Boolean(build); overrideOptions.base = path.dirname(absolutePath); - const compilation = tsb.create(absolutePath, overrideOptions, { verbose: false, transpileOnly, transpileOnlyIncludesDts: transpileOnly }, err => reporter(err.toString())); + const compilation = tsb.create(absolutePath, overrideOptions, { verbose: false, transpileOnly, transpileOnlyIncludesDts: transpileOnly, transpileWithSwc: true }, err => reporter(err.toString())); const pipeline = function () { const input = es.through(); diff --git a/build/lib/tsb/transpiler.js b/build/lib/tsb/transpiler.js index 0609105e869..e5e90128d09 100644 --- a/build/lib/tsb/transpiler.js +++ b/build/lib/tsb/transpiler.js @@ -245,9 +245,23 @@ class SwcTranspiler { return; } const tsSrc = String(file.contents); - const isAmd = /\n(import|export)/m.test(tsSrc); const t1 = Date.now(); - this._jobs.push(swc.transform(tsSrc, isAmd ? SwcTranspiler._swcrcAmd : SwcTranspiler._swcrcEsm).then(output => { + let options = SwcTranspiler._swcrcEsm; + if (this._cmdLine.options.module === ts.ModuleKind.AMD) { + const isAmd = /\n(import|export)/m.test(tsSrc); + if (isAmd) { + options = SwcTranspiler._swcrcAmd; + } + } + else if (this._cmdLine.options.module === ts.ModuleKind.CommonJS) { + options = SwcTranspiler._swcrcCommonJS; + } + this._jobs.push(swc.transform(tsSrc, options).then(output => { + // check if output of a DTS-files isn't just "empty" and iff so + // skip this file + if (file.path.endsWith('.d.ts') && _isDefaultEmpty(output.code)) { + return; + } const outBase = this._cmdLine.options.outDir ?? file.base; const outPath = this._outputFileNames.getOutputFileName(file.path); this.onOutfile(new Vinyl({ @@ -285,6 +299,13 @@ SwcTranspiler._swcrcAmd = { }, minify: false, }; +SwcTranspiler._swcrcCommonJS = { + ..._a._swcrcAmd, + module: { + type: 'commonjs', + importInterop: 'none' + } +}; SwcTranspiler._swcrcEsm = { ..._a._swcrcAmd, module: { diff --git a/build/lib/tsb/transpiler.ts b/build/lib/tsb/transpiler.ts index 062c2b1a179..a82cbaef890 100644 --- a/build/lib/tsb/transpiler.ts +++ b/build/lib/tsb/transpiler.ts @@ -329,9 +329,25 @@ export class SwcTranspiler implements ITranspiler { } const tsSrc = String(file.contents); - const isAmd = /\n(import|export)/m.test(tsSrc); const t1 = Date.now(); - this._jobs.push(swc.transform(tsSrc, isAmd ? SwcTranspiler._swcrcAmd : SwcTranspiler._swcrcEsm).then(output => { + + let options: swc.Options = SwcTranspiler._swcrcEsm; + if (this._cmdLine.options.module === ts.ModuleKind.AMD) { + const isAmd = /\n(import|export)/m.test(tsSrc); + if (isAmd) { + options = SwcTranspiler._swcrcAmd; + } + } else if (this._cmdLine.options.module === ts.ModuleKind.CommonJS) { + options = SwcTranspiler._swcrcCommonJS; + } + + this._jobs.push(swc.transform(tsSrc, options).then(output => { + + // check if output of a DTS-files isn't just "empty" and iff so + // skip this file + if (file.path.endsWith('.d.ts') && _isDefaultEmpty(output.code)) { + return; + } const outBase = this._cmdLine.options.outDir ?? file.base; const outPath = this._outputFileNames.getOutputFileName(file.path); @@ -374,6 +390,14 @@ export class SwcTranspiler implements ITranspiler { minify: false, }; + private static readonly _swcrcCommonJS: swc.Options = { + ...this._swcrcAmd, + module: { + type: 'commonjs', + importInterop: 'none' + } + }; + private static readonly _swcrcEsm: swc.Options = { ...this._swcrcAmd, module: {