Quando se trata de escolher entre Webpack e Browserify com Gulp como ferramentas de automação para o desenvolvimento web, é essencial entend
seen from United Kingdom

seen from United Kingdom

seen from United Kingdom

seen from United States
seen from United States

seen from Canada

seen from Russia
seen from Germany

seen from United States

seen from United States
seen from Bangladesh
seen from United States
seen from Netherlands
seen from United States

seen from China
seen from Romania

seen from China

seen from United States
seen from Kazakhstan
seen from China
Quando se trata de escolher entre Webpack e Browserify com Gulp como ferramentas de automação para o desenvolvimento web, é essencial entend

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Perhaps most of you’ve gotten heard over the course of the program, modular modules and programming, and understand the logic and use it. Nicely, when you're the one who's obtained this text, what's higher, but don’t worry in the event you're not. Here we are going to speak about module programming in javascript. For those who used this technique in other languages, you must know that because the JavaScript in common could be very totally different from the rest of the languages, that is totally different for the same program. Modular Writing may have a common story! In any case, let's take the headline.
Why modular programming
I feel that the answer to this query is just not very troublesome, any programming want to re-write the code that they could use several occasions, and just name it in totally different places. By sorting the program and using the code once more, Hamoon The program also has an incredible structure and extra handy programming. It additionally makes it easier to keep the code and the code is modified later. You would not have to vary every little thing in this system and alter the code. Then again, whenever you write in modular JavaScript, each module will get its personal particular scope, and it's nice that you do not fill the whole area of your program with variables in each module (or you will not be so soiled) that you simply'll end up with later. So let's go to the primary point Now it's time to see how we will write in modular javascript. There are a number of methods to do this, the primary query which may get in your mind is that we’ve got the module discussion in the ES6 and that's the case. However no, it's true that the module discussion has been launched to the eS, but nonetheless no modular programming browser helps JS. What’s our task now? We have now to use the libraries to do this, or use the Module Bundlers more accurately, to try this for us.
So, we need to do precisely what we do in the Nodus JS to the aspect of Clither, in order that we will require our file on the aspect of Clayt. Aha, we’ve not remembered. With no library, we will additionally write modulars and there are some methods to do that. Properly, I don’t advise, and it's actually a little bit of a multitude. When the libraries with a lot functionality do this, we come to ourselves. Let's face it.
So let's see how these libraries are divided. And which one is best to choose? With a view to write modular packages in your shopper aspect, you even have three CommonJS and AMD and ES6, each of which we explain (all of which are based mostly on certainly one of these three)
The two widespread ones, CommonJs and AMD, are generally referred to as Scriptloader. Notice that these aren’t a library and not just a normal or a way to standardize the code that writes the remaining.
Commonjs
The Comenius JS module is a javascript code that comes out of every module in the form of a code, and we will use it. The NodeGS itself additionally makes use of the Comenius GS, so when you work with Node, you possibly can easily study Widespread. Let's see the code from Widespread:
perform myModule () this.hey = perform () return 'hey!';
this.goodbye = perform () return 'goodbye!';
module.exports = myModule;
perform myModule ()
this.hey = perform ()
return 'hey!';
this.goodbye = perform ()
return 'goodbye!';
module.exports = myModule;
Nicely, now, if you wish to use this code, we'll have to use another one:
can myModule = require ('myModule');
can myModuleInstance = new myModule (); myModuleInstance.howdy (); // 'howdy!' myModuleInstance.goodbye (); // 'goodbye!'
can myModule = require ('myModule');
can myModuleInstance = new myModule ();
myModuleInstance.good day (); // 'howdy!'
myModuleInstance.goodbye (); // 'goodbye!'
Properly, that's the code for writing in javascript, as I stated, the scope of all the program might be stored clear, and it's a good suggestion that we will use our code in all places and never repeat it. Within the meantime, you see the code is straightforward.
The necessary factor about CommonJs is that it masses the modules simultaneously. That is, when you have three information, they may load in the order they have been written (one after the opposite)
AMD
Properly, now if you want to obtain the information on the similar time, what?
Will probably be your selection of AMD. Let's check out the code right here:
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule) console.log (myModule.hey ()); );
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule)
console.log (myModule.howdy ());
);
Properly, let's additionally say that many people don’t like this technique (in contrast to time) to load javascript information; as well as the CommonJs and es6 syntax is simpler to know.
Nicely, is it going to end right here and we shouldn’t have another selection?
UMD
For once you want each of you.
ES6
Properly, the final choice is es6. In fact, it's simply JavaScript. Which you in all probability heard too. For extra info on modules on es6, see this hyperlink.
Instance code syntax es6:
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ]; can reply = sum (values);
doc.getElementById (“answer”). innerHTML = reply;
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ];
can answer = sum (values);
document.getElementById (“answer”). innerHTML = answer;
Nicely, earlier than the relevant libraries referring to each of the four strategies I've outlined above, there are a variety of issues to keep in thoughts.
Let's know the books we need to know in the subsequent few strains, as I stated, generally known as the Module Bundler (or Module Bundler). Properly, first, let's see what the bundle module actually means, and what we need to do is add modules to ourselves.
Bundling the modules is actually placing together the modules beneath their proper association and ultimately pushing the output right into a file.
Nicely, might you say what's the appropriate order? Suppose we have now a module that’s affiliated with Jquery. If we need to put modules collectively just in a file, we have to ensure that the Jquery itself is loaded first after which it’s the module we wrote and is affiliated with Jquery. If not, my code won’t work.
Why should we mix our modules and get an output file?
Why should there be an output file, why do not load any of the modules that we’d like in the browser. The reason being apparent as a result of each time you could have a tag You add to your page. In truth, you’re slowly decreasing your load. Now imagine that you’ve 15 javascript information and modules that ought to be loaded for a single web page. And as you combine it, you'll additionally reduce your file and truly have a zipper file on the output.
Let's introduce the library of the methods we outlined above.
For commonjs
For the bundle or combination or whatever you want, it uses the CommonJs to be probably the most recognized Browserify technique. Perhaps you've already heard his identify.
To work with Browserify, assume that you’ve a file named fundamental.js that insert a module and use it:
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
Nicely, here we’ve got just one dependency for Kodemon, and that's truly myDependency. If we need to bundle this file and its affiliation into a file, command:
browserify essential.js -in bundle.js
browserify foremost.js -in bundle.js
I execute this command and truly put the command most important.js file and all its dependencies in the right order inside a file named bundle.js.
The dangerous information is that Browserfy is long been unstoppable and truly lifeless. However many still use it. Anyway, this combiner like the remainder of the Bundlers has plugins that you need to use.
For AMD
To work with AMD can also be probably the most well-known RequireJs. Until I remembered, I might say one thing to AMD, as I mentioned at first, AMD masses the modules in a non-synchronous manner. Whatever three information we’ve got, it does not make much difference for us, as a result of they all begin downloading from the server, so in such a state of affairs, it does not imply that the so-called information ought to be bundled. That's why they’re not referred to AMD Bundler libraries and are often known as Module Loader.
In fact, in reality, we have now three information or could also be affected by our loading velocity, so RequireJS additionally has some ways to bundle the modules.
Till I keep in mind, I say that for ADM, there’s one other library referred to as almond, which is, in fact, lighter.
For ES6
Nicely, yes, what's our JavaScript technique for? One of many largest bundlers is rollup. Which is to say that it claims that the file you’re lastly delivering is smaller in measurement than the other Bundler modules. We've also launched a function referred to as tree-shaking, which we're talking about at the bottom. Roll-up works on a config file, an example of it right here. In case you take a look at the code one factor you are able to do is get issues executed:
“use strict”;
let babel = require (“rollup-plugin-babel”), commonjs = require (“rollup-plugin-commonjs”), npm = require (“rollup-plugin-npm”), uglify = require (“rollup-plugin-uglify”);
module.exports = entry: “./examples/index.js”, format: 'umd', dest: 'bundle.js' plugins: [ babel( exclude: “node_modules/**” ), npm( jsnext: true, foremost: true ), commonjs( embrace: ‘node_modules/**’ ), uglify() ] ;
1
2
three
four
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
“use strict”;
let babel = require (“rollup-plugin-babel”),
commonjs = require (“rollup-plugin-commonjs”),
npm = require (“rollup-plugin-npm”),
uglify = require (“rollup-plugin-uglify”);
module.exports =
entry: “./examples/index.js”,
format: 'umd',
dest: 'bundle.js'
plugins:[[[[
babel (
exclude: “node_modules / **”
)
npm (
jsnext: true
primary: true
)
commonjs (
embrace: 'node_modules / **'
)
uglify ()
]
;
We come to the hardest of them, from the online pack you need to use to combine CommonJs, AMD, and Es information. Webpack, like rollup, has its personal options, for example, introduces a very good code-splitting function, which we're talking about under. The idea of the web-based job is a config file that performs every part inside it. For instance, you’ll be able to see a web-based config file:
module.exports = context: __dirname + “/ src”, entry: “./entry” output: path: __dirname filename: “bundle.js” , module: loaders: [ check: /.js$/, loader: ‘babel-loader’, query: presets: [‘es2015’]
, check: /.espresso$/, loader: 'coffee-loader' , check: /.ts$/, loader: 'ts-loader' ]
1
2
three
4
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
module.exports =
context: __dirname + “/ src”,
entry: “./entry”
output:
path: __dirname
filename: “bundle.js”
,
module:
loaders:[[[[
check: /.js$/
loader: 'babel-loader'
query:
presets: [‘es2015’]
,
check: /.coffee$/,
loader: 'coffee-loader'
,
check: /.ts$/,
loader: 'ts-loader'
]
Properly, let's check out a few of the options we talked about however did not clarify.
Tree-shaking
See what this function is and what it's like. Suppose you’ve got a module referred to as util.js whose content is as follows:
export perform every (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <assortment.length; i ++) iterator (collection[i], i, collection); else for (var key in collection) iterator (collection[key], key, assortment);
export perform filter (assortment, check) can filtered = []; every (collection, perform (item) if (check (merchandise)) filtered.push (item); ); return filtered;
export perform map (assortment, iterator) can mapped = []; every (collection, perform (value, key, collection) mapped.push (iterator (worth)); ); return mapped;
1
2
3
four
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
26
27
28
29
export perform every (collection, iterator)
if (Array.isArray (collection))
for (var i = zero; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (assortment[key], key, assortment);
export perform filter (assortment, check)
can filtered = [];
every (assortment, perform (merchandise)
if (check (item))
filtered.push (item);
);
return filtered;
export perform map (collection, iterator)
can mapped = [];
each (assortment, perform (value, key, assortment)
mapped.push (iterator (value));
);
return mapped;
And now suppose you need to use this module from your app. So you employ it first to learn it and then use it:
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
But in reality, if we get the code above, we'll see that we solely use one part of our file and that it's just the perform of each one. So whether we need to get all the contents of the util.js file, We used a small part? The tree-shaking function provides us the power to routinely add the code we used (in reality). In truth, the shake model of the code we wrote above is as follows:
perform each (assortment, iterator) if (Array.isArray (collection)) for (var i = 0; i <assortment.length; i ++) iterator (assortment[i], i, assortment); else for (var key in assortment) iterator (collection[key], key, assortment); ;
every ([1, 2, 3], perform (x) console.log (x));
perform each (assortment, iterator)
if (Array.isArray (assortment))
for (var i = 0; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (collection[key], key, assortment);
;
each ([1, 2, 3], perform (x) console.log (x));
Not that we get the complete contents of the util.js file.
Code-splitting
So that you noticed in the previous part that it was automated detection. Actually, here we do the work manually. For example, think about the instance above that we simply need to use solely an util half. So suppose we also wrote the code we read, that is:
import * as utils from './utils.js';
Utils.each ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
As soon as again, as soon as we solely wanted this code on a page, we just name that code. That’s, every.bundle.js does not characterize your complete bundle.js. Now suppose we’ve got a web page that solely needs this file, so there's no have to name the whole utilities in that page. Properly, then we think about this file as a bundle. Then compile this file, for example, every file's identify is output, each.bundle.js and the compiled file, which is definitely the identical file, its content with its dependencies on this Face:
perform each (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <collection.size; i ++) iterator (assortment[i], i, assortment); else for (var key in collection) iterator (collection[key], key, collection); ;
each ([1, 2, 3], perform (x) console.log (x));
perform every (collection, iterator)
if (Array.isArray (assortment))
for (var i = zero; i <collection.length; i ++)
iterator (assortment[i], i, assortment);
else
for (var key in collection)
iterator (assortment[key], key, collection);
;
each ([1, 2, 3], perform (x) console.log (x));
One point: As I came upon, the online packer will later help tree-shaking, however if you want to use it now, you need to manually run a collection of configurations. For more info, you possibly can discuss with this hyperlink.
Properly, let's get the least loaded Module loader
With the GPS system, you can too write Commonjs, AMD, and Es6 syntax. The GIS system constructed on this repository is definitely a polyfill for the es6 modules, which we've just talked about above doesn’t help any browser right now. Examples embrace the GIS code:
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
Jspm
Nicely see what jspm is. The truth is, jspm is a bundle for systemjs, and in reality, from npm and github, the packages may be found. jspm isn’t just a package deal, and in reality, other issues can do for you. The truth is, you obtain packages for npm for the shopper aspect and offer you some additional features like bundling information.
The necessary thing about jspm is that it's made with regard to http2.
To read a superb article that compares the webpack with jspm, you possibly can discuss with this hyperlink.
So is that this all the best way to code modular code in javascript?
In fact, no, firstly, in fact, aside from what we launched, there are definitely different banners, however definitely not many and not very well known. Second, there’s another approach to write modules, and that's using transpilers in javascript. For more info, see Transspeeders here.
We’re working here with Typescript and Coffescript.
To work with Typescript, an example code:
import * as _ from 'lodash';
const double = (value: quantity) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
1
2
three
four
5
6
7
8
9
10
11
12
13
14
15
16
17
import * as _ from 'lodash';
const double = (value: number) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
The code for the Typescript module is similar to the es6 itself, and this is excellent.
The subsequent transshiper is Coffescript, the place you possibly can see the sample code for its module half:
sum = require 'lodash / sum'
double = (number) -> number * 2 addFive = (quantity) -> sum ([number, 5])
module.exports = double: double addFive: addFive
sum = require 'lodash / sum'
double = (quantity) -> quantity * 2
addFive = (number) -> sum ([number, 5])
module.exports =
double: double
addFive: addFive
Notice that once you use transpilers, you can’t do something like bundling. And most significantly, these bundles I've introduced have instruments that you should use with them inside (like Webpack) from Typescript.
Properly, hopefully this article has been edited for you. In the long run, I additionally supply some useful hyperlinks that you will discover out for more.
Lasso: An fascinating Bundler module!
Spittable: This one also helps the es6 and commonjs syntax in addition to code-splitting.
Steal.js: A loader module that additionally supports both widespread and AMD.
Hyperlink comparison file measurement of the output of the Bundler module collectively
A hyperlink to match the dimensions of the output file
Examine module code with totally different bundles
Persian tutorial webpack
The post Understanding Modular Programming in Javascript appeared first on Android Smart Gears.
Perhaps most of you’ve gotten heard over the course of the program, modular modules and programming, and understand the logic and use it. Nicely, when you're the one who's obtained this text, what's higher, but don’t worry in the event you're not. Here we are going to speak about module programming in javascript. For those who used this technique in other languages, you must know that because the JavaScript in common could be very totally different from the rest of the languages, that is totally different for the same program. Modular Writing may have a common story! In any case, let's take the headline.
Why modular programming
I feel that the answer to this query is just not very troublesome, any programming want to re-write the code that they could use several occasions, and just name it in totally different places. By sorting the program and using the code once more, Hamoon The program also has an incredible structure and extra handy programming. It additionally makes it easier to keep the code and the code is modified later. You would not have to vary every little thing in this system and alter the code. Then again, whenever you write in modular JavaScript, each module will get its personal particular scope, and it's nice that you do not fill the whole area of your program with variables in each module (or you will not be so soiled) that you simply'll end up with later. So let's go to the primary point Now it's time to see how we will write in modular javascript. There are a number of methods to do this, the primary query which may get in your mind is that we’ve got the module discussion in the ES6 and that's the case. However no, it's true that the module discussion has been launched to the eS, but nonetheless no modular programming browser helps JS. What’s our task now? We have now to use the libraries to do this, or use the Module Bundlers more accurately, to try this for us.
So, we need to do precisely what we do in the Nodus JS to the aspect of Clither, in order that we will require our file on the aspect of Clayt. Aha, we’ve not remembered. With no library, we will additionally write modulars and there are some methods to do that. Properly, I don’t advise, and it's actually a little bit of a multitude. When the libraries with a lot functionality do this, we come to ourselves. Let's face it.
So let's see how these libraries are divided. And which one is best to choose? With a view to write modular packages in your shopper aspect, you even have three CommonJS and AMD and ES6, each of which we explain (all of which are based mostly on certainly one of these three)
The two widespread ones, CommonJs and AMD, are generally referred to as Scriptloader. Notice that these aren’t a library and not just a normal or a way to standardize the code that writes the remaining.
Commonjs
The Comenius JS module is a javascript code that comes out of every module in the form of a code, and we will use it. The NodeGS itself additionally makes use of the Comenius GS, so when you work with Node, you possibly can easily study Widespread. Let's see the code from Widespread:
perform myModule () this.hey = perform () return 'hey!';
this.goodbye = perform () return 'goodbye!';
module.exports = myModule;
perform myModule ()
this.hey = perform ()
return 'hey!';
this.goodbye = perform ()
return 'goodbye!';
module.exports = myModule;
Nicely, now, if you wish to use this code, we'll have to use another one:
can myModule = require ('myModule');
can myModuleInstance = new myModule (); myModuleInstance.howdy (); // 'howdy!' myModuleInstance.goodbye (); // 'goodbye!'
can myModule = require ('myModule');
can myModuleInstance = new myModule ();
myModuleInstance.good day (); // 'howdy!'
myModuleInstance.goodbye (); // 'goodbye!'
Properly, that's the code for writing in javascript, as I stated, the scope of all the program might be stored clear, and it's a good suggestion that we will use our code in all places and never repeat it. Within the meantime, you see the code is straightforward.
The necessary factor about CommonJs is that it masses the modules simultaneously. That is, when you have three information, they may load in the order they have been written (one after the opposite)
AMD
Properly, now if you want to obtain the information on the similar time, what?
Will probably be your selection of AMD. Let's check out the code right here:
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule) console.log (myModule.hey ()); );
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule)
console.log (myModule.howdy ());
);
Properly, let's additionally say that many people don’t like this technique (in contrast to time) to load javascript information; as well as the CommonJs and es6 syntax is simpler to know.
Nicely, is it going to end right here and we shouldn’t have another selection?
UMD
For once you want each of you.
ES6
Properly, the final choice is es6. In fact, it's simply JavaScript. Which you in all probability heard too. For extra info on modules on es6, see this hyperlink.
Instance code syntax es6:
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ]; can reply = sum (values);
doc.getElementById (“answer”). innerHTML = reply;
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ];
can answer = sum (values);
document.getElementById (“answer”). innerHTML = answer;
Nicely, earlier than the relevant libraries referring to each of the four strategies I've outlined above, there are a variety of issues to keep in thoughts.
Let's know the books we need to know in the subsequent few strains, as I stated, generally known as the Module Bundler (or Module Bundler). Properly, first, let's see what the bundle module actually means, and what we need to do is add modules to ourselves.
Bundling the modules is actually placing together the modules beneath their proper association and ultimately pushing the output right into a file.
Nicely, might you say what's the appropriate order? Suppose we have now a module that’s affiliated with Jquery. If we need to put modules collectively just in a file, we have to ensure that the Jquery itself is loaded first after which it’s the module we wrote and is affiliated with Jquery. If not, my code won’t work.
Why should we mix our modules and get an output file?
Why should there be an output file, why do not load any of the modules that we’d like in the browser. The reason being apparent as a result of each time you could have a tag You add to your page. In truth, you’re slowly decreasing your load. Now imagine that you’ve 15 javascript information and modules that ought to be loaded for a single web page. And as you combine it, you'll additionally reduce your file and truly have a zipper file on the output.
Let's introduce the library of the methods we outlined above.
For commonjs
For the bundle or combination or whatever you want, it uses the CommonJs to be probably the most recognized Browserify technique. Perhaps you've already heard his identify.
To work with Browserify, assume that you’ve a file named fundamental.js that insert a module and use it:
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
Nicely, here we’ve got just one dependency for Kodemon, and that's truly myDependency. If we need to bundle this file and its affiliation into a file, command:
browserify essential.js -in bundle.js
browserify foremost.js -in bundle.js
I execute this command and truly put the command most important.js file and all its dependencies in the right order inside a file named bundle.js.
The dangerous information is that Browserfy is long been unstoppable and truly lifeless. However many still use it. Anyway, this combiner like the remainder of the Bundlers has plugins that you need to use.
For AMD
To work with AMD can also be probably the most well-known RequireJs. Until I remembered, I might say one thing to AMD, as I mentioned at first, AMD masses the modules in a non-synchronous manner. Whatever three information we’ve got, it does not make much difference for us, as a result of they all begin downloading from the server, so in such a state of affairs, it does not imply that the so-called information ought to be bundled. That's why they’re not referred to AMD Bundler libraries and are often known as Module Loader.
In fact, in reality, we have now three information or could also be affected by our loading velocity, so RequireJS additionally has some ways to bundle the modules.
Till I keep in mind, I say that for ADM, there’s one other library referred to as almond, which is, in fact, lighter.
For ES6
Nicely, yes, what's our JavaScript technique for? One of many largest bundlers is rollup. Which is to say that it claims that the file you’re lastly delivering is smaller in measurement than the other Bundler modules. We've also launched a function referred to as tree-shaking, which we're talking about at the bottom. Roll-up works on a config file, an example of it right here. In case you take a look at the code one factor you are able to do is get issues executed:
“use strict”;
let babel = require (“rollup-plugin-babel”), commonjs = require (“rollup-plugin-commonjs”), npm = require (“rollup-plugin-npm”), uglify = require (“rollup-plugin-uglify”);
module.exports = entry: “./examples/index.js”, format: 'umd', dest: 'bundle.js' plugins: [ babel( exclude: “node_modules/**” ), npm( jsnext: true, foremost: true ), commonjs( embrace: ‘node_modules/**’ ), uglify() ] ;
1
2
three
four
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
“use strict”;
let babel = require (“rollup-plugin-babel”),
commonjs = require (“rollup-plugin-commonjs”),
npm = require (“rollup-plugin-npm”),
uglify = require (“rollup-plugin-uglify”);
module.exports =
entry: “./examples/index.js”,
format: 'umd',
dest: 'bundle.js'
plugins:[[[[
babel (
exclude: “node_modules / **”
)
npm (
jsnext: true
primary: true
)
commonjs (
embrace: 'node_modules / **'
)
uglify ()
]
;
We come to the hardest of them, from the online pack you need to use to combine CommonJs, AMD, and Es information. Webpack, like rollup, has its personal options, for example, introduces a very good code-splitting function, which we're talking about under. The idea of the web-based job is a config file that performs every part inside it. For instance, you’ll be able to see a web-based config file:
module.exports = context: __dirname + “/ src”, entry: “./entry” output: path: __dirname filename: “bundle.js” , module: loaders: [ check: /.js$/, loader: ‘babel-loader’, query: presets: [‘es2015’]
, check: /.espresso$/, loader: 'coffee-loader' , check: /.ts$/, loader: 'ts-loader' ]
1
2
three
4
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
module.exports =
context: __dirname + “/ src”,
entry: “./entry”
output:
path: __dirname
filename: “bundle.js”
,
module:
loaders:[[[[
check: /.js$/
loader: 'babel-loader'
query:
presets: [‘es2015’]
,
check: /.coffee$/,
loader: 'coffee-loader'
,
check: /.ts$/,
loader: 'ts-loader'
]
Properly, let's check out a few of the options we talked about however did not clarify.
Tree-shaking
See what this function is and what it's like. Suppose you’ve got a module referred to as util.js whose content is as follows:
export perform every (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <assortment.length; i ++) iterator (collection[i], i, collection); else for (var key in collection) iterator (collection[key], key, assortment);
export perform filter (assortment, check) can filtered = []; every (collection, perform (item) if (check (merchandise)) filtered.push (item); ); return filtered;
export perform map (assortment, iterator) can mapped = []; every (collection, perform (value, key, collection) mapped.push (iterator (worth)); ); return mapped;
1
2
3
four
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
26
27
28
29
export perform every (collection, iterator)
if (Array.isArray (collection))
for (var i = zero; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (assortment[key], key, assortment);
export perform filter (assortment, check)
can filtered = [];
every (assortment, perform (merchandise)
if (check (item))
filtered.push (item);
);
return filtered;
export perform map (collection, iterator)
can mapped = [];
each (assortment, perform (value, key, assortment)
mapped.push (iterator (value));
);
return mapped;
And now suppose you need to use this module from your app. So you employ it first to learn it and then use it:
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
But in reality, if we get the code above, we'll see that we solely use one part of our file and that it's just the perform of each one. So whether we need to get all the contents of the util.js file, We used a small part? The tree-shaking function provides us the power to routinely add the code we used (in reality). In truth, the shake model of the code we wrote above is as follows:
perform each (assortment, iterator) if (Array.isArray (collection)) for (var i = 0; i <assortment.length; i ++) iterator (assortment[i], i, assortment); else for (var key in assortment) iterator (collection[key], key, assortment); ;
every ([1, 2, 3], perform (x) console.log (x));
perform each (assortment, iterator)
if (Array.isArray (assortment))
for (var i = 0; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (collection[key], key, assortment);
;
each ([1, 2, 3], perform (x) console.log (x));
Not that we get the complete contents of the util.js file.
Code-splitting
So that you noticed in the previous part that it was automated detection. Actually, here we do the work manually. For example, think about the instance above that we simply need to use solely an util half. So suppose we also wrote the code we read, that is:
import * as utils from './utils.js';
Utils.each ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
As soon as again, as soon as we solely wanted this code on a page, we just name that code. That’s, every.bundle.js does not characterize your complete bundle.js. Now suppose we’ve got a web page that solely needs this file, so there's no have to name the whole utilities in that page. Properly, then we think about this file as a bundle. Then compile this file, for example, every file's identify is output, each.bundle.js and the compiled file, which is definitely the identical file, its content with its dependencies on this Face:
perform each (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <collection.size; i ++) iterator (assortment[i], i, assortment); else for (var key in collection) iterator (collection[key], key, collection); ;
each ([1, 2, 3], perform (x) console.log (x));
perform every (collection, iterator)
if (Array.isArray (assortment))
for (var i = zero; i <collection.length; i ++)
iterator (assortment[i], i, assortment);
else
for (var key in collection)
iterator (assortment[key], key, collection);
;
each ([1, 2, 3], perform (x) console.log (x));
One point: As I came upon, the online packer will later help tree-shaking, however if you want to use it now, you need to manually run a collection of configurations. For more info, you possibly can discuss with this hyperlink.
Properly, let's get the least loaded Module loader
With the GPS system, you can too write Commonjs, AMD, and Es6 syntax. The GIS system constructed on this repository is definitely a polyfill for the es6 modules, which we've just talked about above doesn’t help any browser right now. Examples embrace the GIS code:
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
Jspm
Nicely see what jspm is. The truth is, jspm is a bundle for systemjs, and in reality, from npm and github, the packages may be found. jspm isn’t just a package deal, and in reality, other issues can do for you. The truth is, you obtain packages for npm for the shopper aspect and offer you some additional features like bundling information.
The necessary thing about jspm is that it's made with regard to http2.
To read a superb article that compares the webpack with jspm, you possibly can discuss with this hyperlink.
So is that this all the best way to code modular code in javascript?
In fact, no, firstly, in fact, aside from what we launched, there are definitely different banners, however definitely not many and not very well known. Second, there’s another approach to write modules, and that's using transpilers in javascript. For more info, see Transspeeders here.
We’re working here with Typescript and Coffescript.
To work with Typescript, an example code:
import * as _ from 'lodash';
const double = (value: quantity) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
1
2
three
four
5
6
7
8
9
10
11
12
13
14
15
16
17
import * as _ from 'lodash';
const double = (value: number) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
The code for the Typescript module is similar to the es6 itself, and this is excellent.
The subsequent transshiper is Coffescript, the place you possibly can see the sample code for its module half:
sum = require 'lodash / sum'
double = (number) -> number * 2 addFive = (quantity) -> sum ([number, 5])
module.exports = double: double addFive: addFive
sum = require 'lodash / sum'
double = (quantity) -> quantity * 2
addFive = (number) -> sum ([number, 5])
module.exports =
double: double
addFive: addFive
Notice that once you use transpilers, you can’t do something like bundling. And most significantly, these bundles I've introduced have instruments that you should use with them inside (like Webpack) from Typescript.
Properly, hopefully this article has been edited for you. In the long run, I additionally supply some useful hyperlinks that you will discover out for more.
Lasso: An fascinating Bundler module!
Spittable: This one also helps the es6 and commonjs syntax in addition to code-splitting.
Steal.js: A loader module that additionally supports both widespread and AMD.
Hyperlink comparison file measurement of the output of the Bundler module collectively
A hyperlink to match the dimensions of the output file
Examine module code with totally different bundles
Persian tutorial webpack
The post Understanding Modular Programming in Javascript appeared first on Android Smart Gears.
Perhaps most of you’ve gotten heard over the course of the program, modular modules and programming, and understand the logic and use it. Nicely, when you're the one who's obtained this text, what's higher, but don’t worry in the event you're not. Here we are going to speak about module programming in javascript. For those who used this technique in other languages, you must know that because the JavaScript in common could be very totally different from the rest of the languages, that is totally different for the same program. Modular Writing may have a common story! In any case, let's take the headline.
Why modular programming
I feel that the answer to this query is just not very troublesome, any programming want to re-write the code that they could use several occasions, and just name it in totally different places. By sorting the program and using the code once more, Hamoon The program also has an incredible structure and extra handy programming. It additionally makes it easier to keep the code and the code is modified later. You would not have to vary every little thing in this system and alter the code. Then again, whenever you write in modular JavaScript, each module will get its personal particular scope, and it's nice that you do not fill the whole area of your program with variables in each module (or you will not be so soiled) that you simply'll end up with later. So let's go to the primary point Now it's time to see how we will write in modular javascript. There are a number of methods to do this, the primary query which may get in your mind is that we’ve got the module discussion in the ES6 and that's the case. However no, it's true that the module discussion has been launched to the eS, but nonetheless no modular programming browser helps JS. What’s our task now? We have now to use the libraries to do this, or use the Module Bundlers more accurately, to try this for us.
So, we need to do precisely what we do in the Nodus JS to the aspect of Clither, in order that we will require our file on the aspect of Clayt. Aha, we’ve not remembered. With no library, we will additionally write modulars and there are some methods to do that. Properly, I don’t advise, and it's actually a little bit of a multitude. When the libraries with a lot functionality do this, we come to ourselves. Let's face it.
So let's see how these libraries are divided. And which one is best to choose? With a view to write modular packages in your shopper aspect, you even have three CommonJS and AMD and ES6, each of which we explain (all of which are based mostly on certainly one of these three)
The two widespread ones, CommonJs and AMD, are generally referred to as Scriptloader. Notice that these aren’t a library and not just a normal or a way to standardize the code that writes the remaining.
Commonjs
The Comenius JS module is a javascript code that comes out of every module in the form of a code, and we will use it. The NodeGS itself additionally makes use of the Comenius GS, so when you work with Node, you possibly can easily study Widespread. Let's see the code from Widespread:
perform myModule () this.hey = perform () return 'hey!';
this.goodbye = perform () return 'goodbye!';
module.exports = myModule;
perform myModule ()
this.hey = perform ()
return 'hey!';
this.goodbye = perform ()
return 'goodbye!';
module.exports = myModule;
Nicely, now, if you wish to use this code, we'll have to use another one:
can myModule = require ('myModule');
can myModuleInstance = new myModule (); myModuleInstance.howdy (); // 'howdy!' myModuleInstance.goodbye (); // 'goodbye!'
can myModule = require ('myModule');
can myModuleInstance = new myModule ();
myModuleInstance.good day (); // 'howdy!'
myModuleInstance.goodbye (); // 'goodbye!'
Properly, that's the code for writing in javascript, as I stated, the scope of all the program might be stored clear, and it's a good suggestion that we will use our code in all places and never repeat it. Within the meantime, you see the code is straightforward.
The necessary factor about CommonJs is that it masses the modules simultaneously. That is, when you have three information, they may load in the order they have been written (one after the opposite)
AMD
Properly, now if you want to obtain the information on the similar time, what?
Will probably be your selection of AMD. Let's check out the code right here:
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule) console.log (myModule.hey ()); );
outline ([‘myModule’, ‘myOtherModule’], perform (myModule, myOtherModule)
console.log (myModule.howdy ());
);
Properly, let's additionally say that many people don’t like this technique (in contrast to time) to load javascript information; as well as the CommonJs and es6 syntax is simpler to know.
Nicely, is it going to end right here and we shouldn’t have another selection?
UMD
For once you want each of you.
ES6
Properly, the final choice is es6. In fact, it's simply JavaScript. Which you in all probability heard too. For extra info on modules on es6, see this hyperlink.
Instance code syntax es6:
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ]; can reply = sum (values);
doc.getElementById (“answer”). innerHTML = reply;
import sum from “./sum”;
var values = [ 1, 2, 4, 5, 6, 7, 8, 9 ];
can answer = sum (values);
document.getElementById (“answer”). innerHTML = answer;
Nicely, earlier than the relevant libraries referring to each of the four strategies I've outlined above, there are a variety of issues to keep in thoughts.
Let's know the books we need to know in the subsequent few strains, as I stated, generally known as the Module Bundler (or Module Bundler). Properly, first, let's see what the bundle module actually means, and what we need to do is add modules to ourselves.
Bundling the modules is actually placing together the modules beneath their proper association and ultimately pushing the output right into a file.
Nicely, might you say what's the appropriate order? Suppose we have now a module that’s affiliated with Jquery. If we need to put modules collectively just in a file, we have to ensure that the Jquery itself is loaded first after which it’s the module we wrote and is affiliated with Jquery. If not, my code won’t work.
Why should we mix our modules and get an output file?
Why should there be an output file, why do not load any of the modules that we’d like in the browser. The reason being apparent as a result of each time you could have a tag You add to your page. In truth, you’re slowly decreasing your load. Now imagine that you’ve 15 javascript information and modules that ought to be loaded for a single web page. And as you combine it, you'll additionally reduce your file and truly have a zipper file on the output.
Let's introduce the library of the methods we outlined above.
For commonjs
For the bundle or combination or whatever you want, it uses the CommonJs to be probably the most recognized Browserify technique. Perhaps you've already heard his identify.
To work with Browserify, assume that you’ve a file named fundamental.js that insert a module and use it:
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
can myDependency = require ('myDependency');
can myGrades = [93, 95, 88, 0, 91];
can myAverageGrade = myDependency.average (myGrades);
Nicely, here we’ve got just one dependency for Kodemon, and that's truly myDependency. If we need to bundle this file and its affiliation into a file, command:
browserify essential.js -in bundle.js
browserify foremost.js -in bundle.js
I execute this command and truly put the command most important.js file and all its dependencies in the right order inside a file named bundle.js.
The dangerous information is that Browserfy is long been unstoppable and truly lifeless. However many still use it. Anyway, this combiner like the remainder of the Bundlers has plugins that you need to use.
For AMD
To work with AMD can also be probably the most well-known RequireJs. Until I remembered, I might say one thing to AMD, as I mentioned at first, AMD masses the modules in a non-synchronous manner. Whatever three information we’ve got, it does not make much difference for us, as a result of they all begin downloading from the server, so in such a state of affairs, it does not imply that the so-called information ought to be bundled. That's why they’re not referred to AMD Bundler libraries and are often known as Module Loader.
In fact, in reality, we have now three information or could also be affected by our loading velocity, so RequireJS additionally has some ways to bundle the modules.
Till I keep in mind, I say that for ADM, there’s one other library referred to as almond, which is, in fact, lighter.
For ES6
Nicely, yes, what's our JavaScript technique for? One of many largest bundlers is rollup. Which is to say that it claims that the file you’re lastly delivering is smaller in measurement than the other Bundler modules. We've also launched a function referred to as tree-shaking, which we're talking about at the bottom. Roll-up works on a config file, an example of it right here. In case you take a look at the code one factor you are able to do is get issues executed:
“use strict”;
let babel = require (“rollup-plugin-babel”), commonjs = require (“rollup-plugin-commonjs”), npm = require (“rollup-plugin-npm”), uglify = require (“rollup-plugin-uglify”);
module.exports = entry: “./examples/index.js”, format: 'umd', dest: 'bundle.js' plugins: [ babel( exclude: “node_modules/**” ), npm( jsnext: true, foremost: true ), commonjs( embrace: ‘node_modules/**’ ), uglify() ] ;
1
2
three
four
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
“use strict”;
let babel = require (“rollup-plugin-babel”),
commonjs = require (“rollup-plugin-commonjs”),
npm = require (“rollup-plugin-npm”),
uglify = require (“rollup-plugin-uglify”);
module.exports =
entry: “./examples/index.js”,
format: 'umd',
dest: 'bundle.js'
plugins:[[[[
babel (
exclude: “node_modules / **”
)
npm (
jsnext: true
primary: true
)
commonjs (
embrace: 'node_modules / **'
)
uglify ()
]
;
We come to the hardest of them, from the online pack you need to use to combine CommonJs, AMD, and Es information. Webpack, like rollup, has its personal options, for example, introduces a very good code-splitting function, which we're talking about under. The idea of the web-based job is a config file that performs every part inside it. For instance, you’ll be able to see a web-based config file:
module.exports = context: __dirname + “/ src”, entry: “./entry” output: path: __dirname filename: “bundle.js” , module: loaders: [ check: /.js$/, loader: ‘babel-loader’, query: presets: [‘es2015’]
, check: /.espresso$/, loader: 'coffee-loader' , check: /.ts$/, loader: 'ts-loader' ]
1
2
three
4
5
6
7
eight
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
module.exports =
context: __dirname + “/ src”,
entry: “./entry”
output:
path: __dirname
filename: “bundle.js”
,
module:
loaders:[[[[
check: /.js$/
loader: 'babel-loader'
query:
presets: [‘es2015’]
,
check: /.coffee$/,
loader: 'coffee-loader'
,
check: /.ts$/,
loader: 'ts-loader'
]
Properly, let's check out a few of the options we talked about however did not clarify.
Tree-shaking
See what this function is and what it's like. Suppose you’ve got a module referred to as util.js whose content is as follows:
export perform every (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <assortment.length; i ++) iterator (collection[i], i, collection); else for (var key in collection) iterator (collection[key], key, assortment);
export perform filter (assortment, check) can filtered = []; every (collection, perform (item) if (check (merchandise)) filtered.push (item); ); return filtered;
export perform map (assortment, iterator) can mapped = []; every (collection, perform (value, key, collection) mapped.push (iterator (worth)); ); return mapped;
1
2
3
four
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19th
20
21
22
23
24
25
26
27
28
29
export perform every (collection, iterator)
if (Array.isArray (collection))
for (var i = zero; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (assortment[key], key, assortment);
export perform filter (assortment, check)
can filtered = [];
every (assortment, perform (merchandise)
if (check (item))
filtered.push (item);
);
return filtered;
export perform map (collection, iterator)
can mapped = [];
each (assortment, perform (value, key, assortment)
mapped.push (iterator (value));
);
return mapped;
And now suppose you need to use this module from your app. So you employ it first to learn it and then use it:
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
But in reality, if we get the code above, we'll see that we solely use one part of our file and that it's just the perform of each one. So whether we need to get all the contents of the util.js file, We used a small part? The tree-shaking function provides us the power to routinely add the code we used (in reality). In truth, the shake model of the code we wrote above is as follows:
perform each (assortment, iterator) if (Array.isArray (collection)) for (var i = 0; i <assortment.length; i ++) iterator (assortment[i], i, assortment); else for (var key in assortment) iterator (collection[key], key, assortment); ;
every ([1, 2, 3], perform (x) console.log (x));
perform each (assortment, iterator)
if (Array.isArray (assortment))
for (var i = 0; i <collection.size; i ++)
iterator (assortment[i], i, collection);
else
for (var key in collection)
iterator (collection[key], key, assortment);
;
each ([1, 2, 3], perform (x) console.log (x));
Not that we get the complete contents of the util.js file.
Code-splitting
So that you noticed in the previous part that it was automated detection. Actually, here we do the work manually. For example, think about the instance above that we simply need to use solely an util half. So suppose we also wrote the code we read, that is:
import * as utils from './utils.js';
Utils.each ([1, 2, 3], perform (x) console.log (x));
import * as utils from './utils.js';
Utils.every ([1, 2, 3], perform (x) console.log (x));
As soon as again, as soon as we solely wanted this code on a page, we just name that code. That’s, every.bundle.js does not characterize your complete bundle.js. Now suppose we’ve got a web page that solely needs this file, so there's no have to name the whole utilities in that page. Properly, then we think about this file as a bundle. Then compile this file, for example, every file's identify is output, each.bundle.js and the compiled file, which is definitely the identical file, its content with its dependencies on this Face:
perform each (collection, iterator) if (Array.isArray (assortment)) for (var i = zero; i <collection.size; i ++) iterator (assortment[i], i, assortment); else for (var key in collection) iterator (collection[key], key, collection); ;
each ([1, 2, 3], perform (x) console.log (x));
perform every (collection, iterator)
if (Array.isArray (assortment))
for (var i = zero; i <collection.length; i ++)
iterator (assortment[i], i, assortment);
else
for (var key in collection)
iterator (assortment[key], key, collection);
;
each ([1, 2, 3], perform (x) console.log (x));
One point: As I came upon, the online packer will later help tree-shaking, however if you want to use it now, you need to manually run a collection of configurations. For more info, you possibly can discuss with this hyperlink.
Properly, let's get the least loaded Module loader
With the GPS system, you can too write Commonjs, AMD, and Es6 syntax. The GIS system constructed on this repository is definitely a polyfill for the es6 modules, which we've just talked about above doesn’t help any browser right now. Examples embrace the GIS code:
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
System.import ('./ src / codeFile.js'). Then (perform (m)
// do one thing with 'm'
);
Jspm
Nicely see what jspm is. The truth is, jspm is a bundle for systemjs, and in reality, from npm and github, the packages may be found. jspm isn’t just a package deal, and in reality, other issues can do for you. The truth is, you obtain packages for npm for the shopper aspect and offer you some additional features like bundling information.
The necessary thing about jspm is that it's made with regard to http2.
To read a superb article that compares the webpack with jspm, you possibly can discuss with this hyperlink.
So is that this all the best way to code modular code in javascript?
In fact, no, firstly, in fact, aside from what we launched, there are definitely different banners, however definitely not many and not very well known. Second, there’s another approach to write modules, and that's using transpilers in javascript. For more info, see Transspeeders here.
We’re working here with Typescript and Coffescript.
To work with Typescript, an example code:
import * as _ from 'lodash';
const double = (value: quantity) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
1
2
three
four
5
6
7
8
9
10
11
12
13
14
15
16
17
import * as _ from 'lodash';
const double = (value: number) => value * 2
const addFive = (worth: quantity) => _.sum ([value, 5])
export =
double
addFive
The code for the Typescript module is similar to the es6 itself, and this is excellent.
The subsequent transshiper is Coffescript, the place you possibly can see the sample code for its module half:
sum = require 'lodash / sum'
double = (number) -> number * 2 addFive = (quantity) -> sum ([number, 5])
module.exports = double: double addFive: addFive
sum = require 'lodash / sum'
double = (quantity) -> quantity * 2
addFive = (number) -> sum ([number, 5])
module.exports =
double: double
addFive: addFive
Notice that once you use transpilers, you can’t do something like bundling. And most significantly, these bundles I've introduced have instruments that you should use with them inside (like Webpack) from Typescript.
Properly, hopefully this article has been edited for you. In the long run, I additionally supply some useful hyperlinks that you will discover out for more.
Lasso: An fascinating Bundler module!
Spittable: This one also helps the es6 and commonjs syntax in addition to code-splitting.
Steal.js: A loader module that additionally supports both widespread and AMD.
Hyperlink comparison file measurement of the output of the Bundler module collectively
A hyperlink to match the dimensions of the output file
Examine module code with totally different bundles
Persian tutorial webpack
The post Understanding Modular Programming in Javascript appeared first on Android Smart Gears.
Hangout: Vue.js, Browserify e Elixir Vue.js, Browserify e Elixir com Fabio Vedovelli source

Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
Free to watch • No registration required • HD streaming
Node.js: browserify の復習
browserify の復習。ES2015 とそれ以降の標準をまとめて導入できる babel-preset-env、 コードを圧縮してくれるbabel-preset-babili を一緒に導入する。
yarn add --dev browserify babelify watchify yarn add --dev babel-preset-env babel-preset-babili
トランスパイルの実行は次のとおり。
browserify app.js -o bundle.js -t [ babelify --presets [ env babili ] ]
watchify による監視は次のとおり。
watchify -v -d app.js -o bundle.js -t [ babelify --presets [ env babili ] ]
Building JavaScript apps with ES6 and node.js
Just recently I collated and arranged all my frontend tricks of the trade when it comes to the sneaky build process. Modern frontend development requires a range of transpiling, pre and post processing, and organising various assets into a honed collection of files that one can distribute.
Over the few years I’ve been doing more node-based JavaScript development, there have been a few tools that have been indispensable for my dev work:
Gulp: the node.js task runner
Browserify: package and bundle modular JS code into a single file
Browsersync: lightweight web server that you can stream/reload individual files to while you develop
Babel: write in ES6 style JS
LESS (and SCSS): extended CSS features
and now recently using Jest for testing
Recently a colleague asked for advice about devising a build process for his rather complex app. I looked at all my different permutations of build processes, filtered out all the superfluous stuff and streamlined all the necessaries to put it into a nice template package that I can start any new project with. You can now check that out on github.
I'm a big fan of modular code, so I tried to make the build template as modular as possible, which means that you can easily use only what you need.
This is an issue that I’ve seen a lot of confusion over, and even seasoned JavaScript developers might have missed some of its subtleties. So I thought it was worth a short tutorial.
Rollup and process.env.BROWSER
Basic example
“browser” field, how I love thee
If you search through the Browserify or Webpack documentation for tips on how to solve this problem, you may eventually discover node-browser-resolve. This is a specification for a "browser" field inside of package.json, which can be used to define modules that should be swapped out when building for the browser.
Using this technique, we can add the following to our package.json:
{ /* ... */ "browser": { "./index.js": "./browser.js" } }
And then separate the functions into two different files, index.js and browser.js:
// index.js module.exports = function (string) { return Buffer.from(string, 'binary').toString('base64'); };
// browser.js module.exports = function (string) { return btoa(string); };
After this fix, Browserify and Webpack provide much more reasonable bundles: Browserify’s is 511 bytes minified (315 min+gz), and Webpack’s is 550 bytes minified (297 min+gz).
Advanced techniques
The direct "browser" method works well, but for larger projects I find that it introduces an awkward coupling between package.json and the codebase. For instance, our package.json could quickly end up looking like this: