From 782c1325fdc42a88b05617b6d1697d625bd2230f Mon Sep 17 00:00:00 2001 From: Faris Mahmutovic Date: Fri, 26 Jan 2024 17:18:22 +0100 Subject: [PATCH] fix(legacy): use swc to transform esm to es3 --- lib/esbuild.config.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/esbuild.config.js b/lib/esbuild.config.js index 571b433..eed00b9 100644 --- a/lib/esbuild.config.js +++ b/lib/esbuild.config.js @@ -3,6 +3,8 @@ import { fileURLToPath } from 'url'; import { build, context, analyzeMetafile } from 'esbuild'; import * as dotenv from 'dotenv'; +import swc from '@swc/core'; +import { promises as fs } from 'fs'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -44,6 +46,14 @@ const config = { sourcemap: BUILD === 'development', }; +const swcOptions = { + minify: true, + sourceMaps: true, + jsc: { + target: 'es3', + }, +}; + if (WATCH) { const ctx = await context({ @@ -51,29 +61,30 @@ if (WATCH) { format: 'esm', outfile: resolve(DIST, 'index.mjs'), treeShaking: true, - target: [ - 'es6' - ] }); await ctx.watch(); } else { + // Transpile TypeScript to ESM const resultESM = await build({ ...config, format: 'esm', - outfile: resolve(DIST, 'index.mjs'), + outfile: resolve(DIST, 'index.js'), treeShaking: true, - target: [ - 'es6' - ] }); + // Transpile TypeScript to CommonJS const resultCJS = await build({ ...config, format: 'cjs', outfile: resolve(DIST, 'index.cjs'), - treeShaking: true + treeShaking: true, }); + // Transform ESM to ES3 + const resultES3 = await swc.transformFile(resolve(DIST, 'index.js'), swcOptions); + await fs.writeFile(resolve(DIST, 'index.mjs'), resultES3.code, 'utf-8'); + await fs.writeFile(resolve(DIST, 'index.mjs.map'), resultES3.map, 'utf-8'); + if (DEBUG) { const analyzeESM = await analyzeMetafile(resultESM.metafile, { verbose: false @@ -81,6 +92,7 @@ if (WATCH) { const analyzeCJS = await analyzeMetafile(resultCJS.metafile, { verbose: false }); + console.log(analyzeESM); console.log(analyzeCJS); }