How to get original function parameter names in production webpack build

822 views Asked by At

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?

2

There are 2 answers

0
soufiane yakoubi On BEST ANSWER

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.

1
Raz Ronen On

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, []};
        },
      }),
    ],
  },
};