【问题标题】:Parcel + Babel not transpiling ES6 from node_modules?Parcel + Babel 没有从 node_modules 转译 ES6?
【发布时间】:2019-04-14 11:11:40
【问题描述】:

我似乎无法让 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 文件。有什么我想念的想法吗?

【问题讨论】:

    标签: reactjs babeljs transpiler parceljs


    【解决方案1】:

    我从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 install 之后运行 npm run postinstall。 Babel 现在也应该转译那个 npm 包了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-09
      • 2018-06-04
      • 2020-05-29
      • 1970-01-01
      • 2019-09-04
      • 2019-10-20
      • 2019-08-25
      • 2017-12-30
      相关资源
      最近更新 更多