bunchee 2.0.0-beta.4 → 2.0.1
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 +62 -11
- package/dist/cli.js +11 -4
- package/dist/lib.js +56 -20
- package/dist/src/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ Leverage `exports` field to support different conditions would be also ideal. Mo
|
|
|
54
54
|
"exports": {
|
|
55
55
|
"require": "dist/index.cjs",
|
|
56
56
|
"import": "dist/index.mjs",
|
|
57
|
-
"module": "dist/index.esm.js"
|
|
57
|
+
"module": "dist/index.esm.js"
|
|
58
58
|
},
|
|
59
59
|
"scripts": {
|
|
60
60
|
"build": "bunchee ./src/index.js"
|
|
@@ -85,15 +85,7 @@ Usage:
|
|
|
85
85
|
$ bunchee ./src/index.ts -o ./dist/bundle.js # specify the dist file path
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```js
|
|
91
|
-
import { bundle } from 'bunchee'
|
|
92
|
-
|
|
93
|
-
// options is same to CLI options
|
|
94
|
-
await bundle(entryFilePath, options)
|
|
95
|
-
```
|
|
96
|
-
#### Example Scripts
|
|
88
|
+
Run bunchee via CLI
|
|
97
89
|
|
|
98
90
|
```sh
|
|
99
91
|
cd <project-root-dir>
|
|
@@ -104,7 +96,16 @@ bunchee ./src/index.js -f esm -o ./dist/bundle.esm.js
|
|
|
104
96
|
# bunchee ./src/index.js -o ./dist/bundle.esm.js
|
|
105
97
|
```
|
|
106
98
|
|
|
107
|
-
###
|
|
99
|
+
### Node.js API
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
import { bundle } from 'bunchee'
|
|
103
|
+
|
|
104
|
+
// options is same to CLI options
|
|
105
|
+
await bundle(entryFilePath, options)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Typescript
|
|
108
109
|
|
|
109
110
|
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.
|
|
110
111
|
|
|
@@ -112,4 +113,54 @@ By default bunchee includes Typescript v3.9.x inside as a dependency. If you wan
|
|
|
112
113
|
yarn add -D bunchee typescript
|
|
113
114
|
```
|
|
114
115
|
|
|
116
|
+
Create `tsconfig.json` to specify any compiler options for TypeScript.
|
|
117
|
+
|
|
115
118
|
This library requires at least [TypeScript 3.7](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html).
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
## Advanced
|
|
122
|
+
|
|
123
|
+
### Multiple Exports
|
|
124
|
+
|
|
125
|
+
While `exports` filed is becoming the standard of exporting in node.js, bunchee also supports to build multiple exports all in one command.
|
|
126
|
+
|
|
127
|
+
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:
|
|
128
|
+
|
|
129
|
+
* `index.ts` will match `"."` export name or the if there's only one main export.
|
|
130
|
+
* `lite.ts` will match `"./lite"` export name.
|
|
131
|
+
|
|
132
|
+
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.
|
|
133
|
+
|
|
134
|
+
#### How it works
|
|
135
|
+
|
|
136
|
+
Assuming you have main entry export `"."` and subpath export `"./lite"` with different exports condition listed in package.json
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
{
|
|
140
|
+
"name": "example",
|
|
141
|
+
"scripts": {
|
|
142
|
+
"build": "bunchee"
|
|
143
|
+
},
|
|
144
|
+
"exports": {
|
|
145
|
+
"./lite": "./dist/lite.js"
|
|
146
|
+
".": {
|
|
147
|
+
"import": "./dist/index.mjs",
|
|
148
|
+
"require": "./dist/index.cjs"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Then you need to add two entry files `index.ts` and `lite.ts` in project root directory to match the export name `"."` and `"./lite"`, bunchee will associate these entry files with export names then use them as input source and output paths information.
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
- example/
|
|
158
|
+
|- lite.ts
|
|
159
|
+
|- index.ts
|
|
160
|
+
|- src/
|
|
161
|
+
|- package.json
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### License
|
|
165
|
+
|
|
166
|
+
MIT
|
package/dist/cli.js
CHANGED
|
@@ -87,7 +87,7 @@ var logger = {
|
|
|
87
87
|
}
|
|
88
88
|
};
|
|
89
89
|
|
|
90
|
-
var version = "2.0.
|
|
90
|
+
var version = "2.0.1";
|
|
91
91
|
|
|
92
92
|
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. false by default\n -o, --output <file> specify output filename\n -f, --format <format> specify bundle type: "esm", "cjs", "umd". "esm" by default\n -e, --external <mod> specify an external dependency\n --target <target> js features target: swc target es versions. "es5" by default\n --runtime <runtime> build runtime: "nodejs", "browser". "browser" by default\n --sourcemap enable sourcemap generation, false by default\n --cwd <cwd> specify current working directory\n -h, --help output usage information\n';
|
|
93
93
|
function help() {
|
|
@@ -99,7 +99,7 @@ function exit(err) {
|
|
|
99
99
|
}
|
|
100
100
|
function run(args) {
|
|
101
101
|
return tslib.__awaiter(this, void 0, void 0, function() {
|
|
102
|
-
var source, format, watch, minify, sourcemap, target, runtime, cwd, file, outputConfig, entry, bundle, err_1;
|
|
102
|
+
var source, format, watch, minify, sourcemap, target, runtime, cwd, file, outputConfig, entry, bundle, timeStart, timeEnd, err_1, duration;
|
|
103
103
|
return tslib.__generator(this, function(_a) {
|
|
104
104
|
switch(_a.label){
|
|
105
105
|
case 0:
|
|
@@ -131,6 +131,7 @@ function run(args) {
|
|
|
131
131
|
}
|
|
132
132
|
entry = source ? path__default["default"].resolve(cwd, source) : "";
|
|
133
133
|
bundle = require("./lib").bundle;
|
|
134
|
+
timeStart = Date.now();
|
|
134
135
|
_a.label = 1;
|
|
135
136
|
case 1:
|
|
136
137
|
_a.trys.push([
|
|
@@ -144,9 +145,11 @@ function run(args) {
|
|
|
144
145
|
bundle(entry, outputConfig)
|
|
145
146
|
];
|
|
146
147
|
case 2:
|
|
148
|
+
_a.sent();
|
|
149
|
+
timeEnd = Date.now();
|
|
147
150
|
return [
|
|
148
|
-
|
|
149
|
-
|
|
151
|
+
3,
|
|
152
|
+
4
|
|
150
153
|
];
|
|
151
154
|
case 3:
|
|
152
155
|
err_1 = _a.sent();
|
|
@@ -159,6 +162,10 @@ function run(args) {
|
|
|
159
162
|
}
|
|
160
163
|
throw err_1;
|
|
161
164
|
case 4:
|
|
165
|
+
duration = timeEnd - timeStart;
|
|
166
|
+
if (!watch) {
|
|
167
|
+
logger.log("✅ Finished in ".concat(duration, " ms"));
|
|
168
|
+
}
|
|
162
169
|
return [
|
|
163
170
|
2
|
|
164
171
|
];
|
package/dist/lib.js
CHANGED
|
@@ -150,7 +150,7 @@ function createInputConfig(entry, pkg, options) {
|
|
|
150
150
|
typescript: resolveTypescript(),
|
|
151
151
|
jsx: "react",
|
|
152
152
|
module: "ES6",
|
|
153
|
-
target:
|
|
153
|
+
target: jscTarget,
|
|
154
154
|
noEmitOnError: !options.watch,
|
|
155
155
|
sourceMap: options.sourcemap,
|
|
156
156
|
declaration: !!typings,
|
|
@@ -338,7 +338,7 @@ function getSubExportDist(pkg, exportCondition) {
|
|
|
338
338
|
return dist;
|
|
339
339
|
}
|
|
340
340
|
function createRollupConfig(entry, pkg, cliArgs) {
|
|
341
|
-
var _a;
|
|
341
|
+
var _a, _b;
|
|
342
342
|
var file = cliArgs.file;
|
|
343
343
|
var ext = path.extname(entry);
|
|
344
344
|
var useTypescript = ext === ".ts" || ext === ".tsx";
|
|
@@ -366,7 +366,8 @@ function createRollupConfig(entry, pkg, cliArgs) {
|
|
|
366
366
|
}
|
|
367
367
|
return {
|
|
368
368
|
input: inputOptions,
|
|
369
|
-
output: outputConfigs
|
|
369
|
+
output: outputConfigs,
|
|
370
|
+
exportName: ((_b = options.exportCondition) === null || _b === void 0 ? void 0 : _b.name) || pkg.name
|
|
370
371
|
};
|
|
371
372
|
}
|
|
372
373
|
|
|
@@ -409,10 +410,16 @@ function bundle(entryPath, _a) {
|
|
|
409
410
|
if (options.format === "esm") {
|
|
410
411
|
options.format = "es";
|
|
411
412
|
}
|
|
412
|
-
var
|
|
413
|
-
var packageExports =
|
|
413
|
+
var pkg = getPackageMeta();
|
|
414
|
+
var packageExports = pkg.exports;
|
|
414
415
|
var isSingleEntry = typeof packageExports === "string";
|
|
415
416
|
var hasMultiEntries = packageExports && !isSingleEntry && Object.keys(packageExports).length > 0;
|
|
417
|
+
var bundleOrWatch = function bundleOrWatch(rollupConfig) {
|
|
418
|
+
if (options.watch) {
|
|
419
|
+
return Promise.resolve(runWatch(pkg, rollupConfig));
|
|
420
|
+
}
|
|
421
|
+
return runBundle(pkg, rollupConfig);
|
|
422
|
+
};
|
|
416
423
|
if (isSingleEntry) {
|
|
417
424
|
entryPath = getSourcePathFromExportPath(config.rootDir, ".");
|
|
418
425
|
}
|
|
@@ -432,22 +439,26 @@ function bundle(entryPath, _a) {
|
|
|
432
439
|
name: entryExport,
|
|
433
440
|
export: packageExports[entryExport]
|
|
434
441
|
};
|
|
435
|
-
var rollupConfig = createRollupConfig(path.resolve(cwd, source),
|
|
442
|
+
var rollupConfig = createRollupConfig(path.resolve(cwd, source), pkg, options);
|
|
436
443
|
return rollupConfig;
|
|
444
|
+
}).filter(function(v) {
|
|
445
|
+
return !!v;
|
|
437
446
|
});
|
|
438
|
-
return Promise.all(rollupConfigs.
|
|
439
|
-
return
|
|
447
|
+
return Promise.all(rollupConfigs.map(function(rollupConfig) {
|
|
448
|
+
return bundleOrWatch(rollupConfig);
|
|
440
449
|
}));
|
|
441
450
|
}
|
|
442
451
|
}
|
|
443
|
-
var rollupConfig = createRollupConfig(entryPath,
|
|
444
|
-
|
|
445
|
-
return Promise.resolve(runWatch(rollupConfig));
|
|
446
|
-
}
|
|
447
|
-
return runBundle(rollupConfig);
|
|
452
|
+
var rollupConfig = createRollupConfig(entryPath, pkg, options);
|
|
453
|
+
return bundleOrWatch(rollupConfig);
|
|
448
454
|
}
|
|
449
|
-
function
|
|
450
|
-
var
|
|
455
|
+
function getExportPath(pkg, exportName) {
|
|
456
|
+
var name = pkg.name || "<package>";
|
|
457
|
+
if (exportName === "." || !exportName) return name;
|
|
458
|
+
return path.join(name, exportName);
|
|
459
|
+
}
|
|
460
|
+
function runWatch(pkg, _a) {
|
|
461
|
+
var exportName = _a.exportName, input = _a.input, output = _a.output;
|
|
451
462
|
var watchOptions = [
|
|
452
463
|
tslib.__assign(tslib.__assign({}, input), {
|
|
453
464
|
output: output,
|
|
@@ -459,21 +470,46 @@ function runWatch(_a) {
|
|
|
459
470
|
}),
|
|
460
471
|
];
|
|
461
472
|
var watcher = rollup.watch(watchOptions);
|
|
473
|
+
var exportPath = getExportPath(pkg, exportName);
|
|
474
|
+
var startTime = Date.now();
|
|
462
475
|
watcher.on("event", function(event) {
|
|
463
|
-
|
|
464
|
-
|
|
476
|
+
switch(event.code){
|
|
477
|
+
case "ERROR":
|
|
478
|
+
{
|
|
479
|
+
return onError(event.error);
|
|
480
|
+
}
|
|
481
|
+
case "START":
|
|
482
|
+
{
|
|
483
|
+
startTime = Date.now();
|
|
484
|
+
logger.log("Start building ".concat(exportPath, " ..."));
|
|
485
|
+
}
|
|
486
|
+
case "END":
|
|
487
|
+
{
|
|
488
|
+
var duration = Date.now() - startTime;
|
|
489
|
+
if (duration > 0) {
|
|
490
|
+
logger.log("✨ Built ".concat(exportPath));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
default:
|
|
494
|
+
return;
|
|
465
495
|
}
|
|
466
496
|
});
|
|
467
497
|
return watcher;
|
|
468
498
|
}
|
|
469
|
-
function runBundle(_a) {
|
|
470
|
-
var input = _a.input, output = _a.output;
|
|
499
|
+
function runBundle(pkg, _a) {
|
|
500
|
+
var exportName = _a.exportName, input = _a.input, output = _a.output;
|
|
501
|
+
var startTime = Date.now();
|
|
471
502
|
return rollup.rollup(input).then(function(bundle) {
|
|
472
503
|
var writeJobs = output.map(function(options) {
|
|
473
504
|
return bundle.write(options);
|
|
474
505
|
});
|
|
475
506
|
return Promise.all(writeJobs);
|
|
476
|
-
}, onError)
|
|
507
|
+
}, onError).then(function() {
|
|
508
|
+
var duration = Date.now() - startTime;
|
|
509
|
+
if (duration > 0) {
|
|
510
|
+
logger.log("✨ Built ".concat(getExportPath(pkg, exportName)));
|
|
511
|
+
}
|
|
512
|
+
});
|
|
477
513
|
}
|
|
478
514
|
function onError(error) {
|
|
479
515
|
if (!error) return;
|
package/dist/src/types.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ declare type PackageMetadata = {
|
|
|
27
27
|
};
|
|
28
28
|
declare type ExportCondition = string | Record<ExportType, string>;
|
|
29
29
|
declare type BuncheeRollupConfig = Partial<Omit<RollupOptions, 'input' | 'output'>> & {
|
|
30
|
+
exportName?: string;
|
|
30
31
|
input: InputOptions;
|
|
31
32
|
output: OutputOptions[];
|
|
32
33
|
};
|