3

I've just split up my monolith webpack/koa project into multiple packages using yarn workspaces. I have one package that I call "web" which is just the webpack build. Then I have another one called "server" which is koa serving both the webpack artifacts as well as an api.

The problem I have is that the webpack build seems to resolve dependencies from the server package at run time and therefore fails.

I expose the webpack compiler from my web project to be consumed in dev from the server project:

const webpack = require('webpack')
const devConfig = require('./webpack.dev.js')
const compiler = webpack(devConfig)
module.exports = compiler

And I stick this into Koa as middleware:

import devWebpackCompiler from '@kb-front/web' // the web package
const webpackMiddleware = await koaWebpack({ compiler: devWebpackCompiler })
app.use(webpackMiddleware)

Here I'm using koa-webpack, but I've tried to use 'webpack-dev-middleware' and 'webpack-hot-client' manually with my own wrapping and I get the same exact behaviour.

What I get is things like:

ERROR in multi @babel/polyfill ../web/src/marko/marko-bootstrap ../web/src/materialize/kb-materialize.scss
Module not found: Error: Can't resolve 'babel-loader' in '/Users/viktor/dev/projects/kb-frontend/packages/server'
 @ multi @babel/polyfill ../web/src/marko/marko-bootstrap ../web/src/materialize/kb-materialize.scss main[1]

and such. I've tried to add deps to my server project to fill these out, but its a deep rabbit hole I shouldn't need to dive into, should I?

What's the right way to use webpack-dev-middleware with separation of web build and express/koa server node packages?

I'm using ES6 modules in my server project with --experimental-modules, could that be the culprit?

Viktor Hedefalk
  • 3,572
  • 3
  • 33
  • 48

0 Answers0