Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[@nrwl/webpack] Minification does not work correctly (with node target) #12782

Closed
Lukas-Kullmann opened this issue Oct 24, 2022 · 6 comments · Fixed by #14431
Closed

[@nrwl/webpack] Minification does not work correctly (with node target) #12782

Lukas-Kullmann opened this issue Oct 24, 2022 · 6 comments · Fixed by #14431
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug

Comments

@Lukas-Kullmann
Copy link

Current Behavior

Firstly, a bit of context where I'm coming from (feel free to skip this paragraph since it is not super important):
I recently upgraded nx from 13.0.1 to 15.0.1. However, after that upgrade, I noticed that mikro-orm did not start any more. I got this weird error Please provide either 'type' or 'entity' attribute in n.xxx, which did not occur before. After some digging I noticed that it basically merged all of the metadata for my entities together into one big old definition. And it would throw the error above for entities that did not have the corresponding property (in this case xxx). After even more digging, I found out that everything worked great with nx 14.7.8 and it broke in 14.7.9. I looked at the changes, debugged into the webpack executor and found out, that the optimization does not work the way it used to any more.

Currently, no matter what you set as optimization in the @nwrl/webpack:webpack executor, there will be no optimization applied to the webpack config. This means that it will fall back to the default webpack optimization for production mode (which is basically new TerserPlugin({ terserOptions: { compress: { passes: 2 } } });, see the webpack source code).

However, prior to version 14.7.9, there was a default optimization put in place, but it was more or less removed in PR #12130. In this PR, the optimization will only be applied if options.optimization === true (see Line 154 in file packages/webpack/src/utils/config.ts).
However, options.optimization is never true. It is set to { scripts: true, styles: true } in normalizeOptions (see packages/webpack/src/executors/webpack/lib/normalize-options.ts on line 29) which is used by webpackExecutor to calculate options (see packages/webpack/src/executors/webpack/webpack.impl.ts).

Coming back to my problem that I described in the first paragraph, you can see what went wrong here: Prior to nx 14.7.9, the optimization would leave class names as is. Now, it minifies class names. This seems to break reflect-metadata which is used by mikro-orm to infer types of entities.

This problem is present in all versions since 14.7.9 to this day (current version is 15.0.1).

Expected Behavior

I would expect, that this behavior does not change all of a sudden and in an undocumented way in a patch release.

Steps to Reproduce

I think this is quite hard to reproduce, but I hope it should be clear from the explanation above

Potential fix and workaround

I think the easiest fix would be to change the condition in packages/webpack/src/utils/config.ts on line 157 to internalOptions.isScriptOptimizeOn (just as it is used on line 175 in the same file).

In the mean time, a workaround would be to use a custom webpack config (see webpackConfig option for the @nrwl/webpack:webpack executor):

const { merge } = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');

module.exports = (config) => {
  return merge(config, {
    optimization: {
      minimizer: [
        new TerserPlugin({
          terserOptions: {
            mangle: false,
            keep_classnames: true,
          },
        }),
      ],
    },
  });
};

Failure Logs

nx does not fail in any way.

Environment

@nrwl/webpack > 14.7.8

@jamesmcglinn
Copy link

This is also an issue for NestJS with TypeORM and generates errors in production such as the one below.

CircularRelationsError: Circular relations detected: TypeORMd -> l -> l. To resolve this issue you need to set nullable: true somewhere in this dependency structure.

I used a similar workaround to @Lukas-Kullmann's webpack config above with keep_classnames: true, although in this case mangle: false wasn't needed.

@MarcieMarc425
Copy link

MarcieMarc425 commented Oct 26, 2022

Can confirm, I encounter the same issue with build. Seems like somehow changing to webpack build makes it not able to use Map correctly. Also because of the default obfuscation done by webpack, if you use swagger document in NestJS, the schema class names will come out as single letter as name.

I used the workaround by @Lukas-Kullmann and works well.

@aarifkhamdi
Copy link

#12834 I faced the same problem. Also minimal repo is provided there

@flippidippi
Copy link

flippidippi commented Jan 17, 2023

We are having this issue to. There is currently NO WAY to turn off optimization for @nrwl/node:webpack because of this line

minimize: !!options.optimization,

@rmnunes
Copy link

rmnunes commented Mar 12, 2023

I think this is back on, maybe I'm doing something wrong. I created a nestjs project and I'm using require.resolve and in the dist folder the module is not there. I tried diff solutions, optimization, webpack none worked. could you help? Thank you

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
7 participants