正文开始 <p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">https://blog.fundebug.com/2017/11/29/history-of-javascript-tools/</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><br /></p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">译者按: JavaScript开发要用到的工具越来越多,越来越复杂,为什么呢?你真的弄明白了吗?</p>原文: <a href="https://medium.com/@peterxjang/modern-javascript-explained-for-dinosaurs-f695e9747b70" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Modern JavaScript Explained For Dinosaurs</a><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">为了保证可读性,本文采用意译而非直译。另外,本文版权归原作者所有,翻译仅用于学习。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">如果你不是老司机,面对众多JavaScript开发工具,也许会有些搞不清楚状况。因为,JavaScript的生态系统在迅速的变化,新手很难理解这些工具的功能以及它们所解决的问题。对此,我深有体会。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">我是1998开始编程的,但是我直到2014才开始学习JavaScript。当我第一次接触<a href="http://browserify.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Browserify</a>时,有这样一句介绍:</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; line-height: 1.6em;">通过将依赖打包,Browserify让你可以在浏览器中使用require(‘modules’)</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">当时,我完全无法理解这句话,也不知道Browserify到底有什么用。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这篇博客将从历史演进的角度,告诉大家今天的JavaScript开发工具是怎样发展而来,以及它们到底有什么作用。首先,我们将介绍一个非常简单的网页示例,它是由最原始的HTML与JavaScript写的。然后,我们会逐步介绍不同的工具,它们可以解决不同的问题。</p>原始的JavaScript使用方式<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">最原始的网页,是用HTML和JavaScript编写的,没有那么多幺蛾子。不过,我们需要手动下载并载入依赖的JavaScript文件。如下,index.html中载入1个JavaScript文件:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);"><!-- index.html --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JavaScript Example</title> <script src="index.js"></script></head><body> <h1>Hello from HTML!</h1></body></html></pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><script src="index.js"></script> 载入了同目录的index.js文件:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// index.jsconsole.log("Hello from JavaScript!");</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这样,一个简单的网页就写好了!</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">现在,假设你需要使用一个第三方库比如<a href="http://momentjs.com/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">moment.js</a>,这个库可以帮助我们处理时间数据。比如:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">moment().startOf('day').fromNow(); // 20 hours ago</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">我们需要在的官网下载moment.min.js,放到同一个目录中,然后在index.html中载入:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);"><!-- index.html --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Example</title> <link rel="stylesheet" href="index.css"> <script src="moment.min.js"></script> <script src="index.js"></script></head><body> <h1>Hello from HTML!</h1></body></html></pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">可知,moment.min.js先于index.js载入,这样我们就可以在index.js中调用moment了:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// index.jsconsole.log("Hello from JavaScript!");console.log(moment().startOf('day').fromNow());</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">总结: 直接使用HTML和JavaScript库编写网页非常简单,也很容易理解;然而,当JavaScript库更新时,我们需要手动下载并载入新版本,这样确实很烦…</p>npm:包管理工具<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">大概2010开始,数个JavaScript包管理工具诞生了,它们旨在通过一个中央仓库,使得下载和更新JavaScript库更加自动化。2013年时,<a href="https://bower.io/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Bower</a>可能是最流行的;到了2015年, <a href="https://www.npmjs.com/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">npm</a>逐渐占据统治地位;而2016年,<a href="https://yarnpkg.com/en/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">yarn</a>开始逐渐引起关注,但是它的底层是基于npm的。还有一点,npm最初是为<a href="https://nodejs.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">node.js</a>开发的,并不是为了前端。因此,使用npm管理前端的依赖库显得有点奇怪。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">现在,我们来看看如何使用npm安装moment.js吧。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">如果你已经安装了nodejs,则npm也应该安装好了。这时,进入index.html所在目录,执行以下命令:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm init</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">终端会出现数个问题,仅需使用enter键选择默认配置就好了。命令执行之后,会生成一个package.json文件,npm使用这个文件保存所有的项目信息。默认的package.json是这样的:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">{ "name": "your-project-name", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC"}</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">使用一下命令,即可安装moment.js:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm install moment --save</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这个命令会做两件事情:首先,它会下载moment.js,将其保存到node_modules目录中;然后,它会更新package.json,保存moment安装信息。</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">{ "name": "modern-javascript-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "moment": "^2.19.1" }}</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这样,当我们需要与其他人分享这个项目时,就不需要将node_modules发送给对方了,而只需要给它package.json文件,因为它可以使用npm install安装所有依赖库。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">moment.min.js文件位于node_modules/moment/min目录中,因此我们可以在index.html中直接载入moment.min.js:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);"><!-- index.html --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JavaScript Example</title> <script src="node_modules/moment/min/moment.min.js"></script> <script src="index.js"></script></head><body> <h1>Hello from HTML!</h1></body></html></pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">总结: 现在,我们不需要手动下载moment.js了,而可以通过npm自动下载以及更新,这样方便很多;但是,我们需要在node_modules中找到对应的JS文件,然后将它载入HTML,这样很不方便。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">顺便分享一个好东西: 如果你需要监控线上JavaScript代码的错误的话,欢迎免费使用<a href="https://fundebug.com/" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Fundebug</a>!</p>webpack - 打包工具<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">大多数编程语言都提供了模块管理功能,可以在一个文件中导入另一个文件的代码。然而,JavaScript最初并没有支持这种方式。很长时间以来,组织多个JavaScript文件的代码时,需要使用全局变量。我们在载入moment.min.js时,实际上也定义了一个moment全局变量,因此所有之后载入的JS文件,都可以使用它。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">2009年,一个叫做CommenJS的项目出现了,它为JavaScript模块化定义了一个规范,从而允许JavaScript能够和其他编程语言一样在不同文件中引入模块。Node.js是支持CommenJS规范的,它可以使用require直接引用模块:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// index.jsvar moment = require('moment');console.log("Hello from JavaScript!");console.log(moment().startOf('day').fromNow());</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这样写非常方便,然而,如果你在浏览器中执行上面的代码,则会收到报错,因为”require未定义”。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这时,我们就需要打包工具了,它们可以将源代码构建成为兼容浏览器的代码,来避免上面提到的问题。简单地说,打包工具可以找到所有require语句,然后将它们替代为各个JS文件中代码,最终生成的一个单独的JS文件。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><a href="http://browserify.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Browserify</a>是2011年发布,曾经是最流行的打包工具;到了2015年, <a href="https://webpack.github.io/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">webpack</a>逐渐成为了最主流的打包工具。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">现在,我们来看看如何让require('moment')可以在浏览器中执行。首先,我们需要安装webpack:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm install webpack --save-dev</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">--save-dev选项表示webpack模块时开发环境中需要的依赖库,而生产环境中并不需要。package.json更新之后是这样的:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">{ "name": "modern-javascript-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "moment": "^2.19.1" }, "devDependencies": { "webpack": "^3.7.1" }}</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">使用一下命令运行webpack:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ ./node_modules/.bin/webpack index.js bundle.js</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">bundle.js为生成的打包文件,可以直接在浏览器中使用:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);"><!-- index.html --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>JavaScript Example</title> <script src="bundle.js"></script></head><body> <h1>Hello from HTML!</h1></body></html></pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">每次修改index.js之后,我们都需要执行webpack。webpack的命令比较长,这样很麻烦,尤其是我们需要使用一些高级选项时。这时,我们可以将webpack的配置选项写入webpack.config.js文件:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// webpack.config.jsmodule.exports = { entry: './index.js', output: { filename: 'bundle.js' }};</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这样,我们直接运行wepack,而不需要指定任何配置选项,就可以进行打包了:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ ./node_modules/.bin/webpack</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">总结: 使用打包工具之后,对于第三方JS库,我们不再需要在HTML中使用<script>载入,也不需要定义全局变量了,而是直接在JS代码中使用require语句。另外,将多个JS文件打包成为一个单独的文件也有利于提高网页性能。然而,每次更新代码时,我们都需要手动运行webpack,这很不方便。</p>Babel - 新语法特性转码器<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">转码器可以将代码由一个语言转换为另一个语言,它对于前端开发来说非常重要。浏览器对于语言的新特性支持通常很慢,我们使用新语言特性编写的代码需要转换为兼容的代码才能正常运行。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">对于CSS,转码器有<a href="http://sass-lang.com/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Sass</a>,<a href="http://lesscss.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Less</a>,以及<a href="http://stylus-lang.com/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">Stylus</a>。对于JavaScript,<a href="http://coffeescript.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">CoffeeScript</a> 曾经是最流行的,而现在用的最多的是<a href="https://babeljs.io/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">babel</a>和<a href="http://www.typescriptlang.org/" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">TypeScript</a>。CoffeeScript是一门可以编译到JavaScript的语言,旨在优化JavaScript。Typescript也是一门语言,支持最新的ECMAScript,并且支持静态类型检查。而Babel并非一门语言,而只是一个转码器,可以将ES6以及更高版本的JavaScript代码转为ES5代码,从而兼容各个浏览器。很多人选择babel,因为它最接近原生的JavaScript。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">现在,我们来看看如何使用Babel。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">首先,我们需要安装babel:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm install babel-core babel-preset-env babel-loader --save-dev</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">我们一共安装了3个模块:babel-core是Babel的核心部分;babel-preset-env定义了转码规则;babel-loader是Babel的webpack插件。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">然后,在webpack.config.js中配置babel-loader即可:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// webpack.config.jsmodule.exports = { entry: './index.js', output: { filename: 'bundle.js' }, module: { rules: [ { test: /.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['env'] } } } ] }};</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">webpack的配置文件看着有点晕,大致含义是这样的:告诉webpack找到所有js文件(除了node_modules目录中的文件),根据babel-preset-env中的转码规则,使用babel-loader进行转码。至于webpack配置的细节,可以查看<a href="http://webpack.github.io/docs/configuration.html" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">文档</a>。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">现在,我们可以开始使用ES2015特性编程了。index.js中使用了<a href="https://babeljs.io/learn-es2015/#ecmascript-2015-features-template-strings" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">模板字符串</a>:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// index.jsvar moment = require('moment');console.log("Hello from JavaScript!");console.log(moment().startOf('day').fromNow());var name = "Bob", time = "today";console.log(`Hello ${name}, how are you ${time}?`);</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">我们也可以使用<a href="https://babeljs.io/learn-es2015/#ecmascript-2015-features-modules" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">import</a>来代替require :</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// index.jsimport moment from 'moment';console.log("Hello from JavaScript!");console.log(moment().startOf('day').fromNow());var name = "Bob", time = "today";console.log(`Hello ${name}, how are you ${time}?`);</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改index.js之后,运行webpack重新构建代码:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ ./node_modules/.bin/webpack</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">其实,现在大多数浏览器都支持了ES2015特性,所以你可以测试一下IE9。在bundle.js中,我们可以看到转码后的代码:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">// bundle.js// ...console.log('Hello ' + name + ', how are you ' + time + '?');// ...</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">总结: 有了Babel,我们就可以放心地使用最新的JavaScript语法了。但是使用<a href="https://babeljs.io/learn-es2015/#ecmascript-2015-features-template-strings" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">模板字符串</a>这样简单的语法显然没什么意思,所以不妨试试<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">async/await</a>。不过,现在我们还有两个问题需要解决:bundle.js应该需要压缩,这样才能提高性能,这一点很简单;每次修改代码,都需要手动运行webpack,这样很不方便,下一步我们来解决这个问题。</p>npm scripts - 任务管理工具<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">任务管理工具可以将一些重复性的任务自动化,比如合并文件、压缩代码、优化图片以及运行测试等。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">2013年时,Grunt是最流行的任务管理工具,其次是Gulp。现在,直接使用npm的scripts功能的开发者似乎越来越多了,这样不需要安装额外的插件。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改package.json,即可配置npm scripts:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">{ "name": "modern-javascript-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1", "build": "webpack --progress -p", "watch": "webpack --progress --watch" }, "author": "", "license": "ISC", "dependencies": { "moment": "^2.19.1" }, "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-env": "^1.6.1", "webpack": "^3.7.1" }}</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">我们定义了2个scripts,即build和watch。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">运行build,即可构建代码了(- -progress选项可以显示构建进程,-p选项可以压缩代码):</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm run build</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">运行watch,则一旦javascript修改了,就会自动重新运行wepback,这样开发就方便多了:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm run watch</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">还有,我们可以webpack-dev-server,它可以提供一个网页服务器,而且能够自动重载页面:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm install webpack-dev-server --save-dev</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">修改package.json:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">{ "name": "modern-javascript-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo "Error: no test specified" && exit 1", "build": "webpack --progress -p", "watch": "webpack --progress --watch", "server": "webpack-dev-server --open" }, "author": "", "license": "ISC", "dependencies": { "moment": "^2.19.1" }, "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-preset-env": "^1.6.1", "webpack": "^3.7.1" }}</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">运行:</p><pre style="margin: 0px; padding: 0px; border: none; outline: 0px; font-weight: inherit; font-style: inherit; font-family: 'Helvetica Neue' !important; font-size: 14px; vertical-align: baseline; overflow: auto; color: rgb(0, 0, 0); line-height: 22.4px; background: rgb(255, 255, 255);">$ npm run server</pre><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">这时,浏览器会自动打开localhost:8080,并访问index.html。当我们修改index.js时,代码会自动重新构建,并且页面也会自动刷新。这样我们修改代码之后,就可以看到浏览器中的效果,而不需要任何额外的操作。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">正如前文提到过,npm scripts或者其他任务管理工具可以做的事情还有很多,感兴趣的话,可以看看这个<a href="https://youtu.be/0RYETb9YVrk" target="_blank" rel="external" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 14px; vertical-align: baseline; text-decoration: none; color: rgb(16, 63, 251); word-break: break-all; word-wrap: break-word; background: 0px 0px;">视频</a>。</p>结论<p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">简单总结一下:刚开始我们用HTML和JS写代码;后来我们用包管理工具来安装第三方库;然后我们用打包工具实现模块化;再后来我们用转码器从而使用最新语法;最后我们用任务管理工具来自动化一些重复的任务。对于新手来说,这一切都显得非常头疼,更头疼的是这一切还在不断变化之中。</p><p style="margin: 1.6em 0px; padding: 0px; border: 0px; outline: 0px; font-weight: normal; font-style: normal; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; vertical-align: baseline; line-height: 1.6em; color: rgb(85, 85, 85); font-variant: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">当然也有好消息,各个框架为了方便初学者,都会提供工具,把所有配置都弄好: Ember有ember-cli,Angular有angular-cli, React有create-react-app, Vue有vue-cli。这样,似乎你什么都不用管,只需要写代码就可以了。然而,现实是残酷的,总有一天你需要对Babel或者Webpack进行一些个性化配置。因此,理解每一个工具的作用还是非常有必要的,希望这篇博客可以帮助大家。</p>
正文结束 |
js 正则替换返回值做回调函数 | 前端Js框架汇总 |