bunchee 5.0.0 → 5.1.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 +1 -1
- package/dist/bin/cli.js +149 -79
- package/dist/index.js +247 -158
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
</a>
|
|
15
15
|
</p>
|
|
16
16
|
|
|
17
|
-
**bunchee** is a zero configuration bundler makes bundling JS/
|
|
17
|
+
**bunchee** is a zero configuration bundler makes bundling JS/TS library effortless. It's built on top of Rollup and SWC ⚡️, allowing you to focus on writing code and generating multiple bundles (CommonJS or ESModule) at the same time.
|
|
18
18
|
It uses the standard exports configuration in `package.json` as the only source of truth, and uses entry file conventions to match your exports and build them into bundles.
|
|
19
19
|
|
|
20
20
|
## Quick Start
|
package/dist/bin/cli.js
CHANGED
|
@@ -162,7 +162,8 @@ function isTypescriptFile(filename) {
|
|
|
162
162
|
function fileExists(filePath) {
|
|
163
163
|
return fs__default.default.existsSync(filePath);
|
|
164
164
|
}
|
|
165
|
-
|
|
165
|
+
// 'index.server.js' -> 'index'
|
|
166
|
+
const getFileBasename = (str)=>str.split('.')[0];
|
|
166
167
|
const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
|
|
167
168
|
const getMainFieldExportType = (pkg)=>{
|
|
168
169
|
const isEsmPkg = isESModulePackage(pkg.type);
|
|
@@ -183,23 +184,27 @@ function joinRelativePath(...segments) {
|
|
|
183
184
|
function isESModulePackage(packageType) {
|
|
184
185
|
return packageType === 'module';
|
|
185
186
|
}
|
|
187
|
+
function isBinExportPath(exportPath) {
|
|
188
|
+
return exportPath === BINARY_TAG || exportPath.startsWith(BINARY_TAG + '/');
|
|
189
|
+
}
|
|
186
190
|
|
|
187
191
|
function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exportToDist) {
|
|
188
192
|
// End of searching, export value is file path.
|
|
189
193
|
// <export key>: <export value> (string)
|
|
190
194
|
if (typeof exportValue === 'string') {
|
|
191
195
|
const composedTypes = new Set(exportTypes);
|
|
192
|
-
|
|
193
|
-
|
|
196
|
+
const exportType = exportKey.startsWith('.') ? 'default' : exportKey;
|
|
197
|
+
composedTypes.add(exportType);
|
|
198
|
+
const exportInfo = exportToDist.get(mapExportFullPath(currentPath));
|
|
194
199
|
const exportCondition = Array.from(composedTypes).join('.');
|
|
195
200
|
if (!exportInfo) {
|
|
196
201
|
const outputConditionPair = [
|
|
197
202
|
exportValue,
|
|
198
203
|
exportCondition
|
|
199
204
|
];
|
|
200
|
-
exportToDist
|
|
205
|
+
addToExportDistMap(exportToDist, currentPath, [
|
|
201
206
|
outputConditionPair
|
|
202
|
-
]);
|
|
207
|
+
], runtimeExportConventions.has(exportType) ? exportType : undefined);
|
|
203
208
|
} else {
|
|
204
209
|
exportInfo.push([
|
|
205
210
|
exportValue,
|
|
@@ -225,10 +230,26 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
225
230
|
}
|
|
226
231
|
}
|
|
227
232
|
}
|
|
233
|
+
const mapExportFullPath = (exportPath)=>exportPath === '.' ? './index' : exportPath;
|
|
234
|
+
function addToExportDistMap(exportToDist, exportPath, outputConditionPairs, specialExportType) {
|
|
235
|
+
const fullPath = mapExportFullPath(exportPath);
|
|
236
|
+
// + (specialExportType ? '.' + specialExportType : '')
|
|
237
|
+
const existingExportInfo = exportToDist.get(fullPath);
|
|
238
|
+
if (!existingExportInfo) {
|
|
239
|
+
exportToDist.set(fullPath, outputConditionPairs);
|
|
240
|
+
} else {
|
|
241
|
+
existingExportInfo.push(...outputConditionPairs);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
228
244
|
/**
|
|
229
245
|
* parseExports - parse package.exports field and other fields like main,module to a map
|
|
230
246
|
*
|
|
231
247
|
* map from export path to output path and export conditions
|
|
248
|
+
*
|
|
249
|
+
* exportToDist: {
|
|
250
|
+
* './index': { development: ..., default: ... }
|
|
251
|
+
* './index.react-server': { development: ..., default: ... }
|
|
252
|
+
* }
|
|
232
253
|
*/ function parseExports(pkg) {
|
|
233
254
|
var _pkg_exports;
|
|
234
255
|
const exportsField = (_pkg_exports = pkg.exports) != null ? _pkg_exports : {};
|
|
@@ -243,7 +264,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
243
264
|
exportsField,
|
|
244
265
|
defaultCondition
|
|
245
266
|
];
|
|
246
|
-
exportToDist
|
|
267
|
+
addToExportDistMap(exportToDist, currentPath, [
|
|
247
268
|
outputConditionPair
|
|
248
269
|
]);
|
|
249
270
|
} else {
|
|
@@ -265,7 +286,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
265
286
|
bins,
|
|
266
287
|
defaultCondition
|
|
267
288
|
];
|
|
268
|
-
exportToDist
|
|
289
|
+
addToExportDistMap(exportToDist, BINARY_TAG, [
|
|
269
290
|
outputConditionPair
|
|
270
291
|
]);
|
|
271
292
|
} else {
|
|
@@ -277,7 +298,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
277
298
|
binDistPath,
|
|
278
299
|
exportType
|
|
279
300
|
];
|
|
280
|
-
exportToDist
|
|
301
|
+
addToExportDistMap(exportToDist, exportPath, [
|
|
281
302
|
outputConditionPair
|
|
282
303
|
]);
|
|
283
304
|
}
|
|
@@ -287,9 +308,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
287
308
|
const mainExportPath = pkg.main;
|
|
288
309
|
const moduleExportPath = pkg.module;
|
|
289
310
|
const typesEntryPath = pkg.types;
|
|
290
|
-
|
|
291
|
-
exportToDist.set('.', [
|
|
292
|
-
...existingExportInfo || [],
|
|
311
|
+
addToExportDistMap(exportToDist, './index', [
|
|
293
312
|
Boolean(mainExportPath) && [
|
|
294
313
|
mainExportPath,
|
|
295
314
|
getMainFieldExportType(pkg)
|
|
@@ -455,7 +474,7 @@ function lint$1(pkg) {
|
|
|
455
474
|
}
|
|
456
475
|
}
|
|
457
476
|
|
|
458
|
-
var version = "5.
|
|
477
|
+
var version = "5.1.0";
|
|
459
478
|
|
|
460
479
|
function relativify(path) {
|
|
461
480
|
return path.startsWith('.') ? path : `./${path}`;
|
|
@@ -470,20 +489,20 @@ async function writeDefaultTsconfig(tsConfigPath) {
|
|
|
470
489
|
// shared.<export condition>.ts -> ./shared
|
|
471
490
|
// index.ts -> ./index
|
|
472
491
|
// index.development.ts -> ./index.development
|
|
473
|
-
function
|
|
492
|
+
function sourceFilenameToExportFullPath(filename) {
|
|
474
493
|
const baseName = baseNameWithoutExtension(filename);
|
|
475
494
|
let exportPath = baseName;
|
|
476
495
|
return relativify(exportPath);
|
|
477
496
|
}
|
|
478
|
-
// ./index ->
|
|
497
|
+
// ./index -> default
|
|
479
498
|
// ./index.development -> development
|
|
480
499
|
// ./index.react-server -> react-server
|
|
481
500
|
function getExportTypeFromExportPath(exportPath) {
|
|
482
501
|
// Skip the first two segments: `.` and `index`
|
|
483
502
|
const exportTypes = exportPath.split('.').slice(2);
|
|
484
|
-
return
|
|
503
|
+
return getExportTypeFromExportTypesArray(exportTypes);
|
|
485
504
|
}
|
|
486
|
-
function
|
|
505
|
+
function getSpecialExportTypeFromComposedExportPath(composedExportType) {
|
|
487
506
|
const exportTypes = composedExportType.split('.');
|
|
488
507
|
for (const exportType of exportTypes){
|
|
489
508
|
if (specialExportConventions.has(exportType)) {
|
|
@@ -492,7 +511,7 @@ function getSpecialExportTypeFromExportPath(composedExportType) {
|
|
|
492
511
|
}
|
|
493
512
|
return 'default';
|
|
494
513
|
}
|
|
495
|
-
function
|
|
514
|
+
function getExportTypeFromExportTypesArray(types) {
|
|
496
515
|
let exportType = 'default';
|
|
497
516
|
new Set(types).forEach((value)=>{
|
|
498
517
|
if (specialExportConventions.has(value)) {
|
|
@@ -524,6 +543,95 @@ function normalizeExportPath(exportPath) {
|
|
|
524
543
|
}
|
|
525
544
|
return baseName;
|
|
526
545
|
}
|
|
546
|
+
async function collectSourceEntriesByExportPath(sourceFolderPath, originalSubpath, bins, exportsEntries) {
|
|
547
|
+
const isBinaryPath = isBinExportPath(originalSubpath);
|
|
548
|
+
const subpath = originalSubpath.replace(BINARY_TAG, 'bin');
|
|
549
|
+
const absoluteDirPath = path__default.default.join(sourceFolderPath, subpath);
|
|
550
|
+
const isDirectory = fs__default.default.existsSync(absoluteDirPath) ? (await fsp__default.default.stat(absoluteDirPath)).isDirectory() : false;
|
|
551
|
+
if (isDirectory) {
|
|
552
|
+
if (isBinaryPath) {
|
|
553
|
+
const binDirentList = await fsp__default.default.readdir(absoluteDirPath, {
|
|
554
|
+
withFileTypes: true
|
|
555
|
+
});
|
|
556
|
+
for (const binDirent of binDirentList){
|
|
557
|
+
if (binDirent.isFile()) {
|
|
558
|
+
const binFileAbsolutePath = path__default.default.join(binDirent.path, binDirent.name);
|
|
559
|
+
if (fs__default.default.existsSync(binFileAbsolutePath)) {
|
|
560
|
+
bins.set(normalizeExportPath(originalSubpath), binFileAbsolutePath);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
// Search folder/index.<ext> convention entries
|
|
566
|
+
for (const extension of availableExtensions){
|
|
567
|
+
const indexAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${extension}`);
|
|
568
|
+
// Search folder/index.<special type>.<ext> convention entries
|
|
569
|
+
for (const specialExportType of runtimeExportConventions){
|
|
570
|
+
const indexSpecialAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${specialExportType}.${extension}`);
|
|
571
|
+
if (fs__default.default.existsSync(indexSpecialAbsoluteFile)) {
|
|
572
|
+
// Add special export path
|
|
573
|
+
// { ./<export path>.<special cond>: { <special cond>: 'index.<special cond>.<ext>' } }
|
|
574
|
+
const exportPath = relativify(subpath);
|
|
575
|
+
const specialExportPath = exportPath + '.' + specialExportType;
|
|
576
|
+
const sourceFilesMap = exportsEntries.get(specialExportPath) || {};
|
|
577
|
+
sourceFilesMap[specialExportType] = indexSpecialAbsoluteFile;
|
|
578
|
+
exportsEntries.set(specialExportPath, sourceFilesMap);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) {
|
|
582
|
+
const exportPath = relativify(subpath);
|
|
583
|
+
const sourceFilesMap = exportsEntries.get(exportPath) || {};
|
|
584
|
+
const exportType = getExportTypeFromExportPath(exportPath);
|
|
585
|
+
sourceFilesMap[exportType] = indexAbsoluteFile;
|
|
586
|
+
exportsEntries.set(exportPath, sourceFilesMap);
|
|
587
|
+
break;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
} else {
|
|
592
|
+
// subpath could be a file
|
|
593
|
+
const dirName = path.dirname(subpath);
|
|
594
|
+
const baseName = path.basename(subpath);
|
|
595
|
+
// Read current file's directory
|
|
596
|
+
const dirPath = path__default.default.join(sourceFolderPath, dirName);
|
|
597
|
+
if (!fs__default.default.existsSync(dirPath)) {
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const dirents = await fsp__default.default.readdir(dirPath, {
|
|
601
|
+
withFileTypes: true
|
|
602
|
+
});
|
|
603
|
+
for (const dirent of dirents){
|
|
604
|
+
// index.development.js -> index.development
|
|
605
|
+
const direntBaseName = baseNameWithoutExtension(dirent.name);
|
|
606
|
+
const ext = path.extname(dirent.name).slice(1);
|
|
607
|
+
if (!dirent.isFile() || direntBaseName !== baseName || !availableExtensions.has(ext)) {
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
if (isTestFile(dirent.name)) {
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
const sourceFileAbsolutePath = path__default.default.join(dirent.path, dirent.name);
|
|
614
|
+
if (isBinaryPath) {
|
|
615
|
+
bins.set(originalSubpath, sourceFileAbsolutePath);
|
|
616
|
+
} else {
|
|
617
|
+
let sourceFilesMap = exportsEntries.get(originalSubpath) || {};
|
|
618
|
+
const exportType = getExportTypeFromExportPath(originalSubpath);
|
|
619
|
+
sourceFilesMap[exportType] = sourceFileAbsolutePath;
|
|
620
|
+
if (specialExportConventions.has(exportType)) {
|
|
621
|
+
// e.g. ./foo/index.react-server -> ./foo/index
|
|
622
|
+
const fallbackExportPath = sourceFilenameToExportFullPath(originalSubpath);
|
|
623
|
+
const fallbackSourceFilesMap = exportsEntries.get(fallbackExportPath) || {};
|
|
624
|
+
sourceFilesMap = {
|
|
625
|
+
...fallbackSourceFilesMap,
|
|
626
|
+
...sourceFilesMap
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
exportsEntries.set(originalSubpath, sourceFilesMap);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
// For `prepare`
|
|
527
635
|
async function collectSourceEntries(sourceFolderPath) {
|
|
528
636
|
const bins = new Map();
|
|
529
637
|
const exportsEntries = new Map();
|
|
@@ -536,65 +644,27 @@ async function collectSourceEntries(sourceFolderPath) {
|
|
|
536
644
|
const entryFileDirentList = await fsp__default.default.readdir(sourceFolderPath, {
|
|
537
645
|
withFileTypes: true
|
|
538
646
|
});
|
|
647
|
+
// Collect source files for `exports` field
|
|
539
648
|
for (const dirent of entryFileDirentList){
|
|
540
|
-
if (dirent.
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
for (const extension of availableExtensions){
|
|
557
|
-
const indexAbsoluteFile = path__default.default.join(dirent.path, dirent.name, `index.${extension}`);
|
|
558
|
-
// Search folder/index.<special type>.<ext> convention entries
|
|
559
|
-
for (const specialExportType of runtimeExportConventions){
|
|
560
|
-
const indexSpecialAbsoluteFile = path__default.default.join(dirent.path, dirent.name, `index.${specialExportType}.${extension}`);
|
|
561
|
-
if (fs__default.default.existsSync(indexSpecialAbsoluteFile)) {
|
|
562
|
-
// Add special export path
|
|
563
|
-
// { ./<export path>.<special cond>: { <special cond>: 'index.<special cond>.<ext>' } }
|
|
564
|
-
const exportPath = sourceFilenameToExportPath(dirent.name);
|
|
565
|
-
const specialExportPath = exportPath + '.' + specialExportType;
|
|
566
|
-
const sourceFilesMap = exportsEntries.get(specialExportPath) || {};
|
|
567
|
-
sourceFilesMap[specialExportType] = indexSpecialAbsoluteFile;
|
|
568
|
-
exportsEntries.set(specialExportPath, sourceFilesMap);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) {
|
|
572
|
-
const exportPath = sourceFilenameToExportPath(dirent.name);
|
|
573
|
-
const sourceFilesMap = exportsEntries.get(exportPath) || {};
|
|
574
|
-
const exportType = getExportTypeFromExportPath(exportPath);
|
|
575
|
-
sourceFilesMap[exportType] = indexAbsoluteFile;
|
|
576
|
-
exportsEntries.set(exportPath, sourceFilesMap);
|
|
577
|
-
break;
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
}
|
|
581
|
-
} else if (dirent.isFile()) {
|
|
582
|
-
const isAvailableExtension = availableExtensions.has(path__default.default.extname(dirent.name).slice(1));
|
|
583
|
-
if (isAvailableExtension) {
|
|
584
|
-
const exportPath = sourceFilenameToExportPath(dirent.name);
|
|
585
|
-
const isBinFile = exportPath === './bin';
|
|
586
|
-
const fullPath = path__default.default.join(sourceFolderPath, dirent.name);
|
|
587
|
-
if (isBinFile) {
|
|
588
|
-
bins.set(BINARY_TAG, fullPath);
|
|
589
|
-
} else {
|
|
590
|
-
if (hasAvailableExtension(dirent.name) && !isTestFile(dirent.name)) {
|
|
591
|
-
const sourceFilesMap = exportsEntries.get(exportPath) || {};
|
|
592
|
-
const exportType = getExportTypeFromExportPath(exportPath);
|
|
593
|
-
sourceFilesMap[exportType] = fullPath;
|
|
594
|
-
exportsEntries.set(exportPath, sourceFilesMap);
|
|
595
|
-
}
|
|
596
|
-
}
|
|
649
|
+
if (getFileBasename(dirent.name) === 'bin') {
|
|
650
|
+
continue;
|
|
651
|
+
}
|
|
652
|
+
const exportPath = sourceFilenameToExportFullPath(dirent.name);
|
|
653
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, exportPath, bins, exportsEntries);
|
|
654
|
+
}
|
|
655
|
+
// Collect source files for `bin` field
|
|
656
|
+
const binDirent = entryFileDirentList.find((dirent)=>getFileBasename(dirent.name) === 'bin');
|
|
657
|
+
if (binDirent) {
|
|
658
|
+
if (binDirent.isDirectory()) {
|
|
659
|
+
const binDirentList = await fsp__default.default.readdir(path__default.default.join(binDirent.path, binDirent.name), {
|
|
660
|
+
withFileTypes: true
|
|
661
|
+
});
|
|
662
|
+
for (const binDirent of binDirentList){
|
|
663
|
+
const binExportPath = path.posix.join(BINARY_TAG, getFileBasename(binDirent.name));
|
|
664
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, binExportPath, bins, exportsEntries);
|
|
597
665
|
}
|
|
666
|
+
} else {
|
|
667
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, BINARY_TAG, bins, exportsEntries);
|
|
598
668
|
}
|
|
599
669
|
}
|
|
600
670
|
return {
|
|
@@ -646,7 +716,7 @@ function createExportCondition(exportName, sourceFile, moduleType) {
|
|
|
646
716
|
function createExportConditionPair(exportName, sourceFile, moduleType) {
|
|
647
717
|
// <exportName>.<specialCondition>
|
|
648
718
|
let specialCondition;
|
|
649
|
-
const specialConditionName =
|
|
719
|
+
const specialConditionName = getSpecialExportTypeFromComposedExportPath(exportName);
|
|
650
720
|
const normalizedExportPath = normalizeExportPath(exportName);
|
|
651
721
|
if (specialConditionName !== 'default') {
|
|
652
722
|
// e.g.
|
|
@@ -741,10 +811,10 @@ async function prepare(cwd) {
|
|
|
741
811
|
const pkgExports = {};
|
|
742
812
|
for (const [exportName, sourceFilesMap] of exportsEntries.entries()){
|
|
743
813
|
for (const sourceFile of Object.values(sourceFilesMap)){
|
|
744
|
-
const [
|
|
745
|
-
pkgExports[
|
|
746
|
-
...
|
|
747
|
-
...pkgExports[
|
|
814
|
+
const [normalizedExportPath, conditions] = createExportConditionPair(exportName, sourceFile, pkgJson.type);
|
|
815
|
+
pkgExports[normalizedExportPath] = {
|
|
816
|
+
...conditions,
|
|
817
|
+
...pkgExports[normalizedExportPath]
|
|
748
818
|
};
|
|
749
819
|
}
|
|
750
820
|
}
|
package/dist/index.js
CHANGED
|
@@ -238,7 +238,6 @@ function filePathWithoutExtension(filePath) {
|
|
|
238
238
|
}
|
|
239
239
|
return filePath;
|
|
240
240
|
}
|
|
241
|
-
const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.default.extname(filename).slice(1));
|
|
242
241
|
const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
|
|
243
242
|
const getMainFieldExportType = (pkg)=>{
|
|
244
243
|
const isEsmPkg = isESModulePackage(pkg.type);
|
|
@@ -282,6 +281,9 @@ async function removeOutputDir(output, cwd) {
|
|
|
282
281
|
removedDirs.add(dir);
|
|
283
282
|
}
|
|
284
283
|
}
|
|
284
|
+
function isBinExportPath(exportPath) {
|
|
285
|
+
return exportPath === BINARY_TAG || exportPath.startsWith(BINARY_TAG + '/');
|
|
286
|
+
}
|
|
285
287
|
|
|
286
288
|
const memoize = (fn, cacheKey, cacheArg)=>{
|
|
287
289
|
const cache = cacheArg || new Map();
|
|
@@ -533,27 +535,25 @@ function relativify(path) {
|
|
|
533
535
|
// e.g.
|
|
534
536
|
// For a resolved file, if it's one of the entries,
|
|
535
537
|
// aliases it as export path, such as <absolute file> -> <pkg>/<export path>
|
|
536
|
-
function aliasEntries({ entry, entries, entriesAlias, format, dts }) {
|
|
537
|
-
let currentDistPath = '';
|
|
538
|
+
function aliasEntries({ entry, entries, entriesAlias, format, dts, cwd }) {
|
|
538
539
|
const entryAliasWithoutSelf = {
|
|
539
540
|
...entriesAlias,
|
|
540
541
|
[entry]: null
|
|
541
542
|
};
|
|
542
543
|
const pathToRelativeDistMap = new Map();
|
|
543
544
|
for (const [, exportCondition] of Object.entries(entries)){
|
|
544
|
-
|
|
545
|
-
const { import: importCond, require: requireCond, default: defaultCond } = exportCondition.export;
|
|
546
|
-
const firstCond = (_Object_entries_find = Object.entries(exportCondition.export).find(([key, cond])=>key !== 'types' && cond != null)) == null ? void 0 : _Object_entries_find[1];
|
|
545
|
+
const exportDistMaps = exportCondition.export;
|
|
547
546
|
if (dts) {
|
|
548
|
-
|
|
549
|
-
//
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
547
|
+
var _Object_entries_find;
|
|
548
|
+
// Search types + [format] condition from entries map
|
|
549
|
+
// e.g. import.types, require.types
|
|
550
|
+
const typeCond = (_Object_entries_find = Object.entries(exportDistMaps).find(([composedKey, cond])=>{
|
|
551
|
+
const typesSet = new Set(composedKey.split('.'));
|
|
552
|
+
const formatCond = format === 'cjs' ? 'require' : 'import';
|
|
553
|
+
return typesSet.has('types') && typesSet.has(formatCond) && cond != null;
|
|
554
|
+
})) == null ? void 0 : _Object_entries_find[1];
|
|
555
|
+
if (typeCond) {
|
|
556
|
+
pathToRelativeDistMap.set(exportCondition.source, typeCond);
|
|
557
557
|
}
|
|
558
558
|
}
|
|
559
559
|
}
|
|
@@ -561,23 +561,25 @@ function aliasEntries({ entry, entries, entriesAlias, format, dts }) {
|
|
|
561
561
|
name: 'alias',
|
|
562
562
|
resolveId: {
|
|
563
563
|
async handler (source, importer, options) {
|
|
564
|
-
const
|
|
565
|
-
if (
|
|
564
|
+
const resolved = await this.resolve(source, importer, options);
|
|
565
|
+
if (resolved != null) {
|
|
566
566
|
if (dts) {
|
|
567
567
|
// For types, generate relative path to the other type files,
|
|
568
568
|
// this will be compatible for the node10 ts module resolution.
|
|
569
|
-
const
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
const
|
|
573
|
-
const
|
|
569
|
+
const resolvedDist = pathToRelativeDistMap.get(resolved.id);
|
|
570
|
+
const entryDist = pathToRelativeDistMap.get(entry);
|
|
571
|
+
if (resolved.id !== entry && entryDist && resolvedDist) {
|
|
572
|
+
const absoluteEntryDist = path__default.default.resolve(cwd, entryDist);
|
|
573
|
+
const absoluteResolvedDist = path__default.default.resolve(cwd, resolvedDist);
|
|
574
|
+
const filePathBase = path__default.default.relative(path__default.default.dirname(absoluteEntryDist), absoluteResolvedDist);
|
|
575
|
+
const relativePath = relativify(filePathBase);
|
|
574
576
|
return {
|
|
575
577
|
id: relativePath,
|
|
576
578
|
external: true
|
|
577
579
|
};
|
|
578
580
|
}
|
|
579
581
|
} else {
|
|
580
|
-
const aliasedId = entryAliasWithoutSelf[
|
|
582
|
+
const aliasedId = entryAliasWithoutSelf[resolved.id];
|
|
581
583
|
if (aliasedId != null) {
|
|
582
584
|
return {
|
|
583
585
|
id: aliasedId
|
|
@@ -613,22 +615,40 @@ function prependDirectives() {
|
|
|
613
615
|
};
|
|
614
616
|
}
|
|
615
617
|
|
|
618
|
+
const prependShebang = (entry)=>({
|
|
619
|
+
name: 'prependShebang',
|
|
620
|
+
transform: (code, id)=>{
|
|
621
|
+
if (id !== entry) return;
|
|
622
|
+
const shebang = '#!/usr/bin/env node\n';
|
|
623
|
+
if (code.startsWith(shebang)) return;
|
|
624
|
+
const magicString = new MagicString__default.default(code);
|
|
625
|
+
magicString.prepend(shebang);
|
|
626
|
+
return {
|
|
627
|
+
code: magicString.toString(),
|
|
628
|
+
map: magicString.generateMap({
|
|
629
|
+
hires: true
|
|
630
|
+
})
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
|
|
616
635
|
function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exportToDist) {
|
|
617
636
|
// End of searching, export value is file path.
|
|
618
637
|
// <export key>: <export value> (string)
|
|
619
638
|
if (typeof exportValue === 'string') {
|
|
620
639
|
const composedTypes = new Set(exportTypes);
|
|
621
|
-
|
|
622
|
-
|
|
640
|
+
const exportType = exportKey.startsWith('.') ? 'default' : exportKey;
|
|
641
|
+
composedTypes.add(exportType);
|
|
642
|
+
const exportInfo = exportToDist.get(mapExportFullPath(currentPath));
|
|
623
643
|
const exportCondition = Array.from(composedTypes).join('.');
|
|
624
644
|
if (!exportInfo) {
|
|
625
645
|
const outputConditionPair = [
|
|
626
646
|
exportValue,
|
|
627
647
|
exportCondition
|
|
628
648
|
];
|
|
629
|
-
exportToDist
|
|
649
|
+
addToExportDistMap(exportToDist, currentPath, [
|
|
630
650
|
outputConditionPair
|
|
631
|
-
]);
|
|
651
|
+
], runtimeExportConventions.has(exportType) ? exportType : undefined);
|
|
632
652
|
} else {
|
|
633
653
|
exportInfo.push([
|
|
634
654
|
exportValue,
|
|
@@ -654,10 +674,26 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
654
674
|
}
|
|
655
675
|
}
|
|
656
676
|
}
|
|
677
|
+
const mapExportFullPath = (exportPath)=>exportPath === '.' ? './index' : exportPath;
|
|
678
|
+
function addToExportDistMap(exportToDist, exportPath, outputConditionPairs, specialExportType) {
|
|
679
|
+
const fullPath = mapExportFullPath(exportPath);
|
|
680
|
+
// + (specialExportType ? '.' + specialExportType : '')
|
|
681
|
+
const existingExportInfo = exportToDist.get(fullPath);
|
|
682
|
+
if (!existingExportInfo) {
|
|
683
|
+
exportToDist.set(fullPath, outputConditionPairs);
|
|
684
|
+
} else {
|
|
685
|
+
existingExportInfo.push(...outputConditionPairs);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
657
688
|
/**
|
|
658
689
|
* parseExports - parse package.exports field and other fields like main,module to a map
|
|
659
690
|
*
|
|
660
691
|
* map from export path to output path and export conditions
|
|
692
|
+
*
|
|
693
|
+
* exportToDist: {
|
|
694
|
+
* './index': { development: ..., default: ... }
|
|
695
|
+
* './index.react-server': { development: ..., default: ... }
|
|
696
|
+
* }
|
|
661
697
|
*/ function parseExports(pkg) {
|
|
662
698
|
var _pkg_exports;
|
|
663
699
|
const exportsField = (_pkg_exports = pkg.exports) != null ? _pkg_exports : {};
|
|
@@ -672,7 +708,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
672
708
|
exportsField,
|
|
673
709
|
defaultCondition
|
|
674
710
|
];
|
|
675
|
-
exportToDist
|
|
711
|
+
addToExportDistMap(exportToDist, currentPath, [
|
|
676
712
|
outputConditionPair
|
|
677
713
|
]);
|
|
678
714
|
} else {
|
|
@@ -694,7 +730,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
694
730
|
bins,
|
|
695
731
|
defaultCondition
|
|
696
732
|
];
|
|
697
|
-
exportToDist
|
|
733
|
+
addToExportDistMap(exportToDist, BINARY_TAG, [
|
|
698
734
|
outputConditionPair
|
|
699
735
|
]);
|
|
700
736
|
} else {
|
|
@@ -706,7 +742,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
706
742
|
binDistPath,
|
|
707
743
|
exportType
|
|
708
744
|
];
|
|
709
|
-
exportToDist
|
|
745
|
+
addToExportDistMap(exportToDist, exportPath, [
|
|
710
746
|
outputConditionPair
|
|
711
747
|
]);
|
|
712
748
|
}
|
|
@@ -716,9 +752,7 @@ function collectExportPath(exportValue, exportKey, currentPath, exportTypes, exp
|
|
|
716
752
|
const mainExportPath = pkg.main;
|
|
717
753
|
const moduleExportPath = pkg.module;
|
|
718
754
|
const typesEntryPath = pkg.types;
|
|
719
|
-
|
|
720
|
-
exportToDist.set('.', [
|
|
721
|
-
...existingExportInfo || [],
|
|
755
|
+
addToExportDistMap(exportToDist, './index', [
|
|
722
756
|
Boolean(mainExportPath) && [
|
|
723
757
|
mainExportPath,
|
|
724
758
|
getMainFieldExportType(pkg)
|
|
@@ -836,7 +870,7 @@ function getExportTypeFromFile(filename, pkgType) {
|
|
|
836
870
|
// shared.<export condition>.ts -> ./shared
|
|
837
871
|
// index.ts -> ./index
|
|
838
872
|
// index.development.ts -> ./index.development
|
|
839
|
-
function
|
|
873
|
+
function sourceFilenameToExportFullPath(filename) {
|
|
840
874
|
const baseName = baseNameWithoutExtension(filename);
|
|
841
875
|
let exportPath = baseName;
|
|
842
876
|
return relativify(exportPath);
|
|
@@ -844,8 +878,8 @@ function sourceFilenameToExportPath(filename) {
|
|
|
844
878
|
async function collectEntriesFromParsedExports(cwd, parsedExportsInfo, sourceFile) {
|
|
845
879
|
const entries = {};
|
|
846
880
|
if (sourceFile) {
|
|
847
|
-
const defaultExport = parsedExportsInfo.get('
|
|
848
|
-
entries['
|
|
881
|
+
const defaultExport = parsedExportsInfo.get('./index')[0];
|
|
882
|
+
entries['./index'] = {
|
|
849
883
|
source: sourceFile,
|
|
850
884
|
name: '.',
|
|
851
885
|
export: {
|
|
@@ -854,13 +888,13 @@ async function collectEntriesFromParsedExports(cwd, parsedExportsInfo, sourceFil
|
|
|
854
888
|
};
|
|
855
889
|
}
|
|
856
890
|
// Find source files
|
|
857
|
-
const { bins, exportsEntries } = await
|
|
891
|
+
const { bins, exportsEntries } = await collectSourceEntriesFromExportPaths(path.join(cwd, SRC), parsedExportsInfo);
|
|
858
892
|
// A mapping between each export path and its related special export conditions,
|
|
859
893
|
// excluding the 'default' export condition.
|
|
860
|
-
// { '
|
|
894
|
+
// { './index' => Set('development', 'edge-light') }
|
|
861
895
|
const pathSpecialConditionsMap = {};
|
|
862
896
|
for (const [exportPath] of exportsEntries){
|
|
863
|
-
const normalizedExportPath =
|
|
897
|
+
const normalizedExportPath = stripSpecialCondition(exportPath);
|
|
864
898
|
if (!pathSpecialConditionsMap[normalizedExportPath]) {
|
|
865
899
|
pathSpecialConditionsMap[normalizedExportPath] = new Set();
|
|
866
900
|
}
|
|
@@ -871,17 +905,19 @@ async function collectEntriesFromParsedExports(cwd, parsedExportsInfo, sourceFil
|
|
|
871
905
|
}
|
|
872
906
|
// Traverse source files and try to match the entries
|
|
873
907
|
// Find exports from parsed exports info
|
|
874
|
-
// entryExportPath can be: '
|
|
908
|
+
// entryExportPath can be: './index', './index.development', './shared.edge-light', etc.
|
|
875
909
|
for (const [entryExportPath, sourceFilesMap] of exportsEntries){
|
|
876
|
-
const normalizedExportPath =
|
|
910
|
+
const normalizedExportPath = stripSpecialCondition(entryExportPath);
|
|
877
911
|
const entryExportPathType = getExportTypeFromExportPath(entryExportPath);
|
|
878
912
|
const outputExports = parsedExportsInfo.get(normalizedExportPath);
|
|
879
913
|
if (!outputExports) {
|
|
880
914
|
continue;
|
|
881
915
|
}
|
|
882
|
-
for (const [outputPath,
|
|
883
|
-
const matchedExportType = getSpecialExportTypeFromExportPath(composedExportType);
|
|
916
|
+
for (const [outputPath, outputComposedExportType] of outputExports){
|
|
884
917
|
// export type can be: default, development, react-server, etc.
|
|
918
|
+
const matchedExportType = getSpecialExportTypeFromComposedExportPath(outputComposedExportType);
|
|
919
|
+
const specialSet = pathSpecialConditionsMap[normalizedExportPath];
|
|
920
|
+
const hasSpecialEntry = specialSet.has(matchedExportType);
|
|
885
921
|
const sourceFile = sourceFilesMap[matchedExportType] || sourceFilesMap.default;
|
|
886
922
|
if (!sourceFile) {
|
|
887
923
|
continue;
|
|
@@ -892,18 +928,19 @@ async function collectEntriesFromParsedExports(cwd, parsedExportsInfo, sourceFil
|
|
|
892
928
|
name: normalizedExportPath,
|
|
893
929
|
export: {}
|
|
894
930
|
};
|
|
931
|
+
} else if (matchedExportType === entryExportPathType) {
|
|
932
|
+
entries[entryExportPath].source = sourceFile;
|
|
895
933
|
}
|
|
896
|
-
|
|
897
|
-
if (
|
|
898
|
-
|
|
934
|
+
// output exports match
|
|
935
|
+
if (matchedExportType === entryExportPathType || !hasSpecialEntry && matchedExportType !== 'default') {
|
|
936
|
+
const exportMap = entries[entryExportPath].export;
|
|
937
|
+
exportMap[outputComposedExportType] = outputPath;
|
|
899
938
|
}
|
|
900
|
-
exportMap[composedExportType] = outputPath;
|
|
901
939
|
}
|
|
902
940
|
}
|
|
903
941
|
// Handling binaries
|
|
904
942
|
for (const [exportPath, sourceFile] of bins){
|
|
905
|
-
const
|
|
906
|
-
const outputExports = parsedExportsInfo.get(normalizedExportPath);
|
|
943
|
+
const outputExports = parsedExportsInfo.get(exportPath);
|
|
907
944
|
if (!outputExports) {
|
|
908
945
|
continue;
|
|
909
946
|
}
|
|
@@ -919,15 +956,15 @@ async function collectEntriesFromParsedExports(cwd, parsedExportsInfo, sourceFil
|
|
|
919
956
|
}
|
|
920
957
|
return entries;
|
|
921
958
|
}
|
|
922
|
-
// ./index ->
|
|
959
|
+
// ./index -> default
|
|
923
960
|
// ./index.development -> development
|
|
924
961
|
// ./index.react-server -> react-server
|
|
925
962
|
function getExportTypeFromExportPath(exportPath) {
|
|
926
963
|
// Skip the first two segments: `.` and `index`
|
|
927
964
|
const exportTypes = exportPath.split('.').slice(2);
|
|
928
|
-
return
|
|
965
|
+
return getExportTypeFromExportTypesArray(exportTypes);
|
|
929
966
|
}
|
|
930
|
-
function
|
|
967
|
+
function getSpecialExportTypeFromComposedExportPath(composedExportType) {
|
|
931
968
|
const exportTypes = composedExportType.split('.');
|
|
932
969
|
for (const exportType of exportTypes){
|
|
933
970
|
if (specialExportConventions.has(exportType)) {
|
|
@@ -936,7 +973,7 @@ function getSpecialExportTypeFromExportPath(composedExportType) {
|
|
|
936
973
|
}
|
|
937
974
|
return 'default';
|
|
938
975
|
}
|
|
939
|
-
function
|
|
976
|
+
function getExportTypeFromExportTypesArray(types) {
|
|
940
977
|
let exportType = 'default';
|
|
941
978
|
new Set(types).forEach((value)=>{
|
|
942
979
|
if (specialExportConventions.has(value)) {
|
|
@@ -968,85 +1005,152 @@ function normalizeExportPath(exportPath) {
|
|
|
968
1005
|
}
|
|
969
1006
|
return baseName;
|
|
970
1007
|
}
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
if (binDirent.isFile()) {
|
|
991
|
-
const binFileAbsolutePath = path__default.default.join(sourceFolderPath, dirent.name, binDirent.name);
|
|
992
|
-
const binExportPath = sourceFilenameToExportPath(binDirent.name);
|
|
993
|
-
if (fs__default.default.existsSync(binFileAbsolutePath)) {
|
|
994
|
-
bins.set(path.posix.join(BINARY_TAG, binExportPath), binFileAbsolutePath);
|
|
995
|
-
}
|
|
1008
|
+
// ./index.react-server -> ./index
|
|
1009
|
+
function stripSpecialCondition(exportPath) {
|
|
1010
|
+
return exportPath.split('.').slice(0, 2).join('.');
|
|
1011
|
+
}
|
|
1012
|
+
async function collectSourceEntriesByExportPath(sourceFolderPath, originalSubpath, bins, exportsEntries) {
|
|
1013
|
+
const isBinaryPath = isBinExportPath(originalSubpath);
|
|
1014
|
+
const subpath = originalSubpath.replace(BINARY_TAG, 'bin');
|
|
1015
|
+
const absoluteDirPath = path__default.default.join(sourceFolderPath, subpath);
|
|
1016
|
+
const isDirectory = fs__default.default.existsSync(absoluteDirPath) ? (await fsp__default.default.stat(absoluteDirPath)).isDirectory() : false;
|
|
1017
|
+
if (isDirectory) {
|
|
1018
|
+
if (isBinaryPath) {
|
|
1019
|
+
const binDirentList = await fsp__default.default.readdir(absoluteDirPath, {
|
|
1020
|
+
withFileTypes: true
|
|
1021
|
+
});
|
|
1022
|
+
for (const binDirent of binDirentList){
|
|
1023
|
+
if (binDirent.isFile()) {
|
|
1024
|
+
const binFileAbsolutePath = path__default.default.join(binDirent.path, binDirent.name);
|
|
1025
|
+
if (fs__default.default.existsSync(binFileAbsolutePath)) {
|
|
1026
|
+
bins.set(normalizeExportPath(originalSubpath), binFileAbsolutePath);
|
|
996
1027
|
}
|
|
997
1028
|
}
|
|
998
|
-
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
}
|
|
1015
|
-
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) {
|
|
1016
|
-
const exportPath = sourceFilenameToExportPath(dirent.name);
|
|
1017
|
-
const sourceFilesMap = exportsEntries.get(exportPath) || {};
|
|
1018
|
-
const exportType = getExportTypeFromExportPath(exportPath);
|
|
1019
|
-
sourceFilesMap[exportType] = indexAbsoluteFile;
|
|
1020
|
-
exportsEntries.set(exportPath, sourceFilesMap);
|
|
1021
|
-
break;
|
|
1029
|
+
}
|
|
1030
|
+
} else {
|
|
1031
|
+
// Search folder/index.<ext> convention entries
|
|
1032
|
+
for (const extension of availableExtensions){
|
|
1033
|
+
const indexAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${extension}`);
|
|
1034
|
+
// Search folder/index.<special type>.<ext> convention entries
|
|
1035
|
+
for (const specialExportType of runtimeExportConventions){
|
|
1036
|
+
const indexSpecialAbsoluteFile = path__default.default.join(absoluteDirPath, `index.${specialExportType}.${extension}`);
|
|
1037
|
+
if (fs__default.default.existsSync(indexSpecialAbsoluteFile)) {
|
|
1038
|
+
// Add special export path
|
|
1039
|
+
// { ./<export path>.<special cond>: { <special cond>: 'index.<special cond>.<ext>' } }
|
|
1040
|
+
const exportPath = relativify(subpath);
|
|
1041
|
+
const specialExportPath = exportPath + '.' + specialExportType;
|
|
1042
|
+
const sourceFilesMap = exportsEntries.get(specialExportPath) || {};
|
|
1043
|
+
sourceFilesMap[specialExportType] = indexSpecialAbsoluteFile;
|
|
1044
|
+
exportsEntries.set(specialExportPath, sourceFilesMap);
|
|
1022
1045
|
}
|
|
1023
1046
|
}
|
|
1047
|
+
if (fs__default.default.existsSync(indexAbsoluteFile) && !isTestFile(indexAbsoluteFile)) {
|
|
1048
|
+
const exportPath = relativify(subpath);
|
|
1049
|
+
const sourceFilesMap = exportsEntries.get(exportPath) || {};
|
|
1050
|
+
const exportType = getExportTypeFromExportPath(exportPath);
|
|
1051
|
+
sourceFilesMap[exportType] = indexAbsoluteFile;
|
|
1052
|
+
exportsEntries.set(exportPath, sourceFilesMap);
|
|
1053
|
+
break;
|
|
1054
|
+
}
|
|
1024
1055
|
}
|
|
1025
|
-
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1056
|
+
}
|
|
1057
|
+
} else {
|
|
1058
|
+
// subpath could be a file
|
|
1059
|
+
const dirName = path.dirname(subpath);
|
|
1060
|
+
const baseName = path.basename(subpath);
|
|
1061
|
+
// Read current file's directory
|
|
1062
|
+
const dirPath = path__default.default.join(sourceFolderPath, dirName);
|
|
1063
|
+
if (!fs__default.default.existsSync(dirPath)) {
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
const dirents = await fsp__default.default.readdir(dirPath, {
|
|
1067
|
+
withFileTypes: true
|
|
1068
|
+
});
|
|
1069
|
+
for (const dirent of dirents){
|
|
1070
|
+
// index.development.js -> index.development
|
|
1071
|
+
const direntBaseName = baseNameWithoutExtension(dirent.name);
|
|
1072
|
+
const ext = path.extname(dirent.name).slice(1);
|
|
1073
|
+
if (!dirent.isFile() || direntBaseName !== baseName || !availableExtensions.has(ext)) {
|
|
1074
|
+
continue;
|
|
1075
|
+
}
|
|
1076
|
+
if (isTestFile(dirent.name)) {
|
|
1077
|
+
continue;
|
|
1078
|
+
}
|
|
1079
|
+
const sourceFileAbsolutePath = path__default.default.join(dirent.path, dirent.name);
|
|
1080
|
+
if (isBinaryPath) {
|
|
1081
|
+
bins.set(originalSubpath, sourceFileAbsolutePath);
|
|
1082
|
+
} else {
|
|
1083
|
+
let sourceFilesMap = exportsEntries.get(originalSubpath) || {};
|
|
1084
|
+
const exportType = getExportTypeFromExportPath(originalSubpath);
|
|
1085
|
+
sourceFilesMap[exportType] = sourceFileAbsolutePath;
|
|
1086
|
+
if (specialExportConventions.has(exportType)) {
|
|
1087
|
+
// e.g. ./foo/index.react-server -> ./foo/index
|
|
1088
|
+
const fallbackExportPath = sourceFilenameToExportFullPath(originalSubpath);
|
|
1089
|
+
const fallbackSourceFilesMap = exportsEntries.get(fallbackExportPath) || {};
|
|
1090
|
+
sourceFilesMap = {
|
|
1091
|
+
...fallbackSourceFilesMap,
|
|
1092
|
+
...sourceFilesMap
|
|
1093
|
+
};
|
|
1040
1094
|
}
|
|
1095
|
+
exportsEntries.set(originalSubpath, sourceFilesMap);
|
|
1041
1096
|
}
|
|
1042
1097
|
}
|
|
1043
1098
|
}
|
|
1099
|
+
}
|
|
1100
|
+
/**
|
|
1101
|
+
* exportsEntries {
|
|
1102
|
+
* "./index" => {
|
|
1103
|
+
* "development" => source"
|
|
1104
|
+
* "react-server" => "source"
|
|
1105
|
+
* },
|
|
1106
|
+
* "./index.react-server" => {
|
|
1107
|
+
* "development" => source"
|
|
1108
|
+
* "react-server" => "source"
|
|
1109
|
+
* }
|
|
1110
|
+
* }
|
|
1111
|
+
*/ async function collectSourceEntriesFromExportPaths(sourceFolderPath, parsedExportsInfo) {
|
|
1112
|
+
const bins = new Map();
|
|
1113
|
+
const exportsEntries = new Map();
|
|
1114
|
+
for (const [exportPath, exportInfo] of parsedExportsInfo.entries()){
|
|
1115
|
+
const specialConditions = new Set();
|
|
1116
|
+
for (const [_, composedExportType] of exportInfo){
|
|
1117
|
+
const specialExportType = getSpecialExportTypeFromComposedExportPath(composedExportType);
|
|
1118
|
+
if (specialExportType !== 'default') {
|
|
1119
|
+
specialConditions.add(specialExportType);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, exportPath, bins, exportsEntries);
|
|
1123
|
+
for (const specialCondition of specialConditions){
|
|
1124
|
+
await collectSourceEntriesByExportPath(sourceFolderPath, exportPath + '.' + specialCondition, bins, exportsEntries);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1044
1127
|
return {
|
|
1045
1128
|
bins,
|
|
1046
1129
|
exportsEntries
|
|
1047
1130
|
};
|
|
1048
1131
|
}
|
|
1049
1132
|
|
|
1133
|
+
function getModuleLayer(moduleMeta) {
|
|
1134
|
+
const directives = (moduleMeta.preserveDirectives || {
|
|
1135
|
+
directives: []
|
|
1136
|
+
}).directives.map((d)=>d.replace(/^use /, '')).filter((d)=>d !== 'strict');
|
|
1137
|
+
const moduleLayer = directives[0];
|
|
1138
|
+
return moduleLayer;
|
|
1139
|
+
}
|
|
1140
|
+
function getCustomModuleLayer(moduleId) {
|
|
1141
|
+
const segments = path__default.default.basename(moduleId).split('.');
|
|
1142
|
+
if (segments.length >= 2) {
|
|
1143
|
+
const [layerSegment, ext] = segments.slice(-2);
|
|
1144
|
+
const baseName = segments[0];
|
|
1145
|
+
const match = layerSegment.match(/^(\w+)-runtime$/);
|
|
1146
|
+
const layer = match && match[1];
|
|
1147
|
+
if (availableExtensions.has(ext) && layer && layer.length > 0) {
|
|
1148
|
+
return baseName + '-' + layer;
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
return undefined;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1050
1154
|
const swcMinifyOptions = {
|
|
1051
1155
|
compress: {
|
|
1052
1156
|
directives: false
|
|
@@ -1105,7 +1209,7 @@ const memoizeDtsPluginByKey = memoizeByKey(createDtsPlugin);
|
|
|
1105
1209
|
for (const [entryExportPath, exportCondition] of Object.entries(entries)){
|
|
1106
1210
|
const normalizedExportPath = normalizeExportPath(entryExportPath);
|
|
1107
1211
|
// entryExportPath format: ./index, ./shared, etc.
|
|
1108
|
-
const specialExportType =
|
|
1212
|
+
const specialExportType = getSpecialExportTypeFromComposedExportPath(entryExportPath);
|
|
1109
1213
|
if (specialExportType === 'default') {
|
|
1110
1214
|
alias[exportCondition.source] = path.posix.join(name || '', normalizedExportPath);
|
|
1111
1215
|
}
|
|
@@ -1115,6 +1219,7 @@ const memoizeDtsPluginByKey = memoizeByKey(createDtsPlugin);
|
|
|
1115
1219
|
async function buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts) {
|
|
1116
1220
|
var _bundleConfig_file, _bundleConfig_file1;
|
|
1117
1221
|
const { entries, pkg, cwd, tsOptions: { tsConfigPath, tsCompilerOptions }, pluginContext } = buildContext;
|
|
1222
|
+
const isBinEntry = isBinExportPath(exportCondition.name);
|
|
1118
1223
|
const hasNoExternal = bundleConfig.external === null;
|
|
1119
1224
|
var _bundleConfig_external;
|
|
1120
1225
|
const externals = hasNoExternal ? [] : [
|
|
@@ -1161,18 +1266,20 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
|
|
|
1161
1266
|
// common plugins for both dts and ts assets that need to be processed
|
|
1162
1267
|
// If it's a .d.ts file under non-ESM package or .d.cts file, use cjs types alias.
|
|
1163
1268
|
const aliasFormat = dts ? ((_bundleConfig_file = bundleConfig.file) == null ? void 0 : _bundleConfig_file.endsWith('.d.cts')) || ((_bundleConfig_file1 = bundleConfig.file) == null ? void 0 : _bundleConfig_file1.endsWith('.d.ts')) && !isESModulePackage(pkg.type) ? 'cjs' : 'esm' : bundleConfig.format;
|
|
1269
|
+
const aliasPlugin = aliasEntries({
|
|
1270
|
+
entry,
|
|
1271
|
+
entries,
|
|
1272
|
+
entriesAlias: pluginContext.entriesAlias,
|
|
1273
|
+
format: aliasFormat,
|
|
1274
|
+
dts,
|
|
1275
|
+
cwd
|
|
1276
|
+
});
|
|
1164
1277
|
const commonPlugins = [
|
|
1165
1278
|
json__default.default(),
|
|
1166
|
-
sizePlugin
|
|
1167
|
-
aliasEntries({
|
|
1168
|
-
entry,
|
|
1169
|
-
entries,
|
|
1170
|
-
entriesAlias: pluginContext.entriesAlias,
|
|
1171
|
-
format: aliasFormat,
|
|
1172
|
-
dts
|
|
1173
|
-
})
|
|
1279
|
+
sizePlugin
|
|
1174
1280
|
];
|
|
1175
1281
|
const typesPlugins = [
|
|
1282
|
+
aliasPlugin,
|
|
1176
1283
|
...commonPlugins,
|
|
1177
1284
|
inlineCss({
|
|
1178
1285
|
skip: true
|
|
@@ -1188,14 +1295,15 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
|
|
|
1188
1295
|
}
|
|
1189
1296
|
const plugins = (dts ? typesPlugins : [
|
|
1190
1297
|
...commonPlugins,
|
|
1298
|
+
preserveDirectives__default.default(),
|
|
1299
|
+
aliasPlugin,
|
|
1191
1300
|
inlineCss({
|
|
1192
1301
|
exclude: /node_modules/
|
|
1193
1302
|
}),
|
|
1194
1303
|
rawContent({
|
|
1195
1304
|
exclude: /node_modules/
|
|
1196
1305
|
}),
|
|
1197
|
-
|
|
1198
|
-
prependDirectives(),
|
|
1306
|
+
isBinEntry && prependShebang(entry),
|
|
1199
1307
|
replace__default.default({
|
|
1200
1308
|
values: inlineDefinedValues,
|
|
1201
1309
|
preventAssignment: true
|
|
@@ -1215,7 +1323,8 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
|
|
|
1215
1323
|
}),
|
|
1216
1324
|
commonjs__default.default({
|
|
1217
1325
|
exclude: bundleConfig.external || null
|
|
1218
|
-
})
|
|
1326
|
+
}),
|
|
1327
|
+
prependDirectives()
|
|
1219
1328
|
]).filter(isNotNull);
|
|
1220
1329
|
return {
|
|
1221
1330
|
input: entry,
|
|
@@ -1235,30 +1344,13 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
|
|
|
1235
1344
|
if (code === 'CIRCULAR_DEPENDENCY' && /Circular dependency:(\s|\S)*node_modules/.test(warning.message)) {
|
|
1236
1345
|
return;
|
|
1237
1346
|
}
|
|
1347
|
+
if (code === 'MODULE_LEVEL_DIRECTIVE') {
|
|
1348
|
+
return;
|
|
1349
|
+
}
|
|
1238
1350
|
warn(warning);
|
|
1239
1351
|
}
|
|
1240
1352
|
};
|
|
1241
1353
|
}
|
|
1242
|
-
function getModuleLater(moduleMeta) {
|
|
1243
|
-
const directives = (moduleMeta.preserveDirectives || {
|
|
1244
|
-
directives: []
|
|
1245
|
-
}).directives.map((d)=>d.replace(/^use /, '')).filter((d)=>d !== 'strict');
|
|
1246
|
-
const moduleLayer = directives[0];
|
|
1247
|
-
return moduleLayer;
|
|
1248
|
-
}
|
|
1249
|
-
function getCustomModuleLayer(moduleId) {
|
|
1250
|
-
const segments = path__default.default.basename(moduleId).split('.');
|
|
1251
|
-
if (segments.length >= 2) {
|
|
1252
|
-
const [layerSegment, ext] = segments.slice(-2);
|
|
1253
|
-
const baseName = segments[0];
|
|
1254
|
-
const match = layerSegment.match(/^(\w+)-runtime$/);
|
|
1255
|
-
const layer = match && match[1];
|
|
1256
|
-
if (availableExtensions.has(ext) && layer && layer.length > 0) {
|
|
1257
|
-
return baseName + '-' + layer;
|
|
1258
|
-
}
|
|
1259
|
-
}
|
|
1260
|
-
return undefined;
|
|
1261
|
-
}
|
|
1262
1354
|
// dependencyGraphMap: Map<subModuleId, Set<entryParentId>>
|
|
1263
1355
|
function createSplitChunks(dependencyGraphMap, entryFiles) {
|
|
1264
1356
|
// If there's existing chunk being splitted, and contains a layer { <id>: <chunkGroup> }
|
|
@@ -1270,7 +1362,7 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
|
|
|
1270
1362
|
}
|
|
1271
1363
|
const { isEntry } = moduleInfo;
|
|
1272
1364
|
const moduleMeta = moduleInfo.meta;
|
|
1273
|
-
const moduleLayer =
|
|
1365
|
+
const moduleLayer = getModuleLayer(moduleMeta);
|
|
1274
1366
|
if (!isEntry) {
|
|
1275
1367
|
const cachedCustomModuleLayer = splitChunksGroupMap.get(id);
|
|
1276
1368
|
if (cachedCustomModuleLayer) return cachedCustomModuleLayer;
|
|
@@ -1288,7 +1380,7 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
|
|
|
1288
1380
|
if (!subModuleInfo) {
|
|
1289
1381
|
continue;
|
|
1290
1382
|
}
|
|
1291
|
-
const subModuleLayer =
|
|
1383
|
+
const subModuleLayer = getModuleLayer(moduleMeta);
|
|
1292
1384
|
if (subModuleLayer === moduleLayer) {
|
|
1293
1385
|
if (!dependencyGraphMap.has(subId)) {
|
|
1294
1386
|
dependencyGraphMap.set(subId, new Set());
|
|
@@ -1311,7 +1403,7 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
|
|
|
1311
1403
|
// If other entry is dependency of this entry
|
|
1312
1404
|
if (entryFiles.has(id)) {
|
|
1313
1405
|
const entryModuleInfo = ctx.getModuleInfo(id);
|
|
1314
|
-
const entryModuleLayer =
|
|
1406
|
+
const entryModuleLayer = getModuleLayer(entryModuleInfo ? entryModuleInfo.meta : {});
|
|
1315
1407
|
return entryModuleLayer === moduleLayer;
|
|
1316
1408
|
}
|
|
1317
1409
|
return false;
|
|
@@ -1520,20 +1612,17 @@ function createOutputState({ entries }) {
|
|
|
1520
1612
|
}
|
|
1521
1613
|
};
|
|
1522
1614
|
}
|
|
1523
|
-
function isBin(filename) {
|
|
1524
|
-
return filename === BINARY_TAG || filename.startsWith(BINARY_TAG + '/');
|
|
1525
|
-
}
|
|
1526
1615
|
function isTypeFile(filename) {
|
|
1527
1616
|
return filename.endsWith('.d.ts') || filename.endsWith('.d.mts') || filename.endsWith('.d.cts');
|
|
1528
1617
|
}
|
|
1529
1618
|
function normalizeExportName(exportName) {
|
|
1530
|
-
const isBinary =
|
|
1619
|
+
const isBinary = isBinExportPath(exportName);
|
|
1531
1620
|
let result = exportName;
|
|
1532
1621
|
if (isBinary) {
|
|
1533
1622
|
result = (exportName.replace(new RegExp(`^\\${BINARY_TAG}\\/?`), '') || '.') + ' (bin)';
|
|
1534
1623
|
} else {
|
|
1535
1624
|
const normalizedExportPath = normalizeExportPath(exportName);
|
|
1536
|
-
const specialConditionName =
|
|
1625
|
+
const specialConditionName = getSpecialExportTypeFromComposedExportPath(exportName);
|
|
1537
1626
|
result = normalizedExportPath + (specialConditionName !== 'default' ? ` (${specialConditionName})` : '');
|
|
1538
1627
|
}
|
|
1539
1628
|
return result;
|
|
@@ -1674,7 +1763,7 @@ async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
|
|
|
1674
1763
|
if (options.dts) {
|
|
1675
1764
|
typesEntryPath = getExportFileTypePath(mainExportPath);
|
|
1676
1765
|
}
|
|
1677
|
-
parsedExportsInfo.set('
|
|
1766
|
+
parsedExportsInfo.set('./index', [
|
|
1678
1767
|
[
|
|
1679
1768
|
mainExportPath,
|
|
1680
1769
|
'default'
|