webpack-isomorphic-dev-middleware icon indicating copy to clipboard operation
webpack-isomorphic-dev-middleware copied to clipboard

Get exported function from server's entry file

Open ilyaagarkov opened this issue 7 years ago • 3 comments

Hi!

I have a misunderstanding about how I can export render functions from a server file I get empty object from res.locals.isomorphic.exports

config for server

const server = {
  mode: 'development',
  entry:'./src/server.js',
  output: {
    path: '/',
    publicPath: 'http://localhost:3000/scripts/',
    filename: `server.bundle.js`,
  },
  module: {
    rules: [js]
  },

}

server.js

export const fun =() => console.log('!!!!!!')

export default {
  foo: () => {}
}

ilyaagarkov avatar May 26 '18 12:05 ilyaagarkov

@ilyaagarkov I've never tried using a default export actually. Could you instead export a render function to see if it's available?

(sorry or the late response)

satazor avatar Jul 26 '18 11:07 satazor

I ran into the same problem, and after spending a few hours on it I managed to get it works. In my case the problem was that webpack-isomorphic-dev-middleware uses require-from-string to extract exports from the server bundle. However, this only works is the server bundle is generated in a very specific way.

In my case I fixed by adding library and libraryTarget to output.

    output: {
            ...
            library: 'library',
            libraryTarget: 'commonjs-module',
        },

This makes it so that the server bundle has module.exports (instead of being wrapped in webpack helper functions), so that require-from-string can extract the exports.

arthens avatar Oct 13 '18 06:10 arthens

@arthens Correct, I will leave this open as a reminder to make this clear in the README

satazor avatar Mar 13 '19 01:03 satazor