Skip to content

Commit 11ee069

Browse files
committed
Gracefully handle missing name field in package.json. Fixes #8.
1 parent 950dd05 commit 11ee069

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ export default async function microbundle(options) {
3535
options.pkg = JSON.parse(await readFile(resolve(cwd, 'package.json'), 'utf8'));
3636
}
3737
catch (err) {
38-
console.warn(`Unable to find package.json:\n ${err.message}`);
39-
options.pkg = {
40-
name: basename(options.cwd)
41-
};
38+
console.warn(chalk.yellow(`${chalk.yellow.inverse('WARN')} no package.json found.`));
39+
let msg = String(err.message || err);
40+
if (!msg.match(/ENOENT/)) console.warn(` ${chalk.red.dim(msg)}`);
41+
options.pkg = {};
4242
}
4343

44+
if (!options.pkg.name) {
45+
options.pkg.name = basename(options.cwd);
46+
console.warn(chalk.yellow(`${chalk.yellow.inverse('WARN')} missing package.json "name" field. Assuming "${options.pkg.name}".`));
47+
}
48+
4449
options.input = [].concat(
4550
options.entries && options.entries.length ? options.entries : options.pkg.source || (await isDir(resolve(cwd, 'src')) && 'src/index.js') || (await isFile(resolve(cwd, 'index.js')) && 'index.js') || options.pkg.module
4651
).map( file => resolve(cwd, file) );

test/demo/package.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)