babel

从babel讲到AST

和自甴很熟 提交于 2019-12-03 09:27:11
本文转载于: 猿2048 网站➽ https://www.mk2048.com/blog/blog.php?id=hkcjbcbkaa 前言 最近给团队分享了一篇babel原理,然后我把他整理成一篇blog,本篇总字数6059(含代码),速读3分钟,普通阅读5分钟,有兴趣的可以关注一下我的 github博客 babel 我们来看一段代码: [1,2,3].map(n => n + 1); 经过babel之后,这段代码变成了这样: [1, 2, 3].map(function (n) { return n + 1; }); babel的背后 babel的过程:解析——转换——生成。 这边又一个中间的东西,是抽象语法树(AST) AST的解析过程 一个js语句是怎么被解析成AST的呢?这个中间有两个步骤,一个是分词,第二个是语义分析,怎么理解这两个东西呢? 分词 什么叫分词? 比如我们在读一句话的时候,我们也会做分词操作,比如:“今天天气真好”,我们会把他切割成“今天”,“天气”,“真好”。 那换成js的解析器呢,我们看一下下面一个语句 console.log(1); ,js会看成 console , . , log , ( , 1 , ) , ; 。 所以我们可以把js解析器能识别的最小词法单元。 当然这样的分词器我们可以简易实现一下。 //思路分析:传入的是字符串的参数

npm: ENOENT: no such file or directory

匿名 (未验证) 提交于 2019-12-03 08:59:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using npm 3.3.6 and when I try to install a bunch of modules I get the error below: npm i babel-cli babel-plugin-syntax-async-functions babel-plugin-transform-regenerator babel-polyfill babel-preset-es2015 babelify watchify browserify --save-dev npm ERR! Darwin 15.0.0 npm ERR! argv "/Users/thomas/.nvm/versions/node/v5.0.0/bin/node" "/Users/thomas/.nvm/versions/node/v5.0.0/bin/npm" "i" "babel-cli" "babel-plugin-syntax-async-functions" "babel-plugin-transform-regenerator" "babel-polyfill" "babel-preset-es2015" "babelify" "watchify"

Error using ES7 async/await with node, webpack and babel-loader

匿名 (未验证) 提交于 2019-12-03 08:57:35
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to use javascript ES7 syntax on the server using node.js with webpack and babel-loader (es2015 + stage-0 presets). I've gotten it to work with babel-node but when I run webpack I get the following error at the async keyword (9:22 is after the async keyword): ERROR in ./src/server.js Module parse failed: C:\dev\node-async-sample\src\server.js Unexpected token (9:22) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (9:22) I've put the code on github at https://github.com/qubitron/node-async

Gulp, Vue, Webpack, Babel and Uncaught SyntaxError: Unexpected token import

匿名 (未验证) 提交于 2019-12-03 08:56:10
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've been trying whole day to get it to work without any luck so if someone could shine some light that would be very much appreciated. I'm trying to set up environment for working with ES6 files as well as Vue using Webpack. I've installed all dependencies, and created the following files: webpack.config.js module.exports = { entry: './resources/assets/source/js/app.js', output: { filename: 'app.js' }, devtool: 'source-map', resolve: { alias: { 'vue$': 'vue/dist/vue.js' } }, module: { loaders: [ { test: /\.js$/, loader: 'babel', exclude:

Fetching scripts with an invalid type/language attributes is deprecated and will be removed

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I start to learn react.js using JSX. To use it I connect a babel library (not matter what is a version) and write a JSX code in a JS file: <script src = "js/app.js" type = "text/babel" ></script> type="text/babel" - necessarily. But there is a warning in Chrome: "Fetching scripts with an invalid type/language attributes is deprecated and will be removed in M56, around January 2017. See https://www.chromestatus.com/features/5760718284521472 for more details." But everything is OK, when I write a JSX code in DOM. Actually, the

Add babel polyfill to React Native Project

匿名 (未验证) 提交于 2019-12-03 08:48:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I think it is related to the use of for of, es6. Can i add babel-polyfill to React Native Project ? 回答1: In order to add babel-polyfill to your react-native project after installing npm install --save babel-polyfill just import it like this in your entry endpoint : import 'babel-polyfill'; Now you can use new built-ins like Promise or WeakMap , static methods like Array.from and instance methods like Array.prototype.includes , etc. 回答2: You can use npm install es6-symbol --save Then add in your index.android.js & index.ios.js import 'es6

How do I use .babelrc to get babel-plugin-import working for antd?

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm new to react, babel, and antd. I installed react and started a project using create-react-app. I installed antd (ant.design). It suggests using babel-plugin-import, so I installed that too. If I interpret it right, the usage documentation for babel-plugin-import says to put this in a .babelrc file: { "plugins": [ ["import", { "libraryName": "antd", "style": true }] ] } I'm having trouble getting it to work. My web console still has the warning: You are using a whole package of antd, please use https://www.npmjs.com/package/babel-plugin

using async/await with webpack-simple configuration throwing error: RegeneratorRuntime not defined

匿名 (未验证) 提交于 2019-12-03 08:42:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using webpack-simple template with following configurations: package.json { "name": "vue-wp-simple", "description": "A Vue.js project", "version": "1.0.0", "author": "v", "private": true, "scripts": { "dev": "webpack-dev-server --inline --hot", "build": "cross-env NODE_ENV=production webpack --progress --hide-modules" }, "dependencies": { "vue": "^2.3.3", "vue-router": "^2.7.0", }, "devDependencies": { "babel-core": "^6.0.0", "babel-loader": "^6.0.0", "babel-preset-env": "^1.5.1", "babel-preset-es2015": "^6.24.1", "babel-preset-stage-2"

Babel plugins run order

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: TL;DR: Is there a way how to specify the order in which the Babel plugins are supposed to be run? How does Babel determine this order? Is there any spec how this works apart from diving into Babel sources? I'm developing my own Babel plugin. I noticed, that when I run it, my plugin is run before other es2015 plugins. For example having code such as: const a = () => 1 and visitor such as: visitor: { ArrowFunctionExpression(path) { console.log('ArrowFunction') }, FunctionExpression(path) { console.log('Function') }, } my plugin observes

Unknown option: …/.babelrc.presets

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using Babel 6 for es2015 and react which requires babel-preset-es2015 and babel-preset-react . I add the presets property in .babelrc but it throw me an error: ERROR in ./src/client/entry.js Module build failed: ReferenceError: [BABEL] /Users/brick/Dropbox/learncoding/node.js/isomorphic/src/client/entry.js: Unknown option: /Users/brick/Dropbox/learncoding/node.js/isomorphic/.babelrc.presets at Logger.error (/Users/brick/Dropbox/learncoding/node.js/isomorphic/node_modules/babel-core/lib/transformation/file/logger.js:58:11) at