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
const a = 123;
var whatsTheNumber = a;
Now this will convert into the following
const a = 123;
var whatsTheNumber = 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 = 123
will 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,
const dumyFN = () => {
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
const obj = {
prop1: () => {
return 'value';
}
};
This can be converted into this
const obj = {
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...
const v = 5;
const z = 'a';
function hello(x) {
console.log('hellox', x);
}
const tests = 5 + 2;
const afn = () => {
return v;
};
const fn = function() {
return 'fn';
};
var a = v;
let a2 = z;
const fn2 = function() {
return a2;
};
hello(v);
hello(z);
const he = {
// Not supportable
what: () => {
return 'qwe';
}
};
const fntest = 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...
function hello(x) {
console.log('hellox', x);
}
var a = 5;
let a2 = 'a';
const fn2 = function() {
return a2;
};
hello(5);
hello('a');
const he = {
// Not supportable
what: () => {
return 'qwe';
}
};
const fntest = 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-dev
Then 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...
const v = 5;
const z = 'a';
function hello(x) {
console.log('hellox', x);
}
const tests = 5 + 2;
const afn = () => {
return v;
};
const fn = function() {
return 'fn';
};
var a = v;
let a2 = z;
const fn2 = function() {
return a2;
};
hello(v);
hello(z);
const he = {
// Not supportable
what: () => {
return 'qwe';
}
};
const fntest = 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/minify
got some number of babel plugins which are used for code optimization.babel-plugin-minify-constant-folding
,babel-plugin-minify-dead-code-elimination
somewhat does the same job in better way
v8
does 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.