bunchee 3.5.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # bunchee
2
+
2
3
  > zero config bundler for JavaScript/TypeScript/JSX library
3
4
 
4
5
  ![bunchee](https://repository-images.githubusercontent.com/154026156/5d132698-0ff5-4644-a4fd-d9570e6229bc)
@@ -30,6 +31,7 @@ cd ./my-lib && mkdir src
30
31
  touch ./src/index.js
31
32
  touch package.json
32
33
  ```
34
+
33
35
  Configure module exports
34
36
 
35
37
  [exports sugar in Node.js](https://nodejs.org/api/packages.html#exports-sugar)
@@ -63,8 +65,8 @@ Using pure ESM package?
63
65
 
64
66
  Then just run `npm run build`, or `pnpm build` / `yarn build` if you're using these package managers. The output format will based on the exports condition and also the file extension. Given an example:
65
67
 
66
- * It's CommonJS for `require` and ESM for `import` based on the exports condition.
67
- * It's CommonJS for `.js` and ESM for `.mjs` based on the extension regardless the exports condition. Then for export condition like "node" you could choose the format with your extension.
68
+ - It's CommonJS for `require` and ESM for `import` based on the exports condition.
69
+ - It's CommonJS for `.js` and ESM for `.mjs` based on the extension regardless the exports condition. Then for export condition like "node" you could choose the format with your extension.
68
70
 
69
71
  ## Configuration
70
72
 
@@ -108,14 +110,17 @@ bunchee --external=dependency1,dependency2,dependency3
108
110
  Replace `dependency1`, `dependency2`, and `dependency3` with the names of the dependencies you want to exclude from the bundle.
109
111
 
110
112
  #### Bundling everything without external dependencies
113
+
111
114
  To bundle your library without external dependencies, use the `--no-external` option:
112
115
 
113
116
  ```sh
114
117
  bunchee --no-external
115
118
  ```
119
+
116
120
  This will include all dependencies within your output bundle.
117
121
 
118
122
  ### Environment Variables
123
+
119
124
  To pass environment variables to your bundled code, use the --env option followed by a comma-separated list of environment variable names:
120
125
 
121
126
  ```bash
@@ -130,8 +135,8 @@ While `exports` field is becoming the standard of exporting in node.js, bunchee
130
135
 
131
136
  What you need to do is just add an entry file with the name (`[name].[ext]`) that matches the exported name from exports field in package.json. For instance:
132
137
 
133
- * `<cwd>/src/index.ts` will match `"."` export name or the if there's only one main export.
134
- * `<cwd>/src/lite.ts` will match `"./lite"` export name.
138
+ - `<cwd>/src/index.ts` will match `"."` export name or the if there's only one main export.
139
+ - `<cwd>/src/lite.ts` will match `"./lite"` export name.
135
140
 
136
141
  The build script will be simplified to just `bunchee` in package.json without configure any input sources for each exports. Of course you can still specify other arguments as you need.
137
142
 
@@ -201,14 +206,13 @@ This will match the export name `"react-server"` and `"edge-light"` then use the
201
206
  By default bunchee includes Typescript v3.9.x inside as a dependency. If you want to use your own version, just install typescript as another dev dependency then bunchee will automatically pick it.
202
207
 
203
208
  ```sh
204
- yarn add -D bunchee typescript
209
+ npm i -D bunchee typescript
205
210
  ```
206
211
 
207
212
  Create `tsconfig.json` to specify any compiler options for TypeScript.
208
213
 
209
214
  This library requires at least TypeScript 4.1.x.
210
215
 
211
-
212
216
  Adding `"types"` or `"typing"` field in your package.json, types will be generated with that path.
213
217
 
214
218
  ```json
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node
package/dist/cli.js CHANGED
@@ -9,64 +9,23 @@ var path__default = /*#__PURE__*/_interopDefault(path);
9
9
  var arg__default = /*#__PURE__*/_interopDefault(arg);
10
10
  var fs__default = /*#__PURE__*/_interopDefault(fs);
11
11
 
12
- function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
13
- try {
14
- var info = gen[key](arg);
15
- var value = info.value;
16
- } catch (error) {
17
- reject(error);
18
- return;
19
- }
20
- if (info.done) {
21
- resolve(value);
22
- } else {
23
- Promise.resolve(value).then(_next, _throw);
24
- }
25
- }
26
- function _async_to_generator$1(fn) {
27
- return function() {
28
- var self = this, args = arguments;
29
- return new Promise(function(resolve, reject) {
30
- var gen = fn.apply(self, args);
31
- function _next(value) {
32
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
33
- }
34
- function _throw(err) {
35
- asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
36
- }
37
- _next(undefined);
38
- });
39
- };
40
- }
41
12
  function exit(err) {
42
13
  logger.error(err);
43
14
  process.exit(1);
44
15
  }
45
16
  const formatDuration = (duration)=>duration >= 1000 ? `${duration / 1000}s` : `${duration}ms`;
46
- function hasPackageJson(cwd) {
47
- return _hasPackageJson.apply(this, arguments);
48
- }
49
- function _hasPackageJson() {
50
- _hasPackageJson = _async_to_generator$1(function*(cwd) {
51
- return yield fileExists(path__default.default.resolve(cwd, 'package.json'));
52
- });
53
- return _hasPackageJson.apply(this, arguments);
17
+ async function hasPackageJson(cwd) {
18
+ return await fileExists(path__default.default.resolve(cwd, 'package.json'));
54
19
  }
55
- function getPackageMeta(cwd) {
56
- return _getPackageMeta.apply(this, arguments);
57
- }
58
- function _getPackageMeta() {
59
- _getPackageMeta = _async_to_generator$1(function*(cwd) {
60
- const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
61
- let targetPackageJson = {};
62
- try {
63
- targetPackageJson = JSON.parse((yield fs__default.default.readFile(pkgFilePath, {
64
- encoding: 'utf-8'
65
- })));
66
- } catch (_) {}
67
- return targetPackageJson;
68
- });
69
- return _getPackageMeta.apply(this, arguments);
20
+ async function getPackageMeta(cwd) {
21
+ const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
22
+ let targetPackageJson = {};
23
+ try {
24
+ targetPackageJson = JSON.parse(await fs__default.default.readFile(pkgFilePath, {
25
+ encoding: 'utf-8'
26
+ }));
27
+ } catch (_) {}
28
+ return targetPackageJson;
70
29
  }
71
30
  const logger = {
72
31
  log (arg) {
@@ -79,55 +38,20 @@ const logger = {
79
38
  console.error('\x1b[31m' + (arg instanceof Error ? arg.stack : arg) + '\x1b[0m');
80
39
  }
81
40
  };
82
- function fileExists(filePath) {
83
- return _fileExists.apply(this, arguments);
84
- }
85
- function _fileExists() {
86
- _fileExists = _async_to_generator$1(function*(filePath) {
87
- try {
88
- yield fs__default.default.access(filePath);
89
- return true;
90
- } catch (err) {
91
- if (err.code === 'ENOENT') {
92
- return false;
93
- }
94
- throw err;
41
+ async function fileExists(filePath) {
42
+ try {
43
+ await fs__default.default.access(filePath);
44
+ return true;
45
+ } catch (err) {
46
+ if (err.code === 'ENOENT') {
47
+ return false;
95
48
  }
96
- });
97
- return _fileExists.apply(this, arguments);
49
+ throw err;
50
+ }
98
51
  }
99
52
 
100
- var version = "3.5.0";
53
+ var version = "3.6.0";
101
54
 
102
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
103
- try {
104
- var info = gen[key](arg);
105
- var value = info.value;
106
- } catch (error) {
107
- reject(error);
108
- return;
109
- }
110
- if (info.done) {
111
- resolve(value);
112
- } else {
113
- Promise.resolve(value).then(_next, _throw);
114
- }
115
- }
116
- function _async_to_generator(fn) {
117
- return function() {
118
- var self = this, args = arguments;
119
- return new Promise(function(resolve, reject) {
120
- var gen = fn.apply(self, args);
121
- function _next(value) {
122
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
123
- }
124
- function _throw(err) {
125
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
126
- }
127
- _next(undefined);
128
- });
129
- };
130
- }
131
55
  const helpMessage = `
132
56
  Usage: bunchee [options]
133
57
 
@@ -150,27 +74,21 @@ Options:
150
74
  function help() {
151
75
  logger.log(helpMessage);
152
76
  }
153
- function lintPackage(cwd) {
154
- return _lintPackage.apply(this, arguments);
155
- }
156
- function _lintPackage() {
157
- _lintPackage = _async_to_generator(function*(cwd) {
158
- // Not package.json detected, skip package linting
159
- if (!(yield hasPackageJson(cwd))) {
160
- return;
161
- }
162
- const { publint } = yield import('publint');
163
- const { printMessage } = yield import('publint/utils');
164
- const messages = yield publint({
165
- pkgDir: cwd,
166
- level: 'error'
167
- });
168
- const pkg = yield getPackageMeta(cwd);
169
- for (const message of messages){
170
- console.log(printMessage(message, pkg));
171
- }
77
+ async function lintPackage(cwd) {
78
+ // Not package.json detected, skip package linting
79
+ if (!await hasPackageJson(cwd)) {
80
+ return;
81
+ }
82
+ const { publint } = await import('publint');
83
+ const { printMessage } = await import('publint/utils');
84
+ const messages = await publint({
85
+ pkgDir: cwd,
86
+ level: 'error'
172
87
  });
173
- return _lintPackage.apply(this, arguments);
88
+ const pkg = await getPackageMeta(cwd);
89
+ for (const message of messages){
90
+ console.log(printMessage(message, pkg));
91
+ }
174
92
  }
175
93
  function parseCliArgs(argv) {
176
94
  let args;
@@ -218,77 +136,65 @@ function parseCliArgs(argv) {
218
136
  };
219
137
  return parsedArgs;
220
138
  }
221
- function run(args) {
222
- return _run.apply(this, arguments);
223
- }
224
- function _run() {
225
- _run = _async_to_generator(function*(args) {
226
- var _args_external;
227
- const { source , format , watch , minify , sourcemap , target , runtime , dts , env } = args;
228
- const cwd = args.cwd || process.cwd();
229
- const file = args.file ? path__default.default.resolve(cwd, args.file) : undefined;
230
- const bundleConfig = {
231
- dts,
232
- file,
233
- format,
234
- cwd,
235
- target,
236
- runtime,
237
- external: ((_args_external = args.external) == null ? void 0 : _args_external.split(',')) || [],
238
- watch: !!watch,
239
- minify: !!minify,
240
- sourcemap: sourcemap === false ? false : true,
241
- env: (env == null ? void 0 : env.split(',')) || []
242
- };
243
- if (args.version) {
244
- return logger.log(version);
245
- }
246
- if (args.help) {
247
- return help();
248
- }
249
- const entry = source ? path__default.default.resolve(cwd, source) : '';
250
- const bundle = require('./index').bundle;
251
- let timeStart = Date.now();
252
- let timeEnd;
253
- try {
254
- yield bundle(entry, bundleConfig);
255
- timeEnd = Date.now();
256
- } catch (err) {
257
- if (err.name === 'NOT_EXISTED') {
258
- help();
259
- return exit(err);
260
- }
261
- throw err;
262
- }
263
- const duration = timeEnd - timeStart;
264
- // watching mode
265
- if (watch) {
266
- logger.log(`🔍 Watching assets in ${cwd}...`);
267
- return;
139
+ async function run(args) {
140
+ var _args_external, _env;
141
+ const { source, format, watch, minify, sourcemap, target, runtime, dts, env } = args;
142
+ const cwd = args.cwd || process.cwd();
143
+ const file = args.file ? path__default.default.resolve(cwd, args.file) : undefined;
144
+ const bundleConfig = {
145
+ dts,
146
+ file,
147
+ format,
148
+ cwd,
149
+ target,
150
+ runtime,
151
+ external: ((_args_external = args.external) == null ? void 0 : _args_external.split(',')) || [],
152
+ watch: !!watch,
153
+ minify: !!minify,
154
+ sourcemap: sourcemap === false ? false : true,
155
+ env: ((_env = env) == null ? void 0 : _env.split(',')) || []
156
+ };
157
+ if (args.version) {
158
+ return logger.log(version);
159
+ }
160
+ if (args.help) {
161
+ return help();
162
+ }
163
+ const entry = source ? path__default.default.resolve(cwd, source) : '';
164
+ const bundle = require('./index').bundle;
165
+ let timeStart = Date.now();
166
+ let timeEnd;
167
+ try {
168
+ await bundle(entry, bundleConfig);
169
+ timeEnd = Date.now();
170
+ } catch (err) {
171
+ if (err.name === 'NOT_EXISTED') {
172
+ help();
173
+ return exit(err);
268
174
  }
269
- // build mode
270
- logger.log(`✨ Finished in ${formatDuration(duration)}`);
271
- yield lintPackage(cwd);
272
- });
273
- return _run.apply(this, arguments);
274
- }
275
- function main() {
276
- return _main.apply(this, arguments);
175
+ throw err;
176
+ }
177
+ const duration = timeEnd - timeStart;
178
+ // watching mode
179
+ if (watch) {
180
+ logger.log(`🔍 Watching assets in ${cwd}...`);
181
+ return;
182
+ }
183
+ // build mode
184
+ logger.log(`✨ Finished in ${formatDuration(duration)}`);
185
+ await lintPackage(cwd);
277
186
  }
278
- function _main() {
279
- _main = _async_to_generator(function*() {
280
- let params, error;
281
- try {
282
- params = parseCliArgs(process.argv.slice(2));
283
- } catch (err) {
284
- error = err;
285
- }
286
- if (error || !params) {
287
- if (!error) help();
288
- return exit(error);
289
- }
290
- yield run(params);
291
- });
292
- return _main.apply(this, arguments);
187
+ async function main() {
188
+ let params, error;
189
+ try {
190
+ params = parseCliArgs(process.argv.slice(2));
191
+ } catch (err) {
192
+ error = err;
193
+ }
194
+ if (error || !params) {
195
+ if (!error) help();
196
+ return exit(error);
197
+ }
198
+ await run(params);
293
199
  }
294
200
  main().catch(exit);