@hypernym/bundler 0.6.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Ivo Dolenc, Hypernym Studio
3
+ Copyright (c) 2024 Ivo Dolenc, Hypernym Studio
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -13,6 +13,7 @@ npm i -D @hypernym/bundler
13
13
  - Powered by Rollup
14
14
  - Allows advanced customization
15
15
  - Provides a powerful hooking system
16
+ - Supports all TS module resolutions
16
17
  - Exports fully optimized code
17
18
  - Follows modern practice
18
19
  - Super easy to use
@@ -259,6 +260,40 @@ export default defineConfig({
259
260
  })
260
261
  ```
261
262
 
263
+ ### alias
264
+
265
+ - Type: `true`
266
+ - Default: `undefined`
267
+
268
+ Specifies global path alias support.
269
+
270
+ If true, it enables import prefixes:
271
+
272
+ - `@/*`
273
+ - `~/*`
274
+
275
+ ```ts
276
+ // bundler.config.ts
277
+
278
+ import { defineConfig } from '@hypernym/bundler'
279
+
280
+ export default defineConfig({
281
+ alias: true,
282
+ })
283
+ ```
284
+
285
+ After that it can be imported as:
286
+
287
+ ```ts
288
+ // Imports module from './src/utils/index.js'
289
+ import { module } from '@/utils'
290
+
291
+ // or
292
+
293
+ // Imports module from './src/utils/index.js'
294
+ import { module } from '~/utils'
295
+ ```
296
+
262
297
  ## CLI
263
298
 
264
299
  ### Custom Config
@@ -271,11 +306,13 @@ npx hyperbundler --config my.config.js
271
306
 
272
307
  ## Community
273
308
 
274
- Feel free to use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) for any additional questions.
309
+ Feel free to ask questions or share new ideas.
310
+
311
+ Use the official [discussions](https://github.com/hypernym-studio/bundler/discussions) to get involved.
275
312
 
276
313
  ## License
277
314
 
278
- Developed in 🇭🇷 Croatia
315
+ Developed in 🇭🇷 Croatia.
279
316
 
280
317
  Released under the [MIT](LICENSE.txt) license.
281
318
 
@@ -2,7 +2,7 @@
2
2
  import process, { cwd } from 'node:process';
3
3
  import { createArgs } from '@hypernym/args';
4
4
  import { readFile, stat } from 'node:fs/promises';
5
- import { resolve, parse } from 'node:path';
5
+ import { resolve, dirname, join, parse } from 'node:path';
6
6
  import { exists, writeFile } from '@hypernym/utils/fs';
7
7
  import { cyan, dim, red, magenta, green } from '@hypernym/colors';
8
8
  import { build as build$1, transform } from 'esbuild';
@@ -11,10 +11,12 @@ import { fileURLToPath } from 'node:url';
11
11
  import { isObject } from '@hypernym/utils';
12
12
  import { rollup } from 'rollup';
13
13
  import { getLogFilter } from 'rollup/getLogFilter';
14
- import _replace from '@rollup/plugin-replace';
15
- import _json from '@rollup/plugin-json';
16
- import _resolve from '@rollup/plugin-node-resolve';
14
+ import replacePlugin from '@rollup/plugin-replace';
15
+ import jsonPlugin from '@rollup/plugin-json';
16
+ import resolvePlugin from '@rollup/plugin-node-resolve';
17
+ import aliasPlugin from '@rollup/plugin-alias';
17
18
  import { dts } from 'rollup-plugin-dts';
19
+ import { existsSync, statSync } from 'node:fs';
18
20
  import { createFilter } from '@rollup/pluginutils';
19
21
 
20
22
  const externals = [
@@ -26,7 +28,7 @@ const externals = [
26
28
  ];
27
29
 
28
30
  const name = "bundler";
29
- const version = `0.6.3`;
31
+ const version = `0.8.0`;
30
32
 
31
33
  const cl = console.log;
32
34
  const logger = {
@@ -143,14 +145,31 @@ async function createConfigLoader(cwd, args) {
143
145
  return logger.exit(warnMessage);
144
146
  }
145
147
 
148
+ function resolvePath(path, index = false) {
149
+ const extensions = [".js", ".ts", "jsx", ".tsx"];
150
+ const fileWithoutExt = path.replace(/\.[jt]sx?$/, "");
151
+ for (const ext of extensions) {
152
+ const file = index ? join(path, `index${ext}`) : `${fileWithoutExt}${ext}`;
153
+ if (existsSync(file))
154
+ return file;
155
+ }
156
+ return null;
157
+ }
146
158
  function esbuild(options) {
147
- const isJs = /\.(?:[mc]?js|jsx)$/;
148
159
  const filter = createFilter(/\.([cm]?ts|[jt]sx)$/);
149
160
  return {
150
161
  name: "esbuild",
151
- resolveId(id, importer, options2) {
152
- if (isJs.test(id) && importer) {
153
- return this.resolve(id.replace(/js(x?)$/, "ts$1"), importer, options2);
162
+ resolveId(id, importer) {
163
+ if (importer) {
164
+ const resolved = resolve(importer ? dirname(importer) : cwd(), id);
165
+ let file = resolvePath(resolved);
166
+ if (file)
167
+ return file;
168
+ if (!file && existsSync(resolved) && statSync(resolved).isDirectory()) {
169
+ file = resolvePath(resolved, true);
170
+ if (file)
171
+ return file;
172
+ }
154
173
  }
155
174
  return null;
156
175
  },
@@ -160,7 +179,7 @@ function esbuild(options) {
160
179
  const result = await transform(code, {
161
180
  loader: "default",
162
181
  ...options,
163
- sourcefile: id.replace(/\.[cm]ts/, ".ts")
182
+ sourcefile: id
164
183
  });
165
184
  return {
166
185
  code: result.code,
@@ -185,9 +204,6 @@ function esbuild(options) {
185
204
  };
186
205
  }
187
206
 
188
- const replacePlugin = _replace.default ?? _replace;
189
- const jsonPlugin = _json.default ?? _json;
190
- const resolvePlugin = _resolve.default ?? _resolve;
191
207
  async function build(cwd, options) {
192
208
  const { outDir = "dist", hooks } = options;
193
209
  let start = 0;
@@ -201,6 +217,13 @@ async function build(cwd, options) {
201
217
  await hooks["build:start"](options, buildStats);
202
218
  if (options.entries) {
203
219
  start = Date.now();
220
+ const aliasDir = `${resolve(cwd, "./src")}/`;
221
+ const aliasOptions = {
222
+ entries: [
223
+ { find: /^@\//, replacement: aliasDir },
224
+ { find: /^~\//, replacement: aliasDir }
225
+ ]
226
+ };
204
227
  for (const entry of options.entries) {
205
228
  const entryStart = Date.now();
206
229
  let logFilter = getLogFilter(entry.logFilter || []);
@@ -242,6 +265,9 @@ async function build(cwd, options) {
242
265
  const resolveOptions = isObject(_entry.pluginsOptions.resolve) ? _entry.pluginsOptions.resolve : void 0;
243
266
  _entry.plugins.unshift(resolvePlugin(resolveOptions));
244
267
  }
268
+ if (options.alias) {
269
+ _entry.plugins.unshift(aliasPlugin(aliasOptions));
270
+ }
245
271
  if (hooks?.["build:entry:start"]) {
246
272
  await hooks["build:entry:start"](_entry, buildStats);
247
273
  }
@@ -298,6 +324,9 @@ async function build(cwd, options) {
298
324
  outro: entry.outro,
299
325
  paths: entry.paths
300
326
  };
327
+ if (options.alias) {
328
+ _entry.plugins.unshift(aliasPlugin(aliasOptions));
329
+ }
301
330
  if (hooks?.["build:entry:start"]) {
302
331
  await hooks["build:entry:start"](_entry, buildStats);
303
332
  }
@@ -235,6 +235,24 @@ interface Options {
235
235
  * @default undefined
236
236
  */
237
237
  hooks?: HooksOptions;
238
+ /**
239
+ * Specifies global path alias support.
240
+ *
241
+ * If true, it enables import prefixes:
242
+ *
243
+ * - `@/*`
244
+ * - `~/*`
245
+ *
246
+ * @example
247
+ *
248
+ * ```ts
249
+ * // Imports module from './src/utils/index.js'
250
+ * import { module } from '@/utils/index.js'
251
+ * ```
252
+ *
253
+ * @default undefined
254
+ */
255
+ alias?: true;
238
256
  }
239
257
 
240
258
  interface BuildLogs {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.6.3",
3
+ "version": "0.8.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",
@@ -43,6 +43,10 @@
43
43
  "format": "prettier --config .config/prettier.config.js --write .",
44
44
  "prepublishOnly": "npm run build"
45
45
  },
46
+ "sideEffects": false,
47
+ "engines": {
48
+ "node": ">=v18.0.0"
49
+ },
46
50
  "peerDependencies": {
47
51
  "@types/node": ">=20.0.0",
48
52
  "typescript": ">=5.0.0"
@@ -59,25 +63,23 @@
59
63
  "@hypernym/args": "^0.2.1",
60
64
  "@hypernym/colors": "^1.0.1",
61
65
  "@hypernym/spinner": "^0.2.0",
62
- "@hypernym/utils": "^2.1.0",
66
+ "@hypernym/utils": "^2.3.0",
67
+ "@rollup/plugin-alias": "^5.1.0",
63
68
  "@rollup/plugin-json": "^6.1.0",
64
69
  "@rollup/plugin-node-resolve": "^15.2.3",
65
70
  "@rollup/plugin-replace": "^5.0.5",
66
- "esbuild": "^0.19.9",
67
- "rollup": "^4.9.0",
71
+ "esbuild": "^0.19.11",
72
+ "rollup": "^4.9.5",
68
73
  "rollup-plugin-dts": "^6.1.0"
69
74
  },
70
75
  "devDependencies": {
71
76
  "@hypernym/eslint-config": "^2.0.3",
72
77
  "@hypernym/prettier-config": "^2.0.3",
73
- "@hypernym/tsconfig": "^1.1.0",
74
- "@types/node": "^20.10.4",
75
- "eslint": "^8.55.0",
76
- "prettier": "^3.1.1",
77
- "tsx": "^4.6.2",
78
+ "@hypernym/tsconfig": "^1.2.0",
79
+ "@types/node": "^20.11.5",
80
+ "eslint": "^8.56.0",
81
+ "prettier": "^3.2.4",
82
+ "tsx": "^4.7.0",
78
83
  "typescript": "^5.3.3"
79
- },
80
- "engines": {
81
- "node": ">=v18.0.0"
82
84
  }
83
85
  }