【问题标题】:How to include a custom SASS file in a Vaadin 14 application?如何在 Vaadin 14 应用程序中包含自定义 SASS 文件?
【发布时间】:2019-12-02 14:33:39
【问题描述】:

我正在 Maven 上开发 Vaadin 14 应用程序,因此我使用 vaadin-maven-plugin,根据 Vaadin 的 v14 入门应用程序:

负责同步 java 依赖项和 package.json 和 main.js 文件中的导入。如果尚不存在,它还会创建 webpack.config.js。

运行应用程序后,我在根目录中获得了package.json 文件和node-modules。之后我执行了npm install bootstrap,并在src/main/webapp/frontend/ 中创建了一个自定义SCSS 文件,并带有一些精心挑选的@import 指令来获得我自己的Bootstrap 版本。

Vaadin 关于Including Style Sheets 的文档没有提到任何关于 SCSS 的内容。 如何让 Vaadin 的 Maven 插件编译我的 SCSS 文件,以便将生成的 CSS 文件导入我的应用程序?

【问题讨论】:

标签: twitter-bootstrap maven sass vaadin vaadin14


【解决方案1】:

在 vaadins theming documentation 中提到

Vaadin 14 本身没有使用 Sass,但如果您愿意,当然可以将它用于您自己的应用程序主题。您必须自己设置 sass-compiler 工作流程,但有可用的 Maven 插件。

经过快速搜索,我找到了这个sass-maven-plugin,它可能会满足您的需求;在打包之前将您的 sass/scss 文件编译为 css。

正如 cmets 中所指出的,还有libsass-maven-plugin

我自己还没有使用过这个(我会尝试,一旦我有时间)所以我不能告诉你如何最好地配置所说的插件。但我确信一旦配置正确,这就是将 SCSS/SASS 与 Vaadin 14 一起使用的方法

【讨论】:

  • @Kasper Scherrer,感谢您为我指明了正确的方向。您提到的 Maven 插件的 github 存储库已存档:“版本 3.7.2 可能是该插件的最终和最后一个版本,因为 (Ruby) Sass 已达到生命周期。”我进行了更多搜索,并使用了sass-lang.com/libsass#java 中列出的 LibSass Maven 插件。请使用此插件的参考更新您的答案,以便我接受。
  • 编辑了它。但我注意到最新版本的 libsass-maven-plugin 比最新的 sass-maven-plugin 早一年。我都没有尝试过,所以我不能说哪个更好。一旦我自己尝试一下,我会更新这个答案。
  • 你弄错了 libsass-maven-plugin,它是 mvnrepository.com/artifact/com.gitlab.haynes/…,它是从你添加的那个派生出来的,并且有更新的版本。
【解决方案2】:

按照@Kaspar Scherrer 的回答,通过执行以下步骤,我能够生成 Twitter Bootstrap 的自定义构建,以将其作为 CSS 包含在我的 Vaadin 14 应用程序中:

  1. 通过 npm 包含 Bootstrap 的源 Sass 和 JavaScript 文件,在项目根目录的 CLI 上运行 npm install bootstrap

  2. 在我项目的pom.xml 上设置libsass-maven-plugin

<!-- SASS compiler -->
<plugin>
  <groupId>com.gitlab.haynes</groupId>
  <artifactId>libsass-maven-plugin</artifactId>
  <version>0.2.21</version>
  <executions>
    <execution>
      <phase>generate-resources</phase>
      <goals>
        <goal>compile</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <inputPath>${basedir}/src/main/webapp/frontend/sass/</inputPath>
    <includePath>${basedir}/node_modules/</includePath>
    <outputPath>${basedir}/src/main/webapp/frontend/css/</outputPath>
    <failOnError>true</failOnError>
  </configuration>
</plugin>
  1. inputPath 上设置的目录内创建了自定义 SASS 文件,在此示例中命名为 webapp-bootstrap.scss
html {
  box-sizing: border-box;
  -ms-overflow-style: scrollbar;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

// Required
@import "bootstrap/scss/functions";
@import "bootstrap/scss/variables";

@import "bootstrap/scss/mixins/breakpoints";
@import "bootstrap/scss/mixins/clearfix";
@import "bootstrap/scss/mixins/deprecate";
@import "bootstrap/scss/mixins/grid-framework";
@import "bootstrap/scss/mixins/grid";
@import "bootstrap/scss/mixins/hover";
@import "bootstrap/scss/mixins/screen-reader";
@import "bootstrap/scss/mixins/text-emphasis";
@import "bootstrap/scss/mixins/text-hide";
@import "bootstrap/scss/mixins/text-truncate";


// Optional
@import "bootstrap/scss/grid";
@import "bootstrap/scss/utilities/align";
@import "bootstrap/scss/utilities/clearfix";
@import "bootstrap/scss/utilities/display";
@import "bootstrap/scss/utilities/flex";
@import "bootstrap/scss/utilities/float";
@import "bootstrap/scss/utilities/position";
@import "bootstrap/scss/utilities/screenreaders";
@import "bootstrap/scss/utilities/sizing";
@import "bootstrap/scss/utilities/spacing";
@import "bootstrap/scss/utilities/text";
  1. 通过使用注释 @StyleSheet("css/webapp-bootstrap.css") 在我的应用中包含生成的样式表。

【讨论】:

  • 感谢发布确认它有效,以及插件的配置。我想说你已经赢得了接受自己的答案,我不介意;)干杯
  • 完成。谢谢你的灵感:)
猜你喜欢
  • 2010-12-22
  • 2019-10-27
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
相关资源
最近更新 更多