gpt4 book ai didi

reactjs - 为什么我们需要使用 import 'babel-polyfill' ;在 react 组件中?

转载 作者:行者123 更新时间:2023-12-03 13:14:17 25 4
gpt4 key购买 nike

我想知道如果我们使用 babel loader + 所有预设,为什么我们需要将 babel-polyfill 包含到我们的组件中?我只是认为 babel-loader 应该自己完成所有工作。

示例取自此处 https://github.com/reactjs/redux/tree/master/examples

我要问的是:

import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import App from './containers/App';

这是包示例:

{
"name": "redux-shopping-cart-example",
"version": "0.0.0",
"description": "Redux shopping-cart example",
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-register",
"test:watch": "npm test -- --watch"
},
"repository": {
"type": "git",
"url": "https://github.com/reactjs/redux.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/reactjs/redux/issues"
},
"homepage": "http://redux.js.org",
"dependencies": {
"babel-polyfill": "^6.3.14",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.2.1",
"redux": "^3.2.1",
"redux-thunk": "^1.0.3"
},
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.1.1",
"cross-env": "^1.0.7",
"enzyme": "^2.0.0",
"express": "^4.13.3",
"json-loader": "^0.5.3",
"react-addons-test-utils": "^0.14.7",
"redux-logger": "^2.0.1",
"mocha": "^2.2.5",
"node-libs-browser": "^0.5.2",
"webpack": "^1.9.11",
"webpack-dev-middleware": "^1.2.0",
"webpack-hot-middleware": "^2.9.1"
}
}

这是来自 https://github.com/reactjs/redux/tree/master/examples 的 webpack 配置示例

var path = require('path')
var webpack = require('webpack')

module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: [
'webpack-hot-middleware/client',
'./index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin()
],
module: {
loaders: [
{
test: /\.js$/,
loaders: [ 'babel?presets[]=react,presets[]=es2015,presets[]=react-hmre' ],
exclude: /node_modules/,
include: __dirname
},
{
test: /\.json$/,
loaders: [ 'json' ],
exclude: /node_modules/,
include: __dirname
}
]
}
}

最佳答案

Babel 将您的代码转换为浏览器可以理解的内容,但生成的代码使用的功能可能适用于每个浏览器,也可能不适用于每个浏览器。例如,并非所有浏览器都支持 Object.assign,因此 babel-polyfill 填补了这个漏洞。它只是一个 polyfill 的集合,您通常会包含它们以支持旧版浏览器。

考虑这段代码:

const foo = {
name: 'Homer'
};
const bar = Object.assign({}, foo, {age: '?'});
console.log(Object.keys(foo), Object.keys(bar));

Babel 会将其转译为几乎相同的内容:

'use strict';
var foo = {
name: 'Homer'
};
var bar = Object.assign({}, foo, { age: '?' });
console.log(Object.keys(foo), Object.keys(bar));

因为这是正常的老式 JS 语法。然而,这并不意味着所有浏览器都实现了所使用的 native 函数,因此我们需要包含polyfill。

关于reactjs - 为什么我们需要使用 import 'babel-polyfill' ;在 react 组件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36196592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com