首页 > 解决方案 > 使用 Rails 5 导入 Webpack

问题描述

我有一个相当大的代码库,我正试图从 Rails 4 迁移到 5,其中主要部分是从资产管道过渡到 webpack 以管理需求和包。

<!DOCTYPE html>
<html>
  <% content_for :extra_head_scripts do %>
  <% end %>
  <head>
    <title>Cloverhound</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <%= javascript_pack_tag "application-editor", 'data-turbolinks-track': 'reload' %>
    <%= stylesheet_link_tag "application-editor", :media => "all" %>
    <%= csrf_meta_tags %>
    <%= favicon_link_tag 'favicon.ico' %>
    <%= render partial: "layouts/shared/head_scripts" %>


    <!-- this javascript_tag passes the user's locale to the editor to enable translations -->
    <%= javascript_tag do %>
      I18n.defaultLocale = "<%= I18n.default_locale %>";
      I18n.locale = "<%= current_user.locale.shortcode.to_s %>";
    <% end %>
  </head>
  <body>
    <%= render partial: "shared/svg_sprite" %>
    <%= yield %>
  </body>
</html>

在上面的文件中,我试图加载 I18n 模块以便可以使用它。

console.log('Hello World from Webpacker');
console.log("Application-Editor");

import Sortable from 'sortablejs';
//var I18n = require('i18n-js');
import I18n from 'i18n';

以上是我的简单 webpack 文件,可以正确登录到 chrome 控制台,但是,I18n 仍然未定义。如果我尝试在 application-editor.js 中调用 I18n,它也会返回 undefined。但是,如果我将 i18n 更改为 i18n-bad 或类似的东西,我会收到一条错误消息,指出找不到该模块。这让我相信我的模块正在被找到,但是在导入它的过程中出现了问题。我对 webpacker 很陌生,所以任何想法都会有所帮助。

标签: javascriptwebpackruby-on-rails-5

解决方案


推荐阅读