Skip to content

Commit

Permalink
fix(legacy): use swc to transform esm to es3
Browse files Browse the repository at this point in the history
  • Loading branch information
faris-imi committed Jan 26, 2024
1 parent 537bc1d commit 782c132
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -44,43 +46,53 @@ const config = {
sourcemap: BUILD === 'development',
};

const swcOptions = {
minify: true,
sourceMaps: true,
jsc: {
target: 'es3',
},
};


if (WATCH) {
const ctx = await context({
...config,
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
});
const analyzeCJS = await analyzeMetafile(resultCJS.metafile, {
verbose: false
});

console.log(analyzeESM);
console.log(analyzeCJS);
}
Expand Down

0 comments on commit 782c132

Please sign in to comment.