Skip to content

Commit

Permalink
feat: add new alias option (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored Jan 14, 2024
1 parent ae398f1 commit fe59706
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 17 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,28 @@ export default defineConfig({
})
```

### alias

- Type: `true`
- Default: `undefined`

Specifies global path alias support.

If true, it enables import prefixes:

- `@/*`
- `~/*`

```ts
// Imports module from './src/utils/index.js'
import { module } from '@/utils/index.js'

// or

// Imports module from './src/utils/index.js'
import { module } from '~/utils/index.js'
```

## CLI

### Custom Config
Expand All @@ -271,11 +293,13 @@ npx hyperbundler --config my.config.js

## Community

Feel free to use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) for any additional questions.
Feel free to ask questions or share new ideas.

Use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) to get involved.

## License

Developed in 🇭🇷 Croatia
Developed in 🇭🇷 Croatia.

Released under the [MIT](LICENSE.txt) license.

Expand Down
1 change: 1 addition & 0 deletions bundler.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineConfig } from './src/config.js'
import { name, version } from './package.json'

export default defineConfig({
alias: true,
entries: [
{ input: './src/index.ts' },
{ types: './src/types/index.ts' },
Expand Down
27 changes: 23 additions & 4 deletions src/bin/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import { getLogFilter } from 'rollup/getLogFilter'
import _replace from '@rollup/plugin-replace'
import _json from '@rollup/plugin-json'
import _resolve from '@rollup/plugin-node-resolve'
import _alias from '@rollup/plugin-alias'
import { dts as dtsPlugin } from 'rollup-plugin-dts'
import { esbuild as esbuildPlugin } from '../utils/plugins/esbuild.js'
import { getOutputPath } from '../utils/index.js'
import { esbuild as esbuildPlugin } from '@/utils/plugins/esbuild.js'
import { getOutputPath } from '@/utils/index.js'
import type { Plugin, ModuleFormat } from 'rollup'
import type { Options } from '../types/options.js'
import type { BuildStats, BuildLogs } from '../types/build.js'
import type { Options } from '@/types/options.js'
import type { BuildStats, BuildLogs } from '@/types/build.js'

const replacePlugin = _replace.default ?? _replace
const jsonPlugin = _json.default ?? _json
const resolvePlugin = _resolve.default ?? _resolve
const aliasPlugin = _alias.default ?? _alias

export async function build(
cwd: string,
Expand All @@ -37,6 +39,14 @@ export async function build(
if (options.entries) {
start = Date.now()

const aliasDir = `${resolve(cwd, './src')}/`
const aliasOptions = {
entries: [
{ find: /^@\//, replacement: aliasDir },
{ find: /^~\//, replacement: aliasDir },
],
}

for (const entry of options.entries) {
const entryStart = Date.now()

Expand Down Expand Up @@ -95,13 +105,18 @@ export async function build(
}),
)
}

if (_entry.pluginsOptions?.resolve) {
const resolveOptions = isObject(_entry.pluginsOptions.resolve)
? _entry.pluginsOptions.resolve
: undefined
_entry.plugins.unshift(resolvePlugin(resolveOptions))
}

if (options.alias) {
_entry.plugins.unshift(aliasPlugin(aliasOptions))
}

if (hooks?.['build:entry:start']) {
await hooks['build:entry:start'](_entry, buildStats)
}
Expand Down Expand Up @@ -174,6 +189,10 @@ export async function build(
paths: entry.paths,
}

if (options.alias) {
_entry.plugins.unshift(aliasPlugin(aliasOptions))
}

if (hooks?.['build:entry:start']) {
await hooks['build:entry:start'](_entry, buildStats)
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { green, cyan, magenta, red, dim } from '@hypernym/colors'
import { createSpinner } from '@hypernym/spinner'
import { version } from './meta.js'
import { build } from './build.js'
import { cl, logger, formatMs, formatBytes } from '../utils/index.js'
import type { Options } from '../types/options.js'
import type { Args } from '../types/args.js'
import { cl, logger, formatMs, formatBytes } from '@/utils/index.js'
import type { Options } from '@/types/options.js'
import type { Args } from '@/types/args.js'

export async function createBuilder(cwd: string, args: Args, options: Options) {
const { hooks } = options
Expand Down
4 changes: 2 additions & 2 deletions src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { cwd as _cwd } from 'node:process'
import { createArgs } from '@hypernym/args'
import { createConfigLoader } from './loader.js'
import { createBuilder } from './builder.js'
import { error } from '../utils/index.js'
import type { Args } from '../types/args.js'
import { error } from '@/utils/index.js'
import type { Args } from '@/types/args.js'

async function main() {
const cwd = _cwd()
Expand Down
6 changes: 3 additions & 3 deletions src/bin/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { exists, writeFile } from '@hypernym/utils/fs'
import { cyan } from '@hypernym/colors'
import { build } from 'esbuild'
import { externals } from '../config.js'
import { logger, error } from '../utils/index.js'
import type { Options } from '../types/options.js'
import type { Args } from '../types/args.js'
import { logger, error } from '@/utils/index.js'
import type { Options } from '@/types/options.js'
import type { Args } from '@/types/args.js'

export async function loadConfig(
cwd: string,
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from './types/options.js'
import type { Options } from '@/types/options.js'

/**
* List of global defaults for externals.
Expand Down
18 changes: 18 additions & 0 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ export interface Options {
* @default undefined
*/
hooks?: HooksOptions
/**
* Specifies global path alias support.
*
* If true, it enables import prefixes:
*
* - `@/*`
* - `~/*`
*
* @example
*
* ```ts
* // Imports module from './src/utils/index.js'
* import { module } from '@/utils/index.js'
* ```
*
* @default undefined
*/
alias?: true
}
2 changes: 1 addition & 1 deletion src/utils/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'node:process'
import { logger } from '../utils/logger.js'
import { logger } from '@/utils/logger.js'

export function error(err: any): never {
logger.error('Something went wrong...')
Expand Down
2 changes: 1 addition & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process'
import { cyan, magenta, red, dim } from '@hypernym/colors'
import { name, version } from '../bin/meta.js'
import { name, version } from '@/bin/meta.js'

export const cl = console.log

Expand Down

0 comments on commit fe59706

Please sign in to comment.