bunchee 4.2.6 → 4.2.8
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 +1 -1
- package/dist/index.js +256 -242
- package/package.json +2 -2
package/dist/bin/cli.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
3
3
|
var fs = require('fs/promises');
|
|
4
4
|
var path = require('path');
|
|
5
5
|
var rollup = require('rollup');
|
|
6
|
+
var module$1 = require('module');
|
|
7
|
+
var require$$0 = require('tty');
|
|
6
8
|
var pluginWasm = require('@rollup/plugin-wasm');
|
|
7
9
|
var rollupPluginSwc3 = require('rollup-plugin-swc3');
|
|
8
10
|
var commonjs = require('@rollup/plugin-commonjs');
|
|
@@ -12,167 +14,20 @@ var replace = require('@rollup/plugin-replace');
|
|
|
12
14
|
var esmShim = require('@rollup/plugin-esm-shim');
|
|
13
15
|
var preserveDirectives = require('rollup-preserve-directives');
|
|
14
16
|
var pluginutils = require('@rollup/pluginutils');
|
|
15
|
-
var require$$0 = require('tty');
|
|
16
17
|
var prettyBytes = require('pretty-bytes');
|
|
17
|
-
var module$1 = require('module');
|
|
18
18
|
|
|
19
19
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
20
20
|
|
|
21
21
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
22
22
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
23
|
+
var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
|
|
23
24
|
var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
|
|
24
25
|
var json__default = /*#__PURE__*/_interopDefault(json);
|
|
25
26
|
var replace__default = /*#__PURE__*/_interopDefault(replace);
|
|
26
27
|
var esmShim__default = /*#__PURE__*/_interopDefault(esmShim);
|
|
27
28
|
var preserveDirectives__default = /*#__PURE__*/_interopDefault(preserveDirectives);
|
|
28
|
-
var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
|
|
29
29
|
var prettyBytes__default = /*#__PURE__*/_interopDefault(prettyBytes);
|
|
30
30
|
|
|
31
|
-
function minifyCSS(content) {
|
|
32
|
-
return content.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$|(?:^|\s)(\s+)|\s*([\{\};,:])\s*|\s+(!)\s+/g, (match, p1, p2, p3, p4)=>{
|
|
33
|
-
if (p1) return p1 === ' ' ? '' : p1;
|
|
34
|
-
if (p2) return ' ';
|
|
35
|
-
if (p3) return p3;
|
|
36
|
-
if (p4) return '!';
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
const helpers = {
|
|
40
|
-
cssImport: {
|
|
41
|
-
// have to assign r.type = 'text/css' to make it work in Safari
|
|
42
|
-
global: `\
|
|
43
|
-
function __insertCSS(code) {
|
|
44
|
-
if (!code || typeof document == 'undefined') return
|
|
45
|
-
let head = document.head || document.getElementsByTagName('head')[0]
|
|
46
|
-
let style = document.createElement('style')
|
|
47
|
-
style.type = 'text/css'
|
|
48
|
-
head.appendChild(style)
|
|
49
|
-
;style.styleSheet ? (style.styleSheet.cssText = code) : style.appendChild(document.createTextNode(code))
|
|
50
|
-
}
|
|
51
|
-
`,
|
|
52
|
-
create (code) {
|
|
53
|
-
return `__insertCSS(${JSON.stringify(code)});`;
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
cssAssertionImport: {
|
|
57
|
-
global: '',
|
|
58
|
-
create (code) {
|
|
59
|
-
return `\
|
|
60
|
-
const sheet = new CSSStyleSheet()
|
|
61
|
-
sheet.replaceSync(${JSON.stringify(code)})
|
|
62
|
-
export default sheet`;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function inlineCss(options) {
|
|
67
|
-
const cssIds = new Set();
|
|
68
|
-
var _options_exclude;
|
|
69
|
-
const filter = pluginutils.createFilter([
|
|
70
|
-
'**/*.css'
|
|
71
|
-
], (_options_exclude = options.exclude) != null ? _options_exclude : []);
|
|
72
|
-
// Follow up for rollup 4 for better support of assertion support https://github.com/rollup/rollup/issues/4818
|
|
73
|
-
return {
|
|
74
|
-
name: 'inline-css',
|
|
75
|
-
transform (code, id) {
|
|
76
|
-
if (!filter(id)) return;
|
|
77
|
-
if (options.skip) return '';
|
|
78
|
-
const cssCode = minifyCSS(code);
|
|
79
|
-
cssIds.add(id);
|
|
80
|
-
return {
|
|
81
|
-
code: helpers.cssImport.create(cssCode),
|
|
82
|
-
map: {
|
|
83
|
-
mappings: ''
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
},
|
|
87
|
-
renderChunk (code, options) {
|
|
88
|
-
const dependenciesIds = this.getModuleIds();
|
|
89
|
-
let foundCss = false;
|
|
90
|
-
for (const depId of dependenciesIds){
|
|
91
|
-
if (depId && cssIds.has(depId)) {
|
|
92
|
-
foundCss = true;
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
if (!foundCss) return;
|
|
97
|
-
return {
|
|
98
|
-
code: `${helpers.cssImport.global}\n${code}`,
|
|
99
|
-
map: {
|
|
100
|
-
mappings: ''
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function rawContent({ exclude }) {
|
|
108
|
-
const filter = pluginutils.createFilter([
|
|
109
|
-
'**/*.data',
|
|
110
|
-
'**/*.txt'
|
|
111
|
-
], exclude);
|
|
112
|
-
return {
|
|
113
|
-
name: 'string',
|
|
114
|
-
transform (code, id) {
|
|
115
|
-
if (filter(id)) {
|
|
116
|
-
return {
|
|
117
|
-
code: `const data = ${JSON.stringify(code)};\nexport default data;`,
|
|
118
|
-
map: null
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Alias entries to import path
|
|
127
|
-
// e.g.
|
|
128
|
-
// For a resolved file, if it's one of the entries,
|
|
129
|
-
// aliases it as export path, such as <absolute file> -> <pkg>/<export path>
|
|
130
|
-
function aliasEntries({ entries }) {
|
|
131
|
-
return {
|
|
132
|
-
name: 'alias',
|
|
133
|
-
resolveId: {
|
|
134
|
-
async handler (source, importer, options) {
|
|
135
|
-
const resolvedId = await this.resolve(source, importer, options);
|
|
136
|
-
if (resolvedId != null) {
|
|
137
|
-
const aliasedId = entries[resolvedId.id];
|
|
138
|
-
if (aliasedId != null) {
|
|
139
|
-
return {
|
|
140
|
-
id: aliasedId,
|
|
141
|
-
external: true
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function prependDirectives() {
|
|
152
|
-
return {
|
|
153
|
-
name: 'prependDirective',
|
|
154
|
-
transform: {
|
|
155
|
-
order: 'post',
|
|
156
|
-
handler (code, id) {
|
|
157
|
-
var _moduleInfo_meta;
|
|
158
|
-
const moduleInfo = this.getModuleInfo(id);
|
|
159
|
-
if (moduleInfo == null ? void 0 : (_moduleInfo_meta = moduleInfo.meta) == null ? void 0 : _moduleInfo_meta.preserveDirectives) {
|
|
160
|
-
const firstDirective = moduleInfo.meta.preserveDirectives.directives[0];
|
|
161
|
-
if (firstDirective) {
|
|
162
|
-
const directive = firstDirective.value;
|
|
163
|
-
const directiveCode = `'${directive}';`;
|
|
164
|
-
return directiveCode + '\n' + code;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
code,
|
|
169
|
-
map: null
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
|
|
176
31
|
const availableExtensions = [
|
|
177
32
|
'js',
|
|
178
33
|
'cjs',
|
|
@@ -209,8 +64,7 @@ const disabledWarnings = new Set([
|
|
|
209
64
|
'PREFER_NAMED_EXPORTS',
|
|
210
65
|
'UNRESOLVED_IMPORT',
|
|
211
66
|
'THIS_IS_UNDEFINED',
|
|
212
|
-
'INVALID_ANNOTATION'
|
|
213
|
-
'SOURCEMAP_BROKEN'
|
|
67
|
+
'INVALID_ANNOTATION'
|
|
214
68
|
]);
|
|
215
69
|
|
|
216
70
|
function getDefaultExportFromCjs (x) {
|
|
@@ -361,6 +215,186 @@ const nonNullable = (n)=>Boolean(n);
|
|
|
361
215
|
const hasAvailableExtension = (filename)=>availableExtensions.includes(path__default.default.extname(filename).slice(1));
|
|
362
216
|
const hasCjsExtension = (filename)=>path__default.default.extname(filename) === '.cjs';
|
|
363
217
|
|
|
218
|
+
let hasLoggedTsWarning = false;
|
|
219
|
+
function resolveTypescript(cwd) {
|
|
220
|
+
let ts;
|
|
221
|
+
const m = new module$1.Module('', undefined);
|
|
222
|
+
m.paths = module$1.Module._nodeModulePaths(cwd);
|
|
223
|
+
try {
|
|
224
|
+
ts = m.require('typescript');
|
|
225
|
+
} catch (_) {
|
|
226
|
+
console.error(_);
|
|
227
|
+
if (!hasLoggedTsWarning) {
|
|
228
|
+
hasLoggedTsWarning = true;
|
|
229
|
+
exit('Could not load TypeScript compiler. Try to install `typescript` as dev dependency');
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return ts;
|
|
233
|
+
}
|
|
234
|
+
async function resolveTsConfig(cwd) {
|
|
235
|
+
let tsCompilerOptions = {};
|
|
236
|
+
let tsConfigPath;
|
|
237
|
+
tsConfigPath = path.resolve(cwd, 'tsconfig.json');
|
|
238
|
+
if (await fileExists(tsConfigPath)) {
|
|
239
|
+
const ts = resolveTypescript(cwd);
|
|
240
|
+
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : cwd;
|
|
241
|
+
const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config;
|
|
242
|
+
tsCompilerOptions = ts.parseJsonConfigFileContent(tsconfigJSON, ts.sys, basePath).options;
|
|
243
|
+
} else {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
tsCompilerOptions,
|
|
248
|
+
tsConfigPath
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
async function convertCompilerOptions(cwd, json) {
|
|
252
|
+
const ts = resolveTypescript(cwd);
|
|
253
|
+
return ts.convertCompilerOptionsFromJson(json, './');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function minifyCSS(content) {
|
|
257
|
+
return content.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$|(?:^|\s)(\s+)|\s*([\{\};,:])\s*|\s+(!)\s+/g, (match, p1, p2, p3, p4)=>{
|
|
258
|
+
if (p1) return p1 === ' ' ? '' : p1;
|
|
259
|
+
if (p2) return ' ';
|
|
260
|
+
if (p3) return p3;
|
|
261
|
+
if (p4) return '!';
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
const helpers = {
|
|
265
|
+
cssImport: {
|
|
266
|
+
// have to assign r.type = 'text/css' to make it work in Safari
|
|
267
|
+
global: `\
|
|
268
|
+
function __insertCSS(code) {
|
|
269
|
+
if (!code || typeof document == 'undefined') return
|
|
270
|
+
let head = document.head || document.getElementsByTagName('head')[0]
|
|
271
|
+
let style = document.createElement('style')
|
|
272
|
+
style.type = 'text/css'
|
|
273
|
+
head.appendChild(style)
|
|
274
|
+
;style.styleSheet ? (style.styleSheet.cssText = code) : style.appendChild(document.createTextNode(code))
|
|
275
|
+
}
|
|
276
|
+
`,
|
|
277
|
+
create (code) {
|
|
278
|
+
return `__insertCSS(${JSON.stringify(code)});`;
|
|
279
|
+
}
|
|
280
|
+
},
|
|
281
|
+
cssAssertionImport: {
|
|
282
|
+
global: '',
|
|
283
|
+
create (code) {
|
|
284
|
+
return `\
|
|
285
|
+
const sheet = new CSSStyleSheet()
|
|
286
|
+
sheet.replaceSync(${JSON.stringify(code)})
|
|
287
|
+
export default sheet`;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
function inlineCss(options) {
|
|
292
|
+
const cssIds = new Set();
|
|
293
|
+
var _options_exclude;
|
|
294
|
+
const filter = pluginutils.createFilter([
|
|
295
|
+
'**/*.css'
|
|
296
|
+
], (_options_exclude = options.exclude) != null ? _options_exclude : []);
|
|
297
|
+
// Follow up for rollup 4 for better support of assertion support https://github.com/rollup/rollup/issues/4818
|
|
298
|
+
return {
|
|
299
|
+
name: 'inline-css',
|
|
300
|
+
transform (code, id) {
|
|
301
|
+
if (!filter(id)) return;
|
|
302
|
+
if (options.skip) return '';
|
|
303
|
+
const cssCode = minifyCSS(code);
|
|
304
|
+
cssIds.add(id);
|
|
305
|
+
return {
|
|
306
|
+
code: helpers.cssImport.create(cssCode),
|
|
307
|
+
map: {
|
|
308
|
+
mappings: ''
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
},
|
|
312
|
+
renderChunk (code, options) {
|
|
313
|
+
const dependenciesIds = this.getModuleIds();
|
|
314
|
+
let foundCss = false;
|
|
315
|
+
for (const depId of dependenciesIds){
|
|
316
|
+
if (depId && cssIds.has(depId)) {
|
|
317
|
+
foundCss = true;
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (!foundCss) return;
|
|
322
|
+
return {
|
|
323
|
+
code: `${helpers.cssImport.global}\n${code}`,
|
|
324
|
+
map: {
|
|
325
|
+
mappings: ''
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function rawContent({ exclude }) {
|
|
333
|
+
const filter = pluginutils.createFilter([
|
|
334
|
+
'**/*.data',
|
|
335
|
+
'**/*.txt'
|
|
336
|
+
], exclude);
|
|
337
|
+
return {
|
|
338
|
+
name: 'string',
|
|
339
|
+
transform (code, id) {
|
|
340
|
+
if (filter(id)) {
|
|
341
|
+
return {
|
|
342
|
+
code: `const data = ${JSON.stringify(code)};\nexport default data;`,
|
|
343
|
+
map: null
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Alias entries to import path
|
|
352
|
+
// e.g.
|
|
353
|
+
// For a resolved file, if it's one of the entries,
|
|
354
|
+
// aliases it as export path, such as <absolute file> -> <pkg>/<export path>
|
|
355
|
+
function aliasEntries({ entries }) {
|
|
356
|
+
return {
|
|
357
|
+
name: 'alias',
|
|
358
|
+
resolveId: {
|
|
359
|
+
async handler (source, importer, options) {
|
|
360
|
+
const resolvedId = await this.resolve(source, importer, options);
|
|
361
|
+
if (resolvedId != null) {
|
|
362
|
+
const aliasedId = entries[resolvedId.id];
|
|
363
|
+
if (aliasedId != null) {
|
|
364
|
+
return {
|
|
365
|
+
id: aliasedId,
|
|
366
|
+
external: true
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function prependDirectives() {
|
|
377
|
+
return {
|
|
378
|
+
name: 'prependDirective',
|
|
379
|
+
transform: {
|
|
380
|
+
order: 'post',
|
|
381
|
+
handler (code, id) {
|
|
382
|
+
var _moduleInfo_meta;
|
|
383
|
+
const moduleInfo = this.getModuleInfo(id);
|
|
384
|
+
if (moduleInfo == null ? void 0 : (_moduleInfo_meta = moduleInfo.meta) == null ? void 0 : _moduleInfo_meta.preserveDirectives) {
|
|
385
|
+
const firstDirective = moduleInfo.meta.preserveDirectives.directives[0];
|
|
386
|
+
if (firstDirective) {
|
|
387
|
+
const directive = firstDirective.value;
|
|
388
|
+
const directiveCode = `'${directive}';`;
|
|
389
|
+
return directiveCode + '\n' + code;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return null;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
364
398
|
function getTypings(pkg) {
|
|
365
399
|
return pkg.types || pkg.typings;
|
|
366
400
|
}
|
|
@@ -374,11 +408,11 @@ function isExportLike(field) {
|
|
|
374
408
|
([key, value])=>typeof value === 'string' && !key.startsWith('.'));
|
|
375
409
|
}
|
|
376
410
|
function constructFullExportCondition(exportCondition, packageType) {
|
|
377
|
-
const isEsmPkg = isESModulePackage(packageType);
|
|
378
411
|
let fullExportCond;
|
|
379
412
|
if (typeof exportCondition === 'string') {
|
|
413
|
+
const exportType = getExportTypeFromFile(exportCondition, packageType);
|
|
380
414
|
fullExportCond = {
|
|
381
|
-
[
|
|
415
|
+
[exportType]: exportCondition
|
|
382
416
|
};
|
|
383
417
|
} else {
|
|
384
418
|
const exportTypes = Object.keys(exportCondition);
|
|
@@ -401,21 +435,32 @@ function joinRelativePath(...segments) {
|
|
|
401
435
|
}
|
|
402
436
|
return result;
|
|
403
437
|
}
|
|
404
|
-
function findExport(exportPath, exportCondition, paths, packageType) {
|
|
438
|
+
function findExport(exportPath, exportCondition, paths, packageType, currentPath) {
|
|
439
|
+
// Skip `types` field, it cannot be the entry point
|
|
440
|
+
if (exportPath === 'types') return;
|
|
405
441
|
if (isExportLike(exportCondition)) {
|
|
406
442
|
const fullExportCondition = constructFullExportCondition(exportCondition, packageType);
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
443
|
+
if (exportPath.startsWith('.')) {
|
|
444
|
+
paths[exportPath] = {
|
|
445
|
+
...paths[exportPath],
|
|
446
|
+
...fullExportCondition
|
|
447
|
+
};
|
|
448
|
+
} else {
|
|
449
|
+
// exportPath is exportType, import, require, ...
|
|
450
|
+
// merge to currentPath
|
|
451
|
+
paths[currentPath] = {
|
|
452
|
+
...paths[currentPath],
|
|
453
|
+
[exportPath]: fullExportCondition.default
|
|
454
|
+
};
|
|
455
|
+
}
|
|
411
456
|
return;
|
|
412
457
|
}
|
|
413
458
|
Object.keys(exportCondition).forEach((subpath)=>{
|
|
414
459
|
if (subpath.startsWith('.')) {
|
|
415
460
|
// subpath is actual export path, ./a, ./b, ...
|
|
416
|
-
const nestedExportPath = joinRelativePath(
|
|
461
|
+
const nestedExportPath = joinRelativePath(currentPath, subpath);
|
|
417
462
|
const nestedExportCondition = exportCondition[subpath];
|
|
418
|
-
findExport(nestedExportPath, nestedExportCondition, paths, packageType);
|
|
463
|
+
findExport(nestedExportPath, nestedExportCondition, paths, packageType, nestedExportPath);
|
|
419
464
|
} else {
|
|
420
465
|
// subpath is exportType, import, require, ...
|
|
421
466
|
const exportType = subpath;
|
|
@@ -423,7 +468,7 @@ function findExport(exportPath, exportCondition, paths, packageType) {
|
|
|
423
468
|
const nestedExportCondition = {
|
|
424
469
|
[exportType]: defaultPath
|
|
425
470
|
};
|
|
426
|
-
findExport(exportPath, nestedExportCondition, paths, packageType);
|
|
471
|
+
findExport(exportPath, nestedExportCondition, paths, packageType, currentPath);
|
|
427
472
|
}
|
|
428
473
|
});
|
|
429
474
|
}
|
|
@@ -453,15 +498,16 @@ function findExport(exportPath, exportCondition, paths, packageType) {
|
|
|
453
498
|
*
|
|
454
499
|
*/ function parseExport(exportsCondition, packageType) {
|
|
455
500
|
const paths = {};
|
|
501
|
+
const initialPath = '.';
|
|
456
502
|
if (typeof exportsCondition === 'string') {
|
|
457
|
-
paths[
|
|
503
|
+
paths[initialPath] = constructFullExportCondition(exportsCondition, packageType);
|
|
458
504
|
} else if (typeof exportsCondition === 'object') {
|
|
459
505
|
if (isExportLike(exportsCondition)) {
|
|
460
|
-
paths[
|
|
506
|
+
paths[initialPath] = constructFullExportCondition(exportsCondition, packageType);
|
|
461
507
|
} else {
|
|
462
508
|
Object.keys(exportsCondition).forEach((key)=>{
|
|
463
509
|
const exportCondition = exportsCondition[key];
|
|
464
|
-
findExport(key, exportCondition, paths, packageType);
|
|
510
|
+
findExport(key, exportCondition, paths, packageType, initialPath);
|
|
465
511
|
});
|
|
466
512
|
}
|
|
467
513
|
}
|
|
@@ -508,13 +554,16 @@ function findExport(exportPath, exportCondition, paths, packageType) {
|
|
|
508
554
|
* pkg.main and pkg.module will be added to ['.'] if exists
|
|
509
555
|
*/ function getExportPaths(pkg, pkgType, resolvedWildcardExports) {
|
|
510
556
|
var _pathsMap_;
|
|
511
|
-
|
|
557
|
+
let pathsMap = {};
|
|
512
558
|
const packageType = pkgType != null ? pkgType : getPackageType(pkg);
|
|
513
559
|
const isEsmPackage = isESModulePackage(packageType);
|
|
514
560
|
const exportsConditions = resolvedWildcardExports != null ? resolvedWildcardExports : pkg.exports;
|
|
515
561
|
if (exportsConditions) {
|
|
516
562
|
const paths = parseExport(exportsConditions, packageType);
|
|
517
|
-
|
|
563
|
+
pathsMap = {
|
|
564
|
+
...pathsMap,
|
|
565
|
+
...paths
|
|
566
|
+
};
|
|
518
567
|
}
|
|
519
568
|
if (isEsmPackage && pkg.main && hasCjsExtension(pkg.main)) {
|
|
520
569
|
exit('Cannot export main field with .cjs extension in ESM package, only .mjs and .js extensions are allowed');
|
|
@@ -530,10 +579,16 @@ function findExport(exportPath, exportCondition, paths, packageType) {
|
|
|
530
579
|
defaultMainExport['require'] = pathsMap['.']['require'];
|
|
531
580
|
}
|
|
532
581
|
// Merge the main export into '.' paths
|
|
533
|
-
const mainExport =
|
|
582
|
+
const mainExport = {
|
|
583
|
+
...pathsMap['.'],
|
|
584
|
+
...defaultMainExport
|
|
585
|
+
};
|
|
534
586
|
// main export is not empty
|
|
535
587
|
if (Object.keys(mainExport).length > 0) {
|
|
536
|
-
pathsMap['.'] =
|
|
588
|
+
pathsMap['.'] = {
|
|
589
|
+
...pathsMap['.'],
|
|
590
|
+
...mainExport
|
|
591
|
+
};
|
|
537
592
|
}
|
|
538
593
|
return pathsMap;
|
|
539
594
|
}
|
|
@@ -640,6 +695,13 @@ function getTypeFilePath(entryFilePath, exportCondition, cwd) {
|
|
|
640
695
|
const exportName = (exportCondition == null ? void 0 : exportCondition.name) || 'index';
|
|
641
696
|
return entryFilePath ? name + '.d.ts' : path.resolve(firstDistPath ? path.dirname(firstDistPath) : path.join(cwd, 'dist'), (exportName === '.' ? 'index' : exportName) + '.d.ts');
|
|
642
697
|
}
|
|
698
|
+
function getExportTypeFromFile(filename, pkgType) {
|
|
699
|
+
const isESModule = isESModulePackage(pkgType);
|
|
700
|
+
const isCjsExt = filename.endsWith('.cjs');
|
|
701
|
+
const isEsmExt = filename.endsWith('.mjs');
|
|
702
|
+
const exportType = isEsmExt ? 'import' : isCjsExt ? 'require' : isESModule ? 'import' : 'require';
|
|
703
|
+
return exportType;
|
|
704
|
+
}
|
|
643
705
|
|
|
644
706
|
const swcMinifyOptions = {
|
|
645
707
|
compress: true,
|
|
@@ -727,23 +789,6 @@ async function buildInputConfig(entry, entries, pkg, options, cwd, { tsConfigPat
|
|
|
727
789
|
entries: reversedAlias
|
|
728
790
|
})
|
|
729
791
|
];
|
|
730
|
-
const baseResolvedTsOptions = {
|
|
731
|
-
declaration: true,
|
|
732
|
-
noEmit: false,
|
|
733
|
-
noEmitOnError: true,
|
|
734
|
-
emitDeclarationOnly: true,
|
|
735
|
-
checkJs: false,
|
|
736
|
-
declarationMap: false,
|
|
737
|
-
skipLibCheck: true,
|
|
738
|
-
preserveSymlinks: false,
|
|
739
|
-
// disable incremental build
|
|
740
|
-
incremental: false,
|
|
741
|
-
// use default tsBuildInfoFile value
|
|
742
|
-
tsBuildInfoFile: '.tsbuildinfo',
|
|
743
|
-
target: 'esnext',
|
|
744
|
-
module: 'esnext',
|
|
745
|
-
jsx: tsCompilerOptions.jsx || 'react-jsx'
|
|
746
|
-
};
|
|
747
792
|
const typesPlugins = [
|
|
748
793
|
...commonPlugins,
|
|
749
794
|
inlineCss({
|
|
@@ -751,19 +796,25 @@ async function buildInputConfig(entry, entries, pkg, options, cwd, { tsConfigPat
|
|
|
751
796
|
})
|
|
752
797
|
];
|
|
753
798
|
if (useTypescript) {
|
|
754
|
-
const
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
799
|
+
const { options: overrideResolvedTsOptions } = await convertCompilerOptions(cwd, {
|
|
800
|
+
declaration: true,
|
|
801
|
+
noEmit: false,
|
|
802
|
+
noEmitOnError: true,
|
|
803
|
+
emitDeclarationOnly: true,
|
|
804
|
+
checkJs: false,
|
|
805
|
+
declarationMap: false,
|
|
806
|
+
skipLibCheck: true,
|
|
807
|
+
target: 'ESNext',
|
|
808
|
+
...!tsCompilerOptions.jsx ? {
|
|
809
|
+
jsx: 'react-jsx'
|
|
810
|
+
} : undefined,
|
|
811
|
+
// error TS5074: Option '--incremental' can only be specified using tsconfig, emitting to single
|
|
812
|
+
// file or when option '--tsBuildInfoFile' is specified.
|
|
813
|
+
incremental: false
|
|
814
|
+
});
|
|
764
815
|
const dtsPlugin = require('rollup-plugin-dts').default({
|
|
765
|
-
tsconfig:
|
|
766
|
-
compilerOptions:
|
|
816
|
+
tsconfig: tsConfigPath,
|
|
817
|
+
compilerOptions: overrideResolvedTsOptions
|
|
767
818
|
});
|
|
768
819
|
typesPlugins.push(dtsPlugin);
|
|
769
820
|
}
|
|
@@ -775,6 +826,7 @@ async function buildInputConfig(entry, entries, pkg, options, cwd, { tsConfigPat
|
|
|
775
826
|
rawContent({
|
|
776
827
|
exclude: /node_modules/
|
|
777
828
|
}),
|
|
829
|
+
esmShim__default.default(),
|
|
778
830
|
preserveDirectives__default.default(),
|
|
779
831
|
prependDirectives(),
|
|
780
832
|
replace__default.default({
|
|
@@ -795,8 +847,7 @@ async function buildInputConfig(entry, entries, pkg, options, cwd, { tsConfigPat
|
|
|
795
847
|
exclude: 'node_modules',
|
|
796
848
|
tsconfig: tsConfigPath,
|
|
797
849
|
...swcOptions
|
|
798
|
-
})
|
|
799
|
-
esmShim__default.default()
|
|
850
|
+
})
|
|
800
851
|
]).filter(isNotNull);
|
|
801
852
|
return {
|
|
802
853
|
input: entry,
|
|
@@ -999,12 +1050,8 @@ async function buildEntryConfig(entries, pkg, exportPaths, bundleConfig, cwd, ts
|
|
|
999
1050
|
path.join('bin', key),
|
|
1000
1051
|
binaryExports[key]
|
|
1001
1052
|
]);
|
|
1002
|
-
const isESModule = isESModulePackage(pkg.type);
|
|
1003
1053
|
const binExportPaths = binPairs.reduce((acc, [binName, binDistPath])=>{
|
|
1004
|
-
const
|
|
1005
|
-
const isCjsExt = ext === 'cjs';
|
|
1006
|
-
const isEsmExt = ext === 'mjs';
|
|
1007
|
-
const exportType = isEsmExt ? 'import' : isCjsExt ? 'require' : isESModule ? 'import' : 'require';
|
|
1054
|
+
const exportType = getExportTypeFromFile(binDistPath, pkg.type);
|
|
1008
1055
|
acc[binName] = {
|
|
1009
1056
|
[exportType]: binDistPath
|
|
1010
1057
|
};
|
|
@@ -1026,10 +1073,12 @@ async function buildEntryConfig(entries, pkg, exportPaths, bundleConfig, cwd, ts
|
|
|
1026
1073
|
}
|
|
1027
1074
|
const collectEntriesPromises = Object.keys(exportPaths).map(async (entryExport)=>{
|
|
1028
1075
|
const exportCond = exportPaths[entryExport];
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1076
|
+
if (entryExport.startsWith('.')) {
|
|
1077
|
+
await collectEntry('', exportCond, entryExport);
|
|
1078
|
+
for (const exportType of availableExportConventions){
|
|
1079
|
+
if (exportCond[exportType]) {
|
|
1080
|
+
await collectEntry(exportType, exportCond, entryExport);
|
|
1081
|
+
}
|
|
1033
1082
|
}
|
|
1034
1083
|
}
|
|
1035
1084
|
});
|
|
@@ -1153,41 +1202,6 @@ function logSizeStats(sizeCollector) {
|
|
|
1153
1202
|
});
|
|
1154
1203
|
}
|
|
1155
1204
|
|
|
1156
|
-
let hasLoggedTsWarning = false;
|
|
1157
|
-
function resolveTypescript(cwd) {
|
|
1158
|
-
let ts;
|
|
1159
|
-
const m = new module$1.Module('', undefined);
|
|
1160
|
-
m.paths = module$1.Module._nodeModulePaths(cwd);
|
|
1161
|
-
try {
|
|
1162
|
-
ts = m.require('typescript');
|
|
1163
|
-
} catch (_) {
|
|
1164
|
-
console.error(_);
|
|
1165
|
-
if (!hasLoggedTsWarning) {
|
|
1166
|
-
hasLoggedTsWarning = true;
|
|
1167
|
-
exit('Could not load TypeScript compiler. Try to install `typescript` as dev dependency');
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
return ts;
|
|
1171
|
-
}
|
|
1172
|
-
async function resolveTsConfig(cwd) {
|
|
1173
|
-
let tsCompilerOptions = {};
|
|
1174
|
-
let tsConfigPath;
|
|
1175
|
-
tsConfigPath = path.resolve(cwd, 'tsconfig.json');
|
|
1176
|
-
if (await fileExists(tsConfigPath)) {
|
|
1177
|
-
const ts = resolveTypescript(cwd);
|
|
1178
|
-
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : cwd;
|
|
1179
|
-
const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config;
|
|
1180
|
-
tsCompilerOptions = ts.parseJsonConfigFileContent(tsconfigJSON, ts.sys, basePath).options;
|
|
1181
|
-
} else {
|
|
1182
|
-
tsConfigPath = undefined;
|
|
1183
|
-
return null;
|
|
1184
|
-
}
|
|
1185
|
-
return {
|
|
1186
|
-
tsCompilerOptions,
|
|
1187
|
-
tsConfigPath
|
|
1188
|
-
};
|
|
1189
|
-
}
|
|
1190
|
-
|
|
1191
1205
|
// TODO: support nested wildcard exportsCondition (e.g. './foo/*')
|
|
1192
1206
|
const getWildcardExports = (exportsCondition)=>{
|
|
1193
1207
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bunchee",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.8",
|
|
4
4
|
"description": "zero config bundler for js/ts/jsx libraries",
|
|
5
5
|
"bin": "./dist/bin/cli.js",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"rollup": "^4.9.1",
|
|
63
63
|
"rollup-plugin-dts": "^6.1.0",
|
|
64
64
|
"rollup-plugin-swc3": "^0.11.0",
|
|
65
|
-
"rollup-preserve-directives": "^1.1.
|
|
65
|
+
"rollup-preserve-directives": "^1.1.1",
|
|
66
66
|
"tslib": "^2.6.2"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|