bunchee 6.0.0-rc.0 → 6.0.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 +23 -11
- package/dist/bin/cli.js +139 -39
- package/dist/index.d.ts +1 -0
- package/dist/index.js +52 -46
- package/package.json +10 -8
package/README.md
CHANGED
|
@@ -40,7 +40,21 @@ cd ./my-lib
|
|
|
40
40
|
mkdir src && touch ./src/index.ts
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
####
|
|
43
|
+
#### Build
|
|
44
|
+
|
|
45
|
+
Then files in `src` folders will be treated as entry files and match the export names in package.json. For example:
|
|
46
|
+
`src/index.ts` will match the exports name `"."` or the only main export.
|
|
47
|
+
|
|
48
|
+
Now just run `npm run build` (or `pnpm build` / `yarn build`) if you're using these package managers, `bunchee` will find the entry files and build them.
|
|
49
|
+
The output format will based on the exports condition and also the file extension. Given an example:
|
|
50
|
+
|
|
51
|
+
- It's CommonJS for `require` and ESM for `import` based on the exports condition.
|
|
52
|
+
- 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.
|
|
53
|
+
|
|
54
|
+
> [!NOTE]
|
|
55
|
+
> All the `dependencies` and `peerDependencies` will be marked as external automatically and not included in the bundle. If you want to include them in the bundle, you can use the `--no-external` option.
|
|
56
|
+
|
|
57
|
+
#### Prepare Package
|
|
44
58
|
|
|
45
59
|
```sh
|
|
46
60
|
# Use bunchee to prepare package.json configuration
|
|
@@ -128,19 +142,17 @@ If you're using TypeScript with Node 10 and Node 16 module resolution, you can u
|
|
|
128
142
|
|
|
129
143
|
</details>
|
|
130
144
|
|
|
131
|
-
####
|
|
145
|
+
#### Lint Package
|
|
132
146
|
|
|
133
|
-
|
|
134
|
-
`src/index.ts` will match the exports name `"."` or the only main export.
|
|
147
|
+
`lint` command will check the package.json configuration is valid or not, it can valid few things like:
|
|
135
148
|
|
|
136
|
-
|
|
137
|
-
|
|
149
|
+
- if the entry files are matched with the exports conditions.
|
|
150
|
+
- if the entry files are matched with the exports paths.
|
|
138
151
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
> All the `dependencies` and `peerDependencies` will be marked as external automatically and not included in the bundle. If you want to include them in the bundle, you can use the `--no-external` option.
|
|
152
|
+
```sh
|
|
153
|
+
# Use bunchee to lint if the package.json configuration is valid
|
|
154
|
+
npm exec bunchee lint
|
|
155
|
+
```
|
|
144
156
|
|
|
145
157
|
## Usage
|
|
146
158
|
|
package/dist/bin/cli.js
CHANGED
|
@@ -6,6 +6,7 @@ var perf_hooks = require('perf_hooks');
|
|
|
6
6
|
var fs = require('fs');
|
|
7
7
|
var fsp = require('fs/promises');
|
|
8
8
|
var require$$0 = require('tty');
|
|
9
|
+
var picomatch = require('picomatch');
|
|
9
10
|
var index_js = require('../index.js');
|
|
10
11
|
require('module');
|
|
11
12
|
var glob = require('glob');
|
|
@@ -18,6 +19,7 @@ var yargs__default = /*#__PURE__*/_interopDefault(yargs);
|
|
|
18
19
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
19
20
|
var fsp__default = /*#__PURE__*/_interopDefault(fsp);
|
|
20
21
|
var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
|
|
22
|
+
var picomatch__default = /*#__PURE__*/_interopDefault(picomatch);
|
|
21
23
|
var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
|
|
22
24
|
|
|
23
25
|
const availableExtensions = new Set([
|
|
@@ -208,14 +210,23 @@ function isTypeFile(filename) {
|
|
|
208
210
|
return filename.endsWith('.d.ts') || filename.endsWith('.d.mts') || filename.endsWith('.d.cts');
|
|
209
211
|
}
|
|
210
212
|
// shared.ts -> ./shared
|
|
211
|
-
// shared.<export condition>.ts -> ./shared
|
|
213
|
+
// shared.<export condition>.ts -> ./shared.<export condition>
|
|
212
214
|
// index.ts -> ./index
|
|
213
215
|
// index.development.ts -> ./index.development
|
|
216
|
+
// foo/index.ts -> ./foo
|
|
214
217
|
function sourceFilenameToExportFullPath(filename) {
|
|
215
|
-
const
|
|
216
|
-
|
|
218
|
+
const ext = path__default.default.extname(filename);
|
|
219
|
+
const exportPath = filename.slice(0, -ext.length);
|
|
217
220
|
return relativify(exportPath);
|
|
218
221
|
}
|
|
222
|
+
// If the file is matching the private module convention file export path.
|
|
223
|
+
// './lib/_foo' -> true
|
|
224
|
+
// './_util/index' -> true
|
|
225
|
+
// './lib/_foo/bar' -> true
|
|
226
|
+
// './foo' -> false
|
|
227
|
+
function isPrivateExportPath(exportPath) {
|
|
228
|
+
return /\/_/.test(exportPath);
|
|
229
|
+
}
|
|
219
230
|
|
|
220
231
|
function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exportToDist) {
|
|
221
232
|
// End of searching, export value is file path.
|
|
@@ -362,6 +373,17 @@ function getExportTypeFromFile(filename, pkgType) {
|
|
|
362
373
|
return exportType;
|
|
363
374
|
}
|
|
364
375
|
|
|
376
|
+
const matchFile = (matchingPattern, filePath)=>{
|
|
377
|
+
return matchingPattern.some((pattern)=>{
|
|
378
|
+
// pattern is always posix
|
|
379
|
+
const normalizedPattern = path__default.default.posix.normalize(pattern);
|
|
380
|
+
const expandedPattern = normalizedPattern.endsWith('/') ? `${normalizedPattern}**` : `${normalizedPattern}/**`;
|
|
381
|
+
const matcher = picomatch__default.default(expandedPattern);
|
|
382
|
+
const normalizedFilePath = path__default.default.normalize(filePath.replace(/\\/g, '/'));
|
|
383
|
+
return matcher(normalizedFilePath);
|
|
384
|
+
});
|
|
385
|
+
};
|
|
386
|
+
|
|
365
387
|
function validateTypesFieldCondition(pair) {
|
|
366
388
|
const [outputPath, composedExportType] = pair;
|
|
367
389
|
const exportTypes = new Set(composedExportType.split('.'));
|
|
@@ -370,6 +392,42 @@ function validateTypesFieldCondition(pair) {
|
|
|
370
392
|
}
|
|
371
393
|
return false;
|
|
372
394
|
}
|
|
395
|
+
function validateFilesField(packageJson) {
|
|
396
|
+
const state = {
|
|
397
|
+
definedField: false,
|
|
398
|
+
missingFiles: []
|
|
399
|
+
};
|
|
400
|
+
const filesField = packageJson.files || [];
|
|
401
|
+
const exportsField = packageJson.exports || {};
|
|
402
|
+
state.definedField = !!packageJson.files;
|
|
403
|
+
const resolveExportsPaths = (exports)=>{
|
|
404
|
+
const paths = [];
|
|
405
|
+
if (typeof exports === 'string') {
|
|
406
|
+
paths.push(exports);
|
|
407
|
+
} else if (typeof exports === 'object') {
|
|
408
|
+
for(const key in exports){
|
|
409
|
+
paths.push(...resolveExportsPaths(exports[key]));
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return paths;
|
|
413
|
+
};
|
|
414
|
+
const exportedPaths = resolveExportsPaths(exportsField).map((p)=>path__default.default.normalize(p));
|
|
415
|
+
const commonFields = [
|
|
416
|
+
'main',
|
|
417
|
+
'module',
|
|
418
|
+
'types',
|
|
419
|
+
'module-sync'
|
|
420
|
+
];
|
|
421
|
+
for (const field of commonFields){
|
|
422
|
+
if (field in packageJson) {
|
|
423
|
+
exportedPaths.push(packageJson[field]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
state.missingFiles = exportedPaths.filter((exportPath)=>{
|
|
427
|
+
return !matchFile(filesField, exportPath);
|
|
428
|
+
});
|
|
429
|
+
return state;
|
|
430
|
+
}
|
|
373
431
|
function lint$1(pkg) {
|
|
374
432
|
const { name, main, exports } = pkg;
|
|
375
433
|
const isESM = isESModulePackage(pkg.type);
|
|
@@ -377,7 +435,7 @@ function lint$1(pkg) {
|
|
|
377
435
|
if (!name) {
|
|
378
436
|
logger.warn('Missing package name');
|
|
379
437
|
}
|
|
380
|
-
const
|
|
438
|
+
const exportsState = {
|
|
381
439
|
badMainExtension: false,
|
|
382
440
|
badMainExport: false,
|
|
383
441
|
invalidExportsFieldType: false,
|
|
@@ -404,10 +462,10 @@ function lint$1(pkg) {
|
|
|
404
462
|
if (exports) {
|
|
405
463
|
if (typeof exports === 'string') {
|
|
406
464
|
if (hasCjsExtension(exports)) {
|
|
407
|
-
|
|
465
|
+
exportsState.badMainExport = true;
|
|
408
466
|
}
|
|
409
467
|
} else if (typeof exports !== 'object') {
|
|
410
|
-
|
|
468
|
+
exportsState.invalidExportsFieldType = true;
|
|
411
469
|
} else {
|
|
412
470
|
parsedExports.forEach((outputPairs)=>{
|
|
413
471
|
for (const outputPair of outputPairs){
|
|
@@ -416,7 +474,7 @@ function lint$1(pkg) {
|
|
|
416
474
|
outputPath,
|
|
417
475
|
composedExportType
|
|
418
476
|
])) {
|
|
419
|
-
|
|
477
|
+
exportsState.badTypesExport.push([
|
|
420
478
|
outputPath,
|
|
421
479
|
composedExportType
|
|
422
480
|
]);
|
|
@@ -433,12 +491,12 @@ function lint$1(pkg) {
|
|
|
433
491
|
const requireExt = requirePath && path__default.default.extname(requirePath);
|
|
434
492
|
const importExt = importPath && path__default.default.extname(importPath);
|
|
435
493
|
if (requireExt === '.mjs' || requireExt === '.js') {
|
|
436
|
-
|
|
437
|
-
|
|
494
|
+
exportsState.badEsmRequireExport.value = true;
|
|
495
|
+
exportsState.badEsmRequireExport.paths.push(requirePath);
|
|
438
496
|
}
|
|
439
497
|
if (importExt === '.cjs') {
|
|
440
|
-
|
|
441
|
-
|
|
498
|
+
exportsState.badEsmImportExport.value = true;
|
|
499
|
+
exportsState.badEsmImportExport.paths.push(importPath);
|
|
442
500
|
}
|
|
443
501
|
}
|
|
444
502
|
});
|
|
@@ -447,15 +505,15 @@ function lint$1(pkg) {
|
|
|
447
505
|
} else {
|
|
448
506
|
// Validate CJS package
|
|
449
507
|
if (main && path__default.default.extname(main) === '.mjs') {
|
|
450
|
-
|
|
508
|
+
exportsState.badMainExtension = true;
|
|
451
509
|
}
|
|
452
510
|
if (exports) {
|
|
453
511
|
if (typeof exports === 'string') {
|
|
454
512
|
if (path__default.default.extname(exports) === '.mjs') {
|
|
455
|
-
|
|
513
|
+
exportsState.badMainExport = true;
|
|
456
514
|
}
|
|
457
515
|
} else if (typeof exports !== 'object') {
|
|
458
|
-
|
|
516
|
+
exportsState.invalidExportsFieldType = true;
|
|
459
517
|
} else {
|
|
460
518
|
parsedExports.forEach((outputPairs)=>{
|
|
461
519
|
for (const outputPair of outputPairs){
|
|
@@ -464,7 +522,7 @@ function lint$1(pkg) {
|
|
|
464
522
|
outputPath,
|
|
465
523
|
composedExportType
|
|
466
524
|
])) {
|
|
467
|
-
|
|
525
|
+
exportsState.badTypesExport.push([
|
|
468
526
|
outputPath,
|
|
469
527
|
composedExportType
|
|
470
528
|
]);
|
|
@@ -481,64 +539,78 @@ function lint$1(pkg) {
|
|
|
481
539
|
const requireExt = requirePath && path__default.default.extname(requirePath);
|
|
482
540
|
const importExt = importPath && path__default.default.extname(importPath);
|
|
483
541
|
if (requireExt === '.mjs') {
|
|
484
|
-
|
|
485
|
-
|
|
542
|
+
exportsState.badCjsRequireExport.value = true;
|
|
543
|
+
exportsState.badCjsRequireExport.paths.push(requirePath);
|
|
486
544
|
}
|
|
487
545
|
if (importExt === '.js' || importExt === '.cjs') {
|
|
488
|
-
|
|
489
|
-
|
|
546
|
+
exportsState.badCjsImportExport.value = true;
|
|
547
|
+
exportsState.badCjsImportExport.paths.push(importPath);
|
|
490
548
|
}
|
|
491
549
|
}
|
|
492
550
|
});
|
|
493
551
|
}
|
|
494
552
|
}
|
|
495
553
|
}
|
|
496
|
-
|
|
554
|
+
const fieldState = validateFilesField(pkg);
|
|
555
|
+
const warningsCount = exportsState.badTypesExport.length + fieldState.missingFiles.length;
|
|
556
|
+
if (warningsCount) {
|
|
557
|
+
logger.warn(`Lint: ${warningsCount} issues found.`);
|
|
558
|
+
} else {
|
|
559
|
+
logger.info(`Lint: package.json is healthy.`);
|
|
560
|
+
}
|
|
561
|
+
if (!fieldState.definedField) {
|
|
562
|
+
logger.warn('Missing files field in package.json');
|
|
563
|
+
} else if (fieldState.missingFiles.length) {
|
|
564
|
+
logger.warn('Missing files in package.json');
|
|
565
|
+
fieldState.missingFiles.forEach((p)=>{
|
|
566
|
+
logger.warn(` ${p}`);
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
if (exportsState.badMainExtension) {
|
|
497
570
|
logger.warn('Cannot export `main` field with .mjs extension in CJS package, only .js extension is allowed');
|
|
498
571
|
}
|
|
499
|
-
if (
|
|
572
|
+
if (exportsState.badMainExport) {
|
|
500
573
|
if (isESM) {
|
|
501
574
|
logger.warn('Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
|
|
502
575
|
} else {
|
|
503
576
|
logger.warn('Cannot export `exports` field with .mjs extension in CJS package, only .js and .cjs extensions are allowed');
|
|
504
577
|
}
|
|
505
578
|
}
|
|
506
|
-
if (
|
|
579
|
+
if (exportsState.invalidExportsFieldType) {
|
|
507
580
|
logger.warn('Invalid exports field type, only object or string is allowed');
|
|
508
581
|
}
|
|
509
|
-
if (
|
|
582
|
+
if (exportsState.badCjsRequireExport.value) {
|
|
510
583
|
logger.warn('Cannot export `require` field with .mjs extension in CJS package, only .cjs and .js extensions are allowed');
|
|
511
|
-
|
|
584
|
+
exportsState.badCjsRequireExport.paths.forEach((p)=>{
|
|
512
585
|
logger.warn(` ${p}`);
|
|
513
586
|
});
|
|
514
587
|
}
|
|
515
|
-
if (
|
|
588
|
+
if (exportsState.badCjsImportExport.value) {
|
|
516
589
|
logger.warn('Cannot export `import` field with .js or .cjs extension in CJS package, only .mjs extensions are allowed');
|
|
517
|
-
|
|
590
|
+
exportsState.badCjsImportExport.paths.forEach((p)=>{
|
|
518
591
|
logger.warn(` ${p}`);
|
|
519
592
|
});
|
|
520
593
|
}
|
|
521
|
-
if (
|
|
594
|
+
if (exportsState.badEsmRequireExport.value) {
|
|
522
595
|
logger.warn('Cannot export `require` field with .js or .mjs extension in ESM package, only .cjs extensions are allowed');
|
|
523
|
-
|
|
596
|
+
exportsState.badEsmRequireExport.paths.forEach((p)=>{
|
|
524
597
|
logger.warn(` ${p}`);
|
|
525
598
|
});
|
|
526
599
|
}
|
|
527
|
-
if (
|
|
600
|
+
if (exportsState.badEsmImportExport.value) {
|
|
528
601
|
logger.warn('Cannot export `import` field with .cjs extension in ESM package, only .js and .mjs extensions are allowed');
|
|
529
|
-
|
|
602
|
+
exportsState.badEsmImportExport.paths.forEach((p)=>{
|
|
530
603
|
logger.warn(` ${p}`);
|
|
531
604
|
});
|
|
532
605
|
}
|
|
533
|
-
if (
|
|
534
|
-
|
|
606
|
+
if (exportsState.badTypesExport.length) {
|
|
607
|
+
exportsState.badTypesExport.forEach(([outputPath, composedExportType])=>{
|
|
535
608
|
logger.error(`Bad export types field with ${composedExportType} in ${outputPath}, use "types" export condition for it`);
|
|
536
609
|
});
|
|
537
|
-
process.exit(1);
|
|
538
610
|
}
|
|
539
611
|
}
|
|
540
612
|
|
|
541
|
-
var version = "6.0.0
|
|
613
|
+
var version = "6.0.0";
|
|
542
614
|
|
|
543
615
|
async function writeDefaultTsconfig(tsConfigPath) {
|
|
544
616
|
await fs.promises.writeFile(tsConfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
|
|
@@ -682,7 +754,8 @@ async function collectSourceEntries(sourceFolderPath) {
|
|
|
682
754
|
for (const file of binMatches){
|
|
683
755
|
// convert relative path to export path
|
|
684
756
|
const exportPath = sourceFilenameToExportFullPath(file);
|
|
685
|
-
|
|
757
|
+
const binExportPath = exportPath.replace(/^\.[\//]bin/, BINARY_TAG);
|
|
758
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, binExportPath, bins, exportsEntries);
|
|
686
759
|
}
|
|
687
760
|
for (const file of srcMatches){
|
|
688
761
|
const binExportPath = file.replace(/^bin/, BINARY_TAG)// Remove index.<ext> to [^index].<ext> to build the export path
|
|
@@ -914,12 +987,21 @@ function logOutputState(stats) {
|
|
|
914
987
|
}).forEach((item, index)=>{
|
|
915
988
|
const [filename, , size] = item;
|
|
916
989
|
const normalizedExportName = normalizeExportName(exportName);
|
|
917
|
-
const
|
|
990
|
+
const exportNameWithPadding = index === 0 ? // For other formats, just show the padding spaces.
|
|
991
|
+
normalizedExportName : ' '.repeat(normalizedExportName.length);
|
|
918
992
|
const filenamePadding = ' '.repeat(Math.max(maxLengthOfExportName, 'Exports'.length) - normalizedExportName.length);
|
|
919
993
|
const isType = isTypeFile(filename);
|
|
920
|
-
const sizePadding = ' '.repeat(Math.max(maxFilenameLength, 'File'.length) - filename.length);
|
|
921
994
|
const prettiedSize = prettyBytes__default.default(size);
|
|
922
|
-
|
|
995
|
+
const sizePadding = ' '.repeat(Math.max(maxFilenameLength, 'File'.length) - filename.length);
|
|
996
|
+
// Logging shared in debug mode
|
|
997
|
+
if (isPrivateExportPath(exportName)) {
|
|
998
|
+
if (index === 0 && process.env.DEBUG) {
|
|
999
|
+
const sizePadding = ' '.repeat(Math.max(maxFilenameLength, 'File'.length) - 'private shared'.length);
|
|
1000
|
+
console.log(pc.dim(normalizeExportName(exportName)), filenamePadding, pc.dim('private shared'), sizePadding, pc.dim(prettiedSize));
|
|
1001
|
+
}
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
console.log(exportNameWithPadding, filenamePadding, `${pc[isType ? 'dim' : 'bold'](filename)}`, sizePadding, prettiedSize);
|
|
923
1005
|
});
|
|
924
1006
|
});
|
|
925
1007
|
}
|
|
@@ -928,7 +1010,8 @@ const helpMessage = `
|
|
|
928
1010
|
Usage: bunchee [options]
|
|
929
1011
|
|
|
930
1012
|
Commands:
|
|
931
|
-
prepare auto
|
|
1013
|
+
prepare auto setup package.json for building
|
|
1014
|
+
lint lint configuration in package.json
|
|
932
1015
|
|
|
933
1016
|
Options:
|
|
934
1017
|
-v, --version output the version number
|
|
@@ -1020,11 +1103,25 @@ async function parseCliArgs(argv) {
|
|
|
1020
1103
|
}).option('dts-bundle', {
|
|
1021
1104
|
type: 'boolean',
|
|
1022
1105
|
description: 'bundle type declaration files'
|
|
1106
|
+
}).option('prepare', {
|
|
1107
|
+
type: 'boolean',
|
|
1108
|
+
description: 'auto setup package.json for building'
|
|
1023
1109
|
}).command('prepare', 'auto configure package.json exports for building', ()=>{}, (argv)=>{
|
|
1024
1110
|
return prepare(argv.cwd || process.cwd());
|
|
1025
1111
|
}).command('lint', 'lint package.json', ()=>{}, (argv)=>{
|
|
1026
1112
|
return lint(argv.cwd || process.cwd());
|
|
1027
1113
|
}).version(version).help('help', 'output usage information').showHelpOnFail(true).parse();
|
|
1114
|
+
const cmd = args._[0];
|
|
1115
|
+
if (cmd === 'prepare' || cmd === 'lint') {
|
|
1116
|
+
return {
|
|
1117
|
+
cmd
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
// Warn about this command being deprecated
|
|
1121
|
+
if (args['prepare']) {
|
|
1122
|
+
logger.warn('The "--prepare" option is deprecated. Please use `bunchee prepare` instead.');
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1028
1125
|
const source = args._[0];
|
|
1029
1126
|
const parsedArgs = {
|
|
1030
1127
|
source,
|
|
@@ -1170,6 +1267,9 @@ async function main() {
|
|
|
1170
1267
|
// if (!error) help()
|
|
1171
1268
|
return exit(error);
|
|
1172
1269
|
}
|
|
1270
|
+
if ('cmd' in params) {
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1173
1273
|
await run(params);
|
|
1174
1274
|
}
|
|
1175
1275
|
function logWatcherBuildTime(result, spinner) {
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -369,12 +369,13 @@ function isBinExportPath(exportPath) {
|
|
|
369
369
|
return exportPath === BINARY_TAG || exportPath.startsWith(BINARY_TAG + '/');
|
|
370
370
|
}
|
|
371
371
|
// shared.ts -> ./shared
|
|
372
|
-
// shared.<export condition>.ts -> ./shared
|
|
372
|
+
// shared.<export condition>.ts -> ./shared.<export condition>
|
|
373
373
|
// index.ts -> ./index
|
|
374
374
|
// index.development.ts -> ./index.development
|
|
375
|
+
// foo/index.ts -> ./foo
|
|
375
376
|
function sourceFilenameToExportFullPath(filename) {
|
|
376
|
-
const
|
|
377
|
-
|
|
377
|
+
const ext = path__default.default.extname(filename);
|
|
378
|
+
const exportPath = filename.slice(0, -ext.length);
|
|
378
379
|
return relativify(exportPath);
|
|
379
380
|
}
|
|
380
381
|
|
|
@@ -690,6 +691,10 @@ function getSpecialExportTypeFromComposedExportPath(composedExportType) {
|
|
|
690
691
|
}
|
|
691
692
|
return 'default';
|
|
692
693
|
}
|
|
694
|
+
function getSpecialExportTypeFromSourcePath(sourcePath) {
|
|
695
|
+
const fileBaseName = baseNameWithoutExtension(sourcePath);
|
|
696
|
+
return getSpecialExportTypeFromComposedExportPath(fileBaseName);
|
|
697
|
+
}
|
|
693
698
|
function getExportTypeFromExportTypesArray(types) {
|
|
694
699
|
let exportType = 'default';
|
|
695
700
|
new Set(types).forEach((value)=>{
|
|
@@ -820,36 +825,61 @@ async function collectSourceEntriesByExportPath(sourceFolderPath, originalSubpat
|
|
|
820
825
|
}
|
|
821
826
|
// Search private shared module files which are not in the parsedExportsInfo, but start with _.
|
|
822
827
|
// e.g. _utils.ts, _utils/index.ts
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
828
|
+
// e.g. _utils.development.ts, _utils/index.development.js
|
|
829
|
+
const privatePattern = [
|
|
830
|
+
`**/_*{,/index}.{${[
|
|
831
|
+
...availableExtensions
|
|
832
|
+
].join(',')}}`,
|
|
833
|
+
`**/_*{,/index}.{${[
|
|
834
|
+
...runtimeExportConventions
|
|
835
|
+
].join(',')}}.{${[
|
|
836
|
+
...availableExtensions
|
|
837
|
+
].join(',')}}`
|
|
838
|
+
];
|
|
826
839
|
const privateFiles = await glob.glob(privatePattern, {
|
|
827
840
|
cwd: sourceFolderPath,
|
|
828
841
|
nodir: true
|
|
829
842
|
});
|
|
830
843
|
for (const file of privateFiles){
|
|
831
844
|
const sourceFileAbsolutePath = path.join(sourceFolderPath, file);
|
|
832
|
-
const
|
|
833
|
-
const exportPath = filePathWithoutExtension(relativeFilePath);
|
|
834
|
-
// exportsEntries.set(specialExportPath, sourceFilesMap)
|
|
845
|
+
const exportPath = sourceFilenameToExportFullPath(file);
|
|
835
846
|
const isEsmPkg = isESModulePackage(pkg.type);
|
|
836
|
-
//
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
847
|
+
// const specialItems: [string, string][] = []
|
|
848
|
+
const specialExportType = getSpecialExportTypeFromSourcePath(file);
|
|
849
|
+
const normalizedExportPath = stripSpecialCondition(exportPath);
|
|
850
|
+
const isSpecialExport = specialExportType !== 'default';
|
|
851
|
+
// export type: default => ''
|
|
852
|
+
// export type: development => '.development'
|
|
853
|
+
const condPart = isSpecialExport ? specialExportType + '.' : '';
|
|
854
|
+
// Map private shared files to the dist directory
|
|
855
|
+
// e.g. ./_utils.ts -> ./dist/_utils.js
|
|
856
|
+
const privateExportInfo = [
|
|
840
857
|
[
|
|
841
858
|
relativify(path.join('./dist', exportPath + (isEsmPkg ? '.js' : '.mjs'))),
|
|
842
|
-
'import'
|
|
859
|
+
condPart + 'import'
|
|
843
860
|
],
|
|
844
861
|
[
|
|
845
862
|
relativify(path.join('./dist', exportPath + (isEsmPkg ? '.cjs' : '.js'))),
|
|
846
|
-
'require'
|
|
863
|
+
condPart + 'require'
|
|
847
864
|
]
|
|
848
|
-
]
|
|
865
|
+
];
|
|
866
|
+
const exportsInfo = parsedExportsInfo.get(normalizedExportPath);
|
|
867
|
+
if (!exportsInfo) {
|
|
868
|
+
// Add private shared files to parsedExportsInfo
|
|
869
|
+
parsedExportsInfo.set(normalizedExportPath, privateExportInfo);
|
|
870
|
+
} else {
|
|
871
|
+
// Merge private shared files to the existing exportsInfo
|
|
872
|
+
exportsInfo.push(...privateExportInfo);
|
|
873
|
+
}
|
|
849
874
|
// Insert private shared modules into the entries
|
|
850
|
-
exportsEntries.
|
|
851
|
-
|
|
852
|
-
|
|
875
|
+
const entry = exportsEntries.get(exportPath);
|
|
876
|
+
if (!entry) {
|
|
877
|
+
exportsEntries.set(exportPath, {
|
|
878
|
+
[specialExportType]: sourceFileAbsolutePath
|
|
879
|
+
});
|
|
880
|
+
} else {
|
|
881
|
+
entry[specialExportType] = sourceFileAbsolutePath;
|
|
882
|
+
}
|
|
853
883
|
}
|
|
854
884
|
return {
|
|
855
885
|
bins,
|
|
@@ -1026,7 +1056,7 @@ async function writeDefaultTsconfig(tsConfigPath) {
|
|
|
1026
1056
|
const FILENAME_REGEX = /__filename/;
|
|
1027
1057
|
const DIRNAME_REGEX = /__dirname/;
|
|
1028
1058
|
// not char, or space before require(.resolve)?(
|
|
1029
|
-
const GLOBAL_REQUIRE_REGEX = /(?:^|[^.\w'"`])\brequire(\.resolve)?\(\s*[\r\n]*(['"`])/;
|
|
1059
|
+
const GLOBAL_REQUIRE_REGEX = /(?:^|[^.\w'"`])\brequire(\.resolve)?\(\s*[\r\n]*(\w|['"`])/;
|
|
1030
1060
|
const PolyfillComment = '/** rollup-private-do-not-use-esm-shim-polyfill */';
|
|
1031
1061
|
const createESMShim = ({ filename, dirname, globalRequire })=>{
|
|
1032
1062
|
const useNodeUrl = filename || dirname;
|
|
@@ -1303,28 +1333,6 @@ function aliasEntries({ entry: sourceFilePath, conditionNames, entries, format,
|
|
|
1303
1333
|
};
|
|
1304
1334
|
}
|
|
1305
1335
|
|
|
1306
|
-
function prependDirectives() {
|
|
1307
|
-
return {
|
|
1308
|
-
name: 'prependDirective',
|
|
1309
|
-
transform: {
|
|
1310
|
-
order: 'post',
|
|
1311
|
-
handler (code, id) {
|
|
1312
|
-
var _moduleInfo_meta;
|
|
1313
|
-
const moduleInfo = this.getModuleInfo(id);
|
|
1314
|
-
if (moduleInfo == null ? void 0 : (_moduleInfo_meta = moduleInfo.meta) == null ? void 0 : _moduleInfo_meta.preserveDirectives) {
|
|
1315
|
-
const firstDirective = moduleInfo.meta.preserveDirectives.directives[0];
|
|
1316
|
-
if (firstDirective) {
|
|
1317
|
-
const directive = firstDirective.value;
|
|
1318
|
-
const directiveCode = `'${directive}';`;
|
|
1319
|
-
return directiveCode + '\n' + code;
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
return null;
|
|
1323
|
-
}
|
|
1324
|
-
}
|
|
1325
|
-
};
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1328
1336
|
const prependShebang = (entry)=>({
|
|
1329
1337
|
name: 'prependShebang',
|
|
1330
1338
|
transform: (code, id)=>{
|
|
@@ -1508,8 +1516,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
|
|
|
1508
1516
|
}),
|
|
1509
1517
|
commonjs__default.default({
|
|
1510
1518
|
exclude: bundleConfig.external || null
|
|
1511
|
-
})
|
|
1512
|
-
prependDirectives()
|
|
1519
|
+
})
|
|
1513
1520
|
]).filter(isNotNull);
|
|
1514
1521
|
return {
|
|
1515
1522
|
input: entry,
|
|
@@ -1794,11 +1801,10 @@ function runWatch({ input, output }) {
|
|
|
1794
1801
|
}
|
|
1795
1802
|
function catchErrorHandler(error) {
|
|
1796
1803
|
if (!error) return;
|
|
1797
|
-
logger.error(error);
|
|
1798
1804
|
// filter out the rollup plugin error information such as loc/frame/code...
|
|
1799
1805
|
const err = new Error(error.message);
|
|
1800
1806
|
err.stack = error.stack;
|
|
1801
|
-
throw
|
|
1807
|
+
throw error;
|
|
1802
1808
|
}
|
|
1803
1809
|
|
|
1804
1810
|
function assignDefault(options, name, defaultValue) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunchee",
|
|
3
|
-
"version": "6.0.0
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "zero config bundler for js/ts/jsx libraries",
|
|
5
5
|
"bin": "./dist/bin/cli.js",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -41,18 +41,19 @@
|
|
|
41
41
|
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
42
42
|
"@rollup/plugin-replace": "^6.0.1",
|
|
43
43
|
"@rollup/plugin-wasm": "^6.2.2",
|
|
44
|
-
"@rollup/pluginutils": "^5.1.
|
|
44
|
+
"@rollup/pluginutils": "^5.1.3",
|
|
45
45
|
"@swc/core": "^1.9.3",
|
|
46
46
|
"@swc/helpers": "^0.5.11",
|
|
47
47
|
"clean-css": "^5.3.3",
|
|
48
48
|
"glob": "^11.0.0",
|
|
49
|
-
"magic-string": "^0.30.
|
|
49
|
+
"magic-string": "^0.30.14",
|
|
50
50
|
"ora": "^8.0.1",
|
|
51
|
+
"picomatch": "^4.0.2",
|
|
51
52
|
"pretty-bytes": "^5.6.0",
|
|
52
53
|
"rollup": "^4.27.4",
|
|
53
54
|
"rollup-plugin-dts": "^6.1.1",
|
|
54
55
|
"rollup-plugin-swc3": "^0.11.1",
|
|
55
|
-
"rollup-preserve-directives": "^1.1.
|
|
56
|
+
"rollup-preserve-directives": "^1.1.3",
|
|
56
57
|
"tslib": "^2.7.0",
|
|
57
58
|
"yargs": "^17.7.2"
|
|
58
59
|
},
|
|
@@ -69,12 +70,13 @@
|
|
|
69
70
|
},
|
|
70
71
|
"devDependencies": {
|
|
71
72
|
"@huozhi/testing-package": "1.0.0",
|
|
72
|
-
"@swc-node/register": "^1.9
|
|
73
|
+
"@swc-node/register": "^1.10.9",
|
|
73
74
|
"@swc/jest": "^0.2.31",
|
|
74
75
|
"@swc/types": "^0.1.9",
|
|
75
76
|
"@types/clean-css": "^4.2.11",
|
|
76
77
|
"@types/jest": "29.0.0",
|
|
77
78
|
"@types/node": "^22.9.3",
|
|
79
|
+
"@types/picomatch": "^3.0.1",
|
|
78
80
|
"@types/yargs": "^17.0.33",
|
|
79
81
|
"bunchee": "link:./",
|
|
80
82
|
"cross-env": "^7.0.3",
|
|
@@ -114,10 +116,10 @@
|
|
|
114
116
|
"test:ci": "[ -z $CI ] || pnpm test",
|
|
115
117
|
"clean": "rm -rf ./dist",
|
|
116
118
|
"typecheck": "tsc --noEmit && tsc -p test/tsconfig.json --noEmit",
|
|
117
|
-
"
|
|
118
|
-
"ts-bunchee": "pnpm
|
|
119
|
+
"run-ts": "cross-env SWC_NODE_IGNORE_DYNAMIC=1 node -r @swc-node/register",
|
|
120
|
+
"ts-bunchee": "pnpm run-ts ./src/bin/index.ts",
|
|
119
121
|
"build-dir": "pnpm ts-bunchee --cwd",
|
|
120
|
-
"build": "pnpm
|
|
122
|
+
"build": "pnpm run-ts ./src/bin/index.ts --runtime node",
|
|
121
123
|
"format": "prettier --write ."
|
|
122
124
|
}
|
|
123
125
|
}
|