UNPKG

18.8 kBMarkdownView Raw
1# Bundlib
2
3[![CircleCI](https://circleci.com/gh/manferlo81/bundlib.svg?style=svg)](https://circleci.com/gh/manferlo81/bundlib) [![dependabot](https://api.dependabot.com/badges/status?host=github&repo=manferlo81/bundlib)](https://dependabot.com) [![npm](https://badgen.net/npm/v/bundlib)](https://www.npmjs.com/package/bundlib) [![codecov](https://codecov.io/gh/manferlo81/bundlib/branch/master/graph/badge.svg)](https://codecov.io/gh/manferlo81/bundlib) [![dependencies](https://badgen.net/david/dep/manferlo81/bundlib)](https://david-dm.org/manferlo81/bundlib) [![dev dependencies](https://badgen.net/david/dev/manferlo81/bundlib)](https://david-dm.org/manferlo81/bundlib?type=dev) [![packagephobia](https://badgen.net/packagephobia/install/bundlib)](https://packagephobia.now.sh/result?p=bundlib) [![types](https://img.shields.io/npm/types/bundlib.svg)](https://github.com/microsoft/typescript) [![Known Vulnerabilities](https://snyk.io/test/npm/bundlib/badge.svg)](https://snyk.io/test/npm/bundlib) [![license](https://badgen.net/github/license/manferlo81/bundlib)](LICENSE)
4
5An automatic configuration manager for [Rollup.js](https://github.com/rollup/rollup).
6
7> :warning: Bundlib is under active development, please [file a new issue](https://github.com/manferlo81/bundlib/issues) if you find any issue or bug, suggestions are welcome as well.
8
9## BREAKING CHANGES in version 0.15.x
10
11* [Rollup](https://github.com/rollup/rollup) has to be installed separately
12* `analizePkg` paths are not longer resolved
13* find input files and default to `index.js` instead of `index.ts`
14
15## In this guide
16
17* [Install](#install)
18* [First steps](#first-steps)
19* [Build](#build)
20* [Configuration](#configuration)
21* [Supported Plugins](#supported-plugins)
22* [CLI](#cli)
23* [API](#api)
24* [Types](#types-1)
25* [Known Issues](#known-issues)
26* [Features](#features)
27
28## Install
29
30```bash
31npm i -D bundlib
32```
33
34> :warning: Rollup is a peer dependency and it has to be installed as well.
35
36## First steps
37
38Bundlib will use `src/index.ts` or `src/index.tsx` as entry file for your library if you are using `typescript` (see Plugins for more information) or `src/index.js` otherwise, it can be configured using the [`input`](#input) option.
39
40Add the corresponding scripts to your `package.json` and run them. [see below for CLI options](#cli).
41
42## Build
43
44### CommonJS module
45
46Building a `CommonJS Module` is as simple as adding a `"main"` field to your `package.json` pointing to the output file, [see the configuration section](#configuration) for extra options.
47
48### ES module
49
50To 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.
51
52### IIFE, AMD and UMD build
53
54For `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.
55
56## Configuration
57
58### Automatic Configuration
59
60Bundlib will try to configure Rollup according to you `package.json` data, other advanced options can be set using `"bundlib"` field in your `package.json`, see [Advanced Configuration](#advanced-configuration) for more information.
61
62#### "main"
63
64The `"main"` field will be used as your CommonJS Module output, if not present `CommonJS Module` build will be skipped. You can skip the build manually using the [`"main"`](#main) advanced option.
65
66#### "module" or "jsnext:main"
67
68The `"module"` field will be used as your `ES Module` output, if not present `ES Module` build will be skipped. You can skip the build manually using the [`"module"`](#module) advanced option.`"jsnext:main"` field will also be honored if `"module"` field is not present, but it is recommended to use the `"module"` field.
69
70#### "browser"
71
72The `"browser"` field will be used as your `Browser` build output, if not present `Browser` build will be skipped. You can skip the build manually using the [`"browser"`](#browser) advanced option. `Bundlib` only supports `string` type `"browser"` field, it will throw otherwise.
73
74#### "bin"
75
76The `"bin"` field will be used as your `Binary` build output, if not present `Binary` build will be skipped. You can skip the build manually using the [`"bin"`](#bin) advanced option. `Bundlib` only supports `string` type `"bin"` field, it will throw otherwise.
77
78#### "types" or "typings"
79
80The `"types"` field will be used as your types output if you are using `typescript`. You can skip types generation using the [`"types"`](#types) advanced option. `"typings"` field will also be honored if `"types"` field is not present.
81
82#### "dependencies"
83
84The `"dependencies"` field will be used to detect installed packages, it will also be added as external dependencies for your CommonJS Module, `ES Module`, and Binary builds, for Browser build dependencies will be bundled into the output unless otherwise specified using the [`"globals"`](#globals) advanced option.
85
86#### "devDependencies"
87
88The `"devDependencies"` field will be used to detect installed packages.
89
90#### "peerDependencies"
91
92The `"peerDependencies"` field will be used as external dependencies for your CommonJS Module, ES Module, and Binary builds.
93
94#### "bundlib"
95
96The `"bundlib"` field will be used for advanced configuration, see [Advanced Configuration](#advanced-configuration) for more information.
97
98### Advanced Configuration
99
100Advanced configuration is done through the `"bundlib"` field in your `package.json`. See the [list of options](#options) below.
101
102***example***
103
104```javascript
105// package.json
106{
107 "version": "1.0.0",
108 "name": "my-lib",
109 "browser" : "dist/my-lib.amd.js",
110 // ...
111 "bundlib": {
112 "format": "amd"
113 }
114 // ...
115}
116```
117
118### Options
119
120The `"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.
121
122#### input
123
124```typescript
125input: string | InputOptions;
126
127interface InputOptions {
128 api?: string;
129 bin?: string;
130}
131
132default {
133 api: "src/index.js";
134 bin: "src-bin/index.js";
135};
136```
137
138The path to the file (or files) to be used as entry point(s) for `API` and `Binary` modules. If a `string` is provided, it will be used as `API` entry point and `Binary` entry point will be set to the default value.
139
140#### sourcemap
141
142```typescript
143sourcemap: boolean | "inline";
144
145default true;
146```
147
148Whether or not to generate source maps. Anything other than `false` or `"inline"` will default to `true`.
149
150This option can be overridden using `per-build` options. See [`main`](#main), [`module`](#module), [`browser`](#browser) and [`bin`](#bin) options.
151
152#### esModule
153
154```typescript
155esModule: BuildType | BuildType[] | boolean;
156
157type BuildType = "main" | "browser" | "min";
158
159default false;
160```
161
162Whether or not to add a `__esModule: true` property to your `non ES Module` build. If `esModule = true` it will affect all supported builds.
163
164This option can be overridden using `per-build` options. See [`main`](#main), [`browser`](#browser) and [`bin`](#bin) options.
165
166#### interop
167
168```typescript
169interop: BuildType | BuildType[] | boolean;
170
171type BuildType = "main" | "browser" | "min";
172
173default false;
174```
175
176Whether or not to add an interop block. If `interop = true` it will affect all supported builds.
177
178This option can be overridden using `per-build` options. See [`main`](#main), [`browser`](#browser) and [`bin`](#bin) options.
179
180#### format
181
182```typescript
183format: "iife" | "amd" | "umd";
184
185default "umd";
186```
187
188Defines the format to be used for the `Browser` build.
189
190This option can be overridden using the [`browser`](#browser) option.
191
192#### name
193
194```typescript
195name: string;
196```
197
198The 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.
199
200This option can be overridden using the [`browser`](#browser) option.
201
202#### id
203
204```typescript
205id: string;
206```
207
208Optional amd id for `AMD` or `UMD` build.
209
210This option can be overridden using the [`browser`](#browser) option.
211
212If not present, `AMD` module will be defined with no id.
213
214#### extend
215
216```typescript
217extend: boolean;
218
219default false;
220```
221
222Whether or not to extend the globally exposed name on a `IIFE` or `UMD` build.
223
224This option can be overridden using the [`browser`](#browser) option.
225
226#### globals
227
228```typescript
229globals: { [name: string]: string } | string[];
230
231default {};
232```
233
234Object or array to map names to globals in `Browser` build.
235
236This option can be overridden using the [`browser`](#browser) option.
237
238#### equals
239
240```typescript
241equals: boolean;
242
243default false;
244```
245
246> As of version `0.15.2` this option has no effect, It won't throw to keep compatibility with previous version but it will be ignored. Install [`rollup-plugin-export-equals` Plugin](#supported-plugins) instead.
247
248Transforms type export for `CommonJS Module` using `export = ...` instead of `export default ...`.
249
250This option can be overridden using the [`types`](#types) option.
251
252> :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.*
253
254#### min
255
256```typescript
257min: BuildType | BuildType[] | boolean;
258
259type BuildType = "main" | "module" | "browser" | "min";
260
261default false;
262```
263
264Defines 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).
265
266This option can be overridden using `per-build` options. See [`main`](#main), [`module`](#module), [`browser`](#browser) and [`bin`](#bin) options.
267
268#### cache
269
270```typescript
271cache: string;
272
273default "node_modules/.cache/bundlib"
274```
275
276Defines the directory to be used for cache, relative to the project root.
277
278#### project
279
280```typescript
281cache: string;
282
283default "tsconfig.json"
284```
285
286Defines the location of typescript `tsconfig.json` file.
287
288#### main
289
290```typescript
291main: CommonJSOptions | false;
292
293interface CommonJSOptions {
294 sourcemap?: boolean | "inline";
295 esModule?: boolean | null;
296 interop?: boolean | null;
297 min?: boolean | null;
298}
299```
300
301Specific 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.
302
303If it's set to `false`, it will prevent `CommonJS` build altogether, even if there is a `"main"` field in `package.json`.
304
305#### module
306
307```typescript
308module: ESModuleOptions | false;
309
310interface ESModuleOptions {
311 sourcemap?: boolean | "inline";
312 min?: boolean;
313}
314```
315
316Specific 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.
317
318If 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`.
319
320#### browser
321
322```typescript
323browser: BrowserOptions | false;
324
325interface BrowserOptions {
326 sourcemap?: boolean | "inline";
327 esModule?: boolean;
328 interop?: boolean;
329 min?: boolean;
330 format?: "iife" | "amd" | "umd" ;
331 name?: string;
332 id?: string;
333 extend?: boolean;
334 globals?: { [name: string]: string } | string[];
335}
336```
337
338Specific 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.
339
340If it's set to* `false`, it will prevent `Browser` build altogether, even if there is a `"browser"` field in `package.json`.
341
342#### bin
343
344```typescript
345bin: CommonJSOptions | false;
346
347interface CommonJSOptions {
348 sourcemap?: boolean | "inline";
349 esModule?: boolean;
350 interop?: boolean;
351 min?: boolean;
352}
353```
354
355Specific 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.
356
357If it's set to `false`, it will prevent `Binary` build altogether, even if there is a `"bin"` field in `package.json`.
358
359#### types
360
361```typescript
362types: TypesOptions | false;
363
364interface TypesOptions {
365 equals: boolean;
366}
367```
368
369Specific options to be used for types generation. They will override any corresponding option set in the top-level options. See [`equals`](#equals) option.
370
371If it's set to `false`, it will prevent type declarations generation altogether, even if there is a `"types"` or `"typings"` field in `package.json`.
372
373## Supported Plugins
374
375Any of the following plugins will be automatically configured if they are installed as `"dependencies"` or `"devDependencies"` in `package.json`.
376
377* [`rollup-plugin-eslint`](https://github.com/TrySound/rollup-plugin-eslint)
378* [`rollup-plugin-typescript2`](https://github.com/ezolenko/rollup-plugin-typescript2)
379* [`@rollup/plugin-typescript`](https://github.com/rollup/plugins/tree/master/packages/typescript)
380* [`rollup-plugin-babel`](https://github.com/rollup/rollup-plugin-babel)
381* [`@rollup/plugin-buble`](https://github.com/rollup/plugins/tree/master/packages/buble)
382* [`@rollup/plugin-node-resolve`](https://github.com/rollup/plugins/tree/master/packages/node-resolve)
383* [`@rollup/plugin-commonjs`](https://github.com/rollup/plugins/tree/master/packages/commonjs)
384* [`@rollup/plugin-json`](https://github.com/rollup/plugins/tree/master/packages/json)
385* [`rollup-plugin-terser`](https://github.com/TrySound/rollup-plugin-terser)
386* [`rollup-plugin-strip-shebang`](https://github.com/manferlo81/rollup-plugin-strip-shebang)
387* [`rollup-plugin-add-shebang`](https://github.com/ls-age/rollup-plugin-add-shebang)
388* [`rollup-plugin-export-equals`](https://github.com/manferlo81/rollup-plugin-export-equals)
389
390The following plugins will be supported in the future.
391
392* [`rollup-plugin-prettier`](https://github.com/mjeanroy/rollup-plugin-prettier)
393* [`rollup-plugin-preserve-shebang`](https://github.com/developit/rollup-plugin-preserve-shebang)
394
395Any plugin suggestion will be well received, please [file a new issue](https://github.com/manferlo81/bundlib/issues).
396
397## CLI
398
399```bash
400bundlib [options]
401```
402
403### CLI Options
404
405Combine options according to your needs. Run `bundlib --help` or `bundlib -h` for a detailed help.
406
407#### `--dev`, `-d`
408
409Create development, not minified builds. Builds affected by the [`min`](#min) option will ignore this option.
410
411#### `--watch`, `-w`
412
413Run `bundlib` in watch mode.
414
415#### `--silent`, `-s`
416
417Prevent messages from showing in the console.
418
419#### `--version`, `-v`
420
421Show `bundlib` version.
422
423#### `--help`, `-h`
424
425Show detailed help about the CLI tool.
426
427## API
428
429***example***
430
431```javascript
432// rollup.config.js
433
434import { configsFromPkg } from "bundlib";
435
436const dev = !process.env.production;
437
438export default configsFromPkg(
439 process.cwd(),
440 { dev },
441);
442```
443
444### analizePkg
445
446```typescript
447function analizePkg(
448 cwd: string,
449 pkg: PkgJson = read(cwd + "/package.json"),
450): Promise<PkgAnalized>;
451```
452
453Analizes `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`.
454
455### configsFromPkg
456
457```typescript
458function configsFromPkg(
459 cwd: string,
460 options: { dev? boolean, watch?: boolean } | null | false,
461 pkg: PkgJson = read(cwd + "/package.json"),
462): Promise<RollupOptions[]>;
463```
464
465Returns 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`.
466
467## Types
468
469### PkgAnalized
470
471```typescript
472interface PkgAnalized {
473 cwd: string;
474 pkg: PkgJson;
475 input: {
476 api: string | null;
477 bin: string | null;
478 };
479 output: {
480 main: CommonJSBuildOptions | null;
481 module: ESModuleBuildOptions | null;
482 browser: BrowserBuildOptions | null;
483 bin: CommonJSBuildOptions | null;
484 types: TypesBuildOptions | null;
485 };
486 dependencies: {
487 runtime: { [name: string]: string } | null;
488 dev: { [name: string]: string } | null;
489 peer: { [name: string]: string } | null;
490 };
491 cache: string | null;
492 project: string | null;
493}
494```
495
496*see also:* [`CommonJSBuildOptions`](#commonjsbuildoptions), [`ESModuleBuildOptions`](#esmodulebuildoptions), [`BrowserBuildOptions`](#browserbuildoptions) & [`TypesBuildOptions`](#typesbuildoptions)
497
498### CommonJSBuildOptions
499
500```typescript
501interface CommonJSBuildOptions {
502 path: string;
503 sourcemap: boolean | "inline";
504 esModule: boolean;
505 interop: boolean;
506 min: boolean;
507}
508```
509
510### ESModuleBuildOptions
511
512```typescript
513interface ESModuleBuildOptions {
514 path: string;
515 sourcemap: boolean | "inline";
516 min: boolean;
517}
518```
519
520### BrowserBuildOptions
521
522```typescript
523interface BrowserBuildOptions {
524 path: string;
525 sourcemap: boolean | "inline";
526 esModule: boolean;
527 interop: boolean;
528 min: boolean;
529 format: "iife" | "amd" | "umd";
530 name: string | null;
531 id: string | null;
532 globals: Record<string, string> | null;
533 extend: boolean;
534}
535```
536
537### TypesBuildOptions
538
539```typescript
540interface TypesBuildOptions {
541 path: string;
542}
543```
544
545## Features
546
547* Builds a `CommonJS Module` based on the `"main"` field in your `package.json`
548* Builds an `ES Module` based on the `"module"` (or `"jsnext:main"`) field in your `package.json`
549* Builds a `Browser` module based on the `"browser"` field in your `package.json`
550* Builds a `Binary` module based on the `"bin"` field in your `package.json`
551* Exports type declarations based on the `"types"` (or `"typings"`) field in your `package.json`
552* Skip any build based on options
553* Sets `"dependencies"` and `"peerDependencies"` as external for `CommonJS Module`, `ES Module` and `Binary` builds
554* Uses and configures any [supported rollup plugin](#supported-plugins) if installed
555* Uses `chokidar` if installed
556* Importing an internal file from a package `Ex: lodash/core` will be treated as external if `lodash` is included in your `"dependencies"` or `peerDependencies`
557
558## License
559
560[MIT](LICENSE) &copy; 2019 [Manuel Fernández](https://github.com/manferlo81)