简体   繁体   English

使用默认设置和早午餐运行phoenix / elixir应用程序时,资产错误(js和css)

[英]Errors in assets — js and css — when running a phoenix/elixir app with the default setting with brunch

I have a lot of "Uncaught ReferenceError:" and "jquery.waypoints.min.js:7 Uncaught TypeError: Cannot read property" kinds of errors in my phoenix/elixir app. 我的phoenix / elixir应用程序中有很多“未捕获的ReferenceError:”和“ jquery.waypoints.min.js:7未捕获的TypeError:无法读取属性”错误。 This is because of the wrong order in which the js fileds are concatendated or loaded and namespaces which brunch inserts I think. 这是因为我认为js文件的合并或加载顺序错误,以及早午餐插入的名称空间。 But all the setting are default and there's nothing about that in the documenation. 但是所有设置都是默认设置,文档中没有任何设置。

My js and css are untouched, as well as brunch-config.js. 我的js和CSS以及brunch-config.js都未受影响。 I'm using bootstrap and jquery and my own scripts and css files. 我正在使用bootstrap和jquery以及我自己的脚本和CSS文件。 How can I set them up correctly? 如何正确设置它们? Should I put my jquery script and bootstrap into /vendor? 我应该将我的jquery脚本和引导程序放入/ vendor吗?

Here's my brunch-config.js 这是我的早午餐config.js

exports.config = {
  files: {
    javascripts: {
      joinTo: {
        "js/app.js": /^(web\/static\/js)/,
        "js/vendor.js": /^(web\/static\/vendor)|(deps)/
      },
    },

    stylesheets: {
      joinTo: {
        "css/app.css"
        templates: {
          joinTo: "js/app.js"
        }
      }
    },

    conventions: {
      assets: /^(web\/static\/assets)/
    },

    paths: {
      watched: [
        "web/static",
        "test/static"
      ],

      public: "priv/static"
    },

    plugins: {
      babel: {
        ignore: [/web\/static\/vendor/]
      }
    },

    modules: {
      autoRequire: {
        "js/app.js": ["web/static/js/app"]
      }
    },

    npm: {
      enabled: true,
      whitelist: ["phoenix", "phoenix_html"]
    }
  }
};

And my app.js 还有我的app.js

import "phoenix_html"
import "jquery"
import "bootstrap"

Option 1: 选项1:

If you want to change the order the files are concatenated, you can do this inside your brunch-config.js : 如果要更改文件的连接顺序,可以在brunch-config.js

exports.config = {
  files: {
    javascripts: {
      joinTo: "js/app.js"
      order: {
        before: [
          "web/static/vendor/js/jquery-2.1.1.js",
          "web/static/vendor/js/bootstrap.min.js"
        ]
      }
    }
  }
}

You can do the same for CSS: 您可以对CSS执行相同的操作:

stylesheets: {
  joinTo: "css/app.css",
  order: {
    after: ["web/static/css/app.css"] // concat app.css last
  }
}

You can read the Brunch docs for more info. 您可以阅读早午餐文档以获取更多信息。

Note that if you go with this approach, you'll have to import the module inside your app.js file. 请注意,如果采用这种方法,则必须将模块导入app.js文件中。 For JQuery, assuming you installed it using npm, this should be import $ from "jquery" . 对于JQuery,假设您是使用npm安装的,则应import $ from "jquery"

Option 2: 选项2:

You can also place the files you want inside /priv/static/js and priv/static/css and then load them from inside your html (such as your layout file) like this: 您还可以将所需的文件放在/priv/static/jspriv/static/css ,然后从html内部加载它们(例如布局文件),如下所示:

<script src="<%= static_path(@conn, "/js/myscript.js") %>"></script>
  <link rel="stylesheet" href="<%= static_path(@conn, "/css/mystyles.css") %>">

I've had to do that a few times, such as when the library didn't expose itself as a module. 我不得不这样做几次,例如当库没有将自身公开为模块时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM