Skip to content

Commit

Permalink
also use SWC for extensions transpile
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 7, 2022
1 parent 87e54a0 commit d6f5727
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/gulpfile.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 23 additions & 2 deletions build/lib/tsb/transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -285,6 +299,13 @@ SwcTranspiler._swcrcAmd = {
},
minify: false,
};
SwcTranspiler._swcrcCommonJS = {
..._a._swcrcAmd,
module: {
type: 'commonjs',
importInterop: 'none'
}
};
SwcTranspiler._swcrcEsm = {
..._a._swcrcAmd,
module: {
Expand Down
28 changes: 26 additions & 2 deletions build/lib/tsb/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit d6f5727

Please sign in to comment.