bunchee 4.3.3 → 4.4.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 +31 -11
- package/dist/bin/cli.js +394 -34
- package/dist/index.d.ts +1 -0
- package/dist/index.js +281 -238
- package/package.json +9 -3
package/dist/bin/cli.js
CHANGED
|
@@ -3,6 +3,7 @@ var path = require('path');
|
|
|
3
3
|
var arg = require('arg');
|
|
4
4
|
var fs = require('fs');
|
|
5
5
|
var fsp = require('fs/promises');
|
|
6
|
+
require('rimraf');
|
|
6
7
|
var require$$0 = require('tty');
|
|
7
8
|
var bunchee = require('bunchee');
|
|
8
9
|
|
|
@@ -25,6 +26,7 @@ const availableExtensions = new Set([
|
|
|
25
26
|
'mts'
|
|
26
27
|
]);
|
|
27
28
|
const SRC = 'src';
|
|
29
|
+
const DIST = 'dist';
|
|
28
30
|
const dtsExtensionsMap = {
|
|
29
31
|
js: 'd.ts',
|
|
30
32
|
cjs: 'd.cts',
|
|
@@ -36,6 +38,12 @@ const tsExtensions = new Set([
|
|
|
36
38
|
'cts',
|
|
37
39
|
'mts'
|
|
38
40
|
]);
|
|
41
|
+
const DEFAULT_TS_CONFIG = {
|
|
42
|
+
compilerOptions: {
|
|
43
|
+
module: 'ESNext',
|
|
44
|
+
moduleResolution: 'bundler'
|
|
45
|
+
}
|
|
46
|
+
};
|
|
39
47
|
|
|
40
48
|
function getDefaultExportFromCjs (x) {
|
|
41
49
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
@@ -140,21 +148,372 @@ function fileExists(filePath) {
|
|
|
140
148
|
return fs__default.default.existsSync(filePath);
|
|
141
149
|
}
|
|
142
150
|
const hasAvailableExtension = (filename)=>availableExtensions.has(path__default.default.extname(filename).slice(1));
|
|
151
|
+
const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
|
|
152
|
+
// TODO: add unit test
|
|
143
153
|
const baseNameWithoutExtension = (filename)=>path__default.default.basename(filename, path__default.default.extname(filename));
|
|
154
|
+
const isTestFile = (filename)=>/\.(test|spec)$/.test(baseNameWithoutExtension(filename));
|
|
144
155
|
|
|
145
|
-
|
|
156
|
+
function getTypings(pkg) {
|
|
157
|
+
return pkg.types || pkg.typings;
|
|
158
|
+
}
|
|
159
|
+
// Reached the end of the export path
|
|
160
|
+
function isExportLike(field) {
|
|
161
|
+
if (typeof field === 'string') return true;
|
|
162
|
+
return Object.entries(field).every(// Every value is string and key is not start with '.'
|
|
163
|
+
([key, value])=>typeof value === 'string' && !key.startsWith('.'));
|
|
164
|
+
}
|
|
165
|
+
function constructFullExportCondition(exportCondition, packageType) {
|
|
166
|
+
let fullExportCond;
|
|
167
|
+
if (typeof exportCondition === 'string') {
|
|
168
|
+
const exportType = getExportTypeFromFile(exportCondition, packageType);
|
|
169
|
+
fullExportCond = {
|
|
170
|
+
[exportType]: exportCondition
|
|
171
|
+
};
|
|
172
|
+
} else {
|
|
173
|
+
const exportTypes = Object.keys(exportCondition);
|
|
174
|
+
fullExportCond = {};
|
|
175
|
+
exportTypes.forEach((exportType)=>{
|
|
176
|
+
const condition = exportCondition[exportType];
|
|
177
|
+
// Filter out nullable value
|
|
178
|
+
if (condition) {
|
|
179
|
+
fullExportCond[exportType] = condition;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
return fullExportCond;
|
|
184
|
+
}
|
|
185
|
+
function joinRelativePath(...segments) {
|
|
186
|
+
let result = path.join(...segments);
|
|
187
|
+
// If the first segment starts with '.', ensure the result does too.
|
|
188
|
+
if (segments[0] === '.' && !result.startsWith('.')) {
|
|
189
|
+
result = './' + result;
|
|
190
|
+
}
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
const getFirstExportPath = (fullExportCondition)=>{
|
|
194
|
+
// Handle all export cond { <require|import|default>: ... }
|
|
195
|
+
if (typeof fullExportCondition === 'object') {
|
|
196
|
+
for (const key of Object.keys(fullExportCondition)){
|
|
197
|
+
if (key.startsWith('.') || key === 'types') {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
return fullExportCondition[key];
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return fullExportCondition;
|
|
204
|
+
};
|
|
205
|
+
function findExport(exportPath, exportCondition, paths, packageType, currentPath) {
|
|
206
|
+
// Skip `types` field, it cannot be the entry point
|
|
207
|
+
if (exportPath === 'types') return;
|
|
208
|
+
if (isExportLike(exportCondition)) {
|
|
209
|
+
const fullExportCondition = constructFullExportCondition(exportCondition, packageType);
|
|
210
|
+
if (exportPath.startsWith('.')) {
|
|
211
|
+
paths[exportPath] = {
|
|
212
|
+
...paths[exportPath],
|
|
213
|
+
...fullExportCondition
|
|
214
|
+
};
|
|
215
|
+
} else {
|
|
216
|
+
const exportJsBundlePath = getFirstExportPath(fullExportCondition);
|
|
217
|
+
// exportPath is exportType, import, require, ...
|
|
218
|
+
// merge to currentPath
|
|
219
|
+
paths[currentPath] = {
|
|
220
|
+
...paths[currentPath],
|
|
221
|
+
[exportPath]: exportJsBundlePath
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
Object.keys(exportCondition).forEach((subpath)=>{
|
|
227
|
+
if (subpath.startsWith('.')) {
|
|
228
|
+
// subpath is actual export path, ./a, ./b, ...
|
|
229
|
+
const nestedExportPath = joinRelativePath(currentPath, subpath);
|
|
230
|
+
const nestedExportCondition = exportCondition[subpath];
|
|
231
|
+
findExport(nestedExportPath, nestedExportCondition, paths, packageType, nestedExportPath);
|
|
232
|
+
} else {
|
|
233
|
+
// subpath is exportType, import, require, ...
|
|
234
|
+
const exportType = subpath;
|
|
235
|
+
const defaultPath = typeof exportCondition[subpath] === 'object' ? exportCondition[subpath].default : exportCondition[subpath];
|
|
236
|
+
const nestedExportCondition = {
|
|
237
|
+
[exportType]: defaultPath
|
|
238
|
+
};
|
|
239
|
+
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
*
|
|
245
|
+
* Convert package.exports field to paths mapping
|
|
246
|
+
* Example
|
|
247
|
+
*
|
|
248
|
+
* Input:
|
|
249
|
+
* {
|
|
250
|
+
* "./sub": {
|
|
251
|
+
* "import": {
|
|
252
|
+
* "types": "./sub.js",
|
|
253
|
+
* "default": "./sub.cjs",
|
|
254
|
+
* }
|
|
255
|
+
* }
|
|
256
|
+
* }
|
|
257
|
+
*
|
|
258
|
+
* Output:
|
|
259
|
+
* {
|
|
260
|
+
* "./sub": {
|
|
261
|
+
* "import": "./sub.js",
|
|
262
|
+
* "require": "./sub.cjs",
|
|
263
|
+
* "types": "./sub.d.ts",
|
|
264
|
+
* }
|
|
265
|
+
* }
|
|
266
|
+
*
|
|
267
|
+
*/ function parseExport(exportsCondition, packageType) {
|
|
268
|
+
const paths = {};
|
|
269
|
+
const initialPath = '.';
|
|
270
|
+
if (typeof exportsCondition === 'string') {
|
|
271
|
+
paths[initialPath] = constructFullExportCondition(exportsCondition, packageType);
|
|
272
|
+
} else if (typeof exportsCondition === 'object') {
|
|
273
|
+
if (isExportLike(exportsCondition)) {
|
|
274
|
+
paths[initialPath] = constructFullExportCondition(exportsCondition, packageType);
|
|
275
|
+
} else {
|
|
276
|
+
Object.keys(exportsCondition).forEach((key)=>{
|
|
277
|
+
const exportCondition = exportsCondition[key];
|
|
278
|
+
findExport(key, exportCondition, paths, packageType, initialPath);
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return paths;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Get package exports paths
|
|
286
|
+
*
|
|
287
|
+
* Example:
|
|
288
|
+
*
|
|
289
|
+
* ```json
|
|
290
|
+
* {
|
|
291
|
+
* "exports": {
|
|
292
|
+
* ".": {
|
|
293
|
+
* "require": "./dist/index.cjs",
|
|
294
|
+
* "module": "./dist/index.esm.js",
|
|
295
|
+
* "default": "./dist/index.esm.js"
|
|
296
|
+
* },
|
|
297
|
+
* "./foo": {
|
|
298
|
+
* "require": "./dist/foo.cjs",
|
|
299
|
+
* "module": "./dist/foo.esm.js",
|
|
300
|
+
* "default": "./dist/foo.esm.js"
|
|
301
|
+
* }
|
|
302
|
+
* }
|
|
303
|
+
*
|
|
304
|
+
* ```
|
|
305
|
+
*
|
|
306
|
+
* will be parsed to:
|
|
307
|
+
*
|
|
308
|
+
* ```js
|
|
309
|
+
* {
|
|
310
|
+
* '.': {
|
|
311
|
+
* main: './dist/index.cjs',
|
|
312
|
+
* module: './dist/index.esm.js',
|
|
313
|
+
* export: './dist/index.esm.js'
|
|
314
|
+
* },
|
|
315
|
+
* './foo': {
|
|
316
|
+
* main: './dist/foo.cjs',
|
|
317
|
+
* module: './dist/foo.esm.js',
|
|
318
|
+
* export: './dist/foo.esm.js'
|
|
319
|
+
* }
|
|
320
|
+
*
|
|
321
|
+
*
|
|
322
|
+
* pkg.main and pkg.module will be added to ['.'] if exists
|
|
323
|
+
*/ function getExportPaths(pkg, resolvedWildcardExports) {
|
|
324
|
+
var _pathsMap_;
|
|
325
|
+
let pathsMap = {};
|
|
326
|
+
const packageType = getPackageType(pkg);
|
|
327
|
+
const isEsmPackage = isESModulePackage(packageType);
|
|
328
|
+
const exportsConditions = resolvedWildcardExports != null ? resolvedWildcardExports : pkg.exports;
|
|
329
|
+
if (exportsConditions) {
|
|
330
|
+
const paths = parseExport(exportsConditions, packageType);
|
|
331
|
+
pathsMap = {
|
|
332
|
+
...pathsMap,
|
|
333
|
+
...paths
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
// main export '.' from main/module/typings
|
|
337
|
+
const defaultMainExport = constructFullExportCondition({
|
|
338
|
+
[isEsmPackage ? 'import' : 'require']: pkg.main,
|
|
339
|
+
module: pkg.module,
|
|
340
|
+
types: getTypings(pkg)
|
|
341
|
+
}, packageType);
|
|
342
|
+
if (!isEsmPackage && ((_pathsMap_ = pathsMap['.']) == null ? void 0 : _pathsMap_['require'])) {
|
|
343
|
+
// pathsMap's exports.require are prioritized.
|
|
344
|
+
defaultMainExport['require'] = pathsMap['.']['require'];
|
|
345
|
+
}
|
|
346
|
+
// Merge the main export into '.' paths
|
|
347
|
+
const mainExport = {
|
|
348
|
+
...pathsMap['.'],
|
|
349
|
+
...defaultMainExport
|
|
350
|
+
};
|
|
351
|
+
// main export is not empty
|
|
352
|
+
if (Object.keys(mainExport).length > 0) {
|
|
353
|
+
pathsMap['.'] = {
|
|
354
|
+
...pathsMap['.'],
|
|
355
|
+
...mainExport
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return pathsMap;
|
|
359
|
+
}
|
|
360
|
+
function getPackageType(pkg) {
|
|
361
|
+
return pkg.type || 'commonjs';
|
|
362
|
+
}
|
|
363
|
+
function isESModulePackage(packageType) {
|
|
364
|
+
return packageType === 'module';
|
|
365
|
+
}
|
|
366
|
+
function getExportTypeFromFile(filename, pkgType) {
|
|
367
|
+
const isESModule = isESModulePackage(pkgType);
|
|
368
|
+
const isCjsExt = filename.endsWith('.cjs');
|
|
369
|
+
const isEsmExt = filename.endsWith('.mjs');
|
|
370
|
+
const exportType = isEsmExt ? 'import' : isCjsExt ? 'require' : isESModule ? 'import' : 'require';
|
|
371
|
+
return exportType;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function lint$1(pkg) {
|
|
375
|
+
const { name, main, exports } = pkg;
|
|
376
|
+
const isESM = isESModulePackage(pkg.type);
|
|
377
|
+
const exportPaths = getExportPaths(pkg);
|
|
378
|
+
if (!name) {
|
|
379
|
+
logger.warn('Missing package name');
|
|
380
|
+
}
|
|
381
|
+
const state = {
|
|
382
|
+
badMainExtension: false,
|
|
383
|
+
badMainExport: false,
|
|
384
|
+
invalidExportsFieldType: false,
|
|
385
|
+
badCjsRequireExport: {
|
|
386
|
+
value: false,
|
|
387
|
+
paths: []
|
|
388
|
+
},
|
|
389
|
+
badCjsImportExport: {
|
|
390
|
+
value: false,
|
|
391
|
+
paths: []
|
|
392
|
+
},
|
|
393
|
+
badEsmRequireExport: {
|
|
394
|
+
value: false,
|
|
395
|
+
paths: []
|
|
396
|
+
},
|
|
397
|
+
badEsmImportExport: {
|
|
398
|
+
value: false,
|
|
399
|
+
paths: []
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
// Validate ESM package
|
|
403
|
+
if (isESM) {
|
|
404
|
+
if (main && hasCjsExtension(main)) {
|
|
405
|
+
state.badMainExtension = true;
|
|
406
|
+
}
|
|
407
|
+
if (exports) {
|
|
408
|
+
if (typeof exports === 'string') {
|
|
409
|
+
if (hasCjsExtension(exports)) {
|
|
410
|
+
state.badMainExport = true;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (typeof exports !== 'object') {
|
|
414
|
+
state.invalidExportsFieldType = true;
|
|
415
|
+
} else {
|
|
416
|
+
Object.keys(exportPaths).forEach((key)=>{
|
|
417
|
+
const exportConditions = exportPaths[key];
|
|
418
|
+
if (typeof exportConditions === 'object') {
|
|
419
|
+
var // @ts-ignore TODO: fix the type
|
|
420
|
+
_exportConditions_require, // @ts-ignore TODO: fix the type
|
|
421
|
+
_exportConditions_import;
|
|
422
|
+
var _exportConditions_require_default;
|
|
423
|
+
const requirePath = (_exportConditions_require_default = (_exportConditions_require = exportConditions.require) == null ? void 0 : _exportConditions_require.default) != null ? _exportConditions_require_default : exportConditions.require;
|
|
424
|
+
var _exportConditions_import_default;
|
|
425
|
+
const importPath = (_exportConditions_import_default = (_exportConditions_import = exportConditions.import) == null ? void 0 : _exportConditions_import.default) != null ? _exportConditions_import_default : exportConditions.import;
|
|
426
|
+
const requireExt = requirePath && path__default.default.extname(requirePath);
|
|
427
|
+
const importExt = importPath && path__default.default.extname(importPath);
|
|
428
|
+
if (requireExt === '.mjs' || requireExt === '.js') {
|
|
429
|
+
state.badEsmRequireExport.value = true;
|
|
430
|
+
state.badEsmRequireExport.paths.push(requirePath);
|
|
431
|
+
}
|
|
432
|
+
if (importExt === '.cjs') {
|
|
433
|
+
state.badEsmImportExport.value = true;
|
|
434
|
+
state.badEsmImportExport.paths.push(importPath);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
// Validate CJS package
|
|
442
|
+
if (exports) {
|
|
443
|
+
if (typeof exports === 'string') {
|
|
444
|
+
if (!hasCjsExtension(exports)) {
|
|
445
|
+
state.badMainExport = true;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (typeof exports !== 'object') {
|
|
449
|
+
state.invalidExportsFieldType = true;
|
|
450
|
+
} else {
|
|
451
|
+
Object.keys(exportPaths).forEach((key)=>{
|
|
452
|
+
const exportConditions = exportPaths[key];
|
|
453
|
+
if (typeof exportConditions === 'object') {
|
|
454
|
+
var // @ts-ignore TODO: fix the type
|
|
455
|
+
_exportConditions_require, // @ts-ignore TODO: fix the type
|
|
456
|
+
_exportConditions_import;
|
|
457
|
+
var _exportConditions_require_default;
|
|
458
|
+
const requirePath = (_exportConditions_require_default = (_exportConditions_require = exportConditions.require) == null ? void 0 : _exportConditions_require.default) != null ? _exportConditions_require_default : exportConditions.require;
|
|
459
|
+
var _exportConditions_import_default;
|
|
460
|
+
const importPath = (_exportConditions_import_default = (_exportConditions_import = exportConditions.import) == null ? void 0 : _exportConditions_import.default) != null ? _exportConditions_import_default : exportConditions.import;
|
|
461
|
+
const requireExt = requirePath && path__default.default.extname(requirePath);
|
|
462
|
+
const importExt = importPath && path__default.default.extname(importPath);
|
|
463
|
+
if (requireExt === '.mjs') {
|
|
464
|
+
state.badCjsRequireExport.value = true;
|
|
465
|
+
state.badCjsRequireExport.paths.push(requirePath);
|
|
466
|
+
}
|
|
467
|
+
if (importExt === '.js' || importExt === '.cjs') {
|
|
468
|
+
state.badCjsImportExport.value = true;
|
|
469
|
+
state.badCjsImportExport.paths.push(importPath);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (state.badMainExtension) {
|
|
477
|
+
logger.warn('Cannot export `main` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
|
|
478
|
+
}
|
|
479
|
+
if (state.badMainExport) {
|
|
480
|
+
logger.warn('Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
|
|
481
|
+
}
|
|
482
|
+
if (state.invalidExportsFieldType) {
|
|
483
|
+
logger.warn('Invalid exports field type, only object or string is allowed');
|
|
484
|
+
}
|
|
485
|
+
if (state.badCjsRequireExport.value) {
|
|
486
|
+
logger.warn('Cannot export `require` field with .mjs extension in CJS package, only .cjs and .js extensions are allowed');
|
|
487
|
+
state.badCjsRequireExport.paths.forEach((p)=>{
|
|
488
|
+
logger.warn(` ${p}`);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
if (state.badCjsImportExport.value) {
|
|
492
|
+
logger.warn('Cannot export `import` field with .js or .cjs extension in CJS package, only .mjs extensions are allowed');
|
|
493
|
+
state.badCjsImportExport.paths.forEach((p)=>{
|
|
494
|
+
logger.warn(` ${p}`);
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
if (state.badEsmRequireExport.value) {
|
|
498
|
+
logger.warn('Cannot export `require` field with .js or .mjs extension in ESM package, only .cjs extensions are allowed');
|
|
499
|
+
state.badEsmRequireExport.paths.forEach((p)=>{
|
|
500
|
+
logger.warn(` ${p}`);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
if (state.badEsmImportExport.value) {
|
|
504
|
+
logger.warn('Cannot export `import` field with .cjs extension in ESM package, only .js and .mjs extensions are allowed');
|
|
505
|
+
state.badEsmImportExport.paths.forEach((p)=>{
|
|
506
|
+
logger.warn(` ${p}`);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
var version = "4.4.0";
|
|
146
512
|
|
|
147
513
|
function relativify(path) {
|
|
148
514
|
return path.startsWith('.') ? path : `./${path}`;
|
|
149
515
|
}
|
|
150
516
|
|
|
151
|
-
const DIST = 'dist';
|
|
152
|
-
const DEFAULT_TS_CONFIG = {
|
|
153
|
-
compilerOptions: {
|
|
154
|
-
module: 'ESNext',
|
|
155
|
-
moduleResolution: 'bundler'
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
517
|
// Output with posix style in package.json
|
|
159
518
|
function getDistPath(...subPaths) {
|
|
160
519
|
return `./${DIST}/${subPaths.join('/')}`;
|
|
@@ -165,14 +524,16 @@ const normalizeBaseNameToExportName = (baseName)=>{
|
|
|
165
524
|
function createExportCondition(exportName, sourceFile, moduleType) {
|
|
166
525
|
const isTsSourceFile = isTypescriptFile(sourceFile);
|
|
167
526
|
let cjsExtension = 'js';
|
|
527
|
+
let esmExtension = 'mjs';
|
|
168
528
|
if (moduleType === 'module') {
|
|
169
529
|
cjsExtension = 'cjs';
|
|
530
|
+
esmExtension = 'js';
|
|
170
531
|
}
|
|
171
532
|
if (isTsSourceFile) {
|
|
172
533
|
return {
|
|
173
534
|
import: {
|
|
174
|
-
types: getDistPath('es', `${exportName}
|
|
175
|
-
default: getDistPath('es', `${exportName}
|
|
535
|
+
types: getDistPath('es', `${exportName}.${dtsExtensionsMap[esmExtension]}`),
|
|
536
|
+
default: getDistPath('es', `${exportName}.${esmExtension}`)
|
|
176
537
|
},
|
|
177
538
|
require: {
|
|
178
539
|
types: getDistPath('cjs', `${exportName}.${dtsExtensionsMap[cjsExtension]}`),
|
|
@@ -210,7 +571,7 @@ async function collectSourceEntries(sourceFolderPath) {
|
|
|
210
571
|
// Search folder/<index>.<ext> convention entries
|
|
211
572
|
for (const extension of availableExtensions){
|
|
212
573
|
const indexFile = path__default.default.join(dirent.name, `index.${extension}`);
|
|
213
|
-
if (fs__default.default.existsSync(indexFile)) {
|
|
574
|
+
if (fs__default.default.existsSync(indexFile) && !isTestFile(indexFile)) {
|
|
214
575
|
exportsEntries.set(dirent.name, indexFile);
|
|
215
576
|
break;
|
|
216
577
|
}
|
|
@@ -224,7 +585,7 @@ async function collectSourceEntries(sourceFolderPath) {
|
|
|
224
585
|
if (isBinFile) {
|
|
225
586
|
bins.set('.', dirent.name);
|
|
226
587
|
} else {
|
|
227
|
-
if (hasAvailableExtension(dirent.name)) {
|
|
588
|
+
if (hasAvailableExtension(dirent.name) && !isTestFile(dirent.name)) {
|
|
228
589
|
exportsEntries.set(baseName, dirent.name);
|
|
229
590
|
}
|
|
230
591
|
}
|
|
@@ -292,8 +653,8 @@ async function prepare(cwd) {
|
|
|
292
653
|
isUsingTs = true;
|
|
293
654
|
if (!fs__default.default.existsSync(tsconfigPath)) {
|
|
294
655
|
await fsp__default.default.writeFile(tsconfigPath, JSON.stringify(DEFAULT_TS_CONFIG, null, 2), 'utf-8');
|
|
656
|
+
logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
|
|
295
657
|
}
|
|
296
|
-
logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
|
|
297
658
|
}
|
|
298
659
|
// Configure as ESM package by default if there's no package.json
|
|
299
660
|
if (!hasPackageJson) {
|
|
@@ -335,19 +696,22 @@ async function prepare(cwd) {
|
|
|
335
696
|
const isESM = pkgJson.type === 'module';
|
|
336
697
|
const mainExport = pkgExports['.'];
|
|
337
698
|
const mainCondition = isESM ? 'import' : 'require';
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
if (!pkgJson.module) {
|
|
342
|
-
pkgJson.module = isUsingTs ? mainExport.import.default : mainExport.import;
|
|
343
|
-
}
|
|
699
|
+
pkgJson.main = isUsingTs ? mainExport[mainCondition].default : mainExport[mainCondition];
|
|
700
|
+
pkgJson.module = isUsingTs ? mainExport.import.default : mainExport.import;
|
|
344
701
|
if (isUsingTs) {
|
|
345
702
|
pkgJson.types = mainExport[mainCondition].types;
|
|
346
703
|
}
|
|
347
704
|
}
|
|
348
705
|
// Assign the properties by order: files, main, module, types, exports
|
|
349
706
|
if (Object.keys(pkgExports).length > 0) {
|
|
350
|
-
pkgJson.exports
|
|
707
|
+
if (!pkgJson.exports) {
|
|
708
|
+
pkgJson.exports = pkgExports;
|
|
709
|
+
} else {
|
|
710
|
+
// Update existing exports
|
|
711
|
+
Object.keys(pkgExports).forEach((exportName)=>{
|
|
712
|
+
pkgJson.exports[exportName] = pkgExports[exportName];
|
|
713
|
+
});
|
|
714
|
+
}
|
|
351
715
|
}
|
|
352
716
|
}
|
|
353
717
|
await fsp__default.default.writeFile(pkgJsonPath, JSON.stringify(pkgJson, null, 2));
|
|
@@ -367,6 +731,7 @@ Options:
|
|
|
367
731
|
--prepare auto configure package.json exports for building
|
|
368
732
|
--external <mod> specify an external dependency, separate by comma
|
|
369
733
|
--no-external do not bundle external dependencies
|
|
734
|
+
--no-clean do not clean dist folder before building, default: false
|
|
370
735
|
--target <target> js features target: swc target es versions. default: es2015
|
|
371
736
|
--runtime <runtime> build runtime (nodejs, browser). default: browser
|
|
372
737
|
--env <env> inlined process env variables, separate by comma. default: NODE_ENV
|
|
@@ -377,21 +742,12 @@ Options:
|
|
|
377
742
|
function help() {
|
|
378
743
|
logger.log(helpMessage);
|
|
379
744
|
}
|
|
380
|
-
async function
|
|
745
|
+
async function lint(cwd) {
|
|
381
746
|
// Not package.json detected, skip package linting
|
|
382
747
|
if (!await hasPackageJson(cwd)) {
|
|
383
748
|
return;
|
|
384
749
|
}
|
|
385
|
-
|
|
386
|
-
const { formatMessage } = await import('publint/utils');
|
|
387
|
-
const { messages } = await publint({
|
|
388
|
-
pkgDir: cwd,
|
|
389
|
-
level: 'error'
|
|
390
|
-
});
|
|
391
|
-
const pkg = await getPackageMeta(cwd);
|
|
392
|
-
for (const message of messages){
|
|
393
|
-
console.log(formatMessage(message, pkg));
|
|
394
|
-
}
|
|
750
|
+
await lint$1(await getPackageMeta(cwd));
|
|
395
751
|
}
|
|
396
752
|
function parseCliArgs(argv) {
|
|
397
753
|
let args;
|
|
@@ -410,6 +766,7 @@ function parseCliArgs(argv) {
|
|
|
410
766
|
'--env': String,
|
|
411
767
|
'--external': String,
|
|
412
768
|
'--no-external': Boolean,
|
|
769
|
+
'--no-clean': Boolean,
|
|
413
770
|
'--prepare': Boolean,
|
|
414
771
|
'-h': '--help',
|
|
415
772
|
'-v': '--version',
|
|
@@ -436,6 +793,7 @@ function parseCliArgs(argv) {
|
|
|
436
793
|
runtime: args['--runtime'],
|
|
437
794
|
target: args['--target'],
|
|
438
795
|
external: !!args['--no-external'] ? null : args['--external'],
|
|
796
|
+
clean: !args['--no-clean'],
|
|
439
797
|
env: args['--env'],
|
|
440
798
|
prepare: !!args['--prepare']
|
|
441
799
|
};
|
|
@@ -443,7 +801,7 @@ function parseCliArgs(argv) {
|
|
|
443
801
|
}
|
|
444
802
|
async function run(args) {
|
|
445
803
|
var _args_external;
|
|
446
|
-
const { source, format, watch, minify, sourcemap, target, runtime, dts, env } = args;
|
|
804
|
+
const { source, format, watch, minify, sourcemap, target, runtime, dts, env, clean } = args;
|
|
447
805
|
const cwd = args.cwd || process.cwd();
|
|
448
806
|
const file = args.file ? path__default.default.resolve(cwd, args.file) : undefined;
|
|
449
807
|
const bundleConfig = {
|
|
@@ -457,7 +815,8 @@ async function run(args) {
|
|
|
457
815
|
watch: !!watch,
|
|
458
816
|
minify: !!minify,
|
|
459
817
|
sourcemap: sourcemap === false ? false : true,
|
|
460
|
-
env: (env == null ? void 0 : env.split(',')) || []
|
|
818
|
+
env: (env == null ? void 0 : env.split(',')) || [],
|
|
819
|
+
clean
|
|
461
820
|
};
|
|
462
821
|
if (args.version) {
|
|
463
822
|
return logger.log(version);
|
|
@@ -469,6 +828,8 @@ async function run(args) {
|
|
|
469
828
|
return await prepare(cwd);
|
|
470
829
|
}
|
|
471
830
|
const entry = source ? path__default.default.resolve(cwd, source) : '';
|
|
831
|
+
// lint package
|
|
832
|
+
await lint(cwd);
|
|
472
833
|
try {
|
|
473
834
|
await bunchee.bundle(entry, bundleConfig);
|
|
474
835
|
} catch (err) {
|
|
@@ -486,7 +847,6 @@ async function run(args) {
|
|
|
486
847
|
// build mode
|
|
487
848
|
logger.log();
|
|
488
849
|
paint('✓', 'green', `bunchee ${version} build completed`);
|
|
489
|
-
await lintPackage(cwd);
|
|
490
850
|
}
|
|
491
851
|
async function main() {
|
|
492
852
|
let params, error;
|