bunchee 4.4.1 → 4.4.2
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/dist/bin/cli.js +44 -7
- package/dist/index.js +149 -71
- package/package.json +1 -1
package/dist/bin/cli.js
CHANGED
|
@@ -25,6 +25,13 @@ const availableExtensions = new Set([
|
|
|
25
25
|
'cts',
|
|
26
26
|
'mts'
|
|
27
27
|
]);
|
|
28
|
+
const suffixedExportConventions = new Set([
|
|
29
|
+
'react-server',
|
|
30
|
+
'react-native',
|
|
31
|
+
'edge-light',
|
|
32
|
+
'development',
|
|
33
|
+
'production'
|
|
34
|
+
]);
|
|
28
35
|
const SRC = 'src';
|
|
29
36
|
const DIST = 'dist';
|
|
30
37
|
const dtsExtensionsMap = {
|
|
@@ -203,6 +210,9 @@ const getFirstExportPath = (fullExportCondition)=>{
|
|
|
203
210
|
}
|
|
204
211
|
return fullExportCondition;
|
|
205
212
|
};
|
|
213
|
+
const joinExportAndCondition = (exportPath, condition)=>{
|
|
214
|
+
return (exportPath === '.' ? '' : exportPath) + '.' + condition;
|
|
215
|
+
};
|
|
206
216
|
function findExport(exportPath, exportCondition, paths, packageType, currentPath) {
|
|
207
217
|
// Skip `types` field, it cannot be the entry point
|
|
208
218
|
if (exportPath === 'types') return;
|
|
@@ -215,12 +225,20 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
|
|
|
215
225
|
};
|
|
216
226
|
} else {
|
|
217
227
|
const exportJsBundlePath = getFirstExportPath(fullExportCondition);
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
228
|
+
if (suffixedExportConventions.has(exportPath)) {
|
|
229
|
+
const specialPath = joinExportAndCondition(currentPath, exportPath);
|
|
230
|
+
paths[specialPath] = {
|
|
231
|
+
...paths[specialPath],
|
|
232
|
+
...exportCondition
|
|
233
|
+
};
|
|
234
|
+
} else {
|
|
235
|
+
// exportPath is exportType, import, require, ...
|
|
236
|
+
// merge to currentPath
|
|
237
|
+
paths[currentPath] = {
|
|
238
|
+
...paths[currentPath],
|
|
239
|
+
[exportPath]: exportJsBundlePath
|
|
240
|
+
};
|
|
241
|
+
}
|
|
224
242
|
}
|
|
225
243
|
return;
|
|
226
244
|
}
|
|
@@ -233,6 +251,25 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
|
|
|
233
251
|
} else {
|
|
234
252
|
// subpath is exportType, import, require, ...
|
|
235
253
|
const exportType = subpath;
|
|
254
|
+
if (typeof exportCondition[subpath] === 'object') {
|
|
255
|
+
const defaultPath = exportCondition[subpath].default;
|
|
256
|
+
if (defaultPath) {
|
|
257
|
+
const nestedExportCondition = {
|
|
258
|
+
[exportType]: defaultPath
|
|
259
|
+
};
|
|
260
|
+
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath);
|
|
261
|
+
}
|
|
262
|
+
// Find special export type, such as import: { development: './dev.js', production: './prod.js' }
|
|
263
|
+
const conditionSpecialTypes = Object.keys(exportCondition[exportType]).filter((key)=>suffixedExportConventions.has(key));
|
|
264
|
+
if (conditionSpecialTypes.length > 0) {
|
|
265
|
+
for (const conditionSpecialType of conditionSpecialTypes){
|
|
266
|
+
const nestedExportConditionPath = {
|
|
267
|
+
[exportType]: exportCondition[exportType][conditionSpecialType]
|
|
268
|
+
};
|
|
269
|
+
findExport(conditionSpecialType, nestedExportConditionPath, paths, packageType, currentPath);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
236
273
|
const defaultPath = typeof exportCondition[subpath] === 'object' ? exportCondition[subpath].default : exportCondition[subpath];
|
|
237
274
|
const nestedExportCondition = {
|
|
238
275
|
[exportType]: defaultPath
|
|
@@ -516,7 +553,7 @@ function lint$1(pkg) {
|
|
|
516
553
|
}
|
|
517
554
|
}
|
|
518
555
|
|
|
519
|
-
var version = "4.4.
|
|
556
|
+
var version = "4.4.2";
|
|
520
557
|
|
|
521
558
|
function relativify(path) {
|
|
522
559
|
return path.startsWith('.') ? path : `./${path}`;
|
package/dist/index.js
CHANGED
|
@@ -229,8 +229,14 @@ async function getSourcePathFromExportPath(cwd, exportPath, exportType) {
|
|
|
229
229
|
}
|
|
230
230
|
// TODO: add unit test
|
|
231
231
|
// Unlike path.basename, forcedly removing extension
|
|
232
|
-
function filePathWithoutExtension(
|
|
233
|
-
|
|
232
|
+
function filePathWithoutExtension(filePath) {
|
|
233
|
+
if (!filePath) return;
|
|
234
|
+
const lastDotIndex = filePath.lastIndexOf('.');
|
|
235
|
+
const lastSlashIndex = filePath.lastIndexOf('/');
|
|
236
|
+
if (lastDotIndex !== -1 && lastDotIndex > lastSlashIndex) {
|
|
237
|
+
return filePath.slice(0, filePath.indexOf('.', lastSlashIndex + 1));
|
|
238
|
+
}
|
|
239
|
+
return filePath;
|
|
234
240
|
}
|
|
235
241
|
const nonNullable = (n)=>Boolean(n);
|
|
236
242
|
const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.default.extname(filename).slice(1));
|
|
@@ -367,23 +373,64 @@ function rawContent({ exclude }) {
|
|
|
367
373
|
};
|
|
368
374
|
}
|
|
369
375
|
|
|
376
|
+
function relativify(path) {
|
|
377
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
378
|
+
}
|
|
379
|
+
|
|
370
380
|
// Alias entries to import path
|
|
371
381
|
// e.g.
|
|
372
382
|
// For a resolved file, if it's one of the entries,
|
|
373
383
|
// aliases it as export path, such as <absolute file> -> <pkg>/<export path>
|
|
374
|
-
function aliasEntries({ entries }) {
|
|
384
|
+
function aliasEntries({ entry, entries, entriesAlias, format, dts }) {
|
|
385
|
+
let currentDistPath = '';
|
|
386
|
+
const entryAliasWithoutSelf = {
|
|
387
|
+
...entriesAlias,
|
|
388
|
+
[entry]: null
|
|
389
|
+
};
|
|
390
|
+
const pathToRelativeDistMap = new Map();
|
|
391
|
+
for (const [, exportCondition] of Object.entries(entries)){
|
|
392
|
+
var _Object_entries_find;
|
|
393
|
+
const { import: importCond, require: requireCond, default: defaultCond } = exportCondition.export;
|
|
394
|
+
const firstCond = (_Object_entries_find = Object.entries(exportCondition.export).find(([key, cond])=>key !== 'types' && cond != null)) == null ? void 0 : _Object_entries_find[1];
|
|
395
|
+
if (dts) {
|
|
396
|
+
const fallbackCond = defaultCond || firstCond;
|
|
397
|
+
// For cjs, use require() instead of import
|
|
398
|
+
const firstDistPath = (format === 'cjs' ? requireCond : importCond) || fallbackCond;
|
|
399
|
+
if (firstDistPath) {
|
|
400
|
+
if (entry !== exportCondition.source) {
|
|
401
|
+
pathToRelativeDistMap.set(exportCondition.source, firstDistPath);
|
|
402
|
+
} else {
|
|
403
|
+
currentDistPath = firstDistPath;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
375
408
|
return {
|
|
376
409
|
name: 'alias',
|
|
377
410
|
resolveId: {
|
|
378
411
|
async handler (source, importer, options) {
|
|
379
412
|
const resolvedId = await this.resolve(source, importer, options);
|
|
380
413
|
if (resolvedId != null) {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
414
|
+
if (dts) {
|
|
415
|
+
// For types, generate relative path to the other type files,
|
|
416
|
+
// this will be compatible for the node10 ts module resolution.
|
|
417
|
+
const aliasedId = pathToRelativeDistMap.get(resolvedId.id);
|
|
418
|
+
if (aliasedId != null && aliasedId !== currentDistPath) {
|
|
419
|
+
const ext = path__default.default.extname(aliasedId);
|
|
420
|
+
const filePathBase = filePathWithoutExtension(path__default.default.relative(path__default.default.dirname(currentDistPath), aliasedId));
|
|
421
|
+
const relativePath = relativify(filePathBase + ext);
|
|
422
|
+
return {
|
|
423
|
+
id: relativePath,
|
|
424
|
+
external: true
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
428
|
+
const aliasedId = entryAliasWithoutSelf[resolvedId.id];
|
|
429
|
+
if (aliasedId != null) {
|
|
430
|
+
return {
|
|
431
|
+
id: aliasedId
|
|
432
|
+
};
|
|
433
|
+
}
|
|
387
434
|
}
|
|
388
435
|
}
|
|
389
436
|
return null;
|
|
@@ -463,6 +510,9 @@ const getFirstExportPath = (fullExportCondition)=>{
|
|
|
463
510
|
}
|
|
464
511
|
return fullExportCondition;
|
|
465
512
|
};
|
|
513
|
+
const joinExportAndCondition = (exportPath, condition)=>{
|
|
514
|
+
return (exportPath === '.' ? '' : exportPath) + '.' + condition;
|
|
515
|
+
};
|
|
466
516
|
function findExport(exportPath, exportCondition, paths, packageType, currentPath) {
|
|
467
517
|
// Skip `types` field, it cannot be the entry point
|
|
468
518
|
if (exportPath === 'types') return;
|
|
@@ -475,12 +525,20 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
|
|
|
475
525
|
};
|
|
476
526
|
} else {
|
|
477
527
|
const exportJsBundlePath = getFirstExportPath(fullExportCondition);
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
528
|
+
if (suffixedExportConventions.has(exportPath)) {
|
|
529
|
+
const specialPath = joinExportAndCondition(currentPath, exportPath);
|
|
530
|
+
paths[specialPath] = {
|
|
531
|
+
...paths[specialPath],
|
|
532
|
+
...exportCondition
|
|
533
|
+
};
|
|
534
|
+
} else {
|
|
535
|
+
// exportPath is exportType, import, require, ...
|
|
536
|
+
// merge to currentPath
|
|
537
|
+
paths[currentPath] = {
|
|
538
|
+
...paths[currentPath],
|
|
539
|
+
[exportPath]: exportJsBundlePath
|
|
540
|
+
};
|
|
541
|
+
}
|
|
484
542
|
}
|
|
485
543
|
return;
|
|
486
544
|
}
|
|
@@ -493,6 +551,25 @@ function findExport(exportPath, exportCondition, paths, packageType, currentPath
|
|
|
493
551
|
} else {
|
|
494
552
|
// subpath is exportType, import, require, ...
|
|
495
553
|
const exportType = subpath;
|
|
554
|
+
if (typeof exportCondition[subpath] === 'object') {
|
|
555
|
+
const defaultPath = exportCondition[subpath].default;
|
|
556
|
+
if (defaultPath) {
|
|
557
|
+
const nestedExportCondition = {
|
|
558
|
+
[exportType]: defaultPath
|
|
559
|
+
};
|
|
560
|
+
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath);
|
|
561
|
+
}
|
|
562
|
+
// Find special export type, such as import: { development: './dev.js', production: './prod.js' }
|
|
563
|
+
const conditionSpecialTypes = Object.keys(exportCondition[exportType]).filter((key)=>suffixedExportConventions.has(key));
|
|
564
|
+
if (conditionSpecialTypes.length > 0) {
|
|
565
|
+
for (const conditionSpecialType of conditionSpecialTypes){
|
|
566
|
+
const nestedExportConditionPath = {
|
|
567
|
+
[exportType]: exportCondition[exportType][conditionSpecialType]
|
|
568
|
+
};
|
|
569
|
+
findExport(conditionSpecialType, nestedExportConditionPath, paths, packageType, currentPath);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
496
573
|
const defaultPath = typeof exportCondition[subpath] === 'object' ? exportCondition[subpath].default : exportCondition[subpath];
|
|
497
574
|
const nestedExportCondition = {
|
|
498
575
|
[exportType]: defaultPath
|
|
@@ -655,10 +732,12 @@ function isEsmExportName(name, ext) {
|
|
|
655
732
|
}
|
|
656
733
|
function isCjsExportName(pkg, exportCondition, ext) {
|
|
657
734
|
const isESModule = isESModulePackage(pkg.type);
|
|
658
|
-
|
|
735
|
+
const isCjsCondition = [
|
|
659
736
|
'require',
|
|
660
737
|
'main'
|
|
661
|
-
].includes(exportCondition)
|
|
738
|
+
].includes(exportCondition);
|
|
739
|
+
const isNotEsmExportName = !isEsmExportName(exportCondition, ext);
|
|
740
|
+
return !isESModule && isNotEsmExportName && (ext !== 'mjs' || isCjsCondition) || ext === 'cjs';
|
|
662
741
|
}
|
|
663
742
|
function getExportsDistFilesOfCondition(pkg, parsedExportCondition, cwd) {
|
|
664
743
|
const dist = [];
|
|
@@ -709,7 +788,7 @@ const swcMinifyOptions = {
|
|
|
709
788
|
}
|
|
710
789
|
};
|
|
711
790
|
// return { 'process.env.<key>': '<value>' }
|
|
712
|
-
function getBuildEnv(envs,
|
|
791
|
+
function getBuildEnv(envs, parsedExportCondition) {
|
|
713
792
|
if (!envs.includes('NODE_ENV')) {
|
|
714
793
|
envs.push('NODE_ENV');
|
|
715
794
|
}
|
|
@@ -720,8 +799,10 @@ function getBuildEnv(envs, exportConditions) {
|
|
|
720
799
|
}
|
|
721
800
|
return acc;
|
|
722
801
|
}, {});
|
|
802
|
+
// handle .development, .production
|
|
803
|
+
const condName = parsedExportCondition.name.startsWith('.') ? parsedExportCondition.name.slice(1) : parsedExportCondition.name;
|
|
804
|
+
const exportConditionNames = new Set(Object.keys(parsedExportCondition.export).concat(condName));
|
|
723
805
|
// For development and production convention, we override the NODE_ENV value
|
|
724
|
-
const exportConditionNames = new Set(Object.keys(exportConditions));
|
|
725
806
|
if (exportConditionNames.has('development')) {
|
|
726
807
|
envVars['process.env.NODE_ENV'] = JSON.stringify('development');
|
|
727
808
|
} else if (exportConditionNames.has('production')) {
|
|
@@ -744,15 +825,16 @@ function getBuildEnv(envs, exportConditions) {
|
|
|
744
825
|
}
|
|
745
826
|
return alias;
|
|
746
827
|
}
|
|
747
|
-
async function buildInputConfig(entry,
|
|
828
|
+
async function buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts) {
|
|
829
|
+
var _bundleConfig_file;
|
|
748
830
|
const { entries, pkg, cwd, tsOptions: { tsConfigPath, tsCompilerOptions }, pluginContext } = buildContext;
|
|
749
|
-
const hasNoExternal =
|
|
750
|
-
var
|
|
831
|
+
const hasNoExternal = bundleConfig.external === null;
|
|
832
|
+
var _bundleConfig_external;
|
|
751
833
|
const externals = hasNoExternal ? [] : [
|
|
752
834
|
pkg.peerDependencies,
|
|
753
835
|
pkg.dependencies,
|
|
754
836
|
pkg.peerDependenciesMeta
|
|
755
|
-
].filter((n)=>Boolean(n)).map((o)=>Object.keys(o)).reduce((a, b)=>a.concat(b), []).concat((
|
|
837
|
+
].filter((n)=>Boolean(n)).map((o)=>Object.keys(o)).reduce((a, b)=>a.concat(b), []).concat((_bundleConfig_external = bundleConfig.external) != null ? _bundleConfig_external : []);
|
|
756
838
|
for (const [exportImportPath, exportCondition] of Object.entries(entries)){
|
|
757
839
|
const entryFilePath = exportCondition.source;
|
|
758
840
|
if (entryFilePath !== entry) {
|
|
@@ -760,9 +842,9 @@ async function buildInputConfig(entry, options, buildContext, exportCondition, d
|
|
|
760
842
|
externals.push(entryFilePath);
|
|
761
843
|
}
|
|
762
844
|
}
|
|
763
|
-
const envValues = getBuildEnv(
|
|
845
|
+
const envValues = getBuildEnv(bundleConfig.env || [], exportCondition);
|
|
764
846
|
const { useTypeScript } = buildContext;
|
|
765
|
-
const { runtime, target: jscTarget, minify: shouldMinify } =
|
|
847
|
+
const { runtime, target: jscTarget, minify: shouldMinify } = bundleConfig;
|
|
766
848
|
const hasSpecifiedTsTarget = Boolean(tsCompilerOptions.target && tsConfigPath);
|
|
767
849
|
const swcParserConfig = {
|
|
768
850
|
syntax: useTypeScript ? 'typescript' : 'ecmascript',
|
|
@@ -780,24 +862,25 @@ async function buildInputConfig(entry, options, buildContext, exportCondition, d
|
|
|
780
862
|
...shouldMinify && {
|
|
781
863
|
minify: {
|
|
782
864
|
...swcMinifyOptions,
|
|
783
|
-
sourceMap:
|
|
865
|
+
sourceMap: bundleConfig.sourcemap
|
|
784
866
|
}
|
|
785
867
|
}
|
|
786
868
|
},
|
|
787
|
-
sourceMaps:
|
|
869
|
+
sourceMaps: bundleConfig.sourcemap,
|
|
788
870
|
inlineSourcesContent: false,
|
|
789
871
|
isModule: true
|
|
790
872
|
};
|
|
791
873
|
const sizePlugin = pluginContext.outputState.plugin(cwd);
|
|
792
874
|
// common plugins for both dts and ts assets that need to be processed
|
|
875
|
+
const aliasFormat = dts ? ((_bundleConfig_file = bundleConfig.file) == null ? void 0 : _bundleConfig_file.endsWith('.d.cts')) ? 'cjs' : 'esm' : bundleConfig.format;
|
|
793
876
|
const commonPlugins = [
|
|
794
877
|
sizePlugin,
|
|
795
878
|
aliasEntries({
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
879
|
+
entry,
|
|
880
|
+
entries,
|
|
881
|
+
entriesAlias: pluginContext.entriesAlias,
|
|
882
|
+
format: aliasFormat,
|
|
883
|
+
dts
|
|
801
884
|
})
|
|
802
885
|
];
|
|
803
886
|
const typesPlugins = [
|
|
@@ -866,7 +949,7 @@ async function buildInputConfig(entry, options, buildContext, exportCondition, d
|
|
|
866
949
|
...swcOptions
|
|
867
950
|
}),
|
|
868
951
|
commonjs__default.default({
|
|
869
|
-
exclude:
|
|
952
|
+
exclude: bundleConfig.external || null
|
|
870
953
|
}),
|
|
871
954
|
json__default.default()
|
|
872
955
|
]).filter(isNotNull);
|
|
@@ -976,20 +1059,21 @@ function createSplitChunks(dependencyGraphMap, entryFiles) {
|
|
|
976
1059
|
return;
|
|
977
1060
|
};
|
|
978
1061
|
}
|
|
979
|
-
function buildOutputConfigs(
|
|
980
|
-
const { format } =
|
|
1062
|
+
async function buildOutputConfigs(entry, bundleConfig, exportCondition, buildContext, dts) {
|
|
1063
|
+
const { format } = bundleConfig;
|
|
981
1064
|
const { entries, pkg, exportPaths, cwd, tsOptions: { tsCompilerOptions }, pluginContext } = buildContext;
|
|
982
1065
|
// Add esm mark and interop helper if esm export is detected
|
|
983
1066
|
const useEsModuleMark = hasEsmExport(exportPaths, tsCompilerOptions);
|
|
984
|
-
const absoluteOutputFile = path.resolve(cwd,
|
|
1067
|
+
const absoluteOutputFile = path.resolve(cwd, bundleConfig.file);
|
|
985
1068
|
const name = filePathWithoutExtension(absoluteOutputFile);
|
|
986
1069
|
var _exportCondition_export_types;
|
|
987
|
-
const dtsFile = path.resolve(cwd, dts ?
|
|
1070
|
+
const dtsFile = path.resolve(cwd, dts ? bundleConfig.file : (_exportCondition_export_types = exportCondition.export.types) != null ? _exportCondition_export_types : getExportFileTypePath(bundleConfig.file));
|
|
988
1071
|
const typesDir = path.dirname(dtsFile);
|
|
989
1072
|
const jsDir = path.dirname(absoluteOutputFile);
|
|
990
1073
|
const outputFile = dts ? dtsFile : absoluteOutputFile;
|
|
991
1074
|
const entryFiles = new Set(Object.values(entries).map((entry)=>entry.source));
|
|
992
|
-
|
|
1075
|
+
const inputOptions = await buildInputConfig(entry, bundleConfig, exportCondition, buildContext, dts);
|
|
1076
|
+
const outputOptions = {
|
|
993
1077
|
name: pkg.name || name,
|
|
994
1078
|
dir: dts ? typesDir : jsDir,
|
|
995
1079
|
format,
|
|
@@ -998,7 +1082,7 @@ function buildOutputConfigs(options, exportCondition, buildContext, dts) {
|
|
|
998
1082
|
interop: 'auto',
|
|
999
1083
|
freeze: false,
|
|
1000
1084
|
strict: false,
|
|
1001
|
-
sourcemap:
|
|
1085
|
+
sourcemap: bundleConfig.sourcemap,
|
|
1002
1086
|
manualChunks: createSplitChunks(pluginContext.moduleDirectiveLayerMap, entryFiles),
|
|
1003
1087
|
chunkFileNames: '[name]-[hash].js',
|
|
1004
1088
|
// By default in rollup, when creating multiple chunks, transitive imports of entry chunks
|
|
@@ -1006,19 +1090,24 @@ function buildOutputConfigs(options, exportCondition, buildContext, dts) {
|
|
|
1006
1090
|
hoistTransitiveImports: false,
|
|
1007
1091
|
entryFileNames: path.basename(outputFile)
|
|
1008
1092
|
};
|
|
1093
|
+
return {
|
|
1094
|
+
input: inputOptions,
|
|
1095
|
+
output: outputOptions
|
|
1096
|
+
};
|
|
1009
1097
|
}
|
|
1010
1098
|
async function buildEntryConfig(bundleConfig, pluginContext, dts) {
|
|
1011
1099
|
const configs = [];
|
|
1012
1100
|
const { entries } = pluginContext;
|
|
1013
1101
|
for (const exportCondition of Object.values(entries)){
|
|
1014
|
-
const
|
|
1015
|
-
configs.push(
|
|
1102
|
+
const rollupConfigs = await buildConfig(bundleConfig, exportCondition, pluginContext, dts);
|
|
1103
|
+
configs.push(...rollupConfigs);
|
|
1016
1104
|
}
|
|
1017
|
-
return
|
|
1105
|
+
return configs;
|
|
1018
1106
|
}
|
|
1019
1107
|
async function collectEntry(// export type, e.g. react-server, edge-light those special cases required suffix
|
|
1020
1108
|
exportType, options) {
|
|
1021
|
-
const { cwd, pkg, entries, entryPath, exportCondRef, entryExport } = options;
|
|
1109
|
+
const { cwd, pkg, entries, entryPath, exportCondRef, entryExport: originEntryExport } = options;
|
|
1110
|
+
let entryExport = originEntryExport;
|
|
1022
1111
|
let exportCondForType = {
|
|
1023
1112
|
...exportCondRef
|
|
1024
1113
|
};
|
|
@@ -1027,8 +1116,13 @@ exportType, options) {
|
|
|
1027
1116
|
exportCondForType = {
|
|
1028
1117
|
[exportType]: exportCondRef[exportType]
|
|
1029
1118
|
};
|
|
1030
|
-
|
|
1119
|
+
} else if (exportType[0] === '.' && suffixedExportConventions.has(exportType.slice(1))) {
|
|
1120
|
+
// e.g. .development, .production that has both esm and cjs export
|
|
1121
|
+
exportCondForType = exportCondRef;
|
|
1122
|
+
exportType = exportType.slice(1);
|
|
1123
|
+
entryExport = entryExport.replace(exportType, '');
|
|
1031
1124
|
} else {
|
|
1125
|
+
// Basic export type, pass down the exportPaths with erasing the special ones
|
|
1032
1126
|
for (const exportType of suffixedExportConventions){
|
|
1033
1127
|
delete exportCondForType[exportType];
|
|
1034
1128
|
}
|
|
@@ -1044,7 +1138,7 @@ exportType, options) {
|
|
|
1044
1138
|
}
|
|
1045
1139
|
const exportCondition = {
|
|
1046
1140
|
source,
|
|
1047
|
-
name:
|
|
1141
|
+
name: originEntryExport,
|
|
1048
1142
|
export: exportCondForType
|
|
1049
1143
|
};
|
|
1050
1144
|
const nameWithExportPath = pkg.name ? path__default.default.join(pkg.name, exportCondition.name) : exportCondition.name;
|
|
@@ -1106,6 +1200,8 @@ exportType, options) {
|
|
|
1106
1200
|
for (const exportCondType of suffixedExportConventions){
|
|
1107
1201
|
if (exportCond[exportCondType]) {
|
|
1108
1202
|
await collectEntry(exportCondType, collectEntryOptions);
|
|
1203
|
+
} else if (entryExport === '.' + exportCondType) {
|
|
1204
|
+
await collectEntry(entryExport, collectEntryOptions);
|
|
1109
1205
|
}
|
|
1110
1206
|
}
|
|
1111
1207
|
}
|
|
@@ -1115,14 +1211,8 @@ exportType, options) {
|
|
|
1115
1211
|
}
|
|
1116
1212
|
async function buildConfig(bundleConfig, exportCondition, pluginContext, dts) {
|
|
1117
1213
|
const { file } = bundleConfig;
|
|
1118
|
-
const { pkg, cwd
|
|
1119
|
-
const useTypescript = Boolean(tsOptions.tsConfigPath);
|
|
1120
|
-
const options = {
|
|
1121
|
-
...bundleConfig,
|
|
1122
|
-
useTypescript
|
|
1123
|
-
};
|
|
1214
|
+
const { pkg, cwd } = pluginContext;
|
|
1124
1215
|
const entry = exportCondition.source;
|
|
1125
|
-
const inputOptions = await buildInputConfig(entry, options, pluginContext, exportCondition, dts);
|
|
1126
1216
|
const outputExports = getExportsDistFilesOfCondition(pkg, exportCondition, cwd);
|
|
1127
1217
|
// If there's nothing found, give a default output
|
|
1128
1218
|
if (outputExports.length === 0 && !pkg.bin) {
|
|
@@ -1166,26 +1256,18 @@ async function buildConfig(bundleConfig, exportCondition, pluginContext, dts) {
|
|
|
1166
1256
|
bundleOptions = Array.from(uniqTypes).map((typeFile)=>{
|
|
1167
1257
|
return {
|
|
1168
1258
|
resolvedFile: typeFile,
|
|
1169
|
-
format: '
|
|
1259
|
+
format: 'esm'
|
|
1170
1260
|
};
|
|
1171
1261
|
});
|
|
1172
1262
|
}
|
|
1173
|
-
const outputConfigs = bundleOptions.map((bundleOption)=>{
|
|
1174
|
-
return buildOutputConfigs({
|
|
1263
|
+
const outputConfigs = bundleOptions.map(async (bundleOption)=>{
|
|
1264
|
+
return await buildOutputConfigs(entry, {
|
|
1175
1265
|
...bundleConfig,
|
|
1176
1266
|
file: bundleOption.resolvedFile,
|
|
1177
1267
|
format: bundleOption.format
|
|
1178
1268
|
}, exportCondition, pluginContext, dts);
|
|
1179
1269
|
});
|
|
1180
|
-
return
|
|
1181
|
-
input: inputOptions,
|
|
1182
|
-
output: outputConfigs,
|
|
1183
|
-
exportName: exportCondition.name || '.'
|
|
1184
|
-
};
|
|
1185
|
-
}
|
|
1186
|
-
|
|
1187
|
-
function relativify(path) {
|
|
1188
|
-
return path.startsWith('.') ? path : `./${path}`;
|
|
1270
|
+
return Promise.all(outputConfigs);
|
|
1189
1271
|
}
|
|
1190
1272
|
|
|
1191
1273
|
// Example: @foo/bar -> bar
|
|
@@ -1377,7 +1459,7 @@ function hasMultiEntryExport(exportPaths) {
|
|
|
1377
1459
|
}
|
|
1378
1460
|
async function bundle(cliEntryPath, { cwd: _cwd, ...options } = {}) {
|
|
1379
1461
|
const cwd = path.resolve(process.cwd(), _cwd || '');
|
|
1380
|
-
assignDefault(options, 'format', '
|
|
1462
|
+
assignDefault(options, 'format', 'esm');
|
|
1381
1463
|
assignDefault(options, 'minify', false);
|
|
1382
1464
|
assignDefault(options, 'target', 'es2015');
|
|
1383
1465
|
const pkg = await getPackageMeta(cwd);
|
|
@@ -1518,15 +1600,11 @@ function runWatch({ input, output }) {
|
|
|
1518
1600
|
return watcher;
|
|
1519
1601
|
}
|
|
1520
1602
|
async function removeOutputDir(output) {
|
|
1521
|
-
|
|
1522
|
-
for (const dir of dirs){
|
|
1523
|
-
if (dir) await removeDir(dir);
|
|
1524
|
-
}
|
|
1603
|
+
if (output.dir) await removeDir(output.dir);
|
|
1525
1604
|
}
|
|
1526
1605
|
function runBundle({ input, output }) {
|
|
1527
1606
|
return rollup.rollup(input).then((bundle)=>{
|
|
1528
|
-
|
|
1529
|
-
return Promise.all(writeJobs);
|
|
1607
|
+
return bundle.write(output);
|
|
1530
1608
|
}, catchErrorHandler);
|
|
1531
1609
|
}
|
|
1532
1610
|
function logError(error) {
|