-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
worktop.build
package (#94)
* initial commit * chore: setup pkg files * chore: setup module builder * feat: add `build` export * chore: self-reference type defs * chore: rename `bin` script * chore: update ci steps * feat: add `bin` script * feat(bin): write output file * fix: include `jsc.minify` options * fix(bin): track time * fix: replace swc -> esbuild; - see: swc-project/swc#1725 (comment) * chore: type param * 0.0.1 * fix: resolve output file from --cwd option * feat: pass `--cwd` thru as `absWorkingDir` option * fix: do not double-print esbuild errors * 0.0.2 * feat: add `define` helper * feat: auto-load `worktop.config.js` config file * feat: add "exports" map * 0.0.3 * chore: remove `worktop.build` repo root/config files * chore(bin): modify build system for single-entry pkg
- Loading branch information
Showing
8 changed files
with
263 additions
and
16 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
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,68 @@ | ||
#!/usr/bin/env node | ||
const argv = process.argv.slice(2); | ||
const flags = require('mri')(argv, { | ||
alias: { | ||
C: 'cwd', | ||
h: 'help', | ||
c: 'config', | ||
f: 'format', | ||
l: 'loglevel', | ||
a: 'analyze', | ||
m: 'minify', | ||
}, | ||
default: { | ||
C: '.', | ||
h: false, | ||
a: false, | ||
f: 'module', | ||
c: 'worktop.config.js', | ||
l: 'info', | ||
m: false, | ||
} | ||
}); | ||
|
||
/** @param {string} msg */ | ||
function bail(msg, code = 1) { | ||
console.error(msg); | ||
process.exit(code); | ||
} | ||
|
||
function help() { | ||
let msg = ''; | ||
msg += '\n Usage'; | ||
msg += '\n $ worktop build [input] [options]\n'; | ||
msg += '\n Options'; | ||
msg += '\n -C, --cwd Current working directory (default ".")'; | ||
msg += '\n -c, --config Path to config file (default "worktop.config.js")'; | ||
msg += '\n -f, --format Worker/Script output format (default "module")'; | ||
msg += '\n -l, --loglevel Logging display level (default "info")'; | ||
msg += '\n -a, --analyze Render bundle output analysis'; | ||
msg += '\n -m, --minify Minify built output file(s)'; | ||
msg += '\n -h, --help Displays this message\n'; | ||
msg += '\n Examples'; | ||
msg += '\n $ worktop build'; | ||
msg += '\n $ worktop build --format sw'; | ||
msg += '\n $ worktop build --minify --format sw'; | ||
msg += '\n $ worktop build src/main.ts --format sw'; | ||
console.log(msg + '\n'); | ||
process.exit(0); | ||
} | ||
|
||
if (flags.help) help(); | ||
let [cmd, entry] = flags._; | ||
if (cmd && cmd.toLowerCase() !== 'build') { | ||
bail(`Invalid command: ${cmd}\nPlease run \`worktop --help\` for more information.`); | ||
} | ||
|
||
require('.').build({ | ||
config: flags.config, | ||
input: entry || 'index.ts', | ||
output: 'build/index.mjs', | ||
loglevel: flags.loglevel, | ||
analyze: flags.analyze, | ||
minify: flags.minify, | ||
cwd: flags.cwd, | ||
}).catch(err => { | ||
if (err.errors) process.exitCode = 1; | ||
else bail(err.stack || err.message); | ||
}); |
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,31 @@ | ||
import type { BuildOptions, LogLevel } from 'esbuild'; | ||
|
||
export type Platform = 'node' | 'browser' | 'cloudflare'; | ||
|
||
export interface Options { | ||
input: string; | ||
output: string; | ||
config?: string; | ||
/** @default "cloudflare" */ | ||
platform?: Platform; | ||
/** @default "esnext" */ | ||
target?: string | string[]; | ||
/** @default "esm" */ | ||
format?: 'esm' | 'cjs'; | ||
/** @default false */ | ||
sourcemap?: boolean; | ||
external?: string[]; | ||
/** @default false */ | ||
minify?: boolean; | ||
/** @default false */ | ||
analyze?: boolean; | ||
/** @default "." */ | ||
cwd?: string; | ||
/** @default "info" */ | ||
loglevel?: LogLevel; | ||
overrides?: BuildOptions; | ||
modify?(config: BuildOptions): void; | ||
} | ||
|
||
export function define(options: Options): Options; | ||
export function build(options: Options): Promise<void>; |
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,38 @@ | ||
{ | ||
"version": "0.0.3", | ||
"name": "worktop.build", | ||
"repository": "lukeed/worktop", | ||
"description": "wip", | ||
"module": "index.mjs", | ||
"types": "index.d.ts", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"bin": { | ||
"worktop": "bin.js" | ||
}, | ||
"author": { | ||
"name": "Luke Edwards", | ||
"email": "luke.edwards05@gmail.com", | ||
"url": "https://lukeed.com" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./index.mjs", | ||
"require": "./index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
"index.d.ts", | ||
"index.mjs", | ||
"index.js", | ||
"bin.js" | ||
], | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"dependencies": { | ||
"esbuild": "^0.13.2", | ||
"mri": "^1.2.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,94 @@ | ||
import { existsSync } from 'fs'; | ||
import { dirname, join, resolve } from 'path'; | ||
|
||
import type { BuildOptions } from 'esbuild'; | ||
import type { Options } from '../'; | ||
|
||
let esbuild: typeof import('esbuild'); | ||
|
||
// NOTE: prevent Rollup import() -> require() | ||
async function load<T = any>(ident: string): Promise<T> { | ||
return Function('x', 'return import("file:///" + x)')(ident); | ||
} | ||
|
||
// TODO? custom config OR allow default set | ||
// TODO? transform typescript config file w/o -r hook | ||
// TODO: validate --format and/or --platform combination | ||
export async function build(options: Options): Promise<void> { | ||
esbuild = esbuild || await import('esbuild'); | ||
|
||
let cwd = resolve(options.cwd || '.'); | ||
|
||
if (options.config) { | ||
let tmp, b, dir=cwd; | ||
let root = process.cwd(); | ||
while (true) { | ||
tmp = join(dir, options.config); | ||
if (b = existsSync(tmp)) break; | ||
else if (dir === root) break; | ||
else dir = dirname(dir); | ||
} | ||
if (b) await load(tmp).then(m => { | ||
m = (m.default || m) as Options; | ||
Object.assign(options, m.config || m); | ||
}); | ||
} | ||
|
||
let { platform, sourcemap, external=[] } = options; | ||
let fields = ['worker', 'browser', 'module', 'jsnext', 'main']; | ||
let conds = ['worker', 'browser', 'import', 'production', 'default']; | ||
|
||
let config: BuildOptions = { | ||
bundle: true, | ||
splitting: false, | ||
absWorkingDir: cwd, | ||
outfile: options.output, | ||
entryPoints: [options.input], | ||
format: options.format || 'esm', | ||
target: options.target || 'esnext', | ||
sourcemap: sourcemap ? 'inline' : false, | ||
resolveExtensions: ['.tsx', '.ts', '.jsx', '.mjs', '.js', '.json', '.htm', '.html'], | ||
external: ([] as string[]).concat(external), | ||
logLevel: options.loglevel || 'info', | ||
minify: !!options.minify, | ||
mainFields: fields, | ||
conditions: conds, | ||
charset: 'utf8', | ||
loader: { | ||
'.htm': 'text', | ||
'.html': 'text', | ||
} | ||
}; | ||
|
||
if (platform === 'node') { | ||
fields = fields.slice(2); | ||
conds = ['node', 'require', ...conds.slice(2)]; | ||
} | ||
|
||
if (options.modify) { | ||
options.modify(config); | ||
} else if (options.overrides) { | ||
Object.assign(config, options.overrides); | ||
} | ||
|
||
config.write = true; | ||
|
||
if (options.analyze) { | ||
config.metafile = true; | ||
} | ||
|
||
let result = await esbuild.build(config); | ||
|
||
if (options.analyze) { | ||
console.log( | ||
await esbuild.analyzeMetafile(result.metafile!, { | ||
verbose: /^(debug|verb)$/i.test(config.logLevel!), | ||
color: true, | ||
}) | ||
); | ||
} | ||
} | ||
|
||
export function define(config: Options): Options { | ||
return config; | ||
} |
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