Table of Contents
Bower
Browserify
 Component
Duo
Home Web Front-end HTML Tutorial Introduction to front-end module manager_html/css_WEB-ITnose

Introduction to front-end module manager_html/css_WEB-ITnose

Jun 24, 2016 am 11:53 AM

Modular structure has become the mainstream of website development.

The main task of making a website is no longer writing various functions yourself, but how to combine various different modules together.

The browser itself does not provide a module management mechanism. In order to call each module, sometimes a lot of script tags have to be added to the web page. This makes the web page bloated, difficult to maintain, and generates a large number of HTTP requests, which slows down the display speed and affects the user experience.

In order to solve this problem, the front-end module manager (package management) came into being. It can easily manage the dependencies of various JavaScript scripts and automatically load each module, making the web page structure clear and reasonable. It is no exaggeration to say that all front-end JavaScript projects in the future should be developed in this way.

The earliest and most famous front-end module manager is none other than RequireJS. It uses AMD format and loads various modules asynchronously. For specific usage, please refer to the tutorial I wrote. The problem with Require.js is that the various parameter settings are too cumbersome, difficult to learn, and difficult to fully master. Moreover, in actual applications, it is often necessary to merge all modules on the server side and then load them uniformly, which adds a lot of workload.

Today, I introduce four other front-end module managers: Bower, Browserify, Component and Duo. Each of them has distinctive characteristics, which makes up for the shortcomings of Require.js very well, and is a powerful tool for front-end development.

It should be noted that this article is not a tutorial for these four module managers. I just want to use the simplest examples to explain what they are used for, so that readers can have a general impression and know that there are specific tools that can complete a certain kind of work. For detailed usage, please refer to their respective documentation.

Bower

The main function of Bower is to provide a unified and maintainable management model for the installation, upgrade and deletion of modules.

First, install Bower.

  $ npm install -g bower
Copy after login

Then, use the bower install command to install various modules. Here are some examples.

  # 模块的名称  $ bower install jquery  # github用户名/项目名  $ bower install jquery/jquery  # git代码仓库地址  $ bower install git://github.com/user/package.git  # 模块网址  $ bower install http://example.com/script.js
Copy after login

The so-called "installation" means downloading the module (and its dependent modules) to the bower_components subdirectory of the current directory. Once downloaded, it can be inserted directly into a web page.

  <script src="/bower_componets/jquery/dist/jquery.min.js">
Copy after login

The bower update command is used to update modules.

  $ bower update jquery
Copy after login

If no module name is given, all modules are updated.

The bower uninstall command is used to uninstall modules.

  $ bower uninstall jquery
Copy after login

Note that by default, dependent modules will be uninstalled together. For example, if you uninstall jquery-ui, jquery will be uninstalled together, unless there are other modules that depend on jquery.

Browserify

Browserify itself is not a module manager, it just allows server-side CommonJS format modules to run on the browser side. This means that through it, we can use the npm module manager for Node.js. So, in fact, it indirectly provides the functionality of npm to the browser.

First, install Browserify.

  $ npm install -g browserify
Copy after login

Then, write a server-side script.

  var uniq = require('uniq');  var nums = [ 5, 2, 1, 3, 2, 5, 4, 2, 0, 1 ];  console.log(uniq(nums));
Copy after login

The uniq module in the above code is in CommonJS format and cannot run in the browser. At this time, Browserify comes on the scene, compiling the above code into a browser script.

  $ browserify robot.js > bundle.js
Copy after login

The generated bundle.js can be directly inserted into the web page.

  <script src="bundle.js"></script>
Copy after login

When Browserify is compiled, the modules that the script depends on will be compiled together. This means, it can combine multiple modules into a single file.

 Component

 Component is a module manager developed by TJ Holowaychuk, the author of the Express framework. Its basic idea is to compile various resources (scripts, style sheets, pictures, fonts, etc.) required by the web page and put them in the same directory (the default is the build directory).

First, install Component.

  $ npm install -g component@1.0.0-rc5
Copy after login

The reason why the above code needs to specify the Component version is because version 1.0 has not been officially released yet.

Then, create a new index.html in the project root directory.

  <!DOCTYPE html>  <html>    <head>      <title>Getting Started with Component</title>      <link rel="stylesheet" href="build/build.css">    </head>    <body>      <h1>Getting Started with Component</h1>      <p class="blink">Woo!</p>      <script src="build/build.js"></script>    </body>  </html>
Copy after login

The build.css and build.js in the above code are the target files to be generated by Component.

Next, create a new component.json file in the project root directory as the project configuration file.

  {    "name": "getting-started-with-component",    "dependencies": {      "necolas/normalize.css": "^3.0.0"    },    "scripts": ["index.js"],    "styles": ["index.css"]  }
Copy after login

In the above code, the original files specifying the JavaScript script and style sheet are two files, index.js and index.css, and the style sheet relies on the normalize module (not lower than version 3.0.0, but no higher than version 4.0). It should be noted here that the format of the Component module is "github username/project name".

Finally, run the component build command to compile the file.

  $ component build     installed : necolas/normalize.css@3.0.1 in 267ms         build : resolved in 1221ms         build : files in 12ms         build : build/build.js in 76ms - 1kb         build : build/build.css in 80ms - 7kb
Copy after login

During compilation, Component automatically uses autoprefixer to add browser prefixes to CSS properties.

At present, Component seems to be in a state of discontinued development. The code repository has not been changed for nearly half a year. The official also recommends using the Duo introduced next.

Duo

  Duo是在Component的基础上开发的,语法和配置文件基本通用,并且借鉴了Browserify和Go语言的一些特点,相当地强大和好用。

  首先,安装Duo。

  $ npm install -g duo
Copy after login

  然后,编写一个本地文件index.js。

  var uid = require('matthewmueller/uid');  var fmt = require('yields/fmt');    var msg = fmt('Your unique ID is %s!', uid());  window.alert(msg);
Copy after login

  上面代码加载了uid和fmt两个模块,采用Component的"github用户名/项目名"格式。

  接着,编译最终的脚本文件。

  $ duo index.js > build.js
Copy after login

  编译后的文件可以直接插入网页。

  <script src="build.js"></script>
Copy after login

  Duo不仅可以编译JavaScript,还可以编译CSS。

  @import 'necolas/normalize.css';  @import './layout/layout.css';    body {    color: teal;    background: url('./background-image.jpg');  }
Copy after login

  编译时,Duo自动将normalize.css和layout.css,与当前样式表合并成同一个文件。

  $ duo index.css > build.css
Copy after login

  编译后,插入网页即可。

  <link rel="stylesheet" href="build.css">
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

See all articles