@hypernym/bundler 0.2.0 → 0.3.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.
@@ -25,7 +25,7 @@ const externals = [
25
25
  ];
26
26
 
27
27
  const name = "bundler";
28
- const version = `0.2.0`;
28
+ const version = `0.3.0`;
29
29
 
30
30
  const cl = console.log;
31
31
  const logger = {
@@ -212,13 +212,13 @@ async function build(cwd, options) {
212
212
  _format = "cjs";
213
213
  const output = entry.output || _output;
214
214
  const format = entry.format || _format;
215
- const defaultPlugins = [esbuild(plugins?.esbuild)];
215
+ const _plugins = [esbuild(plugins?.esbuild)];
216
216
  if (plugins?.json) {
217
217
  const jsonOptions = isObject(plugins.json) ? plugins.json : void 0;
218
- defaultPlugins.push(jsonPlugin(jsonOptions));
218
+ _plugins.push(jsonPlugin(jsonOptions));
219
219
  }
220
220
  if (plugins?.replace) {
221
- defaultPlugins.unshift(
221
+ _plugins.unshift(
222
222
  replacePlugin({
223
223
  true: true,
224
224
  ...plugins.replace
@@ -227,12 +227,20 @@ async function build(cwd, options) {
227
227
  }
228
228
  if (plugins?.resolve) {
229
229
  const resolveOptions = isObject(plugins.resolve) ? plugins.resolve : void 0;
230
- defaultPlugins.unshift(resolvePlugin(resolveOptions));
230
+ _plugins.unshift(resolvePlugin(resolveOptions));
231
+ }
232
+ if (hooks?.["rollup:plugins"]) {
233
+ hooks["rollup:plugins"](_plugins, {
234
+ ...entry,
235
+ input,
236
+ output,
237
+ format
238
+ });
231
239
  }
232
240
  const builder = await rollup({
233
241
  input: resolve(cwd, input),
234
242
  external: externals || options.externals,
235
- plugins: defaultPlugins,
243
+ plugins: _plugins,
236
244
  onLog: (level, log) => {
237
245
  if (logFilter(log))
238
246
  buildLogs.push({ level, log });
@@ -263,10 +271,19 @@ async function build(cwd, options) {
263
271
  _format = "cjs";
264
272
  const output = entry.output || _output;
265
273
  const format = entry.format || _format;
274
+ const _plugins = [dts(plugins?.dts)];
275
+ if (hooks?.["rollup:plugins"]) {
276
+ hooks["rollup:plugins"](_plugins, {
277
+ ...entry,
278
+ types,
279
+ output,
280
+ format
281
+ });
282
+ }
266
283
  const builder = await rollup({
267
284
  input: resolve(cwd, types),
268
285
  external: externals || options.externals,
269
- plugins: [dts(plugins?.dts)],
286
+ plugins: _plugins,
270
287
  onLog: (level, log) => {
271
288
  if (logFilter(log))
272
289
  buildLogs.push({ level, log });
@@ -1,4 +1,4 @@
1
- import { OutputOptions, LogLevel, RollupLog } from 'rollup';
1
+ import { OutputOptions, LogLevel, RollupLog, Plugin } from 'rollup';
2
2
  import { RollupReplaceOptions } from '@rollup/plugin-replace';
3
3
  import { RollupJsonOptions } from '@rollup/plugin-json';
4
4
  import { RollupNodeResolveOptions } from '@rollup/plugin-node-resolve';
@@ -13,7 +13,7 @@ interface BuildPlugins {
13
13
  replace?: RollupReplaceOptions;
14
14
  }
15
15
 
16
- interface Entry {
16
+ interface EntryBase {
17
17
  /**
18
18
  * Specifies the path of the transformed module.
19
19
  *
@@ -56,7 +56,7 @@ interface Entry {
56
56
  */
57
57
  logFilter?: string[];
58
58
  }
59
- interface EntryInput extends Entry {
59
+ interface EntryInput extends EntryBase {
60
60
  /**
61
61
  * Specifies the path of the module's build source.
62
62
  */
@@ -68,7 +68,7 @@ interface EntryInput extends Entry {
68
68
  */
69
69
  plugins?: BuildPlugins;
70
70
  }
71
- interface EntryTypes extends Entry {
71
+ interface EntryTypes extends EntryBase {
72
72
  /**
73
73
  * Specifies the path of the module's build source that contains only TS definitions.
74
74
  */
@@ -80,21 +80,48 @@ interface EntryTypes extends Entry {
80
80
  */
81
81
  plugins?: Pick<BuildPlugins, 'dts'>;
82
82
  }
83
- type EntriesOptions = EntryInput | EntryTypes;
83
+ type EntryOptions = EntryInput | EntryTypes;
84
84
 
85
85
  interface BuildLogs {
86
86
  level: LogLevel;
87
87
  log: RollupLog;
88
88
  }
89
89
  interface BuildStats {
90
+ /**
91
+ * The root path of the project.
92
+ */
90
93
  cwd: string;
94
+ /**
95
+ * Final bundle size.
96
+ */
91
97
  size: number;
98
+ /**
99
+ * Total bundle build time.
100
+ */
92
101
  buildTime: number;
102
+ /**
103
+ * List of generated bundle modules.
104
+ */
93
105
  files: {
106
+ /**
107
+ * Module output path.
108
+ */
94
109
  path: string;
110
+ /**
111
+ * Module size.
112
+ */
95
113
  size: number;
114
+ /**
115
+ * Build time of individual module.
116
+ */
96
117
  buildTime: number;
118
+ /**
119
+ * Module format.
120
+ */
97
121
  format: string;
122
+ /**
123
+ * List of warnings from build plugins.
124
+ */
98
125
  logs: BuildLogs[];
99
126
  }[];
100
127
  }
@@ -102,18 +129,105 @@ interface BuildStats {
102
129
  interface HooksOptions {
103
130
  /**
104
131
  * Called just before bundling started.
132
+ *
133
+ * @example
134
+ *
135
+ * ```ts
136
+ * export default defineConfig({
137
+ * hooks: {
138
+ * 'bundle:start': async (options) => {
139
+ * // ...
140
+ * }
141
+ * }
142
+ * })
143
+ * ```
144
+ *
145
+ * @default undefined
105
146
  */
106
147
  'bundle:start'?: (options?: Options) => void | Promise<void>;
107
148
  /**
108
149
  * Called just before building started.
150
+ *
151
+ * @example
152
+ *
153
+ * ```ts
154
+ * export default defineConfig({
155
+ * hooks: {
156
+ * 'build:start': async (options, buildStats) => {
157
+ * // ...
158
+ * }
159
+ * }
160
+ * })
161
+ * ```
162
+ *
163
+ * @default undefined
109
164
  */
110
165
  'build:start'?: (options?: Options, buildStats?: BuildStats) => void | Promise<void>;
166
+ /**
167
+ * Called just before the initialization of the Rollup plugin.
168
+ *
169
+ * Provides the ability to add and manipulate custom plugins.
170
+ *
171
+ * @example
172
+ *
173
+ * ```ts
174
+ * import { plugin1, plugin2, plugin3 } from './plugins'
175
+ *
176
+ * export default defineConfig({
177
+ * hooks: {
178
+ * 'rollup:plugins': (plugins, entry) => {
179
+ * // adds a custom plugin before the default bundler plugins
180
+ * plugins.unshift(plugin1())
181
+ * // adds a custom plugin after the default bundler plugins
182
+ * plugins.push(plugin2())
183
+ * // adds a custom plugin for a specific entry only
184
+ * if (entry?.input?.includes('./src/index.ts')) {
185
+ * plugins.push(plugin3())
186
+ * }
187
+ * // returns the final list of plugins
188
+ * return plugins
189
+ * }
190
+ * }
191
+ * })
192
+ * ```
193
+ *
194
+ * @default undefined
195
+ */
196
+ 'rollup:plugins'?: (plugins: Plugin[], entry?: Partial<EntryInput> & Partial<Omit<EntryTypes, 'plugins'>>) => Plugin[];
111
197
  /**
112
198
  * Called right after building is complete.
199
+ *
200
+ * @example
201
+ *
202
+ * ```ts
203
+ * export default defineConfig({
204
+ * hooks: {
205
+ * 'build:end': async (options, buildStats) => {
206
+ * // ...
207
+ * }
208
+ * }
209
+ * })
210
+ * ```
211
+ *
212
+ * @default undefined
113
213
  */
114
214
  'build:end'?: (options?: Options, buildStats?: BuildStats) => void | Promise<void>;
115
215
  /**
116
216
  * Called right after bundling is complete.
217
+ *
218
+ * @example
219
+ *
220
+ * ```ts
221
+ * export default defineConfig({
222
+ * hooks: {
223
+ * 'bundle:end': async (options) => {
224
+ * // ...
225
+ * }
226
+ * }
227
+ * })
228
+ * ```
229
+ *
230
+ * @default undefined
117
231
  */
118
232
  'bundle:end'?: (options?: Options) => void | Promise<void>;
119
233
  }
@@ -123,8 +237,20 @@ interface Options {
123
237
  * Specifies the bundle's entry points.
124
238
  *
125
239
  * It allows you to manually set all build entries and adjust options for each one individually.
240
+ *
241
+ * @example
242
+ *
243
+ * ```ts
244
+ * export default defineConfig({
245
+ * entries: [
246
+ * { input: './src/index.ts' }, // => './dist/index.mjs'
247
+ * { types: './src/types.ts' }, // => './dist/types.d.ts'
248
+ * // ...
249
+ * ]
250
+ * })
251
+ * ```
126
252
  */
127
- entries: EntriesOptions[];
253
+ entries: EntryOptions[];
128
254
  /**
129
255
  * Specifies the output directory for production bundle.
130
256
  *
@@ -145,11 +271,41 @@ interface Options {
145
271
  /**
146
272
  * Provides a powerful hooking system to further expand bundling mode.
147
273
  *
274
+ * @example
275
+ *
276
+ * ```ts
277
+ * export default defineConfig({
278
+ * hooks: {
279
+ * 'build:end': async (options, buildStats) => {
280
+ * // ...
281
+ * }
282
+ * }
283
+ * })
284
+ * ```
285
+ *
148
286
  * @default undefined
149
287
  */
150
288
  hooks?: HooksOptions;
151
289
  }
152
290
 
291
+ /**
292
+ * List of global defaults for externals.
293
+ *
294
+ * @example
295
+ *
296
+ * ```ts
297
+ * import { externals } from '@hypernym/bundler'
298
+ *
299
+ * export default defineConfig({
300
+ * entries: [
301
+ * {
302
+ * input: './src/index.ts',
303
+ * externals: [...externals, 'id', /regexp/]
304
+ * },
305
+ * ]
306
+ * })
307
+ * ```
308
+ */
153
309
  declare const externals: RegExp[];
154
310
  declare function defineConfig(options: Options): Options;
155
311
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",
@@ -62,9 +62,9 @@
62
62
  "@hypernym/utils": "^2.1.0",
63
63
  "@rollup/plugin-json": "^6.0.1",
64
64
  "@rollup/plugin-node-resolve": "^15.2.3",
65
- "@rollup/plugin-replace": "^5.0.3",
65
+ "@rollup/plugin-replace": "^5.0.4",
66
66
  "esbuild": "^0.19.4",
67
- "rollup": "^4.1.1",
67
+ "rollup": "^4.1.4",
68
68
  "rollup-plugin-dts": "^6.1.0"
69
69
  },
70
70
  "devDependencies": {