UNPKG

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