惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
B
Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
U
Unit 42
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
C
Check Point Blog
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
小众软件
小众软件
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
P
Proofpoint News Feed
罗磊的独立博客
量子位
D
Docker
博客园_首页
D
DataBreaches.Net
Project Zero
Project Zero
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
Security Latest
Security Latest
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
大猫的无限游戏
大猫的无限游戏

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
轶哥 · 2016-05-05 · via 轶哥博客

Node.js ES6支持

  虽然Node.js6这个版本已经能够支持96%的ECMAScript 2015 (ES6),但是仍然不支持import。关于Node.js各个版本对ES6的支持可以浏览这个网站:http://node.green/

  Node.js 6 中可以启用ES6功能。在node命令后面加上“--es_staging”或“--harmony”就好。

解决不支持import的方案

  目前来说只有一个方案就是用Babel

 "dependencies": {
    "babel-core": "^6.7.6",
    "babel-runtime": "^6.6.1"
  },
  "devDependencies": {
    "babel-cli": "^6.7.5",
    "babel-plugin-transform-runtime": "^6.7.5",
    "babel-preset-es2015": "^6.6.0",
    "babel-register": "^6.7.2",
    "browser-sync": "^2.12.3",
    "webpack": "^1.12.15"
  }
    ```

  以上是轶哥推荐的babel搭配。在调试过程中可以使用babel-node XXX.js代替node命令来启动需要运行的js程序。例如:

"scripts": { "start": "babel-node ./server/index.js" }


  然而这也不是最佳方案。Babel推荐把ES6的代码编译成低版本后再运行。也就是说你可能需要一个类似这样的Makefile。

PATH := ./node_modules/.bin:$(PATH) CLIENT := public SERVER := app/server-bundle.js

build: $(CLIENT) $(SERVER)

$(CLIENT): BUNDLE=client NODE_ENV=production webpack --config ./webpack/prod.config.babel.js

$(SERVER): BUNDLE=server NODE_ENV=production webpack --config ./webpack/prod.config.babel.js

dev: nodemon -x babel-node -w ./api ./api &
babel-node ./dev-server &
wait

lint: eslint api app webpack dev-server.js

test: echo THERE ARE NO TESTS YET

clean: rm -rf $(CLIENT) $(SERVER)

.PHONY: build dev lint test clean


  这么一来,你写ES6版本的JavaScript程序就可以体验到写JAVA或C++一样的感觉了。

  首先需要

$ npm install --save-dev babel-cli

  现在,我们不直接从命令行运行 Babel 了,取而代之我们将把运行命令写在 **npm scripts** 里,这样可以使用 Babel 的本地版本。

  只需将`"scripts"` 字段添加到你的 `package.json` 文件内并且把 babel 命令写成 `build` 字段。

{ "name": "my-project", "version": "1.0.0",

  • "scripts": {
  • "build": "babel src -d lib"
    
  • }, "devDependencies": {
    "babel-cli": "^6.0.0"
    
    } } ```

  现在可以在终端里运行:npm run build,这将以与之前同样的方式运行 Babel,但这一次我们使用的是本地副本。

  以上内容参考自:https://github.com/thejameskyle/babel-handbook/blob/master/translations/zh-Hans/user-handbook.md#toc-babel-cli