cowsay icon indicating copy to clipboard operation
cowsay copied to clipboard

Module issues with Webpack

Open johnnysprinkles opened this issue 5 years ago • 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...

johnnysprinkles avatar Jun 10 '19 21:06 johnnysprinkles

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

piuccio avatar Jun 11 '19 03:06 piuccio

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

johnnysprinkles avatar Jun 11 '19 22:06 johnnysprinkles

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'

foxundermoon avatar Jan 15 '20 21:01 foxundermoon

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 {
   ...
  };
};


foxundermoon avatar Jan 15 '20 21:01 foxundermoon

I had some success using

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

Enjoy your cows! :cow2:

mkly avatar Jan 19 '20 18:01 mkly

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 avatar Jan 21 '20 07:01 johnnysprinkles

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

piuccio avatar Jan 21 '20 08:01 piuccio

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 https://github.com/piuccio/cowsay/pull/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

johnnysprinkles avatar Jan 21 '20 16:01 johnnysprinkles

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

piuccio avatar Jan 22 '20 02:01 piuccio

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.

johnnysprinkles avatar Jan 22 '20 05:01 johnnysprinkles

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.

piuccio avatar Jan 22 '20 06:01 piuccio