【问题标题】:`babel-loader` dependency error when `rake assets:precompile` in production`rake assets:precompile` 在生产中时出现`babel-loader` 依赖错误
【发布时间】:2020-01-25 00:55:08
【问题描述】:

我继承了一个 Rails 应用程序,我正试图让它在 production 中手动构建

应用程序使用

  • webpacker-3.5.5 ruby​​ gem 管理 JavaScript 资产
  • webpack v3.12.0
  • sprockets gem 管理传统 CSS / 图片资源

作为 webpacker gem 配置的一部分,它特别在 JSX/React 资产上使用 babel-loader。我不太确定这是做什么的,但我猜它是某种预处理器?

module.exports = {
  test: /\.jsx?$/,
  exclude: /node_modules/,
  use: [{
    loader: "babel-loader",
    options: {
      cacheDirectory: true,
      // Use .babelrc - not webpack config JS - to define all options
      babelrc: true
    }
  }]
}

当我尝试在生产服务器上运行 rake assets:precompile 时,它在尝试使用 webpack 构建资产的步骤中出错

> rake assets:precompile
yarn install v1.17.3
warning package.json: No license field
warning delly@1.0.0: No license field
[1/4] Resolving packages...
success Already up-to-date.
Done in 1.18s.
Webpacker is installed ???? ????
Using /app/config/webpacker.yml file for setting up webpack paths
Compiling…
Compilation failed:

Hash: 27785324c8b2ba6004dd
Version: webpack 3.12.0
Time: 119ms
        Asset     Size  Chunks             Chunk Names
manifest.json  2 bytes          [emitted]

ERROR in Entry module not found: Error: Can't resolve 'babel-loader' in '/app'

ERROR in Entry module not found: Error: Can't resolve 'babel-loader' in '/app'

这不会发生在本地的development,只有production

知道为什么会出错吗?生产中是否需要babel-loader,或者我可以将其完全从生产中删除吗?

这是我的package.json,它定义了软件包的安装:

{
  "name": "delly",
  "version": "1.0.0",
  "scripts": {
    "test": "node_modules/.bin/jest --no-cache --config spec/javascript/jest.config.js",
    "test:debug": "node --inspect-brk node_modules/.bin/jest --no-cache --config spec/javascript/jest.config.js --colors --verbose"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-proposal-object-rest-spread": "^7.4.4",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/plugin-transform-modules-commonjs": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.8.0",
    "babel-loader": "^8.0.6",
    "babel-plugin-module-resolver": "^3.2.0",
    "eslint": "^4.6.1",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.6.1",
    "i18n-js": "^3.2.2",
    "jest": "^24.8.0",
    "jest-dom": "^3.4.0",
    "js-yaml": "^3.13.1",
    "react-testing-library": "^7.0.1",
    "stylelint": "^9.3.0",
    "stylelint-config-rational-order": "^0.0.2",
    "webpack-dev-server": "2.11.2"
  },
  "dependencies": {
    "@rails/webpacker": "^3.5.5",
    "axios": "^0.19.0",
    "core-js": "3",
    "html-react-parser": "^0.4.6",
    "jquery": "^3.3.1",
    "jquery-ujs": "^1.2.2",
    "prop-types": "^15.6.1",
    "rails-erb-loader": "^5.4.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-on-rails": "^11.3.0",
    "react-toggle-switch": "^3.0.4",
    "react-transition-group": "1.x",
    "react_ujs": "^2.4.4"
  }
}

一个

【问题讨论】:

    标签: ruby-on-rails webpack babeljs babel-loader


    【解决方案1】:

    当你在生产模式下运行rake assets:precompile时,它也会在生产模式下运行webpack,在这种情况下,只加载dependencies中设置的包,如果你查看packages.jsonbabel-loader设置在devDependencies.\

    解决方案?将babel-loader 移动到dependencies

    【讨论】:

    • 谢谢!我不知道那个部分是干什么用的。那么所有其他babel-* 项目呢,有些甚至从@babel-* 开始。我也要搬那些吗?一般来说,我不确定 babel 依赖树中需要什么
    • 除了eslintjestwebpack-dev-server之外,我会将大部分包移到dependencies
    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2011-12-24
    • 2016-11-18
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多