import{ cube }from'./math.js';functioncomponent(){const element = document.createElement('pre'); element.innerHTML =['Hello webpack!','5 cubed is equal to '+cube(5)].join('\n\n');return element;}document.body.appendChild(component());
./src/math.js
exportfunctionsquare(x){return x * x;}exportfunctioncube(x){return x * x * x;}
Note that we did not import the square method from the src/math.js module. That function is what's known as "dead code",
meaning an unused export that should be dropped. Now let's build the project.
This will yield the following result:
// ...skipping some trivial codevar __webpack_modules__ ={'./src/index.js':function(module, exports, __webpack_require__){'use strict'; Object.defineProperty(exports,'__esModule',{value:true,});var _mathJs =__webpack_require__('./src/math.js');functioncomponent(){const element = document.createElement('pre'); element.innerHTML =['Hello webpack!','5 cubed is equal to '+(0, _mathJs.cube)(5),].join('\n\n');return element;} document.body.appendChild(component());},'./src/math.js':function(module, exports, __webpack_require__){'use strict'; Object.defineProperty(exports,'__esModule',{value:true,});function_export(target, all){for(var name in all) Object.defineProperty(target, name,{enumerable:true,get: all[name],});}_export(exports,{square:function(){return square;},cube:function(){return cube;},});functionsquare(x){return x * x;}functioncube(x){return x * x * x;}},};// ...
As you can see, if we don't enable the treeShaking, all the code is kept as is, except wrapping with
some runtime code.
Now, we switch to production mode and rebuild the project. In order to make the output more readable, we also turn off the minimize option and switch the moduleIds to named. To compare with the later chapters, we turn off optimization.sideEffects.
In a 100% ESM module world, identifying side effects is straightforward. However, we aren't there quite yet (In real project we using different kinds of format of package cjs, esm, umd and so on.), so in the mean time, it's necessary to provide hints to rspack's compiler on the "pureness" of your code.
The way this is accomplished is the "sideEffects" package.json property.
package.json
{"name":"your-project","sideEffects":false}
The sideEffects field supports the following values:
false All files in this package have no side effects.
string A glob matching files that includes side effects.
Array<string> An array of globs matching files that include side effects.
undefined This is the default value of sideEffects when you don't provide any value to sideEffects field of package.json, When the undefined is set, Rspack will try to analyse if the code has sideEffects when optimization.sideEffects is true
(which is the default value when mode is production), or Rspack will treat all the modules in package has sideEffect
const randomDate = Date.now();exportconstaddRandomDate=(a)=> a + randomDate;exportconstadd=(a, b)=> a + b;
The variable randomDate would still be needed because it includes a side effect that runs during the module's initialization.
However, because the package.json includes the sideEffects field and the value is false, and there are no export variable used in
add.js, so the whole module could be skipped, same for subtract.js
You could override some modules sideEffects by using module.rule.sideEffects.
Why do we need such feature?
Considering if the author of package math forgot to add sideEffects field in package.json:
Then Rspack will try to analyze the code safely and only mark the module is sideEffectFree when
all toplevel statements are sideEffectFree. As you can see, math/index.js, math/subtract.js and math/multiply.js is sideEffectFree
while math/add.js is not, because of const randomDate = Date.now(). When rebuilding the project,
you could see the diff:
That's not what we expect, then we could use module.rule.sideEffects, because it has higher priority than sideEffects in package.json.
Since the initialization side effect is only meaningful when addRandomDate is utilized, we can safely override it. To do so, we can make the following modification to our rspack.config.js: