从Rails 5.1.7的资产管道迁移后,无法使Javascript使用Webpacker正常工作

时间:2020-07-06 10:21:19

标签: ruby-on-rails webpack ruby-on-rails-5 asset-pipeline webpacker

尝试编译时,所有图像均出现以下错误:

const names = {
  animals: ['Dog', 'Cat', 'Snake'],
  flowers: ['Rose', 'Daisy']
};

const res = Object.entries(names)
                  .reduce(
                    ({type, subtype}, [key, values]) => ({
                      type: type.concat(key),
                      subtype: subtype.concat(values)
                    }),
                    { type: [], subtype: [] } // Starting value
                  );

console.log(res);
rails assets:precompile

所以我得到

ModuleNotFoundError: Module not found: Error: Can't resolve 'core-js/modules/es.symbol' in '/***/app/javascript/packs'




            Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.scss doesn't exist
            .css
              Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.css doesn't exist
            .module.sass
              Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.module.sass doesn't exist
            .module.scss
              Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.module.scss doesn't exist
            .module.css
              Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.module.css doesn't exist
            .png
              Field 'browser' doesn't contain a valid alias configuration
              /*/app/javascript/core-js/modules/es.symbol.png doesn't exist
            .svg


我已经通过添加我的gemfile安装了webpacker

Webpacker::Manifest::MissingEntryError in Pages#home

gem 'webpacker'

未创建app / javascript中的文件夹包,所以我自己创建了它,并将当前在app / assets中的application.js添加到app / javascript / packs /

我将此添加到了文件的顶部:

bundle
bundle exec rails webpacker:install
yarn upgrade
yarn install

我确实将所有标签替换为

import 'core-js/stable'
import 'regenerator-runtime/runtime'
const images = require.context('../images', true)
const imagePath = (name) => images(name, true)

javascript_pack_tag 

但是我的清单文件仍然看起来像:

asset_pack_path

在没有进行任何更改的情况下,图像显然已正确编译,并且当我可以访问我的应用程序时,javascript无法正常工作。

1 个答案:

答案 0 :(得分:2)

删除此文件和文件夹:

/node_modules
/tmp/cache
yarn.lock
Gemfile.lock

然后运行:

bundle install
rails webpacker:install
rails assets:precompile
rails webpacker:compile