UNPKG

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