【问题标题】:How to setup Vue.js with Elixir Phoenix 1.6 and esbuild如何使用 Elixir Phoenix 1.6 和 esbuild 设置 Vue.js
【发布时间】:2021-12-31 07:08:49
【问题描述】:

Phoenix 1.6 使用 esbuild 而不是 Webpack。我找不到任何关于如何使用 Phoenix 1.6 和 esbuild 设置 Vue.js(版本 2 或 3)的示例。

如果有人可以提供有关如何执行此操作的分步说明,我将不胜感激。谢谢

【问题讨论】:

    标签: javascript vue.js elixir phoenix-framework esbuild


    【解决方案1】:

    最终的工作代码可以在这里找到: https://github.com/weeksseth/phoneix_vue_chat

    创建 Phoenix 项目

    假设您已安装 Elixir、Hex 和 Phoenix (v 1.6+),请使用 mix phx.new <project_name> 创建一个新的 Phoenix 项目。我添加了--no-ecto 标志,因为我目前没有使用数据库。

    配置 esbuild

    将目录更改为 assets 文件夹并安装所需的依赖项:

    npm i esbuild esbuild-vue -D
    npm i vue ../deps/phoenix ../deps/phoenix_html ../deps/phoenix_live_view
    

    创建一个assets/build.js 文件并在其中添加以下代码:

    const esbuild = require('esbuild')
    const vuePlugin = require("esbuild-vue")
    
    const args = process.argv.slice(2)
    const watch = args.includes('--watch')
    const deploy = args.includes('--deploy')
    
    const loader = {
      // Add loaders for images/fonts/etc, e.g. { '.svg': 'file' }
    }
    
    const plugins = [
      vuePlugin()
    ]
    
    let opts = {
      entryPoints: ['js/app.js'],
      bundle: true,
      target: 'es2017',
      outdir: '../priv/static/assets',
      logLevel: 'info',
      loader,
      plugins
    }
    
    if (watch) {
      opts = {
        ...opts,
        watch,
        sourcemap: 'inline'
      }
    }
    
    if (deploy) {
      opts = {
        ...opts,
        minify: true
      }
    }
    
    const promise = esbuild.build(opts)
    
    if (watch) {
      promise.then(_result => {
        process.stdin.on('close', () => {
          process.exit(0)
        })
    
        process.stdin.resume()
      })
    }
    

    修改config/dev.exs中的观察者使用节点:

    config :hello, HelloWeb.Endpoint,
      ...
      watchers: [
    -     esbuild: {Esbuild, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]}
    +     node: ["build.js", "--watch", cd: Path.expand("../assets", __DIR__)]
      ],
      ...
    

    修改mix.exs中的别名以在安装过程中安装npm包:

    defp aliases do
        [
    -     setup: ["deps.get", "ecto.setup"],
    +     setup: ["deps.get", "ecto.setup", "cmd --cd assets npm install"],
          "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
          "ecto.reset": ["ecto.drop", "ecto.setup"],
          test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
    -     "assets.deploy": ["esbuild default --minify", "phx.digest"]
    +     "assets.deploy": ["cmd --cd assets node build.js --deploy", "phx.digest"]
        ]
      end
    

    删除 esbuild 配置 config/config.exs:

    - config :esbuild,
    -   version: "0.14.0",
    -   default: [
    -     args:
    -       ~w(js/app.js --bundle --target=es2017 --outdir=../priv/static/assets --external:/fonts/* --external:/images/*),
    -     cd: Path.expand("../assets", __DIR__),
    -     env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
    -   ]
    

    最后从mix.exs中移除esbuild依赖:

      defp deps do
        [
          {:phoenix, "~> 1.6.6"},
          {:phoenix_html, "~> 3.0"},
          {:phoenix_live_reload, "~> 1.2", only: :dev},
          {:phoenix_live_view, "~> 0.17.5"},
          {:floki, ">= 0.30.0", only: :test},
          {:phoenix_live_dashboard, "~> 0.6"},
    -     {:esbuild, "~> 0.3", runtime: Mix.env() == :dev},
          {:swoosh, "~> 1.3"},
          {:telemetry_metrics, "~> 0.6"},
          {:telemetry_poller, "~> 1.0"},
          {:gettext, "~> 0.18"},
          {:jason, "~> 1.2"},
          {:plug_cowboy, "~> 2.5"}
        ]
      end
    

    添加 Vue

    在assets/js/components/Component.vue中新建一个Vue组件,内容如下:

    <template>
      <h1>Hello world!</h1>
    </template>
    

    将assets/js/app.js中的代码替换为:

    import Component from "./components/Component.vue";
    import Vue from "vue";
    
    new Vue({
      el: "#app",
      render: (h) => h(Component),
    });
    

    在lib/&lt;project_name&gt;_web/templates/page/index.html.heex末尾添加如下代码:

    <section id="app">
    </section>
    

    最后,使用mix phx.server 启动您的 Phoenix 服务器,您应该会看到默认的 Phoenix 应用程序,最后有一个部分表示问候地球。如果您修改 Vue 组件并保存它,页面应该会自动重新呈现您的更改。

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2022-12-04
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2022-01-11
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多