UNPKG

5.18 kBMarkdownView Raw
1# jsenv bundling
2
3[![github package](https://img.shields.io/github/package-json/v/jsenv/jsenv-bundling.svg?logo=github&label=package)](https://github.com/jsenv/jsenv-bundling/packages)
4[![npm package](https://img.shields.io/npm/v/@jsenv/bundling.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/bundling)
5[![github ci](https://github.com/jsenv/jsenv-bundling/workflows/ci/badge.svg)](https://github.com/jsenv/jsenv-bundling/actions?workflow=ci)
6[![codecov coverage](https://codecov.io/gh/jsenv/jsenv-bundling/branch/master/graph/badge.svg)](https://codecov.io/gh/jsenv/jsenv-bundling)
7
8Generate bundles for the web or node.js.
9
10## Table of contents
11
12- [Presentation](#Presentation)
13- [Bundle to global](#bundle-to-global)
14- [Bundle to systemjs](#bundle-to-systemjs)
15- [Bundle to commonjs](#bundle-to-commonjs)
16- [Code example](#code-example)
17- [Concrete example](#concrete-example)
18 - [Step 1 - Setup basic project](#step-1---setup-basic-project)
19 - [Step 2 - Install dependencies](#step-2---install-dependencies)
20 - [Step 3 - Generate bundles](#step-3---generate-bundles)
21- [Installation](#installation)
22
23## Presentation
24
25`jsenv-bundling` github repository corresponds to `@jsenv/bundling` package published on github and npm package registries.
26
27`@jsenv/bundling` can generates bundle for systemjs, commonjs or global (also known as iife). Each format takes is meant to be used in a specific way explained below.
28
29## Bundle to global
30
31Things to know about global bundle:
32
33- Meant to run in a browser environment
34- Needs collision free global variable
35- Not compatible with code using dynamic import
36- Not compatible with code using top level await
37
38For example [docs/basic-project/index.js](./docs/basic-project/index.js) is bundled to [docs/basic-project/dist/global/main.js](./docs/basic-project/dist/global/main.js).
39
40That global bundle could be used by
41
42```html
43<script src="./dist/global/main.js"></script>
44<script>
45 console.log(window.__whatever__)
46</script>
47```
48
49## Bundle to systemjs
50
51Things to know about systemjs bundle:
52
53- Needs [systemjs](https://github.com/systemjs/systemjs) to be used
54- Compatible with dynamic import
55- Compatible with top level await
56
57For example [docs/basic-project/index.js](./docs/basic-project/index.js) is bundled to [docs/basic-project/dist/systemjs/main.js](./docs/basic-project/dist/systemjs/main.js).
58
59That systemjs bundle could be used by
60
61```html
62<script src="https://unpkg.com/systemjs@6.1.4/dist/system.js"></script>
63<script>
64 window.System.import("./dist/systemjs/main.js").then((namespace) => {
65 console.log(namespace.default)
66 })
67</script>
68```
69
70## Bundle to commonjs
71
72Things to know about commonjs bundle:
73
74- Meant to be required in a node.js environment
75- Not compatible with code using top level await
76
77For example [docs/basic-project/index.js](./docs/basic-project/index.js) is bundled to [docs/basic-project/dist/commonjs/main.js](./docs/basic-project/dist/commonjs/main.js).
78
79That commonjs bundle could be used by
80
81```js
82const exports = require("./dist/commonjs/main.js")
83
84console.log(exports)
85```
86
87### Code example
88
89The following code uses `@jsenv/bundling` to create a systemjs bundle for `index.js` entry point.
90
91```js
92const { generateSystemJsBundle } = require("@jsenv/bundling")
93
94generateSystemJsBundle({
95 projectDirectoryPath: __dirname,
96 bundleDirectoryRelativePath: "./dist",
97 entryPointMap: {
98 main: "./index.js",
99 },
100})
101```
102
103If you want to know more about this function and others check [api documentation](./docs/api.md)
104
105## Concrete example
106
107This part explains how to setup a real environment to see `@jsenv/bundling` in action.<br />
108You will setup a basic project where you can generate different bundle formats.
109
110### Step 1 - Setup basic project
111
112```console
113git clone git@github.com:jsenv/jsenv-bundling.git
114```
115
116### Step 2 - Install dependencies
117
118```console
119cd ./jsenv-bundling/docs/basic-project
120```
121
122If you never configured npm authentification on github registry see [Configure npm authentification on github registry](https://github.com/jsenv/jsenv-core/blob/master/docs/installing-jsenv-package.md#configure-npm-authentification-on-github-registry) first.
123
124```console
125npm install
126```
127
128### Step 3 - Generate bundles
129
130This project has preconfigured 3 bundle. You can generate them with the commands below:
131
132- [docs/basic-project/dist/systemjs/main.js](./docs/basic-project/dist/systemjs/main.js)
133
134 ```console
135 node ./generate-systemjs-bundle.js
136 ```
137
138* [docs/basic-project/dist/global/main.js](./docs/basic-project/dist/global/main.js)
139
140 ```console
141 node ./generate-global-bundle.js
142 ```
143
144- [docs/basic-project/dist/commonjs/main.js](./docs/basic-project/dist/commonjs/main.js)
145
146 ```console
147 node ./generate-commonjs-bundle.js
148 ```
149
150## Installation
151
152If you never installed a jsenv package, read [Installing a jsenv package](https://github.com/jsenv/jsenv-core/blob/master/docs/installing-jsenv-package.md#installing-a-jsenv-package) before going further.
153
154This documentation is up-to-date with a specific version so prefer any of the following commands
155
156```console
157npm install --save-dev @jsenv/bundling@8.1.0
158```
159
160```console
161yarn add --dev @jsenv/bundling@8.1.0
162```