1 | # jsenv bundling
|
2 |
|
3 | [![npm package](https://img.shields.io/npm/v/@jsenv/bundling.svg)](https://www.npmjs.com/package/@jsenv/bundling)
|
4 | [![build](https://travis-ci.com/jsenv/jsenv-bundling.svg?branch=master)](http://travis-ci.com/jsenv/jsenv-bundling)
|
5 | [![codecov](https://codecov.io/gh/jsenv/jsenv-bundling/branch/master/graph/badge.svg)](https://codecov.io/gh/jsenv/jsenv-bundling)
|
6 |
|
7 | ## Introduction
|
8 |
|
9 | `@jsenv/bundling` is used by jsenv projects to generate files published on npm.<br />
|
10 |
|
11 | It has the following exports
|
12 |
|
13 | - `generateGlobalBundle`
|
14 | - `generateCommonJsBundle`
|
15 | - `generateSystemJsBundle`
|
16 |
|
17 | ## How to use
|
18 |
|
19 | To understand how to use jsenv bundling let's use it on a "real" project.<br />
|
20 | We will setup a basic project and generate different bundle formats.
|
21 |
|
22 | ### Basic project setup
|
23 |
|
24 | 1. Create basic project file structure
|
25 |
|
26 | — see [./docs/basic-project](./docs/basic-project)
|
27 |
|
28 | 2. Install dependencies
|
29 |
|
30 | ```console
|
31 | npm install
|
32 | ```
|
33 |
|
34 | ## `generateGlobalBundle` example
|
35 |
|
36 | ```console
|
37 | node ./generate-global-bundle.js
|
38 | ```
|
39 |
|
40 | Once done, terminal will contain a log with the path to the generated bundle.
|
41 |
|
42 | — see [./docs/basic-project/dist/global/main.js](./docs/basic-project/dist/global/main.js)
|
43 |
|
44 | ## `generateCommonJsBundle` example
|
45 |
|
46 | ```console
|
47 | node ./generate-commonjs-bundle.js
|
48 | ```
|
49 |
|
50 | Once done, terminal will contain a log with the path to the generated bundle.<br />
|
51 |
|
52 | — see [./docs/basic-project/dist/commonjs/main.js](./docs/basic-project/dist/commonjs/main.js)
|
53 |
|
54 | ## `generateSystemJsBundle` example
|
55 |
|
56 | ```console
|
57 | node ./generate-systemjs-bundle.js
|
58 | ```
|
59 |
|
60 | Once done, terminal will contain a log with the path to the generated bundle.<br />
|
61 |
|
62 | — see [./docs/basic-project/dist/systemjs/main.js](./docs/basic-project/dist/systemjs/main.js)
|
63 |
|
64 | ## Bundling functions documentation
|
65 |
|
66 | If you want to know more about these functions, there is a dedicated page for that.<br />
|
67 | — see [bundling functions documentation](./docs/bundling-doc.md)
|
68 |
|
69 | ## Supported codebase
|
70 |
|
71 | jsenv bundling can work with syntax like `top level await`.<br />
|
72 | However some syntax are compatible only with a subset of the available bundle formats.<br />
|
73 | For instance `generateCommonJsBundle` throw an error if it encounters a `top level await` in your codebase.<br />
|
74 |
|
75 | You can find below some code examples and the associated bundle format compatibility.
|
76 |
|
77 | ### top level `await`
|
78 |
|
79 | ```js
|
80 | const answer = await Promise.resolve(42)
|
81 | ```
|
82 |
|
83 | Compatibility: ~~`global`~~, ~~`commonjs`~~, `systemjs`.
|
84 |
|
85 | ### dynamic `import`
|
86 |
|
87 | ```js
|
88 | import("./file.js").then((namespace) => {
|
89 | console.log(namespace)
|
90 | })
|
91 | ```
|
92 |
|
93 | Compatibility: ~~`global`~~, `commonjs`, `systemjs`.
|
94 |
|
95 | ### `import.meta.url`
|
96 |
|
97 | ```js
|
98 | const whereAMI = import.meta.url
|
99 | ```
|
100 |
|
101 | Compatibility: `global`, `commonjs`, `systemjs`.
|
102 |
|
103 | ### `import.meta.resolve`
|
104 |
|
105 | ```js
|
106 | import.meta.resolve("./file.js")
|
107 |
|
108 | const lodashHref = import.meta.resolve("lodash")
|
109 | // importMap are applied so that lodashHref might be /node_modules/lodash/index.js
|
110 | ```
|
111 |
|
112 | Compatibility: `global`, `commonjs`.
|
113 |
|
114 | ### leading slash import
|
115 |
|
116 | ```js
|
117 | import "/src/file.js"
|
118 | // search file at "/Users/you/folder/src/file.js"
|
119 | ```
|
120 |
|
121 | Compatibility: `global`, `commonjs`, `systemjs`.
|
122 |
|
123 | ### import remapping
|
124 |
|
125 | ```js
|
126 | import memoize from "lodash"
|
127 | // search file at "/node_modules/lodash/index.js"
|
128 | ```
|
129 |
|
130 | Compatibility: `global`, `commonjs`, `systemjs`.
|
131 |
|
132 | ### json
|
133 |
|
134 | ```js
|
135 | import data from "./data.json"
|
136 | ```
|
137 |
|
138 | Compatibility: `global`, `commonjs`, `systemjs`.
|
139 |
|
140 | ### globalThis
|
141 |
|
142 | ```js
|
143 | globalThis.console.log("Hello")
|
144 | ```
|
145 |
|
146 | Compatibility: `global`, `commonjs`, `systemjs`.
|
147 |
|
148 | ## Installation
|
149 |
|
150 | ```console
|
151 | npm install --save-dev @jsenv/bundling@5.7.0
|
152 | ```
|
153 |
|
154 | ```console
|
155 | yarn add @jsenv/bundling@5.7.0 --dev
|
156 | ```
|