1 | # Bundlib
|
2 |
|
3 | [![CircleCI](https://circleci.com/gh/manferlo81/bundlib.svg?style=svg)](https://circleci.com/gh/manferlo81/bundlib) [![dependabot](https://badgen.net/dependabot/manferlo81/bundlib?icon=dependabot)](https://github.com/manferlo81/bundlib) [![npm](https://badgen.net/npm/v/bundlib)](https://www.npmjs.com/package/bundlib) [![dependencies Status](https://david-dm.org/manferlo81/bundlib/status.svg)](https://david-dm.org/manferlo81/bundlib) [![devDependencies Status](https://david-dm.org/manferlo81/bundlib/dev-status.svg)](https://david-dm.org/manferlo81/bundlib?type=dev) [![install size](https://packagephobia.now.sh/badge?p=bundlib)](https://packagephobia.now.sh/result?p=bundlib) [![Known Vulnerabilities](https://snyk.io/test/github/manferlo81/bundlib/badge.svg?targetFile=package.json)](https://snyk.io/test/github/manferlo81/bundlib?targetFile=package.json) [![codecov](https://codecov.io/gh/manferlo81/bundlib/branch/master/graph/badge.svg)](https://codecov.io/gh/manferlo81/bundlib) [![License](https://badgen.net/badge/license/MIT/blue)](LICENSE)
|
4 |
|
5 | An automatic javascript library bundler using [Rollup.js](https://github.com/rollup/rollup) and optionally [Typescript](https://github.com/Microsoft/TypeScript).
|
6 |
|
7 | > :warning: Bundlib under active development, please [file a new issue](https://github.com/manferlo81/bundlib/issues) if you find any issue/bug, suggestions are welcome as well.
|
8 |
|
9 | ## In this guide
|
10 |
|
11 | * [Install](#install)
|
12 | * [First steps](#first-steps)
|
13 | * [Build](#build)
|
14 | * [Configuration](#configuration)
|
15 | * [CLI](#cli)
|
16 | * [API](#api)
|
17 | * [Types](#types-1)
|
18 | * [Known Issues](#known-issues)
|
19 | * [Features](#features)
|
20 |
|
21 | ## Install
|
22 |
|
23 | ```bash
|
24 | npm i -D bundlib
|
25 | ```
|
26 |
|
27 | ## First steps
|
28 |
|
29 | Bundlib will use `src/index.ts` as entry file for your library by default, it can be configured using the [`input`](#input) option. Add the corresponding scripts to your `package.json` and run them. [see below for CLI options](#cli).
|
30 |
|
31 | ## Build
|
32 |
|
33 | ### CommonJS module
|
34 |
|
35 | Building a `CommonJS Module` is as simple as adding a `"main"` field to your `package.json` pointing to the output file, and `bundlib` will build it for you. [see the configuration section](#configuration) for extra options.
|
36 |
|
37 | ### ES module
|
38 |
|
39 | To build a `ES Module` simply add a `"module"` field to your `package.json` pointing to the output file. [see the configuration section](#configuration) for extra options.
|
40 |
|
41 | ### IIFE, AMD and UMD build
|
42 |
|
43 | For `IIFE`, `AMD` or `UMD` builds, add a `"browser"` field to your `package.json`. The default format is `"umd"` but it can be changed to `"iife"` or `"amd"` using the [`format`](#format) or [`browser`](#browser) option, see the [configuration section](#configuration) for more info.
|
44 |
|
45 | ## Configuration
|
46 |
|
47 | Configuration is done through the `"bundlib"` field in your `package.json`. See the [list of options](#options) below.
|
48 |
|
49 | ***example***
|
50 |
|
51 | ```json
|
52 | // package.json
|
53 | {
|
54 | "version": "1.0.0",
|
55 | "name": "my-lib",
|
56 | "browser" : "dist/my-lib.amd.js",
|
57 | // ...
|
58 | "bundlib": {
|
59 | "format": "amd"
|
60 | }
|
61 | // ...
|
62 | }
|
63 | ```
|
64 |
|
65 | ### Options
|
66 |
|
67 | The `"bundlib"` field in `package.json` may contain any of the following properties. Any invalid or unknown option will cause `Bundlib` to throw at build time. Any option or sub-option set to `null` will be ignored.
|
68 |
|
69 | #### input
|
70 |
|
71 | ```typescript
|
72 | input: string | InputOptions;
|
73 |
|
74 | interface InputOptions {
|
75 | api?: string;
|
76 | bin?: string;
|
77 | }
|
78 |
|
79 | default {
|
80 | api: "src/index.ts";
|
81 | bin: "src-bin/index.ts";
|
82 | };
|
83 | ```
|
84 |
|
85 | The path to the file(s) to be used as entry point(s) for modules and binary. If a `string` is provided, it will be used as `API` entry point and `Binary` entry point will be set to the default value.
|
86 |
|
87 | #### sourcemap
|
88 |
|
89 | ```typescript
|
90 | sourcemap: boolean | "inline";
|
91 |
|
92 | default true;
|
93 | ```
|
94 |
|
95 | Whether or not to generate source maps. Anything other than `false` or `"inline"` will default to `true`.
|
96 |
|
97 | This option can be overridden using `per-build` options. See [`main`](#main), [`module`](#module), [`browser`](#browser) and [`bin`](#bin) options.
|
98 |
|
99 | #### esModule
|
100 |
|
101 | ```typescript
|
102 | esModule: BuildType | BuildType[] | boolean;
|
103 |
|
104 | type BuildType = "main" | "browser" | "min";
|
105 |
|
106 | default false;
|
107 | ```
|
108 |
|
109 | Whether or not to add a `__esModule: true` property to your `non ES Module` build. If `esModule = true` it will affect all supported builds.
|
110 |
|
111 | This option can be overridden using `per-build` options. See [`main`](#main), [`browser`](#browser) and [`bin`](#bin) options.
|
112 |
|
113 | #### interop
|
114 |
|
115 | ```typescript
|
116 | interop: BuildType | BuildType[] | boolean;
|
117 |
|
118 | type BuildType = "main" | "browser" | "min";
|
119 |
|
120 | default false;
|
121 | ```
|
122 |
|
123 | Whether or not to add an interop block. If `interop = true` it will affect all supported builds.
|
124 |
|
125 | This option can be overridden using `per-build` options. See [`main`](#main), [`browser`](#browser) and [`bin`](#bin) options.
|
126 |
|
127 | #### format
|
128 |
|
129 | ```typescript
|
130 | format: "iife" | "amd" | "umd";
|
131 |
|
132 | default "umd";
|
133 | ```
|
134 |
|
135 | Defines the format to be used for the `Browser` build.
|
136 |
|
137 | This option can be overridden using the [`browser`](#browser) option.
|
138 |
|
139 | #### name
|
140 |
|
141 | ```typescript
|
142 | name: string;
|
143 | ```
|
144 |
|
145 | The name to be used to expose your library to the global scope in a `IIFE` or `UMD` browser build. If not provided it will default to the camelcased, unscoped `"name"` field in `package.json` or the camelcased directory name. If none of those can be obtained, it will throw at build time.
|
146 |
|
147 | This option can be overridden using the [`browser`](#browser) option.
|
148 |
|
149 | #### id
|
150 |
|
151 | ```typescript
|
152 | id: string;
|
153 | ```
|
154 |
|
155 | Optional amd id for `AMD` or `UMD` build.
|
156 |
|
157 | This option can be overridden using the [`browser`](#browser) option.
|
158 |
|
159 | If not present, `AMD` module will be defined with no id.
|
160 |
|
161 | #### extend
|
162 |
|
163 | ```typescript
|
164 | extend: boolean;
|
165 |
|
166 | default false;
|
167 | ```
|
168 |
|
169 | Whether or not to extend the globally exposed name on a `IIFE` or `UMD` build.
|
170 |
|
171 | This option can be overridden using the [`browser`](#browser) option.
|
172 |
|
173 | #### globals
|
174 |
|
175 | ```typescript
|
176 | globals: { [name: string]: string } | string[];
|
177 |
|
178 | default {};
|
179 | ```
|
180 |
|
181 | Object or array to map names to globals in `Browser` build.
|
182 |
|
183 | This option can be overridden using the [`browser`](#browser) option.
|
184 |
|
185 | #### equals
|
186 |
|
187 | ```typescript
|
188 | equals: boolean;
|
189 |
|
190 | default false;
|
191 | ```
|
192 |
|
193 | Transforms type export for CommonJS module using `export = ...` instead of `export default ...`.
|
194 |
|
195 | This option can be overridden using the [`types`](#types) option.
|
196 |
|
197 | > :warning: *Note that this option should only be used when your library has a* `default` *export and no* `named` *exports, otherwise it may cause the type declarations to become invalid.*
|
198 |
|
199 | #### min
|
200 |
|
201 | ```typescript
|
202 | min: BuildType | BuildType[] | boolean;
|
203 |
|
204 | type BuildType = "main" | "module" | "browser" | "min";
|
205 |
|
206 | default false;
|
207 | ```
|
208 |
|
209 | Defines which files should be used to build an aditional minified version, if `true` will affect all modules. The minified file will be renamed from `*.ext` to `*.min.ext`. This option will override the default behavior of the [`--dev`, `-d` *cli option*](#-dev-d) , which means only the minified version will be actually minified, the normal version will **NOT** be minified even if you don't set the [`--dev`, `-d` cli option](#-dev-d).
|
210 |
|
211 | This option can be overridden using `per-build` options. See [`main`](#main), [`module`](#module), [`browser`](#browser) and [`bin`](#bin) options.
|
212 |
|
213 | #### cache
|
214 |
|
215 | ```typescript
|
216 | cache: string;
|
217 |
|
218 | default "node_modules/.cache/bundlib"
|
219 | ```
|
220 |
|
221 | Defines the directory to be used for cache, relative to the project root.
|
222 |
|
223 | #### main
|
224 |
|
225 | ```typescript
|
226 | main: CommonJSOptions | false;
|
227 |
|
228 | interface CommonJSOptions {
|
229 | sourcemap?: boolean | "inline";
|
230 | esModule?: boolean | null;
|
231 | interop?: boolean | null;
|
232 | min?: boolean | null;
|
233 | }
|
234 | ```
|
235 |
|
236 | Specific options to be used in the `CommonJS` build. They will override any corresponding option set in the top-level options. See [`sourcemap`](#sourcemap), [`esModule`](#esmodule), [`interop`](#interop) and [`min`](#min) options.
|
237 |
|
238 | If it's set to `false`, it will prevent `CommonJS` build altogether, even if there is a `"main"` field in `package.json`.
|
239 |
|
240 | #### module
|
241 |
|
242 | ```typescript
|
243 | module: ESModuleOptions | false;
|
244 |
|
245 | interface ESModuleOptions {
|
246 | sourcemap?: boolean | "inline";
|
247 | min?: boolean;
|
248 | }
|
249 | ```
|
250 |
|
251 | Specific options to be used in the `ES Module` build. They will override any corresponding option set in the top-level options. See [`sourcemap`](#sourcemap) and [`min`](#min) options.
|
252 |
|
253 | If it's set to `false`, it will prevent `ES Module` build altogether, even if there is a `"module"` or `"jsnext:main"` field in `package.json`.
|
254 |
|
255 | #### browser
|
256 |
|
257 | ```typescript
|
258 | browser: BrowserOptions | false;
|
259 |
|
260 | interface BrowserOptions {
|
261 | sourcemap?: boolean | "inline";
|
262 | esModule?: boolean;
|
263 | interop?: boolean;
|
264 | min?: boolean;
|
265 | format?: "iife" | "amd" | "umd" ;
|
266 | name?: string;
|
267 | id?: string;
|
268 | extend?: boolean;
|
269 | globals?: { [name: string]: string } | string[];
|
270 | }
|
271 | ```
|
272 |
|
273 | Specific options to be used in the `Browser` build. They will override any corresponding option set in the top-level options. See [`sourcemap`](#sourcemap), [`esModule`](#esmodule), [`interop`](#interop), [`min`](#min), [`format`](#format), [`name`](#name), [`id`](#id), [`extend`](#extend) and [`globals`](#globals) options.
|
274 |
|
275 | If it's set to* `false`, it will prevent `Browser` build altogether, even if there is a `"browser"` field in `package.json`.
|
276 |
|
277 | #### bin
|
278 |
|
279 | ```typescript
|
280 | bin: CommonJSOptions | false;
|
281 |
|
282 | interface CommonJSOptions {
|
283 | sourcemap?: boolean | "inline";
|
284 | esModule?: boolean;
|
285 | interop?: boolean;
|
286 | min?: boolean;
|
287 | }
|
288 | ```
|
289 |
|
290 | Specific options to be used in `Binary` build. They will override any corresponding option set in the top-level options. See [`sourcemap`](#sourcemap), [`esModule`](#esmodule), [`interop`](#interop) and [`min`](#min) options.
|
291 |
|
292 | If it's set to `false`, it will prevent `Binary` build altogether, even if there is a `"bin"` field in `package.json`.
|
293 |
|
294 | > :warning: *This option was used to set entry point for* `Binary` *build. For compatibility reasons it still works if you set this option as* `string`*. This behavior will be removed in the future and therefore should not be used. Use* [`input`](#input) *option instead.*
|
295 |
|
296 | #### types
|
297 |
|
298 | ```typescript
|
299 | types: TypesOptions | false;
|
300 |
|
301 | interface TypesOptions {
|
302 | equals: boolean;
|
303 | }
|
304 | ```
|
305 |
|
306 | Specific options to be used for types generation. They will override any corresponding option set in the top-level options. See [`equals`](#equals) option.
|
307 |
|
308 | If it's set to `false`, it will prevent type declarations generation altogether, even if there is a `"types"` or `"typings"` field in `package.json`.
|
309 |
|
310 | ## CLI
|
311 |
|
312 | ```bash
|
313 | bundlib [options]
|
314 | ```
|
315 |
|
316 | ### CLI Options
|
317 |
|
318 | Combine options according to your needs. Run `bundlib --help` or `bundlib -h` for a detailed help.
|
319 |
|
320 | #### `--dev`, `-d`
|
321 |
|
322 | Create development, not minified builds. Builds affected by the [`min`](#min) option will ignore this option.
|
323 |
|
324 | #### `--watch`, `-w`
|
325 |
|
326 | Run `bundlib` in watch mode.
|
327 |
|
328 | #### `--silent`, `-s`
|
329 |
|
330 | Prevent messages from showing in the console.
|
331 |
|
332 | #### `--version`, `-v`
|
333 |
|
334 | Show `bundlib` version.
|
335 |
|
336 | #### `--help`, `-h`
|
337 |
|
338 | Show detailed help about the CLI tool.
|
339 |
|
340 | ## API
|
341 |
|
342 | ***example***
|
343 |
|
344 | ```javascript
|
345 | // rollup.config.js
|
346 |
|
347 | import { configsFromPkg } from "bundlib";
|
348 |
|
349 | const dev = !process.env.production;
|
350 |
|
351 | export default configsFromPkg(
|
352 | process.cwd(),
|
353 | { dev },
|
354 | );
|
355 | ```
|
356 |
|
357 | ### analizePkg
|
358 |
|
359 | ```typescript
|
360 | function analizePkg(
|
361 | cwd: string,
|
362 | pkg: PkgJson = read(cwd + "/package.json"),
|
363 | ): Promise<PkgAnalized>;
|
364 | ```
|
365 |
|
366 | Analizes `package.json` and returns a `Promise` that resolves to useful normalized information, [*see* `PkgAnalized`](#pkganalized). If `pkg` not provided it will be read from the current working directory `cwd`.
|
367 |
|
368 | ### configsFromPkg
|
369 |
|
370 | ```typescript
|
371 | function configsFromPkg(
|
372 | cwd: string,
|
373 | options: { dev? boolean, watch?: boolean } | null | false,
|
374 | pkg: PkgJson = read(cwd + "/package.json"),
|
375 | ): Promise<RollupOptions[]>;
|
376 | ```
|
377 |
|
378 | Returns a `Promise` that resolves to an array of Rollup configs based on the content of `package.json`. If `pkg` not provided it will be read from the current working directory `cwd`.
|
379 |
|
380 | ## Types
|
381 |
|
382 | ### PkgAnalized
|
383 |
|
384 | ```typescript
|
385 | interface PkgAnalized {
|
386 | cwd: string;
|
387 | pkg: PkgJson;
|
388 | input: {
|
389 | api: string;
|
390 | bin: string;
|
391 | };
|
392 | output: {
|
393 | main: CommonJSBuildOptions | null;
|
394 | module: ESModuleBuildOptions | null;
|
395 | browser: BrowserBuildOptions | null;
|
396 | bin: CommonJSBuildOptions | null;
|
397 | types: TypesBuildOptions | null;
|
398 | };
|
399 | dependencies: {
|
400 | runtime: string[] | null;
|
401 | peer: string[] | null;
|
402 | optional: string[] | null;
|
403 | };
|
404 | cache: string;
|
405 | }
|
406 | ```
|
407 |
|
408 | *see also:* [`CommonJSBuildOptions`](#commonjsbuildoptions), [`ESModuleBuildOptions`](#esmodulebuildoptions), [`BrowserBuildOptions`](#browserbuildoptions) & [`TypesBuildOptions`](#typesbuildoptions)
|
409 |
|
410 | ### CommonJSBuildOptions
|
411 |
|
412 | ```typescript
|
413 | interface CommonJSBuildOptions {
|
414 | path: string;
|
415 | sourcemap: boolean | "inline";
|
416 | esModule: boolean;
|
417 | interop: boolean;
|
418 | min: boolean;
|
419 | }
|
420 | ```
|
421 |
|
422 | ### ESModuleBuildOptions
|
423 |
|
424 | ```typescript
|
425 | interface ESModuleBuildOptions {
|
426 | path: string;
|
427 | sourcemap: boolean | "inline";
|
428 | min: boolean;
|
429 | }
|
430 | ```
|
431 |
|
432 | ### BrowserBuildOptions
|
433 |
|
434 | ```typescript
|
435 | interface BrowserBuildOptions {
|
436 | path: string;
|
437 | sourcemap: boolean | "inline";
|
438 | esModule: boolean;
|
439 | interop: boolean;
|
440 | min: boolean;
|
441 | format: "iife" | "amd" | "umd";
|
442 | name: string | null;
|
443 | id: string | null;
|
444 | globals: Record<string, string> | null;
|
445 | extend: boolean;
|
446 | }
|
447 | ```
|
448 |
|
449 | ### TypesBuildOptions
|
450 |
|
451 | ```typescript
|
452 | interface TypesBuildOptions {
|
453 | path: string;
|
454 | equals: boolean;
|
455 | }
|
456 | ```
|
457 |
|
458 | ## Known Issues
|
459 |
|
460 | * [Issue #7](https://github.com/manferlo81/bundlib/issues/7)
|
461 |
|
462 | ## Features
|
463 |
|
464 | * Creates a `CommonJS Module` build based on the `"main"` field in your `package.json`
|
465 | * Creates an `ES Module` build based on the `"module"` or `"jsnext:main"` field in your `package.json`
|
466 | * Creates a `Browser` build based on the `"browser"` field in your `package.json`
|
467 | * Creates an CLI `Binary` build based on the `"bin"` field in your `package.json`
|
468 | * Exports type declarations based on the `"types"` or `"typings"` field in your `package.json`
|
469 | * Skip any build based on options
|
470 | * Sets `"dependencies"`, `"peerDependencies"` and `"optionalDependencies"` as external for `CommonJS Module`, `ES Module` and `Binary` builds
|
471 | * Uses the user copy of `typescript` if installed
|
472 | * Uses `chokidar` if installed
|
473 | * Importing an internal file from a package `Ex: lodash/core` will be treated as external if `lodash` is included in your `"dependencies"`, `peerDependencies` or `optionalDependencies`
|
474 | * Transforms `async/await` using [`Babel`](https://babeljs.io) and [`babel-plugin-transform-async-to-promises`](https://github.com/rpetrich/babel-plugin-transform-async-to-promises) for ES5 support
|
475 | * Dynamic Import support through [`@babel/plugin-syntax-dynamic-import`](https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import)
|
476 | * Tranforms `Object.assign` using [`@babel/plugin-transform-object-assign`](https://babeljs.io/docs/en/babel-plugin-transform-object-assign)
|
477 | * `React JSX` support through [`@babel/preset-react`](https://babeljs.io/docs/en/next/babel-preset-react)
|
478 | * Uses [`@babel/preset-env`](https://babeljs.io/docs/en/next/babel-preset-env)
|
479 | * Minifies build using [`Terser`](https://github.com/terser-js/terser)
|
480 |
|
481 | ## License
|
482 |
|
483 | [MIT](LICENSE) © [Manuel Fernández](https://github.com/manferlo81)
|