twitter-bootstrap-3 - 更改使用 Grunt 编译的 Bootstrap 输出 LESS 文件

标签 twitter-bootstrap-3 gruntjs less

默认情况下,Grunt 将 bootstrap.less 编译为 bootstrap.css。问题是如何让它编译我的自定义 styles.less ,在其中我要将所有 Bootstrap less 文件以及我自己的一些文件导入到 styles.css 中?

最佳答案

我认为这个问题有很多答案。我认为你应该遵循@seven-phases-max的建议并尝试不修改源代码(或至少进行最小的更改)。

Gruntfile.js 中,您会发现两个 Less 编译参数(compileCorecompileTheme):

less: {
  compileCore: {
    options: {
      strictMath: true,
      sourceMap: true,
      outputSourceFiles: true,
      sourceMapURL: '<%= pkg.name %>.css.map',
      sourceMapFilename: 'dist/css/<%= pkg.name %>.css.map'
    },
    src: 'less/bootstrap.less',
    dest: 'dist/css/<%= pkg.name %>.css'
  },
  compileTheme: {
    options: {
      strictMath: true,
      sourceMap: true,
      outputSourceFiles: true,
      sourceMapURL: '<%= pkg.name %>-theme.css.map',
      sourceMapFilename: 'dist/css/<%= pkg.name %>-theme.css.map'
    },
    src: 'less/theme.less',
    dest: 'dist/css/<%= pkg.name %>-theme.css'
  }
}

定义了以下任务:grunt.registerTask('less-compile', ['less:compileCore', 'less:compileTheme']);

添加您自己的子任务和编译参数应该很容易。执行此操作时,您可以将 styles.less 编译为 styles.css。请注意,您将得到一个单独的 CSS 文件。加载该文件需要额外的 http 请求。

除非您在 styles.less 中导入 Bootstrap 的 Less 代码,否则您也不能重用 Bootstrap 的变量和 mixin。如果您需要单独的 CSS 文件,请考虑按如下方式创建 styles.less:

@import (reference) "bootstrap";
//your custom code here

如果您能够将所有代码编译到单个 CSS 文件中,则可以执行以下操作:

  1. 创建一个文件 custom.less 并在该文件中添加您的自定义设置。并将该文件保存在与 bootstrap.less 文件相同的文件夹中。
  2. 然后在 bootstrap.less 文件末尾写入以下代码:@import "custom";

之后你就可以像往常一样编译bootstrap了。

或者按如下方式创建 custom.less:

 @import "bootstrap";
 //your custom code here

要默认编译 custom.less 而不是 bootstrap.less,您应该必须修改 Gruntfile.js。在 CompileCore 参数中,将 src 选项更改为 src: 'less/custom.less',

关于twitter-bootstrap-3 - 更改使用 Grunt 编译的 Bootstrap 输出 LESS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27344892/

相关文章:

javascript - 表详细信息中的 Bootstrap 下拉列表

gruntjs - Grunt - 读取 XML 文件

html - 边界半径 .less 不起作用

netbeans - 无法在 Netbeans 7.4 上添加 less-compiler lessc

html - 将我的 div 文本溢出到左侧,同时保留特殊字符

javascript - 如何销毁移动分辨率的 Bootstrap Carousel

twitter-bootstrap-3 - Jasny Bootstrap 提交后无法清除文件

concurrency - Centos 7 : Running "Grunt Serve" (concurrent) task 上的 Ionic "concurrent:server"错误

node.js - 配置 grunt-ts 并使其与 LiveReload 一起工作

html - 具有第二列响应的固定大小列(列之间的固定边距)