0

I'm trying to implement something similar to dependency injection, By getting parameter names from a function exposed by a javascript module, something like this:

module.exports = (router,form,query,url) => {

    // Do something with these parameters ...

    return response;
};

I can solve this problem by parsing the string representation of the function. There's already a SO thread for that.

My problem becomes apparent when the code is bundled for production with webpack, all the parameter names get mangled, and the original names are lost.

I couldn't find any option in the webpack config that can help me with that.

Is there a way to do what I want without making the module that exports the function worry about anything related to this problem?

soufiane yakoubi
  • 861
  • 11
  • 31

2 Answers2

0

Take a look at webpack TerserPlugin minify options: From here

You can handle this file minification own your own or not minify it at all.

Something like that:

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        minify: (file, sourceMap) => {
          if (file.name == <Your_file_with_DI_function>) {
              return null;
          }

          const { error, map, code, warnings } = require('uglify-module')
               .minify(file, {
              /* Your options for minification */
            });

          return { error, map, code, warnings, []};
        },
      }),
    ],
  },
};
Raz Ronen
  • 2,418
  • 2
  • 14
  • 26
  • I tried this on a small sample project, and it looks like The function gets called only on the result bundle file. The file object doesn't have a `name` property, but it does have the file name as a property. Also, null is not valid as a return value, it has to be an object that contains information about the minified code. I guess I'll just exclude the target modules from the minification process. – soufiane yakoubi Jul 31 '20 at 13:00
0

What I'm trying to do is not possible.

By the time the code gets to the minimizer plugin, it has already been concatenated into a single bundle file.

Another option is to use uglify-loader, but that only works on module level, so you're left with non-minified code that wraps the actual modules.

soufiane yakoubi
  • 861
  • 11
  • 31