Skip to content

Commit

Permalink
Use Original Build Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPulse committed Dec 12, 2023
1 parent d60e463 commit 7a8d181
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 499 deletions.
6 changes: 3 additions & 3 deletions configs/typescript-config/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"incremental": false,
"isolatedModules": true,
"lib": ["es2022", "DOM", "DOM.Iterable"],
"module": "NodeNext",
"module": "ESNext",
"moduleDetection": "force",
"moduleResolution": "NodeNext",
"moduleResolution": "bundler",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"skipLibCheck": false,
"strict": true,
"target": "ES2022"
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"@manypkg/cli": "^0.21.1",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@swc/core": "^1.3.100",
"@types/node": "^20.10.4",
"@vitejs/plugin-react-swc": "^3.5.0",
"consola": "^3.2.3",
"dts-bundle-generator": "^9.0.0",
"execa": "^8.0.1",
"prettier": "^3.1.0",
"rollup-plugin-output-size": "^1.3.0",
"rollup-plugin-swc3": "^0.11.0",
"tsx": "^4.6.2",
"turbo": "latest",
"typescript": "^5.3.3",
"vite": "^5.0.7",
"vite-plugin-inspect": "^0.8.1"
"typescript": "^5.3.3"
},
"packageManager": "pnpm@8.9.0",
"engines": {
Expand Down
77 changes: 77 additions & 0 deletions packages/core/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { consola } from 'consola';
import { basename, dirname, join } from 'path';
import { mkdir, rm, writeFile } from 'fs/promises';
import { transformFile, minify } from '@swc/core';
import { execa } from 'execa';
import tsc from 'tsc-prog';
import packageJson from './package.json' assert { type: 'json' };


const entry = './lib.ts';

consola.start('Building @sugarform/core...');

consola.info(`entrypoint: ${entry}`);

await rm('./dist', { recursive: true, force: true });
consola.success('Cleaned up dist folder.');

await mkdir('./dist', { recursive: true });
consola.success('Created dist folder.');

consola.log('');
await Promise.all([pipeline('es'), pipeline('cjs'), types()]);
consola.log('');


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);
consola.success(`Done! (${output})`);
}

async function pipeline(format: 'es' | 'cjs') {
consola.start(`Building ${format}...`);
const output = join(format === 'es' ? packageJson.exports['.'].import : packageJson.exports['.'].require);

const transformed = await transformFile(entry, {
sourceMaps: false,
isModule: true,
minify: true,
module: {
type: format === 'es' ? 'es6' : 'commonjs',
},
jsc: {
parser: {
syntax: 'typescript',
tsx: true,
},
target: 'es2017',
loose: false,
},
});

const minified = await minify(transformed.code, {
compress: {},
mangle: false,
module: true,
});

const size = {
uncompressed: getStringBytes(transformed.code),
compressed: getStringBytes(minified.code),
};

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

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

function getStringBytes(str: string) {
return Buffer.byteLength(str, 'utf-8');
}
function toKiloBytes(bytes: number) {
return `${Math.round(bytes * 100) / 100}kB`;
}
7 changes: 5 additions & 2 deletions packages/core/lib.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { a } from "./src/util";
export { a };

export function hello() {
return "Hello, World!" == "Hello, World!";
}
return 'a' == a();
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dist"
],
"scripts": {
"build": "vite build",
"build": "tsx build.ts",
"typecheck": "tsc --noEmit --composite false",
"lint": "eslint ."
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function a() {
return 'a';
}
46 changes: 0 additions & 46 deletions packages/core/vite.config.ts

This file was deleted.

Loading

0 comments on commit 7a8d181

Please sign in to comment.