How to exclude specific node module from minification in next.js app

368 Views Asked by At

In my next.js app I am using a package called 'js-interpreter', residing in node_modules/js-interpreter, which is a pre-minified package. So, when I create my build with next build it further minifying the js-interpreter package and crashing its functionality. I want to exclude this package from minification process during build.

I have used terserPlugin in next.config.js and exclude this package but it did not work. What am I doing wrong?

//next.config.js

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

const nextConfig = {
  poweredByHeader: false,
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack, ...rest }) => {
    console.log({
      buildId,
      dev,
      isServer,
      defaultLoaders,
      webpack,
      rest,
    });

    const terserplugin = new TerserPlugin({
      terserOptions: {
        ecma: 5,
        compress: {
          comparisons: !1,
          inline: 2,
          drop_console: true,
          drop_debugger: true,
          pure_funcs: ['console.log'],
        },
        mangle: {
          safari10: !0,
        },
        output: {
          ecma: 5,
          comments: !1,
          ascii_only: !0,
        },
      },
      parallel: !0,
      exclude: /js-interpreter/,
    });
    config.optimization.minimizer = [...config.optimization.minimizer, terserplugin];
  
    return config;
  },
};
module.exports = nextConfig;

I have also tried this:

//next.config.js

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

const nextConfig = {
  poweredByHeader: false,
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack, ...rest }) => {
    console.log({
      buildId,
      dev,
      isServer,
      defaultLoaders,
      webpack,
      rest,
    });

    const terserplugin = new TerserPlugin({
      terserOptions: {
        ecma: 5,
        compress: {
          comparisons: !1,
          inline: 2,
          drop_console: true,
          drop_debugger: true,
          pure_funcs: ['console.log'],
        },
        mangle: {
          safari10: !0,
        },
        output: {
          ecma: 5,
          comments: !1,
          ascii_only: !0,
        },
      },
      parallel: !0,
    });
    config.optimization.minimizer = [...config.optimization.minimizer, terserplugin];
   const terserPluginIndex = config.optimization.minimizer.findIndex(plugin => plugin instanceof TerserPlugin);

    console.log('terser index', terserPluginIndex); // it's 2

    if (terserPluginIndex > -1) {
      config.optimization.minimizer[terserPluginIndex].options.terserOptions = {
        ...config.optimization.minimizer[terserPluginIndex].options.terserOptions,
        // exclude: [/\/node_modules\/js-interpreter\//],
        exclude: /js-interpreter/,
      };
      console.log('terserOptions', config.optimization.minimizer[terserPluginIndex].options); // it is showing /js-interpreter/ has been added to exclude but still minifying it
    }

    return config;
  },
};
module.exports = nextConfig;

Thank you in advance :)

0

There are 0 best solutions below