npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elm-build-config

v1.0.2

Published

Generates static build-time configuration modules for elm apps

Downloads

7

Readme

The Basics

elm-build-config provides a simple utility for generating elm files that contain static configuration key-value pairs defined at build time. e.g.:

module BuildConfig exposing (..)

deployPath : String
deployPath = "/my/deploy/path/"

isProduction : Bool
isProduction = True

To generate a config file, use the createConfigFile function:

import { createConfigFile } from "elm-build-config";

// Normally this would use dynamic values injected by your build tooling.
const config = {
  bool: true,
  string: "hello",
  int: 1,
  float: 3.14159
};

createConfigFile(config);

The configuration object passed to createConfigFile can contain booleans, strings and numbers (which will be converted to int or float depending on value). Any invalid values will cause the file generation to fail.

By default the output file will be located at src/BuildConfig.elm, but you can customize the output with options:

createConfigFile(config, { srcDir: "elmApp/src", moduleName: "Config.Build" });

The available options are:

srcDir: The path to and elm src dir, as defined in elm.json. (Default: src)

moduleName: The desired name of the output module. The output file will be generated at the appropriate path based on the source dir and the chosen name. (Default: BuildConfig)

Example Usage

elm-build-config isn't that useful on it's own. It's intended to be part of a build process. Here we'll outline an example of using it in an elm-spa app built by Vite. In this app, the eventual deploy path is set by an external process that provides a DEPLOY_PATH environment variable.

First, we update our vite.config.js file to run createConfigFile when the build starts, by defining a small plugin in the config file:

import { defineConfig } from "vite";
import elmPlugin from "vite-plugin-elm";
import { createConfigFile } from "elm-build-config";

export default defineConfig(({ command, mode }) => {
  //Pull vite's basePath from the environment if present
  const basePath = process.env.DEPLOY_PATH || "./";

  return {
    root: "public",
    base: basePath,
    plugins: [
      elmPlugin(),
      // We pass the path to a locally defined plugin
      configPlugin({ deployPath: basePath })
    ],
    build: {
      outDir: "../dist",
      emptyOutDir: true
    }
  };
});

// This is a minimal vite plugin that calls `createConfigFile` with our config
function configPlugin(configData, options) {
  return {
    name: "elm-build-config",
    buildStart() {
      createConfigFile(configData, options);
    }
  };
}

This creates a BuildConfig.elm file in our project:

module BuildConfig exposing (..)

deployPath: String
deployPath = "/deploy/path/"

From this, we create a Path.elm file with a few useful utility functions:

module Path exposing (inApp, normalizeUrl)

import BuildConfig exposing (deployPath)
import Url exposing (Url)


{-| Convert a path relative to the root of the app to a path based on our
deploy location. This is useful for generating links and navigation commands.
-}
inApp : String -> String
inApp path =
    deployPath ++ path


{-| Given a Url with a full path, trim the deployPath from the front of it. If
we do this before handing the Url to elm-spa for routing, everything should work
nicely.
-}
normalizeUrl : Url -> Url
normalizeUrl url =
    let
        newPath =
            if String.startsWith deployPath url.path then
                String.dropLeft (String.length deployPath) url.path

            else
                url.path
    in
    { url | path = newPath }

The we update Main.elm (ejecting from .elm-spa/defaults if needed):

init : Shared.Flags -> Url -> Key -> ( Model, Cmd Msg )
init flags rawUrl key =
    let
        -- Normalize the url before init
        url =
            Path.normalizeUrl rawUrl

        ( shared, sharedCmd ) =
            Shared.init (Request.create () url key) flags

        ( page, effect ) =
            Pages.init (Route.fromUrl url) shared url key
    in
    ( Model url key shared page
    , Cmd.batch
        [ Cmd.map Shared sharedCmd
        , Effect.toCmd ( Shared, Page ) effect
        ]
    )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        ChangedUrl rawUrl ->
            let
                -- Normalize incoming Urls
                url =
                    Path.normalizeUrl rawUrl
            in
            if url.path /= model.url.path then
                let
                    ( page, effect ) =
                        Pages.init (Route.fromUrl url) model.shared url model.key
                in
                ( { model | url = url, page = page }
                , Effect.toCmd ( Shared, Page ) effect
                )

            else
                ( { model | url = url }, Cmd.none )

And that's it! (probably, I just built this thing)