
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
babel-elm-assets-plugin
Advanced tools
This Babel plugin allows you to search for a particular function call in Elm, and replace it with a `require()` JS call. This allows you to use webpack generated assets directly in your Elm code.
This Babel plugin allows you to search for a particular function call in Elm, and replace it with a require()
JS call.
This allows you to use webpack generated assets directly in your Elm code.
import WebpackAsset exposing (assetUrl)
imageUrl =
assetUrl "./lost.png"
image : Html Msg
image =
img [ src imageUrl, alt "Image of man looking lost" ] []
At runtime imageUrl
will be set to the full path Webpack generates to point to "list.png". Often this will end up with a hash in the name: lost.6f3a4c.png
, or with a path to the place your assets are stored.
Add the elm package
elm install cultureamp/babel-elm-assets-plugin
Add the babel plugin using Yarn (or NPM)
yarn add --dev babel-elm-assets-plugin
Add the following rules to your webpack config:
// webpack.config.js
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
// babel-loader modifies the ouput of elm-webpack-loader
{
loader: "babel-loader",
options: {
plugins: ["module:babel-elm-assets-plugin"]
}
},
{
loader: "elm-webpack-loader",
options: {
// ...your usual options
// There may be some issues with using this plugin with optimized Elm builds
optimize: false
}
}
]
}
],
You can use multiple Babel plugins to post-process your Elm code. This allows you to:
WebpackAsset.assetUrl
Here's a more complicated example, where we include CSS modules, as well as a custom SVG loader.
-- Icon.SvgAsset.elm
type alias SvgAsset =
{ id : String
, viewBox : String
}
svgAsset : String -> SvgAsset
svgAsset path =
-- these placeholder values are replaced by Webpack at build time
{ id = "elm-svg-asset-placeholder"
, viewBox = "0 0 0 0"
}
Notice here that calls to Icon.SvgAsset.svgAsset
will expect to hold a SvgAsset
, as compared to calls to WebpackAsset.assetUrl
, which will hold a String
.
Now, the webpack config:
// webpack.config.js
rules: [
{
test: /\.elm$/,
exclude: [/elm-stuff/, /node_modules/],
use: [
{
loader: "babel-loader",
options: {
plugins: [
"module:elm-css-modules-plugin",
// If you use a plugin multiple times, you need to specify it as an array ["module:path", options, "unique-name"]
["module:babel-elm-assets-plugin", {}, "assets-plugin-generic"],
[
"module:babel-elm-assets-plugin",
{
// "author/project" is the default value if no "name" field is specified in elm.json.
package: "author/project",
module: "Icon.SvgAsset",
function: "svgAsset"
},
"assets-plugin-svg"
]
]
}
},
{
loader: "elm-webpack-loader",
options: {
// ...your usual options
// There may be some issues with using this plugin with optimized Elm builds
optimize: false
}
}
]
}
],
Let's work through the example above.
-- Main.elm
import WebpackAsset exposing (assetUrl)
imageUrl =
assetUrl "./lost.png"
image : Html Msg
image =
img [ src imageUrl, alt "Image of man looking lost" ] []
Normally when you compile this with Elm, the generated JS will look like:
var $cultureamp$babel_elm_assets_plugin$WebpackAsset$assetUrl = function (path) {
return path;
};
var author$project$Main$imageUrl = $cultureamp$babel_elm_assets_plugin$WebpackAsset$assetUrl(
"./lost.png"
);
Now our Babel plugin can transform that JS to use a require call:
var $cultureamp$babel_elm_assets_plugin$WebpackAsset$assetUrl = function (path) {
return path;
};
var author$project$Main$imageUrl = require("./lost.png");
Which webpack will know how to handle, meaning your final JS will do something similar to:
var $cultureamp$babel_elm_assets_plugin$WebpackAsset$assetUrl = function (path) {
return path;
};
var author$project$Main$myImageUrl = "/assets/lost-0fabe3.png";
The babel-elm-assets-plugin is maintained by the Delivery Engineering Team at Culture Amp.
FAQs
This Babel plugin allows you to search for a particular function call in Elm, and replace it with a `require()` JS call. This allows you to use webpack generated assets directly in your Elm code.
The npm package babel-elm-assets-plugin receives a total of 169 weekly downloads. As such, babel-elm-assets-plugin popularity was classified as not popular.
We found that babel-elm-assets-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.