【browserify】Teach you how to learn browserify step by step

https://www.cnblogs.com/fsg6/p/13139627.html
insert image description here

Browserify

The official website of browserify is http://browserify.org/ , its purpose is to package many resources (css, img, js,...) used in the front end into a js file technology.

For example, when referencing external resources in html, we might write like this

  <script src="/static/libs/landing/js/bootstrap.min.js"></script>
  <script src="/static/libs/landing/js/jquery.flexslider-min.js"></script>
  <script src="/static/libs/landing/js/jquery.nav.js"></script>
  <script src="/static/libs/landing/js/jquery.appear.js"></script>
  <script src="/static/libs/landing/js/headhesive.min.js"></script>
  <script src="/static/libs/jquery/jquery-qrcode/jquery.qrcode.js"></script>
  <script src="/static/libs/jquery/jquery-qrcode/qrcode.js"></script>
  <script src="/static/libs/landing/js/scripts.js"></script>

But with the help of browserify, all these can be compressed into one sentence

<script src="/bundle.js"></script>

And don't worry about conflicts with libraries such as jQuery or underscore.

Although this technology has only become popular in recent years, it has quickly become popular in the front-end field. Another similar browserify is webpack.

Installation It
is very simple to install, but first you need to install nodejs first.

npm install -g browserify

With browserify you can use the require, module.exports functions commonly used in nodejs.

easy entry

Let's take a very simple example.

First create a hello.js file with the following content

module.exports = 'Hello world';

Then create an entry.js file with the content

var msg = require('./hello.js')console.log("MSG:", msg);

Finally, use browserify to package

browserify entry.js -o bundle.js

Then entry.js and hello.js are packaged into a bundle.js file.

Write a simple index.html to verify the effect

<!DOCTYPE html><html>
    <head>
        <meta charset="utf-8" />
        <title>index</title>
    </head>
    <body>
        <script src="bundle.js"></script>
    </body></html>

Then open the file with a browser, and press F12 to enable the debug option. You should see a MSG: Hello world printed out.

This is the simplest packaged application.

Guess you like

Origin blog.csdn.net/qq_39900031/article/details/130376484
Recommended