require("./_.js");
expression. You can also use public modules from npm. For Browserify, it’s easy to create source maps (pre-compression source file maps), so even despite the concatenation, you can debug individual parts of the package exactly the same way you used to do it with separate files.require()
(after checking that they are installed via npm) and that's it! You can also use such popular JavaScript libraries as jQuery , Underscore , Backbone, and even Angular (the unofficial port). They are all available for download and work through npm. If you are working on a site that already uses a node, you simply simplify your development, because now you can use the common architecture of all your JS scripts. I really like this approach! npm install -g browserify
-g
flag tells npm to install the module globally). If you end up with a problem similar to the following: Error: EACCES, mkdir '/usr/local/lib/node_modules/browserify'
main.js
and put it in the js
folder._
variable under Underscore using the Browserify ' require()
method: var _ = require('underscore');
each()
and find()
functions from the Underscore library we have connected. We will search in two arrays of names and at each iteration we will output the value of the superman search condition expression to the console using console.log
. Lex Luther can only dream about it. Our final version of the code will look something like this: var _ = require('underscore'), names = ['Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent'], otherNames = ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen']; _.each([names, otherNames], function(nameGroup) { findSuperman(nameGroup); }); function findSuperman(values) { _.find(values, function(name) { if (name === 'Clark Kent') { console.log('It\'s Superman!'); } else { console.log('... No superman!'); } }); }
npm install underscore
node_modules
directory in the project folder. In this directory will be your Underscore module. The team will get the latest version of Underscore from the npm repository ( https://registry.npmjs.org/underscore ). With this module in our node_modules
directory, Browserify can easily find and use it.findem.js
. I run the command from the project root folder: browserify js/main.js -o js/findem.js -d
findem.js
(of course, including the package dependencies. Note. ), Which was specified using the option- -o
. I also included the -d
option in the request, so our team also generates a source map for our file. Thanks to this, we can debug underscore
and main.js
as two separate files. <script src="js/findem.js"></script>
node_modules
folders. To connect your own javascript code, you can use the same approach with the require()
function. This line of code imports the JS file named your_module.js
into the variable greatestModuleEver
: greatestModuleEver = require('./your_module.js');
module.exports
. One way to do this is shown below. module.exports = function(vars) { // }
module.exports = function() { return ['Barry Allen', 'Hal Jordan', 'Kara Kent', 'Diana Prince', 'Ray Palmer', 'Oliver Queen', 'Bruce Wayne', 'Wally West', 'John Jones', 'Kyle Rayner', 'Arthur Curry', 'Clark Kent']; }
names = require("./names.js");
: var _ = require('underscore'), names = require('./names.js'); findSuperman(names()); function findSuperman(values) { _.find(values, function(name) { if (name === 'Clark Kent') { console.log('It\'s Superman!'); } else { console.log('... No superman!'); } }); }
names
variable refers to the exported function from the module. Therefore, we use the names
variable as a function to pass the array we need to the findSuperman()
function.browserify
command again to compile the code, and then open it in your browser. It should work, as expected, by iterating each value of the array and logging whether the person from the list of names is a superman or not:findSuperman()
function into a separate module. Thus, we can theoretically find a superman from different parts of our application, and we can always replace our superman search module with a more efficient one if it is needed in the future.module.exports
), and to illustrate this, we will create a file findsuperman.js
, which will take an array of names: module.exports = function (values) { var foundSuperman = false; _.find(values, function(name) { if (name === 'Clark Kent') { console.log('It\'s Superman!'); foundSuperman = true; } else { console.log('... No superman!'); } }); return foundSuperman; }
findSuperman()
function. If she finds a superman, returns true
; otherwise, false
. And then let them decide the code that calls this module. But somehow, we missed one thing: our code uses Underscore, but we did not declare it. After adding Underscore, our code took the form: var _ = require('underscore'); module.exports = function (values) { ... }
findsuperman.js
, but when Browserify puts it all together, it will add it only once. Cool, is not it?true/false
. In the case of our example, we simply use document.write
to say if we could find superman in the list of names, or not: var _ = require('underscore'), names = require('./names.js'), findSuperman = require('./findsuperman.js'); if (findSuperman(names())) { document.write('We found Superman'); } else { document.write('No Superman...'); }
findsuperman.js
file.package.json
package.json
in the root directory of our project. This file gives your project a name (make sure there are no spaces), a description, it sets the author and version, and, most importantly, npm dependencies. Those who have already encountered the development of a node - we use the same mechanism: { "name": "FindSuperman", "version": "0.0.1", "author": "Patrick Catanzariti", "description": "Code designed to find the elusive red blue blur", "dependencies": { "underscore": "1.6.x" }, "devDependencies": { "browserify": "latest" } }
"underscore": "1.6.x"
, where the first part of the dependency is the name and the second is the version. "lastest"
or "*"
will allow you to get the latest version that is in npm. Also, you can specify the version number as 1.6
(fixed number) or 1.6.
(for versions from 1.6.0
to 1.7
, not inclusive).findsuperman.js
file without the need to run a Browserify. But we will connect it as one of the devDependencies
- modules needed by developers to support the application.package.json
file, we don’t have to force our friend to run npm install underscore
. It can just run npm install
and all the necessary dependencies will be installed in the node_modules
directory.browserify
command every time is insanely tedious, not to mention that it is wildly uncomfortable Fortunately, there are several ways to automate browsing.scripts
section in package.json
. This is done as follows: "scripts": { "build-js": "browserify js/main.js > js/findem.js" }
npm run build-js
package.json
to devDependencies
, and we will not forget to write an additional line in the scripts
section (it will directly launch our watchify
module, if rebuilding of packages is not required). "devDependencies": { "browserify": "latest", "watchify": "latest" }, "scripts": { "build-js": "browserify js/main.js > js/findem.js", "watch-js": "watchify js/main.js -o js/findem.js" }
npm run watch-js
-v
flag to your watchify command: "watch-js": "watchify js/main.js -o js/findem.js -v"
121104 bytes written to js/findem.js (0.26 seconds) 121119 bytes written to js/findem.js (0.03 seconds)
-d
after your browserify
(or watchify
) watchify
: "scripts": { "build-js": "browserify js/main.js > js/findem.js -d", "watch-js": "watchify js/main.js -o js/findem.js -d" }
"watch-js": "watchify js/main.js -o js/findem.js -dv"
package.json
by adding a directive to connect Grunt there. We will no longer use the scripts
section, this will shift to Grunt's shoulders: { "name": "FindSuperman", "version": "0.0.1", "author": "Patrick Catanzariti", "description": "Code designed to find the elusive red blue blur", "dependencies": { "underscore": "1.6.x" }, "devDependencies": { "browserify": "latest", "grunt": "~0.4.0", "grunt-browserify": "latest", "grunt-contrib-watch": "latest" } }
Gruntfile.js
and put it at the root of the project. Inside this Gruntfile.js
we should write the following: module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-browserify'); grunt.registerTask('default', ['browserify', 'watch']); grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), browserify: { main: { src: 'js/main.js', dest: 'js/findem.js' } }, watch: { files: 'js/*', tasks: ['default'] } }); }
package.json
: grunt.loadNpmTasks('grunt-contrib-watch'); grunt.loadNpmTasks('grunt-browserify');
browserify
and watch
): grunt.registerTask('default', ['browserify', 'watch']);
initConfig
: grunt.initConfig({
package.json
: pkg: grunt.file.readJSON('package.json'),
browserify: { main: { src: 'js/main.js', dest: 'js/findem.js' } }
watch
task for detecting changes in the oodode> directory: watch: { files: 'js/*', tasks: ['default'] }
devDependencies
), we need to first run npm install
. You will have to run this command only once, in the future you will enjoy the benefits of Grunt.grunt-browserify
package, the source maps generation process has changed somewhat, since The Internet was full of wrong decisions. Now, to make Grunt and Browserify properly create a source map, simply add the debug: true
flag to the bundleOptions
: browserify: { main: { options: { bundleOptions: { debug: true } }, src: 'js/main.js', dest: 'js/findem.js' } }
npm build
. npm install -g gulp
package.json
to connect a couple of new devDependencies
that we need: "devDependencies": { "browserify": "latest", "watchify": "latest", "gulp": "3.7.0", "vinyl-source-stream": "latest" }
npm
gulp-browserify
plugin, but Browserify itself prefers to work with it through its streaming API. We use vinyl-source-stream
to get the Browserify output and save it to a file using Gulp.gulpfile.js
file in the root of our project. All Gulp's functionality will be here: var browserify = require('browserify'), watchify = require('watchify'), gulp = require('gulp'), source = require('vinyl-source-stream'), sourceFile = './js/main.js', destFolder = './js/', destFile = 'findem.js'; gulp.task('browserify', function() { return browserify(sourceFile) .bundle() .pipe(source(destFile)) .pipe(gulp.dest(destFolder)); }); gulp.task('watch', function() { var bundler = watchify(sourceFile); bundler.on('update', rebundle); function rebundle() { return bundler.bundle() .pipe(source(destFile)) .pipe(gulp.dest(destFolder)); } return rebundle(); });
gulp.task('default', ['browserify', 'watch']);
sourceFile
- the location and name of the source Browserify file (in our case js/main.js
)destFolder
- the location of the final filedestFile
is the name we want to give our final filegulpfile.js
is “browserify”: gulp.task('browserify', function() {
main.js
to the npm Browserify package: return browserify(sourceFile)
.bundle()
findem.js
and save it in the js
directory. .pipe(source(destFile)) .pipe(gulp.dest(destFolder));
watchify
. gulp.task('watch', function() {
bundler
variable (since we will use it twice): var bundler = watchify(sourceFile);
rebundle
function each time the update
event is raised. , , , rebundle()
: bundler.on('update', rebundle);
rebundle()
? , Browserify: function rebundle() { return bundler.bundle() .pipe(source(destFile)) .pipe(gulp.dest(destFolder)); }; return rebundle(); });
browserify
watchify
, , . , gulpfile.js .gulpfile.js
, ( , grunt). gulp.task('default', ['browserify', 'watch']);
gulp
browserify
-, . gulp browserify
gulp watch
{ debug: true }
bundle()
.browserify
- : gulp.task('browserify', function() { return browserify(sourceFile) .bundle({debug:true}) .pipe(source(destFile)) .pipe(gulp.dest(destFolder)); });
rebundle()
watch
- : function rebundle() { return bundler.bundle({debug:true}) .pipe(source(destFile)) .pipe(gulp.dest(destFolder)); }
Source: https://habr.com/ru/post/224825/
All Articles