bunchee 4.3.0 → 4.3.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/README.md +7 -3
- package/dist/bin/cli.js +11 -17
- package/dist/index.js +43 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -135,6 +135,9 @@ The output format will based on the exports condition and also the file extensio
|
|
|
135
135
|
- It's CommonJS for `require` and ESM for `import` based on the exports condition.
|
|
136
136
|
- It's CommonJS for `.js` and ESM for `.mjs` based on the extension regardless the exports condition. Then for export condition like "node" you could choose the format with your extension.
|
|
137
137
|
|
|
138
|
+
> [!NOTE]
|
|
139
|
+
> All the `dependencies` and `peerDependencies` will be marked as external automatically and not included in the bundle. If you want to include them in the bundle, you can use the `--no-external` option.
|
|
140
|
+
|
|
138
141
|
## Usage
|
|
139
142
|
|
|
140
143
|
### File Conventions
|
|
@@ -277,13 +280,14 @@ bunchee ./src/index.js --runtime node --target es2019
|
|
|
277
280
|
|
|
278
281
|
#### Specifying extra external dependencies
|
|
279
282
|
|
|
280
|
-
|
|
283
|
+
By default, `bunchee` will mark all the `dependencies` and `peerDependencies` as externals so you don't need to pass them as CLI args.
|
|
284
|
+
But if there's any dependency that used but not in the dependency list and you want to mark as external, you can use the `--external` option to specify them.
|
|
281
285
|
|
|
282
286
|
```sh
|
|
283
|
-
bunchee --external=
|
|
287
|
+
bunchee --external=dep1,dep2,dep3
|
|
284
288
|
```
|
|
285
289
|
|
|
286
|
-
Replace `
|
|
290
|
+
Replace `dep1`, `dep2`, and `dep3` with the names of the dependencies you want to exclude from the bundle.
|
|
287
291
|
|
|
288
292
|
#### Bundling everything without external dependencies
|
|
289
293
|
|
package/dist/bin/cli.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
var path = require('path');
|
|
3
3
|
var arg = require('arg');
|
|
4
|
+
var fs = require('fs');
|
|
4
5
|
var fsp = require('fs/promises');
|
|
5
6
|
var require$$0 = require('tty');
|
|
6
7
|
var bunchee = require('bunchee');
|
|
7
|
-
var fs = require('fs');
|
|
8
8
|
|
|
9
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
|
|
11
11
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
12
12
|
var arg__default = /*#__PURE__*/_interopDefault(arg);
|
|
13
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
13
14
|
var fsp__default = /*#__PURE__*/_interopDefault(fsp);
|
|
14
15
|
var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
|
|
15
|
-
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
16
16
|
|
|
17
17
|
const availableExtensions = [
|
|
18
18
|
'js',
|
|
@@ -119,8 +119,8 @@ function exit(err) {
|
|
|
119
119
|
logger.error(err);
|
|
120
120
|
process.exit(1);
|
|
121
121
|
}
|
|
122
|
-
|
|
123
|
-
return
|
|
122
|
+
function hasPackageJson(cwd) {
|
|
123
|
+
return fileExists(path__default.default.resolve(cwd, 'package.json'));
|
|
124
124
|
}
|
|
125
125
|
async function getPackageMeta(cwd) {
|
|
126
126
|
const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
|
|
@@ -136,21 +136,13 @@ function isTypescriptFile(filename) {
|
|
|
136
136
|
const ext = path__default.default.extname(filename).slice(1);
|
|
137
137
|
return tsExtensions.includes(ext);
|
|
138
138
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
await fsp__default.default.access(filePath);
|
|
142
|
-
return true;
|
|
143
|
-
} catch (err) {
|
|
144
|
-
if (err.code === 'ENOENT') {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
throw err;
|
|
148
|
-
}
|
|
139
|
+
function fileExists(filePath) {
|
|
140
|
+
return fs__default.default.existsSync(filePath);
|
|
149
141
|
}
|
|
150
142
|
const hasAvailableExtension = (filename)=>availableExtensions.includes(path__default.default.extname(filename).slice(1));
|
|
151
143
|
const baseNameWithoutExtension = (filename)=>path__default.default.basename(filename, path__default.default.extname(filename));
|
|
152
144
|
|
|
153
|
-
var version = "4.3.
|
|
145
|
+
var version = "4.3.2";
|
|
154
146
|
|
|
155
147
|
function relativify(path) {
|
|
156
148
|
return path.startsWith('.') ? path : `./${path}`;
|
|
@@ -267,9 +259,11 @@ async function prepare(cwd) {
|
|
|
267
259
|
logger.error(`Source folder ${sourceFolder} does not exist. Cannot proceed to configure \`exports\` field.`);
|
|
268
260
|
process.exit(1);
|
|
269
261
|
}
|
|
262
|
+
let hasPackageJson = false;
|
|
270
263
|
const pkgJsonPath = path__default.default.join(cwd, 'package.json');
|
|
271
264
|
let pkgJson = {};
|
|
272
265
|
if (fs__default.default.existsSync(pkgJsonPath)) {
|
|
266
|
+
hasPackageJson = true;
|
|
273
267
|
const pkgJsonString = await fsp__default.default.readFile(pkgJsonPath, 'utf-8');
|
|
274
268
|
pkgJson = JSON.parse(pkgJsonString);
|
|
275
269
|
}
|
|
@@ -296,8 +290,8 @@ async function prepare(cwd) {
|
|
|
296
290
|
logger.log(`Detected using TypeScript but tsconfig.json is missing, created a ${pc.blue('tsconfig.json')} for you.`);
|
|
297
291
|
}
|
|
298
292
|
}
|
|
299
|
-
// Configure as ESM package by default if there's no
|
|
300
|
-
if (!
|
|
293
|
+
// Configure as ESM package by default if there's no package.json
|
|
294
|
+
if (!hasPackageJson) {
|
|
301
295
|
pkgJson.type = 'module';
|
|
302
296
|
}
|
|
303
297
|
if (bins.size > 0) {
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
2
|
|
|
3
|
-
var fs = require('fs/promises');
|
|
4
|
-
var path = require('path');
|
|
5
3
|
var rollup = require('rollup');
|
|
4
|
+
var fsp = require('fs/promises');
|
|
5
|
+
var fs = require('fs');
|
|
6
|
+
var path = require('path');
|
|
6
7
|
var module$1 = require('module');
|
|
7
8
|
var require$$0 = require('tty');
|
|
8
9
|
var pluginWasm = require('@rollup/plugin-wasm');
|
|
@@ -18,6 +19,7 @@ var prettyBytes = require('pretty-bytes');
|
|
|
18
19
|
|
|
19
20
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
20
21
|
|
|
22
|
+
var fsp__default = /*#__PURE__*/_interopDefault(fsp);
|
|
21
23
|
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
22
24
|
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
23
25
|
var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
|
|
@@ -146,22 +148,14 @@ async function getPackageMeta(cwd) {
|
|
|
146
148
|
const pkgFilePath = path__default.default.resolve(cwd, 'package.json');
|
|
147
149
|
let targetPackageJson = {};
|
|
148
150
|
try {
|
|
149
|
-
targetPackageJson = JSON.parse(await
|
|
151
|
+
targetPackageJson = JSON.parse(await fsp__default.default.readFile(pkgFilePath, {
|
|
150
152
|
encoding: 'utf-8'
|
|
151
153
|
}));
|
|
152
154
|
} catch (_) {}
|
|
153
155
|
return targetPackageJson;
|
|
154
156
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
await fs__default.default.access(filePath);
|
|
158
|
-
return true;
|
|
159
|
-
} catch (err) {
|
|
160
|
-
if (err.code === 'ENOENT') {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
throw err;
|
|
164
|
-
}
|
|
157
|
+
function fileExists(filePath) {
|
|
158
|
+
return fs__default.default.existsSync(filePath);
|
|
165
159
|
}
|
|
166
160
|
// . -> pkg name
|
|
167
161
|
// ./lite -> <pkg name>/lite
|
|
@@ -174,15 +168,17 @@ const isNotNull = (n)=>Boolean(n);
|
|
|
174
168
|
function resolveSourceFile(cwd, filename) {
|
|
175
169
|
return path__default.default.resolve(cwd, SRC, filename);
|
|
176
170
|
}
|
|
177
|
-
|
|
171
|
+
function findSourceEntryFile(cwd, exportPath, exportTypeSuffix, ext) {
|
|
178
172
|
const filename = resolveSourceFile(cwd, `${exportPath}${exportTypeSuffix ? `.${exportTypeSuffix}` : ''}.${ext}`);
|
|
179
|
-
if (
|
|
173
|
+
if (fileExists(filename)) {
|
|
180
174
|
return filename;
|
|
181
175
|
}
|
|
182
176
|
const subFolderIndexFilename = resolveSourceFile(cwd, `${exportPath}/index${exportTypeSuffix ? `.${exportTypeSuffix}` : ''}.${ext}`);
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
177
|
+
try {
|
|
178
|
+
if (fileExists(subFolderIndexFilename)) {
|
|
179
|
+
return subFolderIndexFilename;
|
|
180
|
+
}
|
|
181
|
+
} catch {}
|
|
186
182
|
return undefined;
|
|
187
183
|
}
|
|
188
184
|
// Map '.' -> './index.[ext]'
|
|
@@ -191,7 +187,7 @@ async function findSourceEntryFile(cwd, exportPath, exportTypeSuffix, ext) {
|
|
|
191
187
|
async function getSourcePathFromExportPath(cwd, exportPath, exportType) {
|
|
192
188
|
for (const ext of availableExtensions){
|
|
193
189
|
// ignore package.json
|
|
194
|
-
if (exportPath
|
|
190
|
+
if (exportPath === '/package.json') return;
|
|
195
191
|
if (exportPath === '.') exportPath = './index';
|
|
196
192
|
// Find convention-based source file for specific export types
|
|
197
193
|
// $binary represents `pkg.bin`
|
|
@@ -228,11 +224,11 @@ function resolveTypescript(cwd) {
|
|
|
228
224
|
}
|
|
229
225
|
return ts;
|
|
230
226
|
}
|
|
231
|
-
|
|
227
|
+
function resolveTsConfig(cwd) {
|
|
232
228
|
let tsCompilerOptions = {};
|
|
233
229
|
let tsConfigPath;
|
|
234
230
|
tsConfigPath = path.resolve(cwd, 'tsconfig.json');
|
|
235
|
-
if (
|
|
231
|
+
if (fileExists(tsConfigPath)) {
|
|
236
232
|
const ts = resolveTypescript(cwd);
|
|
237
233
|
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : cwd;
|
|
238
234
|
const tsconfigJSON = ts.readConfigFile(tsConfigPath, ts.sys.readFile).config;
|
|
@@ -1149,6 +1145,8 @@ function relativify(path) {
|
|
|
1149
1145
|
return path.startsWith('.') ? path : `./${path}`;
|
|
1150
1146
|
}
|
|
1151
1147
|
|
|
1148
|
+
// Example: @foo/bar -> bar
|
|
1149
|
+
const removeScope = (exportPath)=>exportPath.replace(/^@[^/]+\//, '');
|
|
1152
1150
|
function createOutputState({ entries }) {
|
|
1153
1151
|
const sizeStats = new Map();
|
|
1154
1152
|
function addSize({ fileName, size, sourceFileName, exportPath }) {
|
|
@@ -1181,7 +1179,7 @@ function createOutputState({ entries }) {
|
|
|
1181
1179
|
}
|
|
1182
1180
|
const size = chunk.code.length;
|
|
1183
1181
|
const sourceFileName = chunk.facadeModuleId || '';
|
|
1184
|
-
const exportPath = reversedMapping.get(sourceFileName) || '.';
|
|
1182
|
+
const exportPath = removeScope(reversedMapping.get(sourceFileName) || '.');
|
|
1185
1183
|
addSize({
|
|
1186
1184
|
fileName: path__default.default.isAbsolute(cwd) ? path__default.default.relative(cwd, filePath) : filePath,
|
|
1187
1185
|
size,
|
|
@@ -1273,7 +1271,7 @@ const getWildcardExports = (exportsCondition)=>{
|
|
|
1273
1271
|
};
|
|
1274
1272
|
const isExportable = async (dirent, pathname)=>{
|
|
1275
1273
|
if (dirent.isDirectory()) {
|
|
1276
|
-
const innerDirents = await
|
|
1274
|
+
const innerDirents = await fsp__default.default.readdir(path__default.default.join(pathname, dirent.name), {
|
|
1277
1275
|
withFileTypes: true
|
|
1278
1276
|
});
|
|
1279
1277
|
return innerDirents.some(({ name })=>name.startsWith('index') && hasAvailableExtension(name));
|
|
@@ -1282,7 +1280,7 @@ const isExportable = async (dirent, pathname)=>{
|
|
|
1282
1280
|
};
|
|
1283
1281
|
async function getExportables(cwd, excludeKeys) {
|
|
1284
1282
|
const pathname = path__default.default.resolve(cwd, SRC);
|
|
1285
|
-
const dirents = await
|
|
1283
|
+
const dirents = await fsp__default.default.readdir(pathname, {
|
|
1286
1284
|
withFileTypes: true
|
|
1287
1285
|
});
|
|
1288
1286
|
const exportables = await Promise.all(dirents.map(async (dirent)=>await isExportable(dirent, pathname) && !excludeKeys.includes(dirent.name) ? dirent.name : undefined));
|
|
@@ -1384,12 +1382,21 @@ async function bundle(entryPath, { cwd: _cwd, ...options } = {}) {
|
|
|
1384
1382
|
}
|
|
1385
1383
|
return runBundle(rollupConfig);
|
|
1386
1384
|
};
|
|
1387
|
-
const hasSpecifiedEntryFile = entryPath ?
|
|
1385
|
+
const hasSpecifiedEntryFile = entryPath ? fs__default.default.existsSync(entryPath) && (await fsp__default.default.stat(entryPath)).isFile() : false;
|
|
1388
1386
|
const hasNoEntry = !hasSpecifiedEntryFile && !isMultiEntries && !hasBin;
|
|
1389
1387
|
if (hasNoEntry) {
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1388
|
+
if (entryPath) {
|
|
1389
|
+
const err = new Error(`Entry file "${entryPath}" does not exist`);
|
|
1390
|
+
err.name = 'NOT_EXISTED';
|
|
1391
|
+
return Promise.reject(err);
|
|
1392
|
+
} else if (cwd) {
|
|
1393
|
+
const hasProjectDir = fs__default.default.existsSync(cwd) && (await fsp__default.default.stat(cwd)).isDirectory();
|
|
1394
|
+
if (!hasProjectDir) {
|
|
1395
|
+
const err = new Error(`Project directory "${cwd}" does not exist`);
|
|
1396
|
+
err.name = 'NOT_EXISTED';
|
|
1397
|
+
return Promise.reject(err);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1393
1400
|
}
|
|
1394
1401
|
const entries = await collectEntries(pkg, entryPath, exportPaths, cwd);
|
|
1395
1402
|
const sizeCollector = createOutputState({
|
|
@@ -1435,7 +1442,8 @@ function runWatch({ input, output }, metadata) {
|
|
|
1435
1442
|
switch(event.code){
|
|
1436
1443
|
case 'ERROR':
|
|
1437
1444
|
{
|
|
1438
|
-
|
|
1445
|
+
logError(event.error);
|
|
1446
|
+
break;
|
|
1439
1447
|
}
|
|
1440
1448
|
case 'START':
|
|
1441
1449
|
{
|
|
@@ -1455,14 +1463,18 @@ function runBundle({ input, output }) {
|
|
|
1455
1463
|
return rollup.rollup(input).then((bundle)=>{
|
|
1456
1464
|
const writeJobs = output.map((options)=>bundle.write(options));
|
|
1457
1465
|
return Promise.all(writeJobs);
|
|
1458
|
-
},
|
|
1466
|
+
}, catchErrorHandler);
|
|
1459
1467
|
}
|
|
1460
|
-
function
|
|
1468
|
+
function logError(error) {
|
|
1461
1469
|
if (!error) return;
|
|
1462
1470
|
// logging source code in format
|
|
1463
1471
|
if (error.frame) {
|
|
1464
1472
|
process.stderr.write(error.frame + '\n');
|
|
1465
1473
|
}
|
|
1474
|
+
}
|
|
1475
|
+
function catchErrorHandler(error) {
|
|
1476
|
+
if (!error) return;
|
|
1477
|
+
logError(error);
|
|
1466
1478
|
// filter out the rollup plugin error information such as loc/frame/code...
|
|
1467
1479
|
const err = new Error(error.message);
|
|
1468
1480
|
err.stack = error.stack;
|