@hypernym/bundler 0.4.0 → 0.6.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.
@@ -0,0 +1 @@
1
+ // Generated by @hypernym/bundler
@@ -7,6 +7,7 @@ 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';
9
9
  import { createSpinner } from '@hypernym/spinner';
10
+ import { fileURLToPath } from 'node:url';
10
11
  import { isObject } from '@hypernym/utils';
11
12
  import { rollup } from 'rollup';
12
13
  import { getLogFilter } from 'rollup/getLogFilter';
@@ -25,7 +26,7 @@ const externals = [
25
26
  ];
26
27
 
27
28
  const name = "bundler";
28
- const version = `0.4.0`;
29
+ const version = `0.6.0`;
29
30
 
30
31
  const cl = console.log;
31
32
  const logger = {
@@ -202,7 +203,7 @@ async function build(cwd, options) {
202
203
  start = Date.now();
203
204
  for (const entry of options.entries) {
204
205
  const entryStart = Date.now();
205
- const logFilter = getLogFilter(entry.logFilter || []);
206
+ let logFilter = getLogFilter(entry.logFilter || []);
206
207
  if ("input" in entry) {
207
208
  const _output = getOutputPath(outDir, entry.input);
208
209
  let _format = "esm";
@@ -217,7 +218,13 @@ async function build(cwd, options) {
217
218
  plugins: [esbuild(entry.plugins?.esbuild)],
218
219
  pluginsOptions: entry.plugins,
219
220
  banner: entry.banner,
220
- footer: entry.footer
221
+ footer: entry.footer,
222
+ intro: entry.intro,
223
+ outro: entry.outro,
224
+ paths: entry.paths,
225
+ name: entry.name,
226
+ globals: entry.globals,
227
+ extend: entry.extend
221
228
  };
222
229
  if (_entry.pluginsOptions?.json) {
223
230
  const jsonOptions = isObject(_entry.pluginsOptions.json) ? _entry.pluginsOptions.json : void 0;
@@ -251,7 +258,13 @@ async function build(cwd, options) {
251
258
  file: resolve(cwd, _entry.output),
252
259
  format: _entry.format,
253
260
  banner: _entry.banner,
254
- footer: _entry.footer
261
+ footer: _entry.footer,
262
+ intro: _entry.intro,
263
+ outro: _entry.outro,
264
+ paths: _entry.paths,
265
+ name: _entry.name,
266
+ globals: _entry.globals,
267
+ extend: _entry.extend
255
268
  });
256
269
  const stats = await stat(resolve(cwd, _entry.output));
257
270
  buildStats.files.push({
@@ -280,7 +293,10 @@ async function build(cwd, options) {
280
293
  plugins: [dts(entry.plugins?.dts)],
281
294
  pluginsOptions: entry.plugins,
282
295
  banner: entry.banner,
283
- footer: entry.footer
296
+ footer: entry.footer,
297
+ intro: entry.intro,
298
+ outro: entry.outro,
299
+ paths: entry.paths
284
300
  };
285
301
  if (hooks?.["build:entry:start"]) {
286
302
  await hooks["build:entry:start"](_entry, buildStats);
@@ -298,7 +314,10 @@ async function build(cwd, options) {
298
314
  file: resolve(cwd, _entry.output),
299
315
  format: _entry.format,
300
316
  banner: _entry.banner,
301
- footer: _entry.footer
317
+ footer: _entry.footer,
318
+ intro: _entry.intro,
319
+ outro: _entry.outro,
320
+ paths: _entry.paths
302
321
  });
303
322
  const stats = await stat(resolve(cwd, _entry.output));
304
323
  buildStats.files.push({
@@ -313,6 +332,44 @@ async function build(cwd, options) {
313
332
  await hooks["build:entry:end"](_entry, buildStats);
314
333
  }
315
334
  }
335
+ if ("template" in entry) {
336
+ logFilter = getLogFilter(entry.logFilter || ["!code:EMPTY_BUNDLE"]);
337
+ const _distDir = parse(fileURLToPath(import.meta.url)).dir;
338
+ const _output = entry.output;
339
+ let _format = "esm";
340
+ if (_output.endsWith(".cjs"))
341
+ _format = "cjs";
342
+ const buildLogs = [];
343
+ const _entry = {
344
+ template: resolve(_distDir, "_empty.ts"),
345
+ output: _output,
346
+ content: entry.content,
347
+ plugins: [esbuild(entry.plugins?.esbuild)],
348
+ pluginsOptions: entry.plugins,
349
+ format: entry.format || _format
350
+ };
351
+ const _build = await rollup({
352
+ input: _entry.template,
353
+ plugins: _entry.plugins,
354
+ onLog: (level, log) => {
355
+ if (logFilter(log))
356
+ buildLogs.push({ level, log });
357
+ }
358
+ });
359
+ await _build.write({
360
+ file: resolve(cwd, _entry.output),
361
+ intro: _entry.content
362
+ });
363
+ const stats = await stat(resolve(cwd, _entry.output));
364
+ buildStats.files.push({
365
+ path: _entry.output,
366
+ size: stats.size,
367
+ buildTime: Date.now() - entryStart,
368
+ format: _entry.format,
369
+ logs: buildLogs
370
+ });
371
+ buildStats.size = buildStats.size + stats.size;
372
+ }
316
373
  }
317
374
  buildStats.buildTime = Date.now() - start;
318
375
  }
@@ -51,6 +51,24 @@ interface EntryBase {
51
51
  * @default undefined
52
52
  */
53
53
  footer?: OutputOptions['footer'];
54
+ /**
55
+ * Specifies the code at the beginning that goes inside any _format-specific_ wrapper.
56
+ *
57
+ * @default undefined
58
+ */
59
+ intro?: OutputOptions['intro'];
60
+ /**
61
+ * Specifies the code at the end that goes inside any _format-specific_ wrapper.
62
+ *
63
+ * @default undefined
64
+ */
65
+ outro?: OutputOptions['outro'];
66
+ /**
67
+ * Maps external module IDs to paths.
68
+ *
69
+ * @default undefined
70
+ */
71
+ paths?: OutputOptions['paths'];
54
72
  /**
55
73
  * Specifies custom filters that will display only certain log messages.
56
74
  *
@@ -69,6 +87,28 @@ interface EntryInput extends EntryBase {
69
87
  * @default undefined
70
88
  */
71
89
  plugins?: PluginsInput;
90
+ /**
91
+ * Specifies the global variable name that representing exported bundle.
92
+ *
93
+ * Intended for `umd/iife` formats.
94
+ *
95
+ * @default undefined
96
+ */
97
+ name?: OutputOptions['name'];
98
+ /**
99
+ * Specifies global _module ID_ and _variable name_ pairs necessary for external imports.
100
+ *
101
+ * Intended for `umd/iife` formats.
102
+ *
103
+ * @default undefined
104
+ */
105
+ globals?: OutputOptions['globals'];
106
+ /**
107
+ * Specifies whether to extend the global variable defined by the `name` option.
108
+ *
109
+ * Intended for `umd/iife` formats.
110
+ */
111
+ extend?: OutputOptions['extend'];
72
112
  }
73
113
  interface EntryTypes extends EntryBase {
74
114
  /**
@@ -82,7 +122,64 @@ interface EntryTypes extends EntryBase {
82
122
  */
83
123
  plugins?: PluginsTypes;
84
124
  }
85
- type EntryOptions = EntryInput | EntryTypes;
125
+ interface EntryTemplate extends Pick<EntryBase, 'logFilter'> {
126
+ /**
127
+ * Specifies the build entry as a module template.
128
+ *
129
+ * Provides the ability to dynamically inject template content during the build phase.
130
+ *
131
+ * @example
132
+ *
133
+ * ```ts
134
+ * export default defineConfig({
135
+ * entries: [
136
+ * {
137
+ * template: true,
138
+ * output: './dist/template.ts',
139
+ * content: '// TypeScript code...',
140
+ * },
141
+ * ]
142
+ * })
143
+ * ```
144
+ */
145
+ template: true;
146
+ /**
147
+ * Specifies the path of the transformed module template.
148
+ */
149
+ output: string;
150
+ /**
151
+ * Specifies the content of the module template.
152
+ */
153
+ content: OutputOptions['intro'];
154
+ /**
155
+ * Specifies the format of the generated module template.
156
+ *
157
+ * @example
158
+ *
159
+ * ```ts
160
+ * export default defineConfig({
161
+ * entries: [
162
+ * {
163
+ * template: true,
164
+ * output: './dist/template.json',
165
+ * content: '{}',
166
+ * format: 'json',
167
+ * },
168
+ * ]
169
+ * })
170
+ * ```
171
+ *
172
+ * @default 'esm'
173
+ */
174
+ format?: string;
175
+ /**
176
+ * Specifies plugin options.
177
+ *
178
+ * @default undefined
179
+ */
180
+ plugins?: Pick<PluginsInput, 'esbuild'>;
181
+ }
182
+ type EntryOptions = EntryInput | EntryTypes | EntryTemplate;
86
183
 
87
184
  interface Options {
88
185
  /**
@@ -346,4 +443,4 @@ interface HooksOptions {
346
443
  declare const externals: RegExp[];
347
444
  declare function defineConfig(options: Options): Options;
348
445
 
349
- export { type BuildLogs, type BuildStats, type EntryBase, type EntryInput, type EntryOptions, type EntryTypes, type HooksOptions, type Options, type PluginsInput, type PluginsTypes, defineConfig, externals };
446
+ export { type BuildLogs, type BuildStats, type EntryBase, type EntryInput, type EntryOptions, type EntryTemplate, type EntryTypes, type HooksOptions, type Options, type PluginsInput, type PluginsTypes, defineConfig, externals };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",
@@ -71,8 +71,8 @@
71
71
  "@hypernym/eslint-config": "^2.0.2",
72
72
  "@hypernym/prettier-config": "^2.0.2",
73
73
  "@hypernym/tsconfig": "^1.1.0",
74
- "@types/node": "^20.8.7",
75
- "eslint": "^8.51.0",
74
+ "@types/node": "^20.8.9",
75
+ "eslint": "^8.52.0",
76
76
  "prettier": "^3.0.3",
77
77
  "tsx": "^3.14.0",
78
78
  "typescript": "^5.2.2"