@hypernym/bundler 0.7.0 → 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/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
@@ -271,14 +272,26 @@ If true, it enables import prefixes:
271
272
  - `@/*`
272
273
  - `~/*`
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
+
274
287
  ```ts
275
288
  // Imports module from './src/utils/index.js'
276
- import { module } from '@/utils/index.js'
289
+ import { module } from '@/utils'
277
290
 
278
291
  // or
279
292
 
280
293
  // Imports module from './src/utils/index.js'
281
- import { module } from '~/utils/index.js'
294
+ import { module } from '~/utils'
282
295
  ```
283
296
 
284
297
  ## CLI
@@ -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,11 +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';
17
- import _alias from '@rollup/plugin-alias';
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';
18
18
  import { dts } from 'rollup-plugin-dts';
19
+ import { existsSync, statSync } from 'node:fs';
19
20
  import { createFilter } from '@rollup/pluginutils';
20
21
 
21
22
  const externals = [
@@ -27,7 +28,7 @@ const externals = [
27
28
  ];
28
29
 
29
30
  const name = "bundler";
30
- const version = `0.7.0`;
31
+ const version = `0.8.0`;
31
32
 
32
33
  const cl = console.log;
33
34
  const logger = {
@@ -144,14 +145,31 @@ async function createConfigLoader(cwd, args) {
144
145
  return logger.exit(warnMessage);
145
146
  }
146
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
+ }
147
158
  function esbuild(options) {
148
- const isJs = /\.(?:[mc]?js|jsx)$/;
149
159
  const filter = createFilter(/\.([cm]?ts|[jt]sx)$/);
150
160
  return {
151
161
  name: "esbuild",
152
- resolveId(id, importer, options2) {
153
- if (isJs.test(id) && importer) {
154
- 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
+ }
155
173
  }
156
174
  return null;
157
175
  },
@@ -161,7 +179,7 @@ function esbuild(options) {
161
179
  const result = await transform(code, {
162
180
  loader: "default",
163
181
  ...options,
164
- sourcefile: id.replace(/\.[cm]ts/, ".ts")
182
+ sourcefile: id
165
183
  });
166
184
  return {
167
185
  code: result.code,
@@ -186,10 +204,6 @@ function esbuild(options) {
186
204
  };
187
205
  }
188
206
 
189
- const replacePlugin = _replace.default ?? _replace;
190
- const jsonPlugin = _json.default ?? _json;
191
- const resolvePlugin = _resolve.default ?? _resolve;
192
- const aliasPlugin = _alias.default ?? _alias;
193
207
  async function build(cwd, options) {
194
208
  const { outDir = "dist", hooks } = options;
195
209
  let start = 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",
@@ -76,9 +76,9 @@
76
76
  "@hypernym/eslint-config": "^2.0.3",
77
77
  "@hypernym/prettier-config": "^2.0.3",
78
78
  "@hypernym/tsconfig": "^1.2.0",
79
- "@types/node": "^20.11.0",
79
+ "@types/node": "^20.11.5",
80
80
  "eslint": "^8.56.0",
81
- "prettier": "^3.2.2",
81
+ "prettier": "^3.2.4",
82
82
  "tsx": "^4.7.0",
83
83
  "typescript": "^5.3.3"
84
84
  }