• Stars
    star
    159
  • Rank 228,062 (Top 5 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 3 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A plugin for Vite enables you to compile an Elm application/document/element

vite-plugin-elm

npm npm next channel GitHub Workflow Status

A plugin that enables us to compile an Elm application, document, or element within your Vite project. In development, hot module replacement works to some extent.

import { Elm } from './MyApplication.elm'

Elm.MyApplication.init()

Setup

npm i -D vite-plugin-elm

In vite.config.(js|ts):

import { defineConfig } from 'vite'
import elmPlugin from 'vite-plugin-elm'

export default defineConfig({
  plugins: [elmPlugin()]
})

Then you can import a .elm file and run it like:

import { Elm } from './Hello.elm'

// Mount "Hello" Browser.{element,document} on #root
Elm.Hello.init({
  node: document.getElementById('root'),
  flags: "Initial Message"
})

You can explore the /example directory to experiment with an actual Vite project. Additionally, this functional website can serve as a helpful resource to learn how to use it effectively.

Options

debug (Default: process.env.NODE_ENV !== 'production')

You can control the debug mode of Elm, which toggles the Elm Debugger, by providing a boolean value.

image

elmPlugin({ debug: false })

When set to false, it disables debug mode in both development and production. Conversely, setting it to true enables debug mode even in production. It's important to note that when the production build has debug mode enabled, Elm's compiler optimizations do not take place.

optimize (Default: !debug && process.env.NODE_ENV === 'production')

You can control build optimization by providing a boolean value, which can be useful for using Debug functions in your Elm code.

elmPlugin({ debug: false, optimize: false })

When set to true, it optimizes the build and prohibits the usage of Debug Elm functions. If you specify the optimize attribute, you must indicate whether debugging is needed. This is because you might want to have debug traces for the purpose of observing all actions, not necessarily for debugging specific issues.

Static Assets Handling

This plugin supports importing assets by using a specific tag [VITE_PLUGIN_ELM_ASSET:<path to asset>] to leverage Vite's asset handling. When your Elm code contains a tag, this plugin replaces that string with the imported asset. It's important to note that the string should be a standalone string without any concatenation.

Html.img [ Html.Attributes.src "[VITE_PLUGIN_ELM_ASSET:/assets/logo.jpg]" ] []

Helper package

We can simplify the tagging process by using the Elm package elm-vite-plugin-helper:

elm install hmsk/elm-vite-plugin-helper
import VitePluginHelper

Html.img [ Html.Attributes.src <| VitePluginHelper.asset "/assets/logo.png?inline" ] []

Combine multiple main files (Experimental)

By passing importing paths via the with URL-ish parameter(s), the plugin compiles multiple main files in a single compilation process. This results in a single Elm export that contains multiple properties, each corresponding to a given main file. This feature helps reduce the bundle size when compared to importing each file separately because common modules and Elm core code are not repeated.

// `Elm.App` and `Elm.Another`, both can work as like importing individually.
import { Elm } from './App.elm?with=./Another.elm'

Elm.App.init({
  node: document.getElementById('rootForApp'),
})
Elm.Another.init({
  node: document.getElementById('rootForAnother'),
})

For 3+ main files:

import { Elm } from './App.elm?with=./Another.elm&with=./YetAnother.elm'

Acknowledgement

License

MIT

More Repositories

1

frontmatter-markdown-loader

📝 Webpack Loader for: FrontMatter (.md) -> HTML + Attributes (+ React/Vue Component)
JavaScript
251
star
2

vite-plugin-markdown

A vite plugin to import a Markdown file in various formats like Front Matter, HTML, ToC, and React/Vue Component
TypeScript
204
star
3

jest-matcher-vue-test-utils

✨ Cute jest matchers to test Vue components with vue-test-utils
TypeScript
32
star
4

nuxt-ts

⚠️ DEPRECATED: Enable your Nuxt project to use TypeScript easier and quicker
JavaScript
22
star
5

nuxt-typescript-template

Handy starter for Nuxt 2 + TypeScript
JavaScript
11
star
6

frontmatter-markdown-loader-vue-sample

A sample project for compiled Vue by frontmatter-markdown-loader
JavaScript
10
star
7

hmsk.me

🎮 A personal website of @hmsk with the UI like "Ring Command" from "Secret of Mana"
Elm
10
star
8

netlify-functions-in-go

Deployable template of multiple Go lambda functions for Netlify Functions
Go
8
star
9

hubot-esa

A Hubot script handling webhooks and retrieving info from esa: https://esa.io
CoffeeScript
8
star
10

frontmatter-markdown-loader-nuxt-sample

Nuxt app to demonstrate the usage of frontmatter-markdown-loader
Vue
7
star
11

ring-fit-adventure-icons

128x128 icons for Slack Team/Discord Guild with respecting Ring Fit Adventure by Nitendo.
7
star
12

symlink-webpack-plugin

🔗 Make symbolic links among files built by Webpack
JavaScript
5
star
13

harp-gh-pages-boilerplate

The boilerplate of harp project publishing compiled files to GitHub Pages
JavaScript
4
star
14

text.hmsk.me

A blog system with Elm, siteelm, and Netlify CMS
Elm
3
star
15

generator-harp-gh-pages

Yeoman generater for Harp project deploying with GitHub Pages, automating with Circle CI
JavaScript
3
star
16

webpay-sinatra-sample

Try WebPay API with Ruby, Sinatra
Ruby
3
star
17

persona-js

An unofficial JS client for Persona withpersona.com
TypeScript
3
star
18

hubot-sandbox

Shell
1
star
19

nord-atom-ui-vertical-tabs

Change tab direction of nord-atom-ui
CoffeeScript
1
star
20

elm-css-modern-normalize

An Elm port of modern-normalize for rtfeldman/elm-css.
Elm
1
star
21

lzfx

Ruby
1
star
22

elm-vite-plugin-helper

An Elm package to provide helpers for using vite-plugin-elm
Elm
1
star