Skip to content

Commit

Permalink
Run formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPulse committed Dec 13, 2023
1 parent 3ec62e1 commit 6b01b45
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
53 changes: 36 additions & 17 deletions packages/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { consola } from 'consola';
import { basename, dirname, join } from 'path';
import { mkdir, rm, writeFile } from 'fs/promises';
import { transform, minify } from '@swc/core';
import { build } from 'esbuild';
import { build } from 'esbuild';
import { execa } from 'execa';
import { nodeExternalsPlugin } from 'esbuild-node-externals';
import packageJson from './package.json' assert { type: 'json' };
import { transfer } from 'multi-stage-sourcemap';


const entry = './lib.ts';

consola.start('Building @sugarform/core...');
Expand All @@ -28,7 +27,6 @@ consola.log('');
consola.success('All tasks completed.');
consola.log('');


async function bundle() {
consola.start(`Bundling...`);
const { outputFiles } = await build({
Expand All @@ -43,11 +41,12 @@ async function bundle() {
plugins: [
nodeExternalsPlugin({
devDependencies: false,
})
]
}),
],
});
const sourcemap = outputFiles.find(v => v.path.endsWith('index.ts.map'))?.text;
const source = outputFiles.find(v => v.path.endsWith('index.ts'))?.text;
const sourcemap = outputFiles.find((v) => v.path.endsWith('index.ts.map'))
?.text;
const source = outputFiles.find((v) => v.path.endsWith('index.ts'))?.text;

if (sourcemap === undefined) {
consola.error('Build result of esbuild did not contain sourcemap!');
Expand All @@ -57,22 +56,30 @@ async function bundle() {
consola.error('Build result of esbuild did not contain source!');
process.exit(1);
}
consola.success(`Bundled by esbuild! (${toKiloBytes(getStringBytes(source))})`);
consola.success(
`Bundled by esbuild! (${toKiloBytes(getStringBytes(source))})`
);
return { sourcemap, source };
}

async function types() {
const output = join(packageJson.exports['.'].types);
consola.start('Building types...');
await execa('dts-bundle-generator', ['-o', output, entry]).pipeStdout?.(process.stdout).pipeStderr?.(process.stderr);
await execa('dts-bundle-generator', ['-o', output, entry])
.pipeStdout?.(process.stdout)
.pipeStderr?.(process.stderr);
consola.success(`Done! (${output})`);
}

async function pipeline(format: 'esm' | 'cjs') {
const code = await bundled;

consola.start(`[${format}] Building...`);
const output = join(format === 'esm' ? packageJson.exports['.'].import : packageJson.exports['.'].require);
const output = join(
format === 'esm'
? packageJson.exports['.'].import
: packageJson.exports['.'].require
);

const transformed = await transform(code.source, {
sourceMaps: true,
Expand All @@ -93,11 +100,12 @@ async function pipeline(format: 'esm' | 'cjs') {

const transform_map = transformed.map;
if (transform_map === undefined) {
consola.error(`[${format}] Transform result of SWC did not contain source!`);
consola.error(
`[${format}] Transform result of SWC did not contain source!`
);
process.exit(1);
}


const minified = await minify(transformed.code, {
compress: {},
sourceMap: true,
Expand All @@ -116,18 +124,29 @@ async function pipeline(format: 'esm' | 'cjs') {
minified: getStringBytes(minified.code),
};

consola.info(`[${format}] SWC minified. (${toKiloBytes(size.unminified)} -> ${toKiloBytes(size.minified)}, -${Math.round((1 - size.minified / size.unminified) * 100)}%)`);
consola.info(
`[${format}] SWC minified. (${toKiloBytes(
size.unminified
)} -> ${toKiloBytes(size.minified)}, -${Math.round(
(1 - size.minified / size.unminified) * 100
)}%)`
);

await writeFile(output, minified.code);
consola.success(`[${format}] Done! (${output})`);

consola.start(`[${format}] Merging sourcemaps...`);
const swc_map = transfer({ fromSourceMap: minify_map, toSourceMap: transform_map });
const sourcemap = transfer({ fromSourceMap: swc_map, toSourceMap: (await bundled).sourcemap });
const swc_map = transfer({
fromSourceMap: minify_map,
toSourceMap: transform_map,
});
const sourcemap = transfer({
fromSourceMap: swc_map,
toSourceMap: (await bundled).sourcemap,
});
const sourcemap_output = `${output}.map`;
await writeFile(sourcemap_output, sourcemap);
consola.success(`[${format}] Done! (${sourcemap_output})`);

}

function getStringBytes(str: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { aabbccddeeffgg } from "./src/util";
import { aabbccddeeffgg } from './src/util';

export function hello() {
return 'a' == aabbccddeeffgg();
Expand Down

0 comments on commit 6b01b45

Please sign in to comment.