3

我目前正在使用 ruby​​ gem SASS将我的 *.scss 文件转换为大型项目中的 *.css 文件。这是我正在使用的代码的模型:

<?xml version="1.0"?>
<!-- scss to CSS -->
<project name="StackOverflowScssCss" default="sass-compile-to-css" basedir=".">
    <property file="build.properties" />

    <target name="sass-compile-to-css">
        <echo message="Compiling scss files to css..." />

        <!-- create the css destination dir if it doesn't already exist -->
        <property name="css-dest" location="${css.dir}" />
        <echo message="Creating directory at ${css.dir} [if it doesn't yet exist]" />
        <mkdir dir="${css-dest}" />


        <!-- create subdirs if necessary -->
        <echo message="Creating css directories (and temporary .css files) for .scss to be compiled..." />
        <touch mkdirs="true">
            <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
            <mapper type="glob" from="*.scss" to="${css.dir}/*.css" />
        </touch>
        <echo message="Running sass executable against sass files and compiling to CSS directory [${css-dest}] " />

        <!-- run sass executable -->

        <apply executable="sass" dest="${css-dest}" verbose="true" force="true" failonerror="true">
            <arg value="--unix-newlines" />
            <!-- Disable creation of map file. THIS SHOULD BE A FLAG  -->
            <arg value="--sourcemap=none" />
            <srcfile />
            <targetfile />
            <fileset dir="${sass.dir}" includes="**/*.scss" excludes="**/_*" />
            <mapper type="glob" from="*.scss" to="*.css" />
        </apply>
        <echo message="Done compiling scss files!" />
    </target>
</project>

最终我想删除 ruby​​ 依赖项,所以我一直在查看这个 libsass maven 插件。我知道libsass有很多选择,但我试图严格遵守 Java。有没有人有这样做的经验?我不想运行 Node.js、Sass.js 等任何东西,而且我整天都在绞尽脑汁思考如何做到这一点。任何帮助深表感谢!

4

2 回答 2

0

有一个可以与 Maven 一起使用的Java 包装器。

如果你真的想坚持使用 Java,你可以

  • 使用该包装器并mvn从您的 ant 脚本中运行任务(请参见此处
  • 调整该包装器并从中构建您自己的 Ant 任务
于 2015-06-23T05:42:59.030 回答
0

我最终使用了 JRuby 依赖项以便在 JVM 上运行。我的代码如下:

<!-- Jruby Dependent SCSS to CSS conversion -->
<path id="JRuby">
    <fileset file="packages/jruby-complete-1.7.20.1.jar"/> <!-- Location of JRuby jar file -->
</path>  
<target name="compileSass" depends="cleanSass">
    <echo message="Compiling scss files..." />

    <!-- JRuby Script to convert files into new directory -->
    <property name="filesIn" value="${dir.scss}/**/[^_]*.scss" />
    <property name="projectDirectory" value="${user.dir}"/>
    <script language="ruby" classpathref="JRuby">
        <![CDATA[
            require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass'
            require ($project.getProperty('projectDirectory')) + '/packages/sass-3.4.14/lib/sass/exec'

            files = Dir.glob($project.getProperty('filesIn'))
            files.each do 
                | file |
                newOutDir = File.dirname(file).sub! 'scss', 'css'
                FileUtils::mkdir_p newOutDir
                puts "[sass compiler] From:" + file
                puts "[sass compiler]   To:" + newOutDir + "/" + File.basename(file, ".*") + ".css"

                opts = Sass::Exec::SassScss.new(["--load-path", File.dirname(file), file, File.join(newOutDir, File.basename(file, ".*") + ".css")], 'scss')
                opts.parse
            end
        ]]>
    </script>
    <echo message="Done compiling scss files!" />
</target>


<target name="cleanSass">
    <echo message="removing .css files..." />
    <delete includeemptydirs="true" failonerror="false">
        <fileset dir="${dir.css}" includes="**/*.css" />
    </delete>
    <echo message="removing .css.map files..." />
    <delete includeemptydirs="true" failonerror="false">
        <fileset dir="${dir.css}" includes="**/*.css.map" />
    </delete>
</target>
于 2015-06-29T13:09:01.010 回答