Rails 6.1.0 [webpack-cli] TypeError:无法读取未定义的属性“插件”

问题描述

我的 Rails+Webpack 应用程序在 6.0.3.4 和 Docker 中运行良好

"webpack": "^4.44.1","webpack-cli": "^3.3.9"

升级到 6.1.0 后 webpack-cli 无法读取 environment.js 中的属性“plugins”(或“config”或“loaders”){{ 1}}。

assets:precompile

config/webpack/production.js

...Step 20/22 : RUN bundle exec rake RAILS_ENV=staging assets:precompile
 ---> Running in bca898bb9014
yarn install v1.22.5
warning package.json: No license field
warning No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@1.2.13: The platform "linux" is incompatible with this module.
info "fsevents@1.2.13" is an optional dependency and Failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "moment@^2.29.0".
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "moment-timezone@^0.5.31".
warning " > tempusdominus-bootstrap-4@5.39.0" has unmet peer dependency "tempusdominus-core@5.19.0".
warning "webpack-dev-server > webpack-dev-middleware@3.7.2" has incorrect peer dependency "webpack@^4.0.0".
[4/4] Building fresh packages...
Done in 4.28s.
I,[2021-01-10T14:31:28.268903 #7]  INFO -- : Writing /var/www/<RoR-app>/public/assets/manifest-a72913f4c604bc8f97dd576fc8777bf029401f6af0e14b6a6c9d3874acfe73e4.js
I,[2021-01-10T14:31:28.269857 #7]  INFO -- : Writing /var/www/<RoR-app>/public/assets/manifest-a72913f4c604bc8f97dd576fc8777bf029401f6af0e14b6a6c9d3874acfe73e4.js.gz
Compiling...
Compilation Failed:
warning package.json: No license field
[webpack-cli] Failed to load '/var/www/<RoR-app>/config/webpack/production.js'
[webpack-cli] TypeError: Cannot read property 'plugins' of undefined
    at Object.<anonymous> (/var/www/<RoR-app>/config/webpack/environment.js:7:13)
    at Module._compile (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/var/www/<RoR-app>/config/webpack/production.js:3:21)
    at Module._compile (/var/www/<RoR-app>/node_modules/v8-compile-cache/v8-compile-cache.js:192:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

ERROR: Service 'ruby' Failed to build: The command '/bin/sh -c bundle exec rake RAILS_ENV=staging assets:precompile' returned a non-zero code: 1

config/webpack/environment.js

process.env.NODE_ENV = process.env.NODE_ENV || 'production'
const environment = require('./environment')
module.exports = environment.toWebpackConfig()

package.json

const { environment } = require('@rails/webpacker');
    const webpack = require('webpack');
    const expose = require('./loaders/expose');
    const coffee = require('./loaders/coffee');
    const erb = require('./loaders/erb');
    
    environment.plugins.prepend('Provide',new webpack.ProvidePlugin({
        $: 'jquery',jQuery: 'jquery',jquery: 'jquery',Popper: ['popper.js','default'],Rails: ['@rails/ujs'],moment: 'moment'
      })
    );
    ...

解决方法

toWebpackConfig 在 6.x 中不存在。 (https://github.com/rails/webpacker/issues/2875)

webpacker gem v.6.0 尚未发布 (https://rubygems.org/gems/webpacker)