Skip to content

Commit fe59706

Browse files
authored
feat: add new alias option (#10)
1 parent ae398f1 commit fe59706

10 files changed

Lines changed: 79 additions & 17 deletions

File tree

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,28 @@ export default defineConfig({
259259
})
260260
```
261261

262+
### alias
263+
264+
- Type: `true`
265+
- Default: `undefined`
266+
267+
Specifies global path alias support.
268+
269+
If true, it enables import prefixes:
270+
271+
- `@/*`
272+
- `~/*`
273+
274+
```ts
275+
// Imports module from './src/utils/index.js'
276+
import { module } from '@/utils/index.js'
277+
278+
// or
279+
280+
// Imports module from './src/utils/index.js'
281+
import { module } from '~/utils/index.js'
282+
```
283+
262284
## CLI
263285

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

272294
## Community
273295

274-
Feel free to use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) for any additional questions.
296+
Feel free to ask questions or share new ideas.
297+
298+
Use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) to get involved.
275299

276300
## License
277301

278-
Developed in 🇭🇷 Croatia
302+
Developed in 🇭🇷 Croatia.
279303

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

bundler.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from './src/config.js'
33
import { name, version } from './package.json'
44

55
export default defineConfig({
6+
alias: true,
67
entries: [
78
{ input: './src/index.ts' },
89
{ types: './src/types/index.ts' },

src/bin/build.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import { getLogFilter } from 'rollup/getLogFilter'
77
import _replace from '@rollup/plugin-replace'
88
import _json from '@rollup/plugin-json'
99
import _resolve from '@rollup/plugin-node-resolve'
10+
import _alias from '@rollup/plugin-alias'
1011
import { dts as dtsPlugin } from 'rollup-plugin-dts'
11-
import { esbuild as esbuildPlugin } from '../utils/plugins/esbuild.js'
12-
import { getOutputPath } from '../utils/index.js'
12+
import { esbuild as esbuildPlugin } from '@/utils/plugins/esbuild.js'
13+
import { getOutputPath } from '@/utils/index.js'
1314
import type { Plugin, ModuleFormat } from 'rollup'
14-
import type { Options } from '../types/options.js'
15-
import type { BuildStats, BuildLogs } from '../types/build.js'
15+
import type { Options } from '@/types/options.js'
16+
import type { BuildStats, BuildLogs } from '@/types/build.js'
1617

1718
const replacePlugin = _replace.default ?? _replace
1819
const jsonPlugin = _json.default ?? _json
1920
const resolvePlugin = _resolve.default ?? _resolve
21+
const aliasPlugin = _alias.default ?? _alias
2022

2123
export async function build(
2224
cwd: string,
@@ -37,6 +39,14 @@ export async function build(
3739
if (options.entries) {
3840
start = Date.now()
3941

42+
const aliasDir = `${resolve(cwd, './src')}/`
43+
const aliasOptions = {
44+
entries: [
45+
{ find: /^@\//, replacement: aliasDir },
46+
{ find: /^~\//, replacement: aliasDir },
47+
],
48+
}
49+
4050
for (const entry of options.entries) {
4151
const entryStart = Date.now()
4252

@@ -95,13 +105,18 @@ export async function build(
95105
}),
96106
)
97107
}
108+
98109
if (_entry.pluginsOptions?.resolve) {
99110
const resolveOptions = isObject(_entry.pluginsOptions.resolve)
100111
? _entry.pluginsOptions.resolve
101112
: undefined
102113
_entry.plugins.unshift(resolvePlugin(resolveOptions))
103114
}
104115

116+
if (options.alias) {
117+
_entry.plugins.unshift(aliasPlugin(aliasOptions))
118+
}
119+
105120
if (hooks?.['build:entry:start']) {
106121
await hooks['build:entry:start'](_entry, buildStats)
107122
}
@@ -174,6 +189,10 @@ export async function build(
174189
paths: entry.paths,
175190
}
176191

192+
if (options.alias) {
193+
_entry.plugins.unshift(aliasPlugin(aliasOptions))
194+
}
195+
177196
if (hooks?.['build:entry:start']) {
178197
await hooks['build:entry:start'](_entry, buildStats)
179198
}

src/bin/builder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { green, cyan, magenta, red, dim } from '@hypernym/colors'
44
import { createSpinner } from '@hypernym/spinner'
55
import { version } from './meta.js'
66
import { build } from './build.js'
7-
import { cl, logger, formatMs, formatBytes } from '../utils/index.js'
8-
import type { Options } from '../types/options.js'
9-
import type { Args } from '../types/args.js'
7+
import { cl, logger, formatMs, formatBytes } from '@/utils/index.js'
8+
import type { Options } from '@/types/options.js'
9+
import type { Args } from '@/types/args.js'
1010

1111
export async function createBuilder(cwd: string, args: Args, options: Options) {
1212
const { hooks } = options

src/bin/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { cwd as _cwd } from 'node:process'
44
import { createArgs } from '@hypernym/args'
55
import { createConfigLoader } from './loader.js'
66
import { createBuilder } from './builder.js'
7-
import { error } from '../utils/index.js'
8-
import type { Args } from '../types/args.js'
7+
import { error } from '@/utils/index.js'
8+
import type { Args } from '@/types/args.js'
99

1010
async function main() {
1111
const cwd = _cwd()

src/bin/loader.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { exists, writeFile } from '@hypernym/utils/fs'
44
import { cyan } from '@hypernym/colors'
55
import { build } from 'esbuild'
66
import { externals } from '../config.js'
7-
import { logger, error } from '../utils/index.js'
8-
import type { Options } from '../types/options.js'
9-
import type { Args } from '../types/args.js'
7+
import { logger, error } from '@/utils/index.js'
8+
import type { Options } from '@/types/options.js'
9+
import type { Args } from '@/types/args.js'
1010

1111
export async function loadConfig(
1212
cwd: string,

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Options } from './types/options.js'
1+
import type { Options } from '@/types/options.js'
22

33
/**
44
* List of global defaults for externals.

src/types/options.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@ export interface Options {
5555
* @default undefined
5656
*/
5757
hooks?: HooksOptions
58+
/**
59+
* Specifies global path alias support.
60+
*
61+
* If true, it enables import prefixes:
62+
*
63+
* - `@/*`
64+
* - `~/*`
65+
*
66+
* @example
67+
*
68+
* ```ts
69+
* // Imports module from './src/utils/index.js'
70+
* import { module } from '@/utils/index.js'
71+
* ```
72+
*
73+
* @default undefined
74+
*/
75+
alias?: true
5876
}

src/utils/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import process from 'node:process'
2-
import { logger } from '../utils/logger.js'
2+
import { logger } from '@/utils/logger.js'
33

44
export function error(err: any): never {
55
logger.error('Something went wrong...')

src/utils/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import process from 'node:process'
22
import { cyan, magenta, red, dim } from '@hypernym/colors'
3-
import { name, version } from '../bin/meta.js'
3+
import { name, version } from '@/bin/meta.js'
44

55
export const cl = console.log
66

0 commit comments

Comments
 (0)