V
V
Vladimir Druzhaev2018-09-01 16:15:48
Node.js
Vladimir Druzhaev, 2018-09-01 16:15:48

How to take into account js functions when compiling a file?

Hello.
Webpack does not take into account the functions in the home.js file when building.
For example, this construction:

home.js
jQuery(function($){
    console.log('home1');
});

function somea(){
    console.log('home2');
}

function someb(){
    console.log('home3');
}

console.log('home4');

in a minified form leads to bundle.js to this:
bundle.js
!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t){jQuery(function(e){console.log("home1")}),console.log("home4")}]);

as you can see: I only have home1 and home4 present. And what is inside the functions is not included in the assembly (home2 and home3 from function somea() and function someb(), respectively).
Tell me: why is that?
Webpack 5.6.0
Config:
const path = require('path');

module.exports = {
  entry: './home.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  }
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Perov, 2018-09-01
@OtshelnikFm

Perhaps because they are not used anywhere?
tree shaking (may be wrong)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question