Generate type-safe image asset record.

This commit is contained in:
Dillon Kearns 2019-08-21 08:18:13 -07:00
parent 9b863c1b9f
commit 94a3f1d8b5
3 changed files with 25 additions and 4 deletions

View File

@ -109,6 +109,11 @@ function webpackOptions(
}
}
]),
new CopyPlugin([
{
from: "images/**/*"
}
]),
new ImageminPlugin({
test: /\.(jpe?g|png|gif|svg)$/i,
disable: !production,

View File

@ -1,10 +1,18 @@
const exposingList = "(application, PageRoute, all, pages, routeToString)";
const exposingList =
"(application, PageRoute, all, pages, routeToString, Image, imageUrl, images)";
function staticRouteStuff(staticRoutes) {
return `
type PageRoute = PageRoute (List String)
type Image = Image (List String)
imageUrl : Image -> String
imageUrl (Image path) =
"/"
++ String.join "/" path
${staticRoutes.allRoutes}
${staticRoutes.routeRecord}

View File

@ -35,10 +35,11 @@ function unpackFile() {
function relativeImagePath(imageFilepath) {
var pathFragments = imageFilepath;
//remove extesion and split into fragments
const fragmentsWithExtension = pathFragments.split(path.sep);
pathFragments = pathFragments.replace(/\.[^/.]+$/, "").split(path.sep);
const fullPath = imageFilepath;
var relative = imageFilepath.slice(dir.length - 1);
return { path: relative, pathFragments };
return { path: relative, pathFragments, fragmentsWithExtension };
}
function generate(scanned) {
@ -92,7 +93,7 @@ function generate(scanned) {
// routeToMetadata: formatCaseStatement("toMetadata", routeToMetadata),
// routeToDocExtension: formatCaseStatement("toExt", routeToExt),
// routeToSource: formatCaseStatement("toSourcePath", routeToSource),
imageAssetsRecord: toElmRecord("assets", getImageAssets(), false)
imageAssetsRecord: toElmRecord("images", getImageAssets(), true)
};
}
function getImageAssets() {
@ -103,7 +104,14 @@ function getImageAssets() {
.filter(filePath => !fs.lstatSync(filePath).isDirectory())
.map(relativeImagePath)
.forEach(info => {
captureRouteRecord(info.pathFragments, info.path, assetsRecord);
const elmType =
"(Image [ " +
info.fragmentsWithExtension
.map(fragment => `"${fragment}"`)
.join(", ") +
" ])";
captureRouteRecord(info.pathFragments, elmType, assetsRecord);
});
return assetsRecord;
}