简体   繁体   English

避免命名函数调用的 Webpack 配置

[英]Webpack configuration that avoids named function calling

I want to trade disk space for no named function calling in webpack so that I can do language analysis easier.我想用磁盘空间来交换 webpack 中没有命名函数的调用,以便我可以更轻松地进行语言分析。

However webpack' ing the following modules,但是 webpack' ing以下模块,

  • index.js import foo.js; foo() index.js import foo.js; foo() import foo.js; foo()
    • foo.js import bar.js; console.log(bar()) console.log(bar()) console.log(bar()) foo.js import bar.js; console.log(bar()) console.log(bar()) console.log(bar()) import bar.js; console.log(bar()) console.log(bar()) console.log(bar())
      • bar.js return "THIS IS BAR!" bar.js return "THIS IS BAR!"

Results in:结果是:

function bar() { return "THIS IS BAR!"; }
function foo() {
  console.log(bar());
  console.log(bar());
  console.log(bar());
}
foo();

And I want,而且我要,

function foo() {
  console.log((() => { return "THIS IS BAR!" })());
  console.log((() => { return "THIS IS BAR!" })());
  console.log((() => { return "THIS IS BAR!" })());
}
foo();

Even while this will increase the space of the bundle.即使这会增加包的空间。 Because I want to avoid function calling.因为我想避免函数调用。

How can I do this?我怎样才能做到这一点?

So far I have tried with several { optimization: { ... } } rules but none of them leads to the desired result.到目前为止,我已经尝试了几个{ optimization: { ... } }规则,但没有一个会导致预期的结果。

Did you try with the mode production.您是否尝试过模式生产。 With current setting.使用当前设置。 I can see desire output.我可以看到欲望输出。

// foo.js // foo.js

import bar from './bar'
export default function print() {
    console.log(bar())
    console.log(bar())
    console.log(bar())
}

// bar.js // bar.js

export default function bar() {
    return "THIS IS TEST"
}

// webpack.config.js // webpack.config.js

const path = require('path');

module.exports = {
    mode: "production",
  entry: './src/foo.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'foo.bundle.js'
  }
};

// Version // 版本

"webpack": "^4.42.0", "webpack-cli": "^3.3.11" "webpack": "^4.42.0", "webpack-cli": "^3.3.11"

// Output // 输出

([function(e,t,n){"use strict";function r(){console.log("THIS IS TEST"),console.log("THIS IS TEST"),console.log("THIS IS TEST")}n.r(t),n.d(t,"default",(function(){return r}))}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM