babel

旧create-react-app项目集成jest+enzyme

瘦欲@ 提交于 2020-01-10 11:37:03
/*--> */ /*--> */ 在create-react-app创建的老项目上面集成jest-enzyme发现用上一篇博客的配置报错:   看报错知道是上篇博客的.babelrc.js的配置没有生效。   使用npm run eject指令将create-react-app项目的配置暴露出来,命令执行结果: Copying files into D:\oy\temp\xxx Adding \config\env.js to the project Adding \config\paths.js to the project Adding \config\polyfills.js to the project Adding \config\webpack.config.dev.js to the project Adding \config\webpack.config.prod.js to the project Adding \config\webpackDevServer.config.js to the project Adding \config\jest\cssTransform.js to the project Adding \config\jest\fileTransform.js to the project Adding \scripts\build.js

babel学习笔记

泄露秘密 提交于 2020-01-10 00:22:27
babel 到底做了什么?怎么做的? 简单来说,babel主要用于把 JavaScript 中 es2015/2016/2017/2046 的新语法转化为 es5,让低端运行环境(如浏览器和 node )能够认识并执行。本文以 babel 6.x 为基准进行讨论。 严格来说,babel 也可以转化为更低的规范。但以目前情况来说,es5 规范已经足以覆盖绝大部分浏览器,因此常规来说转到 es5 是一个安全且流行的做法。 接下来,我们逐个给大家介绍一下babel里的包都是干什么的 包 babel-core 可以看做 babel 的编译器。babel 的核心 api 都在这里面,比如 transform,主要用来处理转码。它会把我们的 js 代码,抽象成 ast,即抽象语法树,是源代码的抽象语法结构的树状表现形式。我们可以理解为,它定义的一种分析 js 语法的树状结构。也就是说 es6 的新语法,跟老语法是不一样的,那我们怎么去定义这个语法呢。所以必须要先转成 ast,去发现这个语法的 kind,分别做对应的处理,才能转化成 es5。 babel-cli 提供命令行运行 babel。也就是你可以 babel filename 去对文件转码。 如下图: babel script.js --out-file script-compiled.js babel-runtime 这个包很简单

ReactJS: webpack: Failed to compile with spread syntax

走远了吗. 提交于 2020-01-09 11:54:33
问题 I want to show you my problem. My env: Node: 8.9.1 npm: 5.6.0 yarn: 1.3.2 package.json "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-env": "^1.6.1", "babel-preset-react": "^6.24.1", "copy-webpack-plugin": "^4.1.1", ... "webpack": "^3.7.1", "webpack-dev-server": "^2.9.2" }, webpack.config.js ... module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', options

ReactJS: webpack: Failed to compile with spread syntax

狂风中的少年 提交于 2020-01-09 11:54:07
问题 I want to show you my problem. My env: Node: 8.9.1 npm: 5.6.0 yarn: 1.3.2 package.json "devDependencies": { "babel-core": "^6.26.0", "babel-loader": "^7.1.2", "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-env": "^1.6.1", "babel-preset-react": "^6.24.1", "copy-webpack-plugin": "^4.1.1", ... "webpack": "^3.7.1", "webpack-dev-server": "^2.9.2" }, webpack.config.js ... module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: { loader: 'babel-loader', options

React.js - Syntax error: this is a reserved word in render() function

久未见 提交于 2020-01-08 17:39:11
问题 I'm stuck on a error for the reserved keyword "this". In my React Component below shows me passing in a state from a my main component "App.js" to my "RecipeList.js" component to then map the data and render each RecipeItem Component. I just don't understand why I get this error React.js - Syntax error: this is a reserved word The error is called in RecipeList inside the render return method; If anybody could help that would great! Thanks App.js //main imports import React, { Component } from

未定义Babel 6 regeneratorRuntime

别来无恙 提交于 2020-01-08 16:13:51
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我正在尝试使用异步,在Babel 6上从头开始,但是我得到的regeneratorRuntime尚未定义。 .babelrc文件 { "presets": [ "es2015", "stage-0" ] } package.json文件 "devDependencies": { "babel-core": "^6.0.20", "babel-preset-es2015": "^6.0.15", "babel-preset-stage-0": "^6.0.15" } .js文件 "use strict"; async function foo() { await bar(); } function bar() { } exports.default = foo; 正常使用它而无需异步/等待就可以了。 有什么想法我做错了吗? #1楼 babel-polyfill 。 您还必须安装它才能使异步/等待工作。 npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader package.json "devDependencies": { "babel-core": "^6.0.20", "babel

webpack4的配置你都掌握了么?

别说谁变了你拦得住时间么 提交于 2020-01-08 10:56:12
webpack5都出了,webpack4的的基本配置,解析ES6,引入CSS,编译Less,设置image等等,你都会了么? ​解析ES6 了解Babel Babel是一个JavaScript编译器,可以实现将ES6+转换成浏览器能够 识别的代码。 Babel在执行编译时,可以依赖.babelrc文件,当设置依赖文件时, 会从项目的根目录下读取 .babelrc 的配置项,.babelrc配置文件 主要是对 预设(presets) 和 插件(plugins) 进行配置。 其中,presets可以标识需要转换的源码使用了哪些新特性, 可以理解为一系列plugins的集合,例如babel-preset-es2015,可以 将es6转换为es5;而plugins则指示babel如何对代码进行转换, 例如plugin-transform-arrow-functions可将ES6语法转换为 ES5。 解析ES6 1、安装依赖 npm i @babel/core @babel/preset-env babel-loader -D 2、配置webpack.config.js设置loader module: { rules: [ { test: /.js$/, use: 'babel-loader' } ] } 3、根目录创建.babelrc,并配置preset-env对ES6+语法特性进行转换

vue 兼容ie

家住魔仙堡 提交于 2020-01-07 20:04:55
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 1、安装babel 2、安装babel-polyfill npm install babel-polyfill --save-dev import 'babel-polyfill' // main.js 3、 build -> webpack.base.conf.js 中 找到 module -> rules => { test:/\js&/, loaderl:"babel-loader", include:[  resolve('src'),resolve('test'),  resolve('node_modules/weback-dev-server/client'),  resolve('node_modules/element-ui/packages), resolve('node_modules/element-ui/src), resolve('node_modules/vue-echarts), resolve('node_modules/resize-detector), ] } 修改 添加 来源: oschina 链接: https://my.oschina.net/bing309/blog/3154571

VUE生产bug汇总

那年仲夏 提交于 2020-01-07 16:55:41
一、 IE、360显示空白 360浏览器模式 用.com、.cn结尾的安全域名访问网站,360浏览器默认是极速模式(推荐),为chrome内核 用ip或不规范的域名访问网站,360浏览器默认是兼容模式的,为IE内核,对于ES6语法解析存在问题,需要添加babel解析 解决方法 下载安装 babel-polyfill: npm install babel-polyfill --save 在webpack.base.conf.js中进行配置 entry: { // "babel-polyfill"用来解决的兼容性 app: ['./node_modules/babel-polyfill/dist/polyfill.js','./src/main.js'] } 二、发版缓存问题 每次打包发版之后,需要清浏览器缓存重新进入,否则代码不更新 加入版本号处理: build -->webpack.prod.conf.js中js加上版本号 const Version = new Date().getTime() output: { path: config.build.assetsRoot, filename: utils.assetsPath('js/[name].[chunkhash]' + Version + '.js'), chunkFilename: utils.assetsPath(

babel 编译后 this 变成了 undefined

谁都会走 提交于 2020-01-07 05:57:42
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近有在用webpack,使用了babel这个模块来编译js jsx文件,但是发现文件编译后this变成了undefined。 源文件 module.exports = React.createClass({ render: () => { return (<div>{this.props.name}</div>); } }); 编译后 module.exports = React.createClass({ render: () => { return (<div>{undefined.props.name}</div>); } }); 这个原因是因为webpack规则中babel用了es2015的编译规则 { test: /\.jsx?$/, loader: "babel", query: { presets: ['react', 'es2015'] } } 解决的方法是这样修改 module.exports = React.createClass({ render: function(){ return (<div>{this.props.name}</div>); } }); 其根本的原因是es2015的箭头函数,这个一个不能忽视的问题 普通function函数和箭头函数的行为有一个微妙的区别