piuccio/cowsay

Module issues with Webpack

johnnysprinkles opened this issue · 11 comments

I have a Javascript project that's compiled with Webpack/Babel for use in NodeJS runtime, and when I try to depend on cowsay I get this error at compile time:

[node] Error: Cannot find module '.cows/ackbar.cow'
[node]     at webpackMissingModule (webpack:///./node_modules/cowsay/build/cowsay.es.js?:50:45)
[node]     at eval (webpack:///./node_modules/cowsay/build/cowsay.es.js?:50:136)
[node]     at Module../node_modules/cowsay/build/cowsay.es.js (/local/home/simnsj/workspace/nodetemplate/src/NodeStarterTemplate/dist/server/main.js:782:1)
...

My code for including cowsay looks something like this:

import cowsay from 'cowsay';

class Cowsay extends React.Component {
  render() {
    return <div>{cowsay.say({text: this.props.children})}</div>;
  }
}

I don't understand javascript modules that well, and I think they're a moving target...

Hmm it looks weird.
Are you trying to render react code on the server?
Might be that you're missing a file loader, the .cow files should be loaded as text.
Is your project public? Maybe I can have a look

It's not but I'll boil down to a repro case, soon.

same issue .
on next.js server render

Failed to compile.

./node_modules/cowsay/build/cowsay.umd.js
Module not found: Can't resolve '.cows/C3PO.cow' in '/Volumes/data/workspace/github/foxmn/web-tools/show-user-agent/node_modules/cowsay/build'
import cowsay from "cowsay";
...
IndexPage.getInitialProps = async function({ req, res }) {
  const isServer = typeof window === "undefined";
  if (notBrowser) {
    const say = chalk.blueBright('xxxxxxxxxx');
    res?.write(cowsay.say({ text: say, f: "cat" }));
    res?.end();
  }
  return {
   ...
  };
};
mkly commented

I had some success using

resolve: {
  alias: {
    ".cows": "cowsay/cows"
  },
},

Enjoy your cows! 🐄

I ended up kind of forking this project, with pure Javascript cows instead of text file Perl cows. I think JS Cows are the Cows of the future. https://github.com/johnnysprinkles/cowsay/tree/master/cows

@johnnysprinkles if you argument better why that's better and send a PR, I might merge it...

That would be a sizeable PR! And probably warrant a v2 release. This fork also does a number of other things, curious how much you'd be interested in a PR for all these things:

  • Update to modern style (var => let, single quotes, 2-space tabs, etc)
  • Update to modern language features, e.g. max can be a one-liner using array spread
  • Update to the top level API to make the text be a first positional parameter, and all the other options in a second object param
  • Pure Javascript cows using template literals. I'm using the Perl heredoc literals almost unchanged, but the "superfluous escapes" mentioned in #50 are no longer an issue this way
  • Rewrote balloon#split() to handle word breaking correctly
  • The code to convert Perl heredoc to JS template is under util/ but really that doesn't even need to be included, could go in a gist or somewhere. But some things in your cowsay could be removed like replacer.js and all the .cow files.
  • ESLint checks

If you care about having them upstream, send some PR, I'll review (best to separate in multiple PRs)

Hmm, it's diverged quite a bit, not really a fork more like looking at your implementation for reference. I guess I'm trying to gauge your interest in owning and maintaining NPM cowsay, sounds like you are still interested.

Up to you, if you feel like your code would benefit the people using npm cowsay we can try to converge. I don't mind having you as contributor if it makes sense.