NOTE
This is an experimental and learning project and still in development. Use it on your own risk! This would not work properly if you can reused variable or function names in different files.
This is a simple loader, it will simply pre process few of the code which wont effect the normal execution of the program in the run time. It will pre process codes like these
consta=123;varwhatsTheNumber=a;Now this will convert into the following
consta=123;varwhatsTheNumber=123;Why ? as a is const and its value will not change in the rest of the program, We can simply change all those variables which are assign using a with a's value that is 123
in future release, this line
const a = 123will be removed from the output file as it doesnt have anything to do with the program after compiling using this loader
Another example can be,
constdumyFN=()=>{return'demo';};console.log(dumyFN());This code will convert into the following
console.log('demo');Preprocessing few codes like this increases the run time of the program as the program doesn't need to jump from one part to another to get the value of the variable.
As of now, the support for pre processing is very limited with this loader, for example
constobj={prop1: ()=>{return'value';}};This can be converted into this
constobj={prop1: 'value'};but this loader doesnt support this. Support of this will be added in future. function bodies where the return statements are wrapped with other statements like another function or if-statement or callbacks are not supported by this loader as of now. Support of this will be added in future
// Source code here...constv=5;constz='a';functionhello(x){console.log('hellox',x);}consttests=5+2;constafn=()=>{returnv;};constfn=function(){return'fn';};vara=v;leta2=z;constfn2=function(){returna2;};hello(v);hello(z);consthe={// Not supportablewhat: ()=>{return'qwe';}};constfntest=he.what();// needs to cover this !!!console.log('fntest',fntest);console.log('tests',tests);console.log('fn',fn());console.log('afn',afn());console.log('fn2',fn2());hello(afn());console.log('v',v);// Bundle code here...functionhello(x){console.log('hellox',x);}vara=5;leta2='a';constfn2=function(){returna2;};hello(5);hello('a');consthe={// Not supportablewhat: ()=>{return'qwe';}};constfntest=he.what();// needs to cover this !!!console.log('fntest',fntest);console.log('tests',7);console.log('fn','fn');console.log('afn',5);console.log('fn2',fn2());hello(5);console.log('v',5);To begin, you'll need to install preprocessing-loader:
$ npm install preprocessing-loader --save-devThen add the loader to your webpack config. For example:
webpack.config.js
module.exports={module: {rules: [{test: /.ext$/,use: [{loader: 'preprocessing-loader',options: { ...options}}]}]}};And run webpack via your preferred method.
As of now, no options are available
webpack.config.js
module.exports={entry: 'index.js',output: {filename: 'bundle.js'},module: {rules: [{test: /\.js$/,use: [{loader: 'preprocessing-loader'}]}]}};index.js
// Source code here...constv=5;constz='a';functionhello(x){console.log('hellox',x);}consttests=5+2;constafn=()=>{returnv;};constfn=function(){return'fn';};vara=v;leta2=z;constfn2=function(){returna2;};hello(v);hello(z);consthe={// Not supportablewhat: ()=>{return'qwe';}};constfntest=he.what();// needs to cover this !!!console.log('fntest',fntest);console.log('tests',tests);console.log('fn',fn());console.log('afn',afn());console.log('fn2',fn2());hello(afn());console.log('v',v);Babel/minifygot some number of babel plugins which are used for code optimization.babel-plugin-minify-constant-folding,babel-plugin-minify-dead-code-eliminationsomewhat does the same job in better way
v8does this kind of code optimizations too using graphs (CFG)- google's closure compiler for javascript does this too with other optimizations features as well at a awesome level
- There are other minifiers or code optimization libraries are present too which implements these approaches as well
Please take a moment to read our contributing guidelines if you haven't yet done so.
