UNPKG

3.63 kBMarkdownView Raw
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
11It has the following exports
12
13- `generateGlobalBundle`
14- `generateCommonJsBundle`
15- `generateSystemJsBundle`
16
17## How to use
18
19To understand how to use jsenv bundling let's use it on a "real" project.<br />
20We will setup a basic project and generate different bundle formats.
21
22### Basic project setup
23
241. Create basic project file structure
25
26 — see [./docs/basic-project](./docs/basic-project)
27
282. Install dependencies
29
30 ```console
31 npm install
32 ```
33
34## `generateGlobalBundle` example
35
36```console
37node ./generate-global-bundle.js
38```
39
40Once 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
47node ./generate-commonjs-bundle.js
48```
49
50Once 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
57node ./generate-systemjs-bundle.js
58```
59
60Once 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
66If 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
71jsenv bundling can work with syntax like `top level await`.<br />
72However some syntax are compatible only with a subset of the available bundle formats.<br />
73For instance `generateCommonJsBundle` throw an error if it encounters a `top level await` in your codebase.<br />
74
75You can find below some code examples and the associated bundle format compatibility.
76
77### top level `await`
78
79```js
80const answer = await Promise.resolve(42)
81```
82
83Compatibility: ~~`global`~~, ~~`commonjs`~~, `systemjs`.
84
85### dynamic `import`
86
87```js
88import("./file.js").then((namespace) => {
89 console.log(namespace)
90})
91```
92
93Compatibility: ~~`global`~~, `commonjs`, `systemjs`.
94
95### `import.meta.url`
96
97```js
98const whereAMI = import.meta.url
99```
100
101Compatibility: `global`, `commonjs`, `systemjs`.
102
103### `import.meta.resolve`
104
105```js
106import.meta.resolve("./file.js")
107
108const lodashHref = import.meta.resolve("lodash")
109// importMap are applied so that lodashHref might be /node_modules/lodash/index.js
110```
111
112Compatibility: `global`, `commonjs`.
113
114### leading slash import
115
116```js
117import "/src/file.js"
118// search file at "/Users/you/folder/src/file.js"
119```
120
121Compatibility: `global`, `commonjs`, `systemjs`.
122
123### import remapping
124
125```js
126import memoize from "lodash"
127// search file at "/node_modules/lodash/index.js"
128```
129
130Compatibility: `global`, `commonjs`, `systemjs`.
131
132### json
133
134```js
135import data from "./data.json"
136```
137
138Compatibility: `global`, `commonjs`, `systemjs`.
139
140### globalThis
141
142```js
143globalThis.console.log("Hello")
144```
145
146Compatibility: `global`, `commonjs`, `systemjs`.
147
148## Installation
149
150```console
151npm install --save-dev @jsenv/bundling@5.7.0
152```
153
154```console
155yarn add @jsenv/bundling@5.7.0 --dev
156```