Skip to content

Commit

Permalink
feat: update bundle formats (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored Sep 24, 2024
1 parent 1fa4cc3 commit 5a1b129
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 128 deletions.
67 changes: 49 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { defineConfig } from '@hypernym/bundler'
export default defineConfig({
entries: [
{ input: './src/index.ts' },
{ declaration: './src/types/index.ts' },
{ dts: './src/types/index.ts' },
{
input: './src/utils/index.ts',
output: './dist/utils/utils.min.mjs',
Expand Down Expand Up @@ -99,11 +99,34 @@ Set a custom config path via the CLI command:
npx hyperbundler --config hyper.config.ts
```

## Options
## Formats

During transformation, file formats are automatically resolved and in most cases there is no need for additional configuration.

`Hyperbundler` module environment for generated files defaults to `esm`, which means the outputs will have a `.mjs` extension unless otherwise specified. For TypeScript declarations, the appropriate extension will be `.d.mts`.

Formats can also be explicitly specified for each entry, if necessary.

### Inputs

Default transformation behaviour for all `chunk` entries:

- `./srcDir/file.js` resolves to `./outDir/file.mjs`
- `./srcDir/file.mjs` resolves to `./outDir/file.mjs`
- `./srcDir/file.cjs` resolves to `./outDir/file.cjs`
- `./srcDir/file.ts` resolves to `./outDir/file.mjs`
- `./srcDir/file.mts` resolves to `./outDir/file.mjs`
- `./srcDir/file.cts` resolves to `./outDir/file.cjs`

All options are documented with descriptions and examples so auto-completion will be offered as you type.
### Declarations

Simply hover over the property and see what it does in the `quickinfo`.
Default transformation behaviour for all `dts` entries:

- `./srcDir/file.ts` resolves to `./outDir/file.d.mts`

## Options

All options are documented with descriptions and examples so auto-completion will be offered as you type. Simply hover over the property and see what it does in the `quickinfo`.

### entries

Expand All @@ -121,7 +144,7 @@ import { defineConfig } from '@hypernym/bundler'
export default defineConfig({
entries: [
{ input: './src/index.ts' }, // => './dist/index.mjs'
{ declaration: './src/types.ts' }, // => './dist/types.d.ts'
{ dts: './src/types.ts' }, // => './dist/types.d.mts'
// ...
],
})
Expand All @@ -140,9 +163,7 @@ import { defineConfig } from '@hypernym/bundler'

export default defineConfig({
entries: [
{
input: './src/index.ts', // => './dist/index.mjs'
},
{ input: './src/index.ts' }, // => './dist/index.mjs'
],
})
```
Expand All @@ -160,9 +181,19 @@ import { defineConfig } from '@hypernym/bundler'

export default defineConfig({
entries: [
{
{ declaration: './src/types.ts' }, // => './dist/types.d.ts'
},
{ declaration: './src/types.ts' }, // => './dist/types.d.mts'
],
})
```

Also, it is possible to use `dts` alias.

```ts
import { defineConfig } from '@hypernym/bundler'

export default defineConfig({
entries: [
{ dts: './src/types.ts' }, // => './dist/types.d.mts'
],
})
```
Expand Down Expand Up @@ -373,7 +404,7 @@ export default defineConfig({

### build:entry:start

- Type: `(options: BuildEntryOptions, stats: BuildStats) => void | Promise<void>`
- Type: `(entry: BuildEntryOptions, stats: BuildEntryStats) => void | Promise<void>`
- Default: `undefined`

Called on each entry just before the build process.
Expand All @@ -388,12 +419,12 @@ import { plugin1, plugin2 } from './src/utils/plugins.js'

export default defineConfig({
hooks: {
'build:entry:start': async (options, stats) => {
'build:entry:start': async (entry, stats) => {
// adds custom plugins for a specific entry only
if (options.input?.includes('./src/index.ts')) {
options.defaultPlugins = [
if (entry.input?.includes('./src/index.ts')) {
entry.defaultPlugins = [
plugin1(), // adds a custom plugin before the default bundler plugins
...options.defaultPlugins, // list of default bundler plugins
...entry.defaultPlugins, // list of default bundler plugins
plugin2(), // adds a custom plugin after the default bundler plugins
]
}
Expand All @@ -404,7 +435,7 @@ export default defineConfig({

### build:entry:end

- Type: `(options: BuildEntryOptions, stats: BuildStats) => void | Promise<void>`
- Type: `(entry: BuildEntryOptions, stats: BuildEntryStats) => void | Promise<void>`
- Default: `undefined`

Called on each entry right after the build process is completed.
Expand All @@ -416,7 +447,7 @@ import { defineConfig } from '@hypernym/bundler'

export default defineConfig({
hooks: {
'build:entry:end': async (options, stats) => {
'build:entry:end': async (entry, stats) => {
// ...
},
},
Expand Down
11 changes: 9 additions & 2 deletions bundler.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import { version } from './package.json'
export default defineConfig({
entries: [
{ input: './src/index.ts' },
{ declaration: './src/types/index.ts' },
{ dts: './src/types/index.ts' },
{
input: './src/index.ts',
output: './dist/index.cjs',
},
{
dts: './src/types/index.ts',
output: './dist/types/index.d.cts',
},
{
input: './src/bin/index.ts',
transformers: {
replace: {
preventAssignment: true,
__version__: version,
},
},
Expand Down
103 changes: 46 additions & 57 deletions src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export async function build(
}

const fileStats = {
cwd,
path: `${parseOutput(_entry.output)}/${parseInput(copyInput)}`,
size: totalSize,
buildTime: Date.now() - entryStart,
Expand All @@ -166,17 +167,17 @@ export async function build(
if (entry.input) {
const logFilter = getLogFilter(entry.logFilter || [])

const _output = getOutputPath(outDir, entry.input)
const _output = entry.output || getOutputPath(outDir, entry.input)
let _format: ModuleFormat = 'esm'
if (_output.endsWith('.cjs')) _format = 'cjs'

const buildLogs: BuildLogs[] = []
const _entry = {
input: entry.input,
output: entry.output || _output,
output: _output,
externals: entry.externals || options.externals,
format: entry.format || _format,
transformers: entry.transformers,
...entry,
defaultPlugins: [
esbuildPlugin({
minify: !isUndefined(entry.minify)
Expand All @@ -185,15 +186,6 @@ export async function build(
...entry.transformers?.esbuild,
}),
],
plugins: entry.plugins,
banner: entry.banner,
footer: entry.footer,
intro: entry.intro,
outro: entry.outro,
paths: entry.paths,
name: entry.name,
globals: entry.globals,
extend: entry.extend,
}

if (!entry.plugins) {
Expand Down Expand Up @@ -225,7 +217,16 @@ export async function build(
)
}

await hooks?.['build:entry:start']?.(_entry, buildStats)
const fileStats = {
cwd,
path: _entry.output,
size: 0,
buildTime: entryStart,
format: _entry.format,
logs: buildLogs,
}

await hooks?.['build:entry:start']?.(_entry, fileStats)

const _build = await rollup({
input: resolve(cwd, _entry.input),
Expand All @@ -249,43 +250,30 @@ export async function build(
})
const stats = await stat(resolve(cwd, _entry.output))

const file = {
path: _entry.output,
size: stats.size,
buildTime: Date.now() - entryStart,
format: _entry.format,
logs: buildLogs,
}
fileStats.size = stats.size
fileStats.buildTime = Date.now() - entryStart
fileStats.logs = buildLogs

buildStats.files.push(file)
buildStats.files.push(fileStats)
buildStats.size = buildStats.size + stats.size

logModuleStats(file, longestOutput)
logModuleStats(fileStats, longestOutput)

await hooks?.['build:entry:end']?.(_entry, buildStats)
await hooks?.['build:entry:end']?.(_entry, fileStats)
}

if (entry.declaration) {
if (entry.dts || entry.declaration) {
const logFilter = getLogFilter(entry.logFilter || [])

const _output = getOutputPath(outDir, entry.declaration, true)
let _format: ModuleFormat = 'esm'
if (_output.endsWith('.d.cts')) _format = 'cjs'

const buildLogs: BuildLogs[] = []
const dts = entry.dts! || entry.declaration!

const _entry = {
input: entry.declaration,
output: entry.output || _output,
dts,
output: entry.output || getOutputPath(outDir, dts, true),
externals: entry.externals || options.externals,
format: entry.format || _format,
transformers: entry.transformers,
format: entry.format || 'esm',
...entry,
defaultPlugins: [dtsPlugin(entry.transformers?.dts)],
plugins: entry.plugins,
banner: entry.banner,
footer: entry.footer,
intro: entry.intro,
outro: entry.outro,
paths: entry.paths,
}

if (!entry.plugins) {
Expand All @@ -294,10 +282,19 @@ export async function build(
)
}

await hooks?.['build:entry:start']?.(_entry, buildStats)
const fileStats = {
cwd,
path: _entry.output,
size: 0,
buildTime: entryStart,
format: 'dts',
logs: buildLogs,
}

await hooks?.['build:entry:start']?.(_entry, fileStats)

const _build = await rollup({
input: resolve(cwd, _entry.input),
input: resolve(cwd, _entry.dts),
external: _entry.externals,
plugins: _entry.plugins || _entry.defaultPlugins,
onLog: (level, log) => {
Expand All @@ -315,36 +312,28 @@ export async function build(
})
const stats = await stat(resolve(cwd, _entry.output))

const fileStats = {
path: _entry.output,
size: stats.size,
buildTime: Date.now() - entryStart,
format: 'dts',
logs: buildLogs,
}
fileStats.size = stats.size
fileStats.buildTime = Date.now() - entryStart
fileStats.logs = buildLogs

buildStats.files.push(fileStats)
buildStats.size = buildStats.size + stats.size

logModuleStats(fileStats, longestOutput)

await hooks?.['build:entry:end']?.(_entry, buildStats)
await hooks?.['build:entry:end']?.(_entry, fileStats)
}

if (entry.template && entry.output) {
const buildLogs: BuildLogs[] = []

const _entry = {
template: entry.template,
output: entry.output,
}

await write(_entry.output, _entry.template)
await write(entry.output, entry.template)

const stats = await stat(resolve(cwd, _entry.output))
const stats = await stat(resolve(cwd, entry.output))

const fileStats = {
path: _entry.output,
cwd,
path: entry.output,
size: stats.size,
buildTime: Date.now() - entryStart,
format: 'tmp',
Expand Down
Loading

0 comments on commit 5a1b129

Please sign in to comment.