gpt4 book ai didi

reactjs - Parcel + Babel 没有从 node_modules 转译 ES6?

转载 作者:行者123 更新时间:2023-12-03 14:18:16 24 4
gpt4 key购买 nike

尽管预设会自动安装,但我似乎无法让 Babel 与 Parcel 一起使用。它可以在本地和 Chrome 中运行,但不会转译 node_modules es6 文件,因此输出仍然包含 const/let/... 并且无法在 Safari 中运行。

.babelrc

{
"presets": ["@babel/preset-env","@babel/preset-react"]
}

(我还尝试了 envreact )。

package.json 脚本

"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html",

为什么会这样?

完整的 package.json (请注意,这是在尝试使其正常工作后进行的)

{
"name": "my-react-app",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/preset-env": "^7.1.5",
"@babel/preset-react": "^7.0.0",
"axios": "^0.18.0",
"babel-polyfill": "^6.26.0",
"browserslist": "^4.3.4",
"lodash": "^4.17.11",
"node-sass": "^4.10.0",
"pinyin": "^2.8.3",
"prop-types": "^15.6.2",
"react": "^16.6.1",
"react-dom": "^16.6.1",
"react-notifications": "^1.4.3",
"react-router-dom": "^4.3.1"
},
"devDependencies": {
"cssnano": "^4.1.7",
"sass": "^1.14.3"
}
}

仍然使用 constlet 获取 .js 文件。我缺少什么想法吗?

最佳答案

我从 https://github.com/parcel-bundler/parcel/issues/1655#issuecomment-425593377 找到了解决方案

// .browserslistrc.packages
node 10.11

// package.json
{
"scripts": {
"postinstall": "npm-run-all -p \"postinstall:*\"",
"postinstall:p-retry": "cpy --rename=.browserslistrc .browserslistrc.packages node_modules/p-retry",
"postinstall:query-string": "cpy --rename=.browserslistrc .browserslistrc.packages node_modules/query-string"
}
}

为需要添加转译的每个 npm 包添加一个 postinstall:package-name(在我的例子中,pinyin)并运行 npm run postinstall 每次 npm install 之后。 Babel 现在还应该转译该 npm 包!

关于reactjs - Parcel + Babel 没有从 node_modules 转译 ES6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53250175/

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