sindresorhus/meow

Bundle dependencies

sindresorhus opened this issue ยท 17 comments

I usually don't care to do this, but it would be nice if meow was simply one dependency as it would help the install time on lots of small CLIs.

Not exactly sure the best way to do this. It should be something that doesn't add any build-script for development, meaning, everything should be handled in a prepublish script.

Maybe npm-packager or just build it with Webpack.

Suggestions and help welcome.

https://github.com/gajus/bundle-dependencies ?

Although I'm not sure if it has any benefits

Yeah, I guess that tool would work too, although I don't like the manual steps in the "Publishing" section. Should really be handled by the tool.

kevva commented

Keep in mind that this will require a version bump every time we want to update the dependencies. Might not matter too much though since this already works pretty much perfectly in the current state.

I think npm-packer looks pretty good.

@kevva Yes, but the dependencies of meow are pretty mature and stable, so I don't really think that's going to be a problem.

kevva commented

Did some digging into this. Since neither webpackor browserify support module.parent we can't use any of those two. The one drawback npm-packer had was that you had to manually build it, cd into the built directory and publish it from there. Also tried rollup which too didn't work.

Alternatively, https://github.com/zertosh/v8-compile-cache could also help startup performance without having to bundle dependencies.

@sindresorhus I feel like zeit/ncc would be a good choice for this.

Edit: I guess ncc uses webpack under the hood. I got this working with a plugin I've developed that uses Rollup. I will try to submit a PR soon.

Or publish-packed by Pnpm creator. It seems good enough. But have some limitations and bugs, on which i'm working. It basically moves the node_modules and prune (using nm-prune which I'm not sure how good is; we can just use globby or such to cleanup) and then publish (included in that cli).

@tunnckoCore I've looked at that before, but I've decided to go with ncc.

Good choice anyway :)

@sindresorhus I tried rolling up a Meow CLI with ncc and it failed unless I edited one of Meow's core library files.

@sindresorhus I tried rolling up a Meow CLI with ncc and it failed unless I edited one of Meow's core library files.

@brandonpittman What failed? What did you have to edit?

@sindresorhus

In meow's index.js, I had to change the line about module.parent.filename to:

let parentDir = ''
if (module.parent && module.parent.filename) {
  parentDir = path.dirname(module.parent.filename);
}

Then the ncc-built CLI will run. Otherwise, I get an error like this:

/Users/brandon-pittman/dev/gridtastic/dist/index.js:21107
const parentDir = path.dirname(module.parent.filename);
                                             ^

TypeError: Cannot read property 'filename' of undefined
    at Object.<anonymous> (/Users/brandon-pittman/dev/gridtastic/dist/index.js:21107:46)
    at __webpack_require__ (/Users/brandon-pittman/dev/gridtastic/dist/index.js:23:30)
    at Object.<anonymous> (/Users/brandon-pittman/dev/gridtastic/dist/index.js:23491:14

@brandonpittman it's okay that ncc will probably run that way, but the bundle won't work properly (the CLIs built with that bundle)?

Yea yea, it won't be a problem that parentDir is empty string (passed as cwd to read-pkg-up and find-up), it goes down to find-up which uses path.resolve(cwd || '') anyway.

@sindresorhus due to webpack/webpack#20 (comment) would you be open to perhaps moving the bulk of index.js (without module.parent) into another file which would get bundled, and then index.js would import that file? Or maybe we can revisit the need for module.parent?

Actually, I just noticed that https://github.com/sindresorhus/conf/blob/bbf4fd40e944146f63f40a06fe14a531dcf8e941/source/index.ts#L27 does

const parentDir = path.dirname(module.parent?.filename ?? '.');

Would it be acceptable to add this fallback to meow as well? That would resolve this ncc issue? Unless I'm misunderstanding what the fallback is for?