scala - Get assets to compile when running via "activator run" in Play -
i using sass css preprocesser, , i'm trying have run via asset pipeline. i've tried implementing sasstask source file task , web asset task, i'm running problems both ways.
if run sass source task (see below), gets triggered during activator run when page requested , updated files found upon page reloads. problem i'm running resulting css files getting dumped straight target/web/public/main/lib, instead of subdirectories reflecting ones getting built under resources-managed directory. can't figure out how create happen.
instead, tried implementing sass compilation web asset task (see below). working way, far can tell, resources-managed not come play, , compile files straight target/web/public/main/lib. i'm sure i'm not doing dynamically enough, don't know how better. biggest problem here pipeline apparently not run when working through activator run. can run using activator stage, need work in regular development workflow can alter style files dev server running, same scala files.
i have tried combing through these forums, through sbt-web docs, , through of existing plugins, finding process highly frustrating, due complexity of sbt , opaqueness of happening in build process.
sass compilation source file task:
lazy val sasstask = taskkey[seq[java.io.file]]("sasstask", "compiles sass files") sasstask := { import sys.process._ val x = (webkeys.nodemodules in assets).value val sourcedir = (sourcedirectory in assets).value val targetdir = (resourcemanaged in assets).value seq("sass", "-i", "target/web/web-modules/main/webjars/lib/susy/sass", "--update", s"$sourcedir:$targetdir").! val sources = sourcedir ** "*.scss" val mappings = sources pair relativeto(sourcedir) val renamed = mappings map { case (file, path) => file -> path.replaceall("scss", "css") } val copies = renamed map { case (file, path) => file -> targetdir / path } copies map (_._2) } sourcegenerators in assets <+= sasstask sass compilation web asset task:
lazy val sasstask = taskkey[pipeline.stage]("compiles sass files") sasstask := { (mappings: seq[pathmapping]) => import sys.process._ val sourcedir = (sourcedirectory in assets).value val targetdir = target.value / "web" / "public" / "main" val libdir = (target.value / "web" / "web-modules" / "main" / "webjars" / "lib" / "susy" / "sass").tostring seq("sass", "-i", libdir, "--update", s"$sourcedir:$targetdir").! val sources = sourcedir ** "*.scss" val mappings = sources pair relativeto(sourcedir) val renamed = mappings map { case (file, path) => file -> path.replaceall("scss", "css") } renamed } pipelinestages := seq(sasstask)
i think according documentation related asset pipeline, source file task way go:
examples of source file tasks plugins coffeescript, less , jshint. of these take source file , produce target web asset e.g. coffeescript produces js files. plugins in category mutually exclusive each other in terms of function i.e. 1 coffeescript plugin take coffeescript sources , produce target js files. in summary, source file plugins produce web assets.
i think seek accomplish falls category.
tl;dr; -build.sbt val sasstask = taskkey[seq[file]]("compiles sass files") val sassoutputdir = settingkey[file]("output directory sass generated files") sassoutputdir := target.value / "web" / "sass" / "main" resourcedirectories in assets += sassoutputdir.value sasstask := { val sourcedir = (sourcedirectory in assets).value val outputdir = sassoutputdir.value val sourcefiles = (sourcedir ** "*.scss").get seq("sass", "--update", s"$sourcedir:$outputdir").! (outputdir ** "*.css").get } sourcegenerators in assets += sasstask.taskvalue explanation assuming have sass file in app/assets/<whatever> directory, , want create css files in web/public/main/<whatever> directory, do.
create task, read in files in app/assets/<whatever> directory , subdirectories, , output them our defined sassoutputdir.
val sasstask = taskkey[seq[file]]("compiles sass files") val sassoutputdir = settingkey[file]("output directory sass generated files") sassoutputdir := target.value / "web" / "sass" / "main" resourcedirectories in assets += sassoutputdir.value sasstask := { val sourcedir = (sourcedirectory in assets).value val outputdir = sassoutputdir.value val sourcefiles = (sourcedir ** "*.scss").get seq("sass", "--update", s"$sourcedir:$outputdir").! (outputdir ** "*.css").get } this not plenty though. if want maintain directory construction have add together sassoutputdir resourcedirectories in assets. because mappings in sbt-web declared this:
mappings := { val files = (sources.value ++ resources.value ++ webmodules.value) --- (sourcedirectories.value ++ resourcedirectories.value ++ webmoduledirectories.value) files pair relativeto(sourcedirectories.value ++ resourcedirectories.value ++ webmoduledirectories.value) | flat } which means unmapped files mapped using alternative flat strategy. prepare simple, add together build.sbt
resourcedirectories in assets += sassoutputdir.value this create sure directory construction preserved.
scala playframework sbt sbt-web
No comments:
Post a Comment