Skip to content

Commit

Permalink
programmatic usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed May 16, 2024
1 parent 868d1c3 commit 4f5d3dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ $ mach build ./src/index.html
$ mach dev ./src/index.html #todo
```

## Programatic Usage

```javascript
import { Mach } from '@alshdavid/mach'

// Create a Mach instance
const mach = new Mach()

// Listen to build events
mach.subscribe('build_event', event => console.log(event))

// Build a target
const report = await mach.build({
projectRoot: process.cwd(),
outFolder: 'dist',
entries: ['src/index.js']
})
```

## Supported Types

Mach comes preconfigured with sensible defaults and does not need configuration. Mach ships with built-in support for the most common source files in web development.
Expand Down
1 change: 1 addition & 0 deletions npm/mach/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class MachInitError extends Error {
}
}

export * from './mach.js'
export const Resolver = globalThis.Mach?.Resolver || MachInitError
export const Transformer = globalThis.Mach?.Transformer || MachInitError
export const Dependency = globalThis.Mach?.Dependency || MachInitError
Expand Down
12 changes: 11 additions & 1 deletion npm/mach/lib/mach.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ export class Mach {

if (options.bundleSplitting)
cli_args.push('--bundle-splitting', `${options.bundleSplitting}`)
if (options.clean) cli_args.push('--clean', `${options.clean}`)
if (options.clean) cli_args.push('--clean')
if (options.nodeWorkers)
cli_args.push('--node-workers', `${options.nodeWorkers}`)
if (options.optimize) cli_args.push('--no-optimize', `${!options.optimize}`)
if (options.outFolder) cli_args.push('--dist', options.outFolder)
if (options.threads) cli_args.push('--threads', `${options.threads}`)

for (const entry of options.entries) {
cli_args.push(entry)
}

console.log(cli_args.join(' '))
const child = child_process.spawn(BIN_PATH, cli_args, {
shell: true,
cwd: options.projectRoot ?? process.cwd(),
Expand Down Expand Up @@ -136,4 +141,9 @@ export class Mach {
setTimeout(() => subscriber(event), 0)
}
}

static build(options) {
const mach = new Mach(options)
return mach.build(options)
}
}
2 changes: 1 addition & 1 deletion npm/mach/types/mach.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type BuildProgress = {}

export type BuildReport = {
bundleManifest: Record<string, string>
output: Record<string, string>
entries: Record<string, string>
}

export declare class Mach {
Expand Down
4 changes: 3 additions & 1 deletion testing/tests/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ describe('javascript', { concurrency: true }, () => {
test('synchronous passing test', async (t) => {
const report = await Mach.build({
projectRoot: FIXTURES('js-commonjs'),
clean: true,
outFolder: 'dist',
entries: ['src/index.js']
})

await using nodejs = new NodejsContext({ type: 'commonjs' })

await nodejs.import(FIXTURES('js-commonjs', report.output['src/index.js']))
await nodejs.import(FIXTURES('js-commonjs', 'dist', report.entries['src/index.js']))
await nodejs.get_global('onready')

const values = {
Expand Down

0 comments on commit 4f5d3dc

Please sign in to comment.