liady / webpack-node-externals

Easily exclude node modules in Webpack
MIT License
1.3k stars 62 forks source link

correct configuration for files that do not exist in development #42

Open dagda1 opened 6 years ago

dagda1 commented 6 years ago

I have the following code:

    const configureProduction = (app: any) => {
      const clientStats = require('./stats.json');
      const serverRender = require('./server.js').default;
      const publicPath = '/';
      const outputPath = join(__dirname, '.');

      app.use(publicPath, express.static(outputPath));
      app.use(
        serverRender({
          clientStats,
          outputPath
        })
      );

      app.set('views', join(__dirname, 'views'));
    };

    const app = express();

    if (process.env.NODE_ENV === 'development') {
      configureDevelopment(app);
    } else {
      configureProduction(app);
    }

When running in development, the configureProduction function is not called but I get the following webpack errors:

ERROR in ./src/index.tsx Module not found: Error: Can't resolve './server.js' in '/Users/paulcowan/projects/cuttingedge/packages/website/src' @ ./src/index.tsx 38:23-45 @ multi ./src/index

ERROR in ./src/index.tsx Module not found: Error: Can't resolve './stats.json' in '/Users/paulcowan/projects/cuttingedge/packages/website/src' @ ./src/index.tsx 37:22-45 @ multi ./src/index

My nodeExternals is set like this:

    externals: [
      nodeExternals({
        modulesDir: path.join(process.cwd(), '../../node_modules'),
        whitelist: [
          isDevelopment ? 'webpack/hot/poll?300' : null,
          './stats.json',
          './server.js',
          /\.(eot|woff|woff2|ttf|otf)$/,
          /\.(svg|png|jpg|jpeg|gif|ico)$/,
          /\.(mp4|mp3|ogg|swf|webp)$/,
          /\.(css|scss|sass|sss|less)$/
        ].filter(x => x)
      })
    ],

How can I configure this correctly?