nestjs / ng-universal

Angular Universal module for Nest framework (node.js) 🌷
https://nestjs.com
MIT License
442 stars 68 forks source link

Support for @nestjs/graphql #120

Closed inradius closed 5 years ago

inradius commented 5 years ago

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When implementing the @nestjs/graphql package into a project using @nestjs/ng-universal following the provided installation instructions, the webpack command results in errors when building.

Seems to be related to the WebpackConfigFactory and it not being configured to build for this scenario (.mjs file extensions, etc.)

Expected behavior

A basic @kamilmysliwiec/universal-nest starter application can be extended to use @nestjs/graphql without failing to compile

Minimal reproduction of the problem with instructions

You should see this fail when getting to the webpack --config webpack.server.config.js --progress --colors portion of the build.

What is the motivation / use case for changing the behavior?

Integrate @Nestjs/GraphQL with an Angular Universal application running on Nest.js Universal module.

Environment


Nest version: 6.5.2


For Tooling issues:
- Node version: v10.16.0  
- Platform:  Mac

Others:

kamilmysliwiec commented 5 years ago

Unfortunately, some Node.js packages (especially native bindings, e.g. fsevents in this case) cannot be easily bundled using Webpack. In order to get rid of errors, you have to adjust your webpack configuration (webpack.server.config.js):

const webpack = require('webpack');
const WebpackConfigFactory = require('@nestjs/ng-universal')
  .WebpackConfigFactory;
const nodeExternals = require('webpack-node-externals');

/**
 * In fact, passing following configuration to the WebpackConfigFactory is not required
 * default options object returned from this method has equivalent entries defined by default.
 *
 * Example: WebpackConfigFactory.create(webpack);
 */
const webpackOptions = WebpackConfigFactory.create(webpack, {
  // This is our Nest server for Dynamic universal
  server: './server/main.ts',
  // This is an example of Static prerendering (generative)
  prerender: './prerender.ts',
});

webpackOptions.resolve.extensions.push('.mjs', '.graphql', '.gql');
webpackOptions.module.rules.push({
  test: /\.mjs$/,
  include: /node_modules/,
  type: 'javascript/auto',
});

const whitelistedPackages = /^(?!(livereload|concurrently|fsevents)).*/;
webpackOptions.externals[1] = nodeExternals({
  whitelist: whitelistedPackages,
});

webpackOptions.plugins.push(
  new webpack.IgnorePlugin({
    checkResource(resource) {
      const lazyImports = [
        '@nestjs/microservices',
        'cache-manager',
        'class-validator',
        'class-transformer',
        'apollo-server-fastify',
        'bufferutil',
        'utf-8-validate',
      ];
      if (!lazyImports.includes(resource)) {
        return false;
      }
      try {
        require.resolve(resource);
      } catch (err) {
        return true;
      }
      return false;
    },
  }),
);

module.exports = webpackOptions;

Then, it will work fine