HTML5 Boilerplate - 处理多个 CSS 文件?

发布于 2024-12-27 09:41:28 字数 3432 浏览 1 评论 0原文

我有一个使用 html5 样板构建的 Web 应用程序,有多个可用的皮肤。

每个皮肤都包含在一个单独的 css 文件中,并且使用哪个皮肤是通过配置文件设置的。

目前,HTML5 样板构建脚本缩小并重命名了 style.css(默认 css 文件)中的第一个皮肤,它还缩小了我在同一文件夹中的其他两个 css 文件,但它确实如此重命名它们,这将导致实时服务器上的缓存出现问题 - 当 css 文件具有遥远的到期日期时,用户的 Web 浏览器将不会拾取其他皮肤的更新。

谁能给我一些关于如何添加对额外 css 文件的支持的提示?

为了澄清,在运行构建脚本之前,我的 css 文件夹中有以下文件:
样式.css
皮肤2.css
Skin3.css

和运行构建脚本后:
e3b847ea91a5666541ef13b4d9e0797342f5fc31.css ->好
皮肤2.css ->不好
Skin3.css->糟糕,

我已经从构建脚本中提取了我认为相关的代码,并添加了一些注释来解释正在发生的事情:

    <!-- copy source file to intermediate directory -->
    <copy file="${dir.source}/${dir.css}/${file.root.stylesheet}" tofile="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"/>

    <!-- copy skeleton to concat file -->
    <copy file="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"
          tofile="${dir.intermediate}/${dir.css}/style-concat.css" overwrite="true"/>

    <!-- load the file into a property -->
    <loadfile property="imports" srcfile="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"/>

    <var name="concat-files" value="${file.root.stylesheet}"/>


    <!--minify CSS-->
    <apply executable="java" parallel="false">
        <fileset dir="${dir.intermediate}/${dir.css}/" includes="style-concat.css"/>
        <arg line="-jar"/>
        <arg path="${dir.build.tools}/${tool.yuicompressor}"/>
        <srcfile/>
        <arg line="-o"/>
        <mapper type="merge" to="${basedir}/${dir.intermediate}/${dir.css}/style-concat.min.css"/>
        <targetfile/>
    </apply>

<!--calculate checksum of css file (this is used for filename)-->
    <checksum file="${dir.intermediate}/${dir.css}/style-concat.min.css" algorithm="sha" property="css.sha" />
    <if>
        <isset property="gae.css_dir" />
        <then>
            <property name="style.css" value="${gae.css_dir}/${css.sha}.css" />
        </then>
        <else>
            <property name="style.css" value="${dir.css}/${css.sha}.css" />
        </else>
    </if>
    <copy file="${dir.intermediate}/${dir.css}/style-concat.min.css" tofile="${dir.publish}/${dir.css}/${css.sha}.css" />

    <!--minify REMAINING CSS files (my other skins)-->

    <apply executable="java" parallel="false">
        <fileset dir="${dir.source}/${dir.css}/" excludes="${concat-files}" includes="**/*.css"/>
        <arg line="-jar"/>
        <arg path="${dir.build.tools}/${tool.yuicompressor}"/>
        <srcfile/>
        <arg line="-o"/>
        <mapper type="glob" from="*.css" to="${basedir}/${dir.publish}/${dir.css}/*.css"/>
        <targetfile/>
    </apply>
    <foreach list="${file.stylesheets}" param="css_file" target="-css-remove-concatenated-stylesheets" />   


    <!--replace reference to css in source with new filename-->
    <replaceregexp match="&lt;!-- CSS concatenated [\d\w\s\W]*?!-- end CSS--&gt;" replace="&lt;link rel='stylesheet' href='${style.css}'&gt;" flags="m">
        <fileset dir="${dir.intermediate}" includes="${page-files}"/>
    </replaceregexp>

完整的构建脚本可在此处获得:http://pastebin.com/Cm1LzJpE

I have a web app built with html5 boilerplate that has multiple skins available.

Each skin is contained in a separate css file, and which skin to use is set via a config file.

At the moment the HTML5 boilerplate build script minifies and renames the first skin, which is in style.css (the default css file), it also minifies the other two css files I have in the same folder, but it doesn't rename them, this will cause problems with caching on the live server - updates to the other skins won't be picked up by a user's web browser when the css files have far-future expiration dates.

Can anyone give me some tips on how to add support for the extra css files?

To clarify, before running the build script I have the following files in my css folder:
style.css
skin2.css
skin3.css

and after running the build script:
e3b847ea91a5666541ef13b4d9e0797342f5fc31.css -> good
skin2.css -> bad
skin3.css -> bad

I've pulled out what I believe to be the relevant code from the build script, and added some comments to explain what is going on:

    <!-- copy source file to intermediate directory -->
    <copy file="${dir.source}/${dir.css}/${file.root.stylesheet}" tofile="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"/>

    <!-- copy skeleton to concat file -->
    <copy file="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"
          tofile="${dir.intermediate}/${dir.css}/style-concat.css" overwrite="true"/>

    <!-- load the file into a property -->
    <loadfile property="imports" srcfile="${dir.intermediate}/${dir.css}/${file.root.stylesheet}"/>

    <var name="concat-files" value="${file.root.stylesheet}"/>


    <!--minify CSS-->
    <apply executable="java" parallel="false">
        <fileset dir="${dir.intermediate}/${dir.css}/" includes="style-concat.css"/>
        <arg line="-jar"/>
        <arg path="${dir.build.tools}/${tool.yuicompressor}"/>
        <srcfile/>
        <arg line="-o"/>
        <mapper type="merge" to="${basedir}/${dir.intermediate}/${dir.css}/style-concat.min.css"/>
        <targetfile/>
    </apply>

<!--calculate checksum of css file (this is used for filename)-->
    <checksum file="${dir.intermediate}/${dir.css}/style-concat.min.css" algorithm="sha" property="css.sha" />
    <if>
        <isset property="gae.css_dir" />
        <then>
            <property name="style.css" value="${gae.css_dir}/${css.sha}.css" />
        </then>
        <else>
            <property name="style.css" value="${dir.css}/${css.sha}.css" />
        </else>
    </if>
    <copy file="${dir.intermediate}/${dir.css}/style-concat.min.css" tofile="${dir.publish}/${dir.css}/${css.sha}.css" />

    <!--minify REMAINING CSS files (my other skins)-->

    <apply executable="java" parallel="false">
        <fileset dir="${dir.source}/${dir.css}/" excludes="${concat-files}" includes="**/*.css"/>
        <arg line="-jar"/>
        <arg path="${dir.build.tools}/${tool.yuicompressor}"/>
        <srcfile/>
        <arg line="-o"/>
        <mapper type="glob" from="*.css" to="${basedir}/${dir.publish}/${dir.css}/*.css"/>
        <targetfile/>
    </apply>
    <foreach list="${file.stylesheets}" param="css_file" target="-css-remove-concatenated-stylesheets" />   


    <!--replace reference to css in source with new filename-->
    <replaceregexp match="<!-- CSS concatenated [\d\w\s\W]*?!-- end CSS-->" replace="<link rel='stylesheet' href='${style.css}'>" flags="m">
        <fileset dir="${dir.intermediate}" includes="${page-files}"/>
    </replaceregexp>

Full build script is available here: http://pastebin.com/Cm1LzJpE

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千年*琉璃梦 2025-01-03 09:41:28

如果我正确理解您的构建文件,您就不会执行

<checksum>
 ...
<copy file="${dir.intermediate}/${dir.css}/style-concat.min.css" tofile="${dir.publish}/${dir.css}/${css.sha}.css" />

skin*.css 文件的部分。你这样做只是为了
style.css

第二次应用后没有校验和部分。您还需要对每个样式文件重复校验和复制。
此问题可能有助于完成此任务: Ant:重命名要包含的文件他们的MD5

If I understand your buildfile correctly you don't do the

<checksum>
 ...
<copy file="${dir.intermediate}/${dir.css}/style-concat.min.css" tofile="${dir.publish}/${dir.css}/${css.sha}.css" />

part for the skin*.css files. You only do it for the
style.css.

After the second apply there is no checksum part. You need to repeat checksumming and copying for every style file too.
This question might help with this task: Ant: Rename files to include their MD5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文