bunchee 2.2.0 → 3.0.0-beta.10
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 +18 -11
- package/dist/cli.js +113 -249
- package/dist/lib.d.ts +5 -5
- package/dist/lib.js +235 -413
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -13,10 +13,7 @@
|
|
|
13
13
|
</a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
|
-
Bunchee
|
|
17
|
-
|
|
18
|
-
Let you focus on writing code and can generate multiple types of module (CommonJS, ESModules) at the same time.
|
|
19
|
-
|
|
16
|
+
Bunchee makes bundling your library into one file effortless, with zero configuration required. It is built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple module types (CommonJS, ESModules) simultaneously.
|
|
20
17
|
|
|
21
18
|
## Installation
|
|
22
19
|
|
|
@@ -25,12 +22,11 @@ npm install --save-dev bunchee
|
|
|
25
22
|
```
|
|
26
23
|
|
|
27
24
|
## Usage
|
|
28
|
-
###
|
|
29
|
-
|
|
30
|
-
Declare your main field and module field in package.json, then call bunchee cli in build scripts. If you're using typescript, types will be generated automatically based on your package.json field `typings` or `types`.
|
|
25
|
+
### Package.json Configuration
|
|
31
26
|
|
|
27
|
+
Declare the main and module fields in your package.json file, then call the bunchee CLI in the build scripts. If you are using TypeScript, types will be generated automatically based on the typings or types field in your package.json file.
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
#### Configure `main` and `module` fields
|
|
34
30
|
|
|
35
31
|
You can have Commonjs + ESModules output as the simple config
|
|
36
32
|
|
|
@@ -45,9 +41,12 @@ You can have Commonjs + ESModules output as the simple config
|
|
|
45
41
|
}
|
|
46
42
|
```
|
|
47
43
|
|
|
48
|
-
|
|
44
|
+
#### Configure `exports` field
|
|
45
|
+
|
|
46
|
+
[exports sugar in Node.js](https://nodejs.org/api/packages.html#exports-sugar)
|
|
47
|
+
|
|
48
|
+
You can use the exports field to support different conditions and leverage the same functionality as other bundlers, such as webpack. The exports field allows you to define multiple conditions.
|
|
49
49
|
|
|
50
|
-
Leverage `exports` field to support different conditions would be also ideal. Most of the bundler such as `webpack` can already handle the [`package exports`](https://webpack.js.org/guides/package-exports/) well. It's convenient to define multiple conditions in exports.
|
|
51
50
|
|
|
52
51
|
```json
|
|
53
52
|
{
|
|
@@ -75,7 +74,7 @@ Options:
|
|
|
75
74
|
-f, --format <format> type of output (esm, amd, cjs, iife, umd, system), default: esm
|
|
76
75
|
-e, --external <mod> specify an external dependency
|
|
77
76
|
-h, --help output usage information
|
|
78
|
-
--target <target> js features target: swc target es versions. default:
|
|
77
|
+
--target <target> js features target: swc target es versions. default: es2015
|
|
79
78
|
--runtime <runtime> build runtime (nodejs, browser). default: browser
|
|
80
79
|
--cwd <cwd> specify current working directory
|
|
81
80
|
--sourcemap enable sourcemap generation, default: false
|
|
@@ -116,6 +115,10 @@ await bundle(path.resolve('./src/index.ts'), {
|
|
|
116
115
|
})
|
|
117
116
|
```
|
|
118
117
|
|
|
118
|
+
#### Watch Mode
|
|
119
|
+
|
|
120
|
+
Bunchee offers a convenient watch mode for rebuilding your library whenever changes are made to the source files. To enable this feature, use either -w or --watch.
|
|
121
|
+
|
|
119
122
|
### Typescript
|
|
120
123
|
|
|
121
124
|
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.
|
|
@@ -129,6 +132,10 @@ Create `tsconfig.json` to specify any compiler options for TypeScript.
|
|
|
129
132
|
This library requires at least [TypeScript 3.7](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html).
|
|
130
133
|
|
|
131
134
|
|
|
135
|
+
#### `target`
|
|
136
|
+
|
|
137
|
+
If you specify `target` option in `tsconfig.json`, then you don't have to pass it again through CLI.
|
|
138
|
+
|
|
132
139
|
## Advanced
|
|
133
140
|
|
|
134
141
|
### Multiple Exports
|
package/dist/cli.js
CHANGED
|
@@ -3,31 +3,24 @@ var path = require('path');
|
|
|
3
3
|
var arg = require('arg');
|
|
4
4
|
require('fs');
|
|
5
5
|
|
|
6
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
7
|
-
|
|
8
|
-
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
9
|
-
var arg__default = /*#__PURE__*/_interopDefaultLegacy(arg);
|
|
10
|
-
|
|
11
6
|
function exit(err) {
|
|
12
7
|
logger.error(err);
|
|
13
8
|
process.exit(1);
|
|
14
9
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
var logger = {
|
|
19
|
-
log: function log(arg) {
|
|
10
|
+
const formatDuration = (duration)=>duration >= 1000 ? `${duration / 1000}s` : `${duration}ms`;
|
|
11
|
+
const logger = {
|
|
12
|
+
log (arg) {
|
|
20
13
|
console.log(arg);
|
|
21
14
|
},
|
|
22
|
-
warn
|
|
23
|
-
console.log(
|
|
15
|
+
warn (arg) {
|
|
16
|
+
console.log('\x1b[33m' + arg + '\x1b[0m');
|
|
24
17
|
},
|
|
25
|
-
error
|
|
26
|
-
console.error(
|
|
18
|
+
error (arg) {
|
|
19
|
+
console.error('\x1b[31m' + arg + '\x1b[0m');
|
|
27
20
|
}
|
|
28
21
|
};
|
|
29
22
|
|
|
30
|
-
var version = "
|
|
23
|
+
var version = "3.0.0-beta.10";
|
|
31
24
|
|
|
32
25
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
33
26
|
try {
|
|
@@ -58,148 +51,69 @@ function _asyncToGenerator(fn) {
|
|
|
58
51
|
});
|
|
59
52
|
};
|
|
60
53
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
function verb(n) {
|
|
79
|
-
return function(v) {
|
|
80
|
-
return step([
|
|
81
|
-
n,
|
|
82
|
-
v
|
|
83
|
-
]);
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
function step(op) {
|
|
87
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
88
|
-
while(_)try {
|
|
89
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
90
|
-
if (y = 0, t) op = [
|
|
91
|
-
op[0] & 2,
|
|
92
|
-
t.value
|
|
93
|
-
];
|
|
94
|
-
switch(op[0]){
|
|
95
|
-
case 0:
|
|
96
|
-
case 1:
|
|
97
|
-
t = op;
|
|
98
|
-
break;
|
|
99
|
-
case 4:
|
|
100
|
-
_.label++;
|
|
101
|
-
return {
|
|
102
|
-
value: op[1],
|
|
103
|
-
done: false
|
|
104
|
-
};
|
|
105
|
-
case 5:
|
|
106
|
-
_.label++;
|
|
107
|
-
y = op[1];
|
|
108
|
-
op = [
|
|
109
|
-
0
|
|
110
|
-
];
|
|
111
|
-
continue;
|
|
112
|
-
case 7:
|
|
113
|
-
op = _.ops.pop();
|
|
114
|
-
_.trys.pop();
|
|
115
|
-
continue;
|
|
116
|
-
default:
|
|
117
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
118
|
-
_ = 0;
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
122
|
-
_.label = op[1];
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
126
|
-
_.label = t[1];
|
|
127
|
-
t = op;
|
|
128
|
-
break;
|
|
129
|
-
}
|
|
130
|
-
if (t && _.label < t[2]) {
|
|
131
|
-
_.label = t[2];
|
|
132
|
-
_.ops.push(op);
|
|
133
|
-
break;
|
|
134
|
-
}
|
|
135
|
-
if (t[2]) _.ops.pop();
|
|
136
|
-
_.trys.pop();
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
op = body.call(thisArg, _);
|
|
140
|
-
} catch (e) {
|
|
141
|
-
op = [
|
|
142
|
-
6,
|
|
143
|
-
e
|
|
144
|
-
];
|
|
145
|
-
y = 0;
|
|
146
|
-
} finally{
|
|
147
|
-
f = t = 0;
|
|
148
|
-
}
|
|
149
|
-
if (op[0] & 5) throw op[1];
|
|
150
|
-
return {
|
|
151
|
-
value: op[0] ? op[1] : void 0,
|
|
152
|
-
done: true
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
var helpMessage = "\nUsage: bunchee [options]\n\nOptions:\n -v, --version output the version number\n -w, --watch watch src files changes\n -m, --minify compress output. default: false\n -o, --output <file> specify output filename\n -f, --format <format> type of output (esm, amd, cjs, iife, umd, system), default: esm\n -e, --external <mod> specify an external dependency\n -h, --help output usage information\n --target <target> js features target: swc target es versions. default: es5\n --runtime <runtime> build runtime (nodejs, browser). default: browser\n --cwd <cwd> specify current working directory\n --sourcemap enable sourcemap generation, default: false\n --dts determine if need to generate types, default: false\n";
|
|
54
|
+
const helpMessage = `
|
|
55
|
+
Usage: bunchee [options]
|
|
56
|
+
|
|
57
|
+
Options:
|
|
58
|
+
-v, --version output the version number
|
|
59
|
+
-w, --watch watch src files changes
|
|
60
|
+
-m, --minify compress output. default: false
|
|
61
|
+
-o, --output <file> specify output filename
|
|
62
|
+
-f, --format <format> type of output (esm, amd, cjs, iife, umd, system), default: esm
|
|
63
|
+
-e, --external <mod> specify an external dependency
|
|
64
|
+
-h, --help output usage information
|
|
65
|
+
--target <target> js features target: swc target es versions. default: es2015
|
|
66
|
+
--runtime <runtime> build runtime (nodejs, browser). default: browser
|
|
67
|
+
--cwd <cwd> specify current working directory
|
|
68
|
+
--sourcemap enable sourcemap generation, default: false
|
|
69
|
+
--dts determine if need to generate types, default: false
|
|
70
|
+
`;
|
|
157
71
|
function help() {
|
|
158
72
|
logger.log(helpMessage);
|
|
159
73
|
}
|
|
160
74
|
function parseCliArgs(argv) {
|
|
161
|
-
|
|
162
|
-
args =
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
75
|
+
let args;
|
|
76
|
+
args = arg({
|
|
77
|
+
'--cwd': String,
|
|
78
|
+
'--dts': Boolean,
|
|
79
|
+
'--output': String,
|
|
80
|
+
'--format': String,
|
|
81
|
+
'--watch': Boolean,
|
|
82
|
+
'--minify': Boolean,
|
|
83
|
+
'--help': Boolean,
|
|
84
|
+
'--version': Boolean,
|
|
85
|
+
'--runtime': String,
|
|
86
|
+
'--target': String,
|
|
87
|
+
'--sourcemap': Boolean,
|
|
88
|
+
'--external': [
|
|
175
89
|
String
|
|
176
90
|
],
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
91
|
+
'-h': '--help',
|
|
92
|
+
'-v': '--version',
|
|
93
|
+
'-w': '--watch',
|
|
94
|
+
'-o': '--output',
|
|
95
|
+
'-f': '--format',
|
|
96
|
+
'-m': '--minify',
|
|
97
|
+
'-e': '--external'
|
|
184
98
|
}, {
|
|
185
99
|
permissive: true,
|
|
186
|
-
argv
|
|
100
|
+
argv
|
|
187
101
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
source
|
|
191
|
-
format: args[
|
|
192
|
-
file: args[
|
|
193
|
-
watch: args[
|
|
194
|
-
minify: args[
|
|
195
|
-
sourcemap: !!args[
|
|
196
|
-
cwd: args[
|
|
197
|
-
dts: args[
|
|
198
|
-
help: args[
|
|
199
|
-
version: args[
|
|
200
|
-
runtime: args[
|
|
201
|
-
target: args[
|
|
202
|
-
external: args[
|
|
102
|
+
const source = args._[0];
|
|
103
|
+
const parsedArgs = {
|
|
104
|
+
source,
|
|
105
|
+
format: args['--format'],
|
|
106
|
+
file: args['--output'],
|
|
107
|
+
watch: args['--watch'],
|
|
108
|
+
minify: args['--minify'],
|
|
109
|
+
sourcemap: !!args['--sourcemap'],
|
|
110
|
+
cwd: args['--cwd'],
|
|
111
|
+
dts: args['--dts'],
|
|
112
|
+
help: args['--help'],
|
|
113
|
+
version: args['--version'],
|
|
114
|
+
runtime: args['--runtime'],
|
|
115
|
+
target: args['--target'],
|
|
116
|
+
external: args['--external']
|
|
203
117
|
};
|
|
204
118
|
return parsedArgs;
|
|
205
119
|
}
|
|
@@ -207,80 +121,46 @@ function run(args) {
|
|
|
207
121
|
return _run.apply(this, arguments);
|
|
208
122
|
}
|
|
209
123
|
function _run() {
|
|
210
|
-
_run = _asyncToGenerator(function(args) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
bundle = require("./lib").bundle;
|
|
244
|
-
timeStart = Date.now();
|
|
245
|
-
_state.label = 1;
|
|
246
|
-
case 1:
|
|
247
|
-
_state.trys.push([
|
|
248
|
-
1,
|
|
249
|
-
3,
|
|
250
|
-
,
|
|
251
|
-
4
|
|
252
|
-
]);
|
|
253
|
-
return [
|
|
254
|
-
4,
|
|
255
|
-
bundle(entry, cliArgs)
|
|
256
|
-
];
|
|
257
|
-
case 2:
|
|
258
|
-
_state.sent();
|
|
259
|
-
timeEnd = Date.now();
|
|
260
|
-
return [
|
|
261
|
-
3,
|
|
262
|
-
4
|
|
263
|
-
];
|
|
264
|
-
case 3:
|
|
265
|
-
err = _state.sent();
|
|
266
|
-
if (err.name === "NOT_EXISTED") {
|
|
267
|
-
help();
|
|
268
|
-
return [
|
|
269
|
-
2,
|
|
270
|
-
exit(err)
|
|
271
|
-
];
|
|
272
|
-
}
|
|
273
|
-
throw err;
|
|
274
|
-
case 4:
|
|
275
|
-
duration = timeEnd - timeStart;
|
|
276
|
-
if (!watch) {
|
|
277
|
-
logger.log("✨ Finished in " + formatDuration(duration));
|
|
278
|
-
}
|
|
279
|
-
return [
|
|
280
|
-
2
|
|
281
|
-
];
|
|
124
|
+
_run = _asyncToGenerator(function*(args) {
|
|
125
|
+
const { source , format , watch , minify , sourcemap , target , runtime , dts } = args;
|
|
126
|
+
const cwd = args.cwd || process.cwd();
|
|
127
|
+
const file = args.file ? path.resolve(cwd, args.file) : undefined;
|
|
128
|
+
const cliArgs = {
|
|
129
|
+
dts,
|
|
130
|
+
file,
|
|
131
|
+
format,
|
|
132
|
+
cwd,
|
|
133
|
+
target,
|
|
134
|
+
runtime,
|
|
135
|
+
external: args.external || [],
|
|
136
|
+
watch: !!watch,
|
|
137
|
+
minify: !!minify,
|
|
138
|
+
sourcemap: sourcemap === false ? false : true
|
|
139
|
+
};
|
|
140
|
+
if (args.version) {
|
|
141
|
+
return logger.log(version);
|
|
142
|
+
}
|
|
143
|
+
if (args.help) {
|
|
144
|
+
return help();
|
|
145
|
+
}
|
|
146
|
+
const entry = source ? path.resolve(cwd, source) : '';
|
|
147
|
+
const { bundle } = require('./lib');
|
|
148
|
+
let timeStart = Date.now();
|
|
149
|
+
let timeEnd;
|
|
150
|
+
try {
|
|
151
|
+
yield bundle(entry, cliArgs);
|
|
152
|
+
timeEnd = Date.now();
|
|
153
|
+
} catch (err) {
|
|
154
|
+
if (err.name === 'NOT_EXISTED') {
|
|
155
|
+
help();
|
|
156
|
+
return exit(err);
|
|
282
157
|
}
|
|
283
|
-
|
|
158
|
+
throw err;
|
|
159
|
+
}
|
|
160
|
+
const duration = timeEnd - timeStart;
|
|
161
|
+
if (!watch) {
|
|
162
|
+
logger.log(`✨ Finished in ${formatDuration(duration)}`);
|
|
163
|
+
}
|
|
284
164
|
});
|
|
285
165
|
return _run.apply(this, arguments);
|
|
286
166
|
}
|
|
@@ -288,34 +168,18 @@ function main() {
|
|
|
288
168
|
return _main.apply(this, arguments);
|
|
289
169
|
}
|
|
290
170
|
function _main() {
|
|
291
|
-
_main = _asyncToGenerator(function() {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return [
|
|
304
|
-
2,
|
|
305
|
-
exit(error)
|
|
306
|
-
];
|
|
307
|
-
}
|
|
308
|
-
return [
|
|
309
|
-
4,
|
|
310
|
-
run(params)
|
|
311
|
-
];
|
|
312
|
-
case 1:
|
|
313
|
-
_state.sent();
|
|
314
|
-
return [
|
|
315
|
-
2
|
|
316
|
-
];
|
|
317
|
-
}
|
|
318
|
-
});
|
|
171
|
+
_main = _asyncToGenerator(function*() {
|
|
172
|
+
let params, error;
|
|
173
|
+
try {
|
|
174
|
+
params = parseCliArgs(process.argv.slice(2));
|
|
175
|
+
} catch (err) {
|
|
176
|
+
error = err;
|
|
177
|
+
}
|
|
178
|
+
if (error || !params) {
|
|
179
|
+
if (!error) help();
|
|
180
|
+
return exit(error);
|
|
181
|
+
}
|
|
182
|
+
yield run(params);
|
|
319
183
|
});
|
|
320
184
|
return _main.apply(this, arguments);
|
|
321
185
|
}
|
package/dist/lib.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { JscTarget } from '@swc/core';
|
|
2
2
|
import { RollupOptions, InputOptions, OutputOptions } from 'rollup';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
type ExportType = 'require' | 'export' | 'default' | string;
|
|
5
|
+
type CommonConfig = {
|
|
6
6
|
dts?: boolean;
|
|
7
7
|
format?: OutputOptions['format'];
|
|
8
8
|
minify?: boolean;
|
|
@@ -15,14 +15,14 @@ declare type CommonConfig = {
|
|
|
15
15
|
export: ExportCondition;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
type ExportCondition = string | Record<ExportType, string>;
|
|
19
|
+
type BuncheeRollupConfig = Partial<Omit<RollupOptions, 'input' | 'output'>> & {
|
|
20
20
|
exportName?: string;
|
|
21
21
|
input: InputOptions;
|
|
22
22
|
output: OutputOptions[];
|
|
23
23
|
dtsOnly: boolean;
|
|
24
24
|
};
|
|
25
|
-
|
|
25
|
+
type CliArgs = CommonConfig & {
|
|
26
26
|
file?: string;
|
|
27
27
|
watch?: boolean;
|
|
28
28
|
cwd?: string;
|