-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(core): refactor, add advance configuration, config file suppor…
…t, increase performance etc
1 parent
8d987cc
commit 1cb8dc6
Showing
29 changed files
with
2,941 additions
and
1,125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig( [ { | ||
entries : [ './src/main', './src/cli' ], | ||
sourcemap : false, | ||
declaration : true, | ||
rollup : { esbuild: { minify: true } }, | ||
} ] ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from 'binarium' | ||
|
||
export default defineConfig( { | ||
name : 'my-app-name', | ||
onlyOs : true, | ||
options : { esbuild: { tsconfig: './tsconfig.builder.json' } }, | ||
} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"onlyOS": true, | ||
"options": { | ||
"esbuild": { | ||
"tsconfig": "./tsconfig.builder.json" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import archiver from 'archiver' | ||
import { | ||
createWriteStream, | ||
existsSync, | ||
} from 'node:fs' | ||
import { | ||
mkdir, | ||
readdir, | ||
} from 'node:fs/promises' | ||
import { cpus } from 'node:os' | ||
import { join } from 'node:path' | ||
|
||
// Function that handles the zipping of a single file | ||
const zipFileWorker = ( sourceFilePath: string, zipName: string, outputDirectory: string ) => { | ||
|
||
return new Promise<void>( ( resolve, reject ) => { | ||
|
||
const output = createWriteStream( join( outputDirectory, zipName ) ) | ||
const archive = archiver( 'zip', { zlib: { level: 6 } } ) // Reduced compression level for speed | ||
|
||
output.on( 'close', () => { | ||
|
||
console.log( `Zip file [${zipName}] created successfully!` ) | ||
resolve() | ||
|
||
} ) | ||
|
||
archive.on( 'error', err => { | ||
|
||
console.error( `💥 Error creating ${zipName}:`, err ) | ||
reject( err ) | ||
|
||
} ) | ||
|
||
archive.pipe( output ) | ||
archive.file( sourceFilePath, { name: zipName.replace( '.zip', '' ) } ) | ||
archive.finalize() | ||
|
||
} ) | ||
|
||
} | ||
|
||
// Function to execute zipping in worker threads | ||
const createZipForFileInThread = async ( sourceDirectory: string, file: string, outputDirectory: string ) => { | ||
|
||
const sourceFilePath = join( sourceDirectory, file ) | ||
const zipName = `${file}.zip` | ||
return zipFileWorker( sourceFilePath, zipName, outputDirectory ) | ||
|
||
} | ||
|
||
export const zipFilesInDirectory = async ( sourceDirectory: string, outputDirectory: string ) => { | ||
|
||
// Function to filter out invisible files | ||
const filter = ( file: string ) => !( /(^|\/)\.[^\\/\\.]/g ).test( file ) | ||
|
||
// Ensure that the output directory exists or create it if it doesn't | ||
if ( !existsSync( outputDirectory ) ) { | ||
|
||
await mkdir( outputDirectory, { recursive: true } ) | ||
|
||
} | ||
|
||
const files = await readdir( sourceDirectory ) | ||
|
||
// Filter out invisible files | ||
const visibleFiles = files.filter( filter ) | ||
|
||
// Get available CPUs for worker threads | ||
const cpuCount = cpus().length | ||
|
||
// Split the files to be processed in chunks based on CPU cores | ||
const fileChunks = [] | ||
for ( let i = 0; i < visibleFiles.length; i += cpuCount ) { | ||
|
||
fileChunks.push( visibleFiles.slice( i, i + cpuCount ) ) | ||
|
||
} | ||
|
||
// Process each chunk in parallel using workers | ||
await Promise.all( fileChunks.map( async chunk => { | ||
|
||
await Promise.all( chunk.map( file => createZipForFileInThread( sourceDirectory, file, outputDirectory ) ) ) | ||
|
||
} ) ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const catchError = async <T>( promise: Promise<T> ): Promise<[undefined, T] | [Error]> => { | ||
|
||
return promise | ||
.then( value => ( [ undefined, value ] as unknown as [undefined, T] ) ) | ||
.catch( error => ( [ error ] ) ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
|
||
export const logger = ( | ||
{ | ||
icon = '📦', | ||
icon, | ||
name, | ||
isDebug = false, | ||
}: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
|
||
import { spawn } from 'node:child_process' | ||
import process from 'node:process' | ||
|
||
export const exit = process.exit | ||
|
||
/** | ||
* This is not recomended but is for not display `(node:31972) [DEP0040] DeprecationWarning: | ||
* The `punycode` module is deprecated. Please use a userland alternative instead.` message. | ||
* | ||
* @example setNoDeprecationAlerts() | ||
* | ||
*/ | ||
export const setNoDeprecationAlerts = () => { | ||
|
||
// @ts-ignore | ||
process.noDeprecation = true | ||
|
||
} | ||
export const onExit = ( cb: NodeJS.ExitListener ) => { | ||
|
||
process.on( 'exit', cb ) | ||
|
||
} | ||
|
||
export const getFlagValue = ( key: string ) =>{ | ||
|
||
const flags = process.argv | ||
for ( const flag of flags ) { | ||
|
||
if ( flag.startsWith( `--${key}=` ) ) return flag.split( '=' )[ 1 ] | ||
|
||
} | ||
return undefined | ||
|
||
} | ||
export const existsFlag = ( v: string ) => process.argv.includes( `--${v}` ) | ||
|
||
export const exec = async ( cmd: string ) => { | ||
|
||
await new Promise<void>( ( resolve, reject ) => { | ||
|
||
const childProcess = spawn( cmd, { | ||
shell : true, | ||
stdio : 'inherit', | ||
} ) | ||
|
||
childProcess.on( 'close', code => { | ||
|
||
if ( code === 0 ) resolve() | ||
else { | ||
|
||
const error = new Error( `Command failed with code ${code}` ) | ||
console.error( error ) | ||
reject( error ) | ||
|
||
} | ||
|
||
} ) | ||
|
||
} ) | ||
|
||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
type FnDefault = () => Promise<any> | ||
type OnWriteParams<FN extends FnDefault> = { | ||
fn : FN | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
on( value:string ): Promise<string | undefined> | ||
} | ||
export const onOutputWrite = async <FN extends FnDefault>( { | ||
fn, on, | ||
}: OnWriteParams<FN> ): Promise<ReturnType<FN>> => { | ||
|
||
const originalWrite = process.stdout.write | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
stdout.write = async ( chunk, ...args ) => { | ||
|
||
const value = chunk.toString() | ||
const validated = await on( value ) | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
if ( validated ) originalWrite.call( stdout, chunk, ...args ) | ||
|
||
} | ||
|
||
try { | ||
|
||
// Llamamos a la función original | ||
return await fn() | ||
|
||
} finally { | ||
|
||
// Restauramos process.stdout.write a su comportamiento original | ||
process.stdout.write = originalWrite | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { performance } from 'node:perf_hooks' | ||
|
||
export const perf = () => { | ||
|
||
const start = performance.now() | ||
const stop = () => `${( ( performance.now() - start ) / 1000 ).toFixed( 2 )}` | ||
return { stop } | ||
|
||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
import { | ||
existsFlag, | ||
getFlagValue, | ||
} from './_shared/process' | ||
import { | ||
existsPath, | ||
getArch, | ||
getFilename, | ||
getPlatform, | ||
joinPath, | ||
readConfigFile, | ||
resolvePath, | ||
} from './_shared/sys' | ||
import { | ||
ERROR_ID, | ||
target, | ||
BUILDER_TYPE, | ||
ARCH, | ||
} from './const' | ||
import { BuildError } from './error' | ||
|
||
import type { getLog } from './log' | ||
import type { | ||
BuilderParams, | ||
ConfigParams, | ||
} from './types' | ||
|
||
type BuildConstructorParams = BuilderParams & { log: ReturnType<typeof getLog> } | ||
|
||
const getInput = async ( path: string ) => { | ||
|
||
const validExtensions = [ | ||
'.ts', | ||
'.js', | ||
'.mjs', | ||
'.mts', | ||
'.cjs', | ||
'.cts', | ||
] | ||
|
||
if( !validExtensions.some( ext => path.endsWith( ext ) ) ){ | ||
|
||
for ( let index = 0; index < validExtensions.length; index++ ) { | ||
|
||
const input = path + validExtensions[ index ] | ||
|
||
const exists = await existsPath( input ) | ||
if( exists ) return input | ||
|
||
} | ||
return undefined | ||
|
||
} | ||
const exists = await existsPath( path ) | ||
if( exists ) return path | ||
return undefined | ||
|
||
} | ||
|
||
const getConfigfile = async ( path?: string ): Promise<ConfigParams | undefined> => { | ||
|
||
try { | ||
|
||
if( !path ) return undefined | ||
|
||
const exists = await existsPath( path ) | ||
if( !exists ) throw new Error( 'Config file does not exist' ) | ||
|
||
const config = await readConfigFile( path ) as ConfigParams | ||
|
||
return { | ||
input : config?.input || undefined, | ||
name : config?.name || undefined, | ||
outDir : config?.outDir || undefined, | ||
onlyOs : config?.onlyOs || undefined, | ||
type : ( config?.type && Object.values( BUILDER_TYPE ).includes( config.type ) ) ? config.type : undefined, | ||
options : { | ||
esbuild : config?.options?.esbuild || undefined, | ||
ncc : config?.options?.ncc || undefined, | ||
pkg : config?.options?.pkg || undefined, | ||
}, | ||
} | ||
|
||
}catch( error ){ | ||
|
||
throw new BuildError( ERROR_ID.ON_CONFIG, { | ||
path, | ||
error, | ||
} ) | ||
|
||
} | ||
|
||
} | ||
export const getData = async ( { | ||
input, | ||
name, | ||
config, | ||
outDir = resolvePath( 'build' ), | ||
onlyOs = false, | ||
type = BUILDER_TYPE.ALL, | ||
log, | ||
}: BuildConstructorParams )=> { | ||
|
||
const version = existsFlag( 'version' ) | ||
const help = existsFlag( 'help' ) | ||
|
||
if( help ) log.printHelp( ) | ||
if( version ) log.printVersion() | ||
|
||
const params = Object.assign( {}, { | ||
input, | ||
name, | ||
outDir, | ||
onlyOs, | ||
type, | ||
config, | ||
} ) | ||
|
||
const flags = { | ||
input : getFlagValue( 'input' ), | ||
onlyOs : existsFlag( 'onlyOs' ), | ||
outDir : getFlagValue( 'outDir' ), | ||
type : getFlagValue( 'type' ) as typeof type, | ||
name : getFlagValue( 'name' ), | ||
config : getFlagValue( 'config' ), | ||
} | ||
|
||
if( flags.config ) config = flags.config | ||
const configfile = await getConfigfile( config ) | ||
|
||
if( configfile ){ | ||
|
||
if( configfile.name ) name = configfile.name | ||
if( configfile.input ) input = configfile.input | ||
if( configfile.onlyOs ) onlyOs = configfile.onlyOs | ||
if( configfile.outDir ) outDir = configfile.outDir | ||
|
||
} | ||
|
||
if( flags.name ) name = flags.name | ||
if( flags.input ) input = flags.input | ||
if( flags.onlyOs ) onlyOs = flags.onlyOs | ||
if( flags.outDir ) outDir = flags.outDir | ||
|
||
if( flags.type && Object.values( BUILDER_TYPE ).includes( flags.type ) ) type = flags.type | ||
|
||
log.info( 'Starting construction...' ) | ||
console.log() | ||
|
||
const arch = await getArch() | ||
const plat = await getPlatform() | ||
|
||
const projectBuild = outDir | ||
if( !name ) name = getFilename( input ) | ||
|
||
const opts = { | ||
input, | ||
name, | ||
outDir, | ||
onlyOs, | ||
type, | ||
options : configfile?.options, | ||
} | ||
|
||
const data = { | ||
platform : plat, | ||
arch, | ||
opts, | ||
} | ||
|
||
const projectBuildBin = joinPath( projectBuild, 'bin' ) | ||
const projectBuildZip = joinPath( projectBuild, 'zip' ) | ||
const projectBuildCjs = joinPath( projectBuild, 'cjs' ) | ||
|
||
// GET TARGETS | ||
const getTargets = ( arch: typeof ARCH[keyof typeof ARCH] ) => ( onlyOs ? [ `${target}-${plat}-${arch}` ] : [ | ||
`${target}-alpine-${arch}`, | ||
`${target}-linux-${arch}`, | ||
`${target}-linuxstatic-${arch}`, | ||
`${target}-macos-${arch}`, | ||
`${target}-win-${arch}`, | ||
] ) | ||
|
||
const targets = arch === ARCH.ARM64 | ||
? [ ...getTargets( ARCH.ARM64 ), ...getTargets( ARCH.X64 ) ] | ||
: getTargets( ARCH.X64 ) | ||
|
||
log.debug( JSON.stringify( { | ||
message : 'Init data: function params, process flags, final options..', | ||
data : { | ||
params, | ||
flags, | ||
configfile, | ||
...data, | ||
targets, | ||
}, | ||
}, null, 2 ) ) | ||
|
||
// EXIST INPUT | ||
|
||
const exists = await getInput( input ) | ||
if( !exists ) throw new BuildError( ERROR_ID.NO_INPUT, data ) | ||
else input = exists | ||
|
||
if( plat === 'unknown' ) throw new BuildError( ERROR_ID.PLATFORM_UNKWON, data ) | ||
|
||
return { | ||
...data.opts, | ||
targets, | ||
binDir : projectBuildBin, | ||
jsDir : projectBuildCjs, | ||
compressDir : projectBuildZip, | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { exit } from './_shared/process' | ||
|
||
export const printHelp = ( name: string ): void => { | ||
|
||
name = name.toLowerCase() | ||
console.log( `Usage: ${name} [options] | ||
Options: | ||
--input Input file path. | ||
Accepted files: .ts, .js, .mjs, .mts, .cjs, .cts | ||
The input can be provided without an extension. | ||
--outDir Output directory path. | ||
--name Binary name. | ||
--type Binary type build | ||
Supported values: all, cjs, bin | ||
--onlyOs Build only binary for your current OS. | ||
If is not set ${name} will build a binary for every OS. | ||
--config Config file path. | ||
Files supported: .mjs, .js, .json, .yml, .yaml, .toml, .tml | ||
Global options: | ||
--help Show help message | ||
--version Show version | ||
--debug Debug mode | ||
Examples: | ||
${name} --input src/main | ||
${name} --input src/main.js --outDir out | ||
${name} --input src/main.ts --name my-app | ||
${name} --input src/main.ts --config my-config.js | ||
` ) | ||
exit( 0 ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { logger } from './_shared/logger' | ||
import { | ||
existsFlag, | ||
exit, | ||
} from './_shared/process' | ||
import { | ||
version, | ||
name, | ||
BINARIUM_CONSTS, | ||
} from './const' | ||
import { printHelp } from './help' | ||
|
||
export const getLog = () => { | ||
|
||
const projectName = BINARIUM_CONSTS.name || name | ||
const isDebug = BINARIUM_CONSTS.debug || existsFlag( 'debug' ) || false | ||
return { | ||
...logger( { | ||
icon : BINARIUM_CONSTS.icon || '📦', | ||
name : projectName, | ||
isDebug, | ||
} ), | ||
isDebug, | ||
printHelp : BINARIUM_CONSTS.onHelp ? () => BINARIUM_CONSTS.onHelp : () => printHelp( projectName ), | ||
printVersion : BINARIUM_CONSTS.onVersion ? () => BINARIUM_CONSTS.onVersion : () => { | ||
|
||
console.log( version ) | ||
exit( 0 ) | ||
|
||
}, | ||
} | ||
|
||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* PKG BUILD. | ||
* | ||
* @see https://www.npmjs.com/package/@yao-pkg/pkg | ||
*/ | ||
import { exec as execPkg } from '@yao-pkg/pkg' | ||
|
||
import { catchError } from '../_shared/error' | ||
import { joinPath } from '../_shared/sys' | ||
|
||
import type { Config } from './types' | ||
|
||
type Opts = { | ||
input: string, | ||
output: string, | ||
name: string, | ||
targets: string[], | ||
config?: Config['pkg'] | ||
debug: ( data: object | string ) => void | ||
isDebug?: boolean | ||
} | ||
|
||
export default async ( { | ||
input, output, name, targets, debug, config, isDebug, | ||
}: Opts ) => { | ||
|
||
const defConfig = [ | ||
input, | ||
'--targets', | ||
targets.join( ',' ), | ||
'--output', | ||
joinPath( output, name ), | ||
'--compress', | ||
'GZip', | ||
...( isDebug ? [ '--debug' ] : [] ), | ||
] | ||
|
||
const buildConfig = config ? [ ...defConfig, ...config ] : defConfig | ||
|
||
debug( { pkgOpts: buildConfig } ) | ||
return await catchError( execPkg( buildConfig ) ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* ESBUILD BUILD. | ||
* | ||
* @see https://esbuild.github.io/api/#build | ||
*/ | ||
import { build } from 'esbuild' | ||
|
||
import { catchError } from '../_shared/error' | ||
import { | ||
existsPath, | ||
joinPath, | ||
packageDir, | ||
resolvePath, | ||
} from '../_shared/sys' | ||
|
||
import type { Config } from './types' | ||
|
||
type Opts = { | ||
input: string, | ||
output: string, | ||
target: string, | ||
config?: Config['esbuild'] | ||
isDebug?: boolean | ||
debug: ( data: object | string ) => void | ||
} | ||
|
||
export default async ( data: Opts ) => { | ||
|
||
if( data.config === false ) return | ||
|
||
const getTsConfig = async () =>{ | ||
|
||
const userTs = resolvePath( 'tsconfig.json' ) | ||
const existUserTs = await existsPath( userTs ) | ||
if( existUserTs ) return userTs | ||
return joinPath( packageDir, 'tsconfig.builder.json' ) | ||
|
||
} | ||
|
||
const defConfig: NonNullable<Config['esbuild']> = { | ||
entryPoints : [ data.input ], | ||
minify : true, | ||
bundle : true, | ||
format : 'cjs', | ||
platform : 'node', | ||
target : data.target, | ||
outfile : data.output, | ||
tsconfig : await getTsConfig(), | ||
} | ||
|
||
const buildConfig = data.config ? { | ||
...defConfig, | ||
...data.config, | ||
} : defConfig | ||
|
||
data.debug( { esbuildConfig: buildConfig } ) | ||
|
||
return await catchError( build( buildConfig ) ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* NCC BUILD. | ||
* | ||
* @see https://github.com/vercel/ncc?tab=readme-ov-file#programmatically-from-nodejs | ||
*/ | ||
// @ts-ignore | ||
import ncc from '@vercel/ncc' | ||
|
||
import { catchError } from '../_shared/error' | ||
import { | ||
deleteFile, | ||
writeFile, | ||
} from '../_shared/sys' | ||
|
||
import type { Config } from './types' | ||
|
||
type Opts = { | ||
input: string, | ||
output: string, | ||
config?: Config['ncc'] | ||
isDebug?: boolean | ||
debug: ( data: object | string ) => void | ||
} | ||
|
||
export default async ( { | ||
input, output, debug, config, isDebug, | ||
}: Opts ) => { | ||
|
||
if( config === false ) return | ||
|
||
const defConfig = { | ||
minify : true, | ||
cache : false, | ||
debugLog : isDebug || false, | ||
// target, | ||
} | ||
|
||
const buildConfig = config ? { | ||
...defConfig, | ||
...config, | ||
} : defConfig | ||
|
||
const build = async () => { | ||
|
||
const { code } = await ncc( input, buildConfig ) | ||
|
||
await writeFile( output, code ) | ||
await deleteFile( input ) | ||
|
||
} | ||
|
||
debug( { nccOpts: buildConfig } ) | ||
|
||
return await catchError( build() ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
import { zipFilesInDirectory } from '../_shared/compress' | ||
import { catchError } from '../_shared/error' | ||
|
||
type Opts = { | ||
input: string, | ||
output: string, | ||
isDebug?: boolean | ||
debug: ( data: object | string ) => void | ||
} | ||
|
||
export default async ( { | ||
input, output, debug, | ||
}: Opts ) => { | ||
|
||
debug( { | ||
compressOpts : { | ||
input, | ||
output, | ||
}, | ||
} ) | ||
|
||
return await catchError( zipFilesInDirectory( input, output ) ) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { BuildOptions } from 'esbuild' | ||
|
||
export type Config = { | ||
/** | ||
* PKG configuration. | ||
* | ||
* @see https://www.npmjs.com/package/@yao-pkg/pkg | ||
*/ | ||
pkg?: string[] | ||
/** | ||
* ESBUILD configuration. | ||
* | ||
* @see https://esbuild.github.io/api/#build | ||
*/ | ||
esbuild?: BuildOptions | false | ||
/** | ||
* NCC configuration. | ||
* | ||
* @see https://github.com/vercel/ncc?tab=readme-ov-file#programmatically-from-nodejs | ||
*/ | ||
ncc?: { | ||
/** | ||
* Provide a custom cache path or disable caching . | ||
*/ | ||
cache?: string | false; | ||
/** | ||
* Externals to leave as requires of the build. | ||
*/ | ||
externals?: string[]; | ||
/** | ||
* Directory outside of which never to emit assets. | ||
*/ | ||
filterAssetBase?: string; | ||
minify?: boolean; | ||
sourceMap?: boolean; | ||
assetBuilds?: boolean; | ||
sourceMapBasePrefix?: string; | ||
// when outputting a sourcemap, automatically include | ||
// source-map-support in the output file (increases output by 32kB). | ||
sourceMapRegister?: boolean; | ||
watch?: boolean; | ||
license?: string; | ||
target?: string; | ||
v8cache?: boolean; | ||
} | false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.