Elixir Phoenix 从 /priv/static 提供 JavaScript。

huangapple go评论50阅读模式
英文:

Elixir Phoenix serve Javascript from /priv/static

问题

I am currently fighting esbuild in my phoenix project. I have a heex template on which I want to use Trumbowyg as a text editor. First I tried to import the javascript file via vendoring it and doing import trumbowyg from '../vendor/trumbowyg.min.js in app.js.

我目前正在处理我的Phoenix项目中的esbuild问题。我有一个heex模板,我想在其中使用Trumbowyg作为文本编辑器。首先,我尝试通过将其放入vendoring文件夹并在app.js中使用import trumbowyg from '../vendor/trumbowyg.min.js来导入JavaScript文件。

I thought this would work because it did for AlpineJs. But it didn't. It complained about missing jQuery. So I vendored jQuery the same way: import {jQuery, $} from '../vendor/jquery.min.js. But no success. Same error message Uncaught ReferenceError: $ is not defined.

我认为这应该可以工作,因为对于AlpineJs来说是可以的。但实际上并不行。它抱怨缺少jQuery。所以我以同样的方式vendored jQuery:import {jQuery, $} from '../vendor/jquery.min.js。但没有成功。同样的错误消息Uncaught ReferenceError: $ is not defined

Then I had the idea to fix it via importing the scripts in the template withing <script> tags. So I just threw the two js files into the /priv/static/assets/ folder. This worked in development with the following tags:

然后,我想通过在模板中使用<script>标签导入脚本来修复它。所以我只是将这两个js文件放到/priv/static/assets/文件夹中。在开发环境中,以下标签可以工作:

<script type="text/javascript" src={Routes.static_path(@conn, "/assets/jquery.min.js")}></script>
<script type="text/javascript" src={Routes.static_path(@conn, "/assets/trumbowyg.min.js")}></script>

But this stopped working in production (I use the docker deploy method). Then I tried using some kind of Plug.Static implementation but I did not get it to work.

但这在生产环境中停止工作(我使用Docker部署方法)。然后,我尝试使用一种Plug.Static实现,但没有成功。

So my question now is: How can I make those scripts load in a production environment? Even better would be to know how to configure esbuild to use deploy the script files correctly. I don't know what to change, because my AlpineJs import is working fine.

所以现在我的问题是:如何在生产环境中加载这些脚本?更好的办法是了解如何配置esbuild以正确部署脚本文件。我不知道要更改什么,因为我的AlpineJs导入运行良好。

That's the content of my app.js file. But like I said, adding: import trumbowyg from '../vendor/trumbowyg.min.js or import {jQuery, $} from '../vendor/jquery.min.js gets me errors like Uncaught ReferenceError: $ is not defined and Uncaught ReferenceError: jQuery is not defined.

这就是我的app.js文件的内容。但像我说的,添加import trumbowyg from '../vendor/trumbowyg.min.jsimport {jQuery, $} from '../vendor/jquery.min.js会导致错误,如Uncaught ReferenceError: $ is not definedUncaught ReferenceError: jQuery is not defined

Every help is appreciated. Thanks in advance.

感谢您的任何帮助。提前感谢。

英文:

I am currently fighting esbuild in my phoenix project. I have a heex template on which I want to use Trumbowyg as a text editor. First I tried to import the javascript file via vendoring it and doing import trumbowyg from '../vendor/trumbowyg.min.js in app.js.

I thought this would work because it did for AlpineJs. But it didn't. It complained about missing jQuery. So I vendored jQuery the same way: import {jQuery, $} from '../vendor/jquery.min.js. But no success. Same error message Uncaught ReferenceError: $ is not defined.

Then I had the idea to fix it via importing the scripts in the template withing <script> tags. SO i just threw the two js files into the /priv/static/assets/ folder. This worked in development with the following tags:

<script type="text/javascript" src={Routes.static_path(@conn, "/assets/jquery.min.js")}></script>
<script type="text/javascript" src={Routes.static_path(@conn, "/assets/trumbowyg.min.js")}></script>

But this stopped working in production (I use the docker deploy method). Then I tried using some kind of Plug.Static implementation but I did not get it to work.

So my question now is: How can I make those scripts load in a production environment? Even better would be to know how to configure esbuild to use deploy the script files correctly. I don't know what to change, because my AlpineJs import is working fine.

import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"

import Alpine from "../vendor/alpine.min"

window.Alpine = Alpine;



let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {
    params: {_csrf_token: csrfToken},
    dom: {
      onBeforeElUpdated(from, to) {
        if (from._x_dataStack) {
          window.Alpine.clone(from, to)
        }
      }
    }
  })

// Show progress bar on live navigation and form submits
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())

// connect if there are any LiveViews on the page
liveSocket.connect()

// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000)  // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket

That's the content of my app.js file. But like I said, adding: import trumbowyg from '../vendor/trumbowyg.min.js or import {jQuery, $} from '../vendor/jquery.min.js gets me errors like Uncaught ReferenceError: $ is not defined and Uncaught ReferenceError: jQuery is not defined.

Every help is appreciated. Thanks in advance.

答案1

得分: 1

这里是您要翻译的内容:

"Not sure if that's the best way of doing things in Phoenix but what I do is to use phx-hook for these purposes.
Set up a new JS hook with something like:

import YourJSObject from 'your-lib'
Trumbowyg = {
mounted() {
....
},
updated(){
....
}
}
export Trumbowyg;

import it in your app.js and add it your hooks:

let Hooks = { ..., Trumbowyg, ... }
...
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})

Once all this is set up you add it to your live component root element with phx-hook

and your imported JS should work on that component.

Hope that helps."

英文:

Not sure if that's the best way of doing things in Phoenix but what I do is to use phx-hook for these purposes.
Set up a new JS hook with something like:

import YourJSObject from 'your-lib'
Trumbowyg = {
mounted() {
....
},
updated(){ 
....
}
}
export default Trumbowyg;

import it in your app.js and add it your hooks:

let Hooks = { ..., Trumbowyg, ...  }
...
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: Hooks})

Once all this is set up you add it to your live component root element with phx-hook

<div id="some-id" class="some-classes" phx-hook="Trumbowyg">

and your imported JS should work on that component.

Hope that helps.

huangapple
  • 本文由 发表于 2023年2月16日 06:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定