加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在Play中通过“activator run”运行时获取要编译的资源

发布时间:2020-12-16 18:04:55 所属栏目:安全 来源:网络整理
导读:我正在使用Sass作为我的CSS preprocesser,我正试图让它通过资产管道运行.我已经尝试将这个sassTask实现为源文件任务和Web资产任务,但我遇到了两个方面的问题. 如果我将Sass作为源任务运行(见下文),则在请求页面时激活器运行期间会触发它,并在页面重新加载时
我正在使用Sass作为我的CSS preprocesser,我正试图让它通过资产管道运行.我已经尝试将这个sassTask实现为源文件任务和Web资产任务,但我遇到了两个方面的问题.

如果我将Sass作为源任务运行(见下文),则在请求页面时激活器运行期间会触发它,并在页面重新加载时找到更新的文件.我遇到的问题是生成的CSS文件都被直接转储到target / web / public / main / lib中,而不是转移到子目录中,这些子目录反映了它们在资源管理目录下构建的子目录.我无法弄清楚如何实现这一目标.

相反,我尝试将Sass编译实现为Web资产任务(见下文).以这种方式工作,据我所知,资源管理没有发挥作用,所以我将我的文件直接编译到target / web / public / main / lib.我确定我没有足够动态地做这件事,但我不知道怎么做得更好.但这里最大的问题是,当通过激活器运行时,管道显然不会运行.我可以使用激活器阶段运行它,但我真的需要在常规开发工作流程中工作,以便我可以在开发服务器运行时更??改样式文件,与Scala文件相同.

我试过梳理这些论坛,通过sbt-web文档,以及一些现有的插件,但我发现这个过程非常令人沮丧,因为SBT的复杂性和实际发生的事情的不透明性.构建过程.

Sass编译作为源文件任务:

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编译为Web资产任务:

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",libDir,s"$sourceDir:$targetDir").!
    val sources = sourceDir ** "*.scss"
    val mappings = sources pair relativeTo(sourceDir)
    val renamed = mappings map { case (file,"css") }
    renamed
}

pipelineStages := Seq(sassTask)

解决方法

我认为根据与 Asset Pipeline相关的文档,源文件任务是一种方法:

Examples of source file tasks as plugins are CoffeeScript,LESS and
JSHint. Some of these take a source file and produce a target web
asset e.g. CoffeeScript produces JS files. Plugins in this category
are mutually exclusive to each other in terms of their function i.e.
only one CoffeeScript plugin will take CoffeeScript sources and
produce target JS files. In summary,source file plugins produce web
assets.

我认为你试图实现的目标属于这一类.

TL; DR; – build.sbt

val sassTask = taskKey[Seq[File]]("Compiles Sass files")

val sassOutputDir = settingKey[File]("Output directory for 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",s"$sourceDir:$outputDir").!
  (outputDir ** "*.css").get
}

sourceGenerators in Assets += sassTask.taskValue

说明

假设你在app / assets /< whatever>中有sass文件目录,以及你想在web / public / main /< whatever>中创建css文件目录,这是你可以做的.

创建一个任务,该任务将读入app / assets /< whatever>中的文件.目录和子目录,并将它们输出到我们定义的sassOutputDir.

val sassTask = taskKey[Seq[File]]("Compiles Sass files")

val sassOutputDir = settingKey[File]("Output directory for 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",s"$sourceDir:$outputDir").!
  (outputDir ** "*.css").get
}

但这还不够.如果要保留目录结构,则必须将sassOutputDir添加到Assets中的resourceDirectories.这是因为sbt-web中的映射声明如下:

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
}

这意味着所有未映射的文件都使用alternative平面策略进行映射.但是它的修复很简单,只需将它添加到build.sbt即可

resourceDirectories in Assets += sassOutputDir.value

这将确保保留目录结构.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读