Table of Contents
源代码
优化
安装 Browserify
Home Web Front-end HTML Tutorial 异步加载非核心 CSS_html/css_WEB-ITnose

异步加载非核心 CSS_html/css_WEB-ITnose

Jun 24, 2016 am 11:33 AM

在生产环境中如果依靠前端引用JSXTransformer.js文件来实现JSX向JavaScript的转换,那是绝对不靠谱的。所以,使用Reactjs的童鞋就需要使用更有逼格的方式来完成这项任务。作为现在最常用的前端构建工具gulp搭配上Browserify来搞定这个问题那真是拉风的不要不要的 :)

废话不多说,咱们来点直接的吧。

源代码

我们这次的任务就是让这段满目疮痍的源代码变得更优化,千里之行,这是开始。如果有童鞋看不懂这段代码是用来做神马的,那么我只能说,“妈妈叫你回家看基础了。” 回家之路 (看不懂代码的,请猛戳)。

<div id="app"></div><script type="text/jsx">	var HelloWorld = React.createClass({ 		render: function() { 			return (				<div> Hello World </div> 			);		}	}); 	React.render(<HelloWorld />, document.getElementById('app'));</script><script type="text/jsx">	var Child = React.createClass({ 		render: function() { 			return (				<div> The Child </div> 			);		}	}); 	var Parent = React.createClass({ 		render: function() { 			return (				<div> Hello World </div>				<Child/> 			); 		} 	}); 	React.render(<Parent />, document.getElementById('app'));</script>
Copy after login

优化

前面把两个组件都写到一块了,现在来分割成独立的文件。其中一个命名为js/components/Parent.jsx,内容如下:

var Child = require('./Child.jsx');var Parent = React.createClass({  render: function(){    return (      <div>        <div> Hello World </div>        <Child/>      </div>    )  }});module.exports = Parent;
Copy after login

Parent 的子元器件Child写到js/components/Child.jsx中,内容如下:

var Child = React.createClass({  render: function(){    return (      <div> The Child </div>    )  }});module.exports = Child;
Copy after login

写到这里,我们已经将两个组件做了初步的拆分,如果有童鞋对module.exports是神马还是一知半解或者根本不懂这是要搞什么飞机,那么请去百度自行谷歌CMD规范,相信度娘会很妩媚的帮你解惑。

如果要真正让元器件显示在页面上需要执行React.render函数,这个是写在js/app.js中的,内容如下:

var Parent = require('./components/Parent.jsx');React.render(<Parent />, document.getElementById('app'));
Copy after login

做到这里,我们还顺带了做了一件很有意义的事情,就是对文件文件目录做了优化,组件放置在/components文件夹中,启动放置在根目录/js下,清晰明了。

使用Browerify之后,html文件中只需要引入一个自定义script文件就好了 ,如下:

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

所有依赖的js内容未来都会被编译到bundle.js文件中。

安装 Browserify

首先要安装 Gulp 。这里我假设大家的系统上都安装好了 nodejs 并且也全局安装了 gulp ,也就是敲gulp -v
是可以看到输出的。

首先确认你的电脑已经成功安装了gulp,至于怎么安装,这我就不多说了。去谷歌百度一下会是一个好办法。

如果你在命令行敲击如下命令

gulp -v
Copy after login

输出结果大概如下面这个这样子的话,那就说明你已经成功安装了gulp。

CLI version 3.9.0Local version 3.9.0
Copy after login

在这个基础上,进入项目目录还需要来局部安装 gulp ,browserify 以及相关的辅助工具:

npm install --save-dev gulp browserify vinyl-source-stream babelify
Copy after login

使用Mac的童鞋不要忘记加sudo哟。

说一下上面四个包的各自作用:

  • gulp 是任务运行环境,用来进行任务调度

  • browserify 用来 require js 的模块

  • vinyl-source-stream 把 browserify 输出的数据进行准换,使之流符合 gulp 的标准

  • reactify 使用它来实现 JSX 编译功能

然后到 gulpfile.js 中,填写如下内容:

var gulp = require("gulp"),	browserify = require('browserify'),	reactify = require('reactify'),	source = require('vinyl-source-stream');gulp.task('jsx', function() {	browserify('./js/app.js')		.transform(reactify)		.bundle()		.pipe(source('bundle.js'))		.pipe(gulp.dest('js'));});
Copy after login

来解释一下上面的脚本流程。首先就是把入口文件 app.js 交给 browserify 进行处理,对于 jsx 的编译,官方推荐使用的就是reactify。下一步,运行 bundle()来把所有依赖都打包成 bundle.js ,但是注意,browserify 不是一个专门为 gulp 写的包,所有它输出的数据流并不能直接 pipe 给 gulp 使用,所以,需要用到 source()接口,也就是 vinyl-source-stream 这个工具来处理一下,然后 pipe 给 gulp ,gulp.dest 会把输出的 bundle.js 文件保存到 js 文件夹中。

任务写好了,在命令行执行:

gulp jsx
Copy after login

这样就生成了 js/bundle.js 文件了。由于这个文件的标签已经添加到 index.html 中了,所以直接用 chrome 打开就可以看到运行效果了。

文章改编自 《当 React 遇见 Gulp 和 Browserify》

原文 http://segmentfault.com/a/1190000004002631
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

See all articles