purs-loader icon indicating copy to clipboard operation
purs-loader copied to clipboard

webpack dev server breaks if not bundling with multiple rules

Open shmish111 opened this issue 4 years ago • 1 comments

I am trying to build a project with a web-worker so I have 2 rules that both use purs-loader. If I set bundle: true then I can run a dev server and it seems to work correctly. Unfortunately due to https://github.com/ethul/purs-loader/issues/94 I have to save Main.purs every time I make a change as well as things being very slow, I think because of the bundling. If I set bundle: false (which is what I had in dev-server mode before I started playing with web workers) then I get the following types of errors:

Module build failed (from ./node_modules/purs-loader/lib/index.js):
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type object

I've managed to track it down to https://github.com/ethul/purs-loader/blob/3.7.1/src/index.js#L230 where psModuleName is sometimes null because it gets to this line when source is actually a javascript file, (it is a compiled purescript file in fact).

To be honest I have no idea how any of this works so this is as far as I managed to get.

shmish111 avatar Dec 18 '19 13:12 shmish111

This seems to fix the problem:

if (psModuleName === null && this.resource.lastIndexOf('.js') === this.resource.length - 3) {
  var parentSource = this.fs.readFileSync(this.resource.replace(/\.js$/g, '.purs')).toString()
  psModuleName = PsModuleMap.matchModule(parentSource);
}

Although I'm not knowledgeable enough in PureScript nor Webpack to say it's a correct way to do it, so I don't send a PR.

Edit: this doesn't produce working code. Better fix:

var isJS = false;

  if (psModuleName === null && this.resource.lastIndexOf('.js') === this.resource.length - 3) {
    var parentSource = this.fs.readFileSync(this.resource.replace(/\.js$/g, '.purs')).toString()
    psModuleName = PsModuleMap.matchModule(parentSource);
    isJS = true
  }
...
jsPath: path.resolve(path.join(options.output, psModuleName, isJS ? 'foreign.js' : 'index.js')),

szatkus avatar Dec 26 '19 21:12 szatkus