mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-20 17:54:23 +01:00
make customizer compilation code more generic
This commit is contained in:
parent
bf5017c01c
commit
07309edce5
@ -173,20 +173,55 @@ window.onload = function () { // wait for load in a dumb way because B-0
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an Array of @import'd filenames from 'bootstrap.less' in the order
|
// Returns an Array of @import'd filenames in the order
|
||||||
// in which they appear in the file.
|
// in which they appear in the file.
|
||||||
function bootstrapLessFilenames() {
|
function includedLessFilenames(lessFilename) {
|
||||||
var IMPORT_REGEX = /^@import \"(.*?)\";$/
|
var IMPORT_REGEX = /^@import \"(.*?)\";$/
|
||||||
var bootstrapLessLines = __less['bootstrap.less'].split('\n')
|
var lessLines = __less[lessFilename].split('\n')
|
||||||
|
|
||||||
for (var i = 0, imports = []; i < bootstrapLessLines.length; i++) {
|
for (var i = 0, imports = []; i < lessLines.length; i++) {
|
||||||
var match = IMPORT_REGEX.exec(bootstrapLessLines[i])
|
var match = IMPORT_REGEX.exec(lessLines[i])
|
||||||
if (match) imports.push(match[1])
|
if (match) imports.push(match[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
return imports
|
return imports
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function generateLESS(lessFilename, lessFileIncludes, vars) {
|
||||||
|
var lessSource = __less[lessFilename]
|
||||||
|
|
||||||
|
$.each(includedLessFilenames(lessFilename), function(index, filename) {
|
||||||
|
var fileInclude = lessFileIncludes[filename]
|
||||||
|
|
||||||
|
// Files not explicitly unchecked are compiled into the final stylesheet.
|
||||||
|
// Core stylesheets like 'normalize.less' are not included in the form
|
||||||
|
// since disabling them would wreck everything, and so their 'fileInclude'
|
||||||
|
// will be 'undefined'.
|
||||||
|
if (fileInclude || (fileInclude == null)) lessSource += __less[filename]
|
||||||
|
|
||||||
|
// Custom variables are added after Bootstrap variables so the custom
|
||||||
|
// ones take precedence.
|
||||||
|
if (('variables.less' === filename) && vars) lessSource += generateCustomCSS(vars)
|
||||||
|
})
|
||||||
|
|
||||||
|
lessSource = lessSource.replace(/@import[^\n]*/gi, '') //strip any imports
|
||||||
|
return lessSource
|
||||||
|
}
|
||||||
|
|
||||||
|
function compileLESS(lessSource, baseFilename, intoResult) {
|
||||||
|
var parser = new less.Parser({
|
||||||
|
paths: ['variables.less', 'mixins.less'],
|
||||||
|
optimization: 0,
|
||||||
|
filename: baseFilename + '.css'
|
||||||
|
}).parse(lessSource, function (err, tree) {
|
||||||
|
if (err) {
|
||||||
|
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
||||||
|
}
|
||||||
|
intoResult[baseFilename + '.css'] = cw + tree.toCSS()
|
||||||
|
intoResult[baseFilename + '.min.css'] = cw + tree.toCSS({ compress: true })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function generateCSS() {
|
function generateCSS() {
|
||||||
var oneChecked = false
|
var oneChecked = false
|
||||||
var lessFileIncludes = {}
|
var lessFileIncludes = {}
|
||||||
@ -202,43 +237,16 @@ window.onload = function () { // wait for load in a dumb way because B-0
|
|||||||
|
|
||||||
var result = {}
|
var result = {}
|
||||||
var vars = {}
|
var vars = {}
|
||||||
var css = ''
|
|
||||||
|
|
||||||
$('#less-variables-section input')
|
$('#less-variables-section input')
|
||||||
.each(function () {
|
.each(function () {
|
||||||
$(this).val() && (vars[$(this).prev().text()] = $(this).val())
|
$(this).val() && (vars[$(this).prev().text()] = $(this).val())
|
||||||
})
|
})
|
||||||
|
|
||||||
$.each(bootstrapLessFilenames(), function(index, filename) {
|
var bsLessSource = generateLESS('bootstrap.less', lessFileIncludes, vars)
|
||||||
var fileInclude = lessFileIncludes[filename]
|
|
||||||
|
|
||||||
// Files not explicitly unchecked are compiled into the final stylesheet.
|
|
||||||
// Core stylesheets like 'normalize.less' are not included in the form
|
|
||||||
// since disabling them would wreck everything, and so their 'fileInclude'
|
|
||||||
// will be 'undefined'.
|
|
||||||
if (fileInclude || (fileInclude == null)) css += __less[filename]
|
|
||||||
|
|
||||||
// Custom variables are added after Bootstrap variables so the custom
|
|
||||||
// ones take precedence.
|
|
||||||
if (('variables.less' === filename) && vars) css += generateCustomCSS(vars)
|
|
||||||
})
|
|
||||||
|
|
||||||
css = css.replace(/@import[^\n]*/gi, '') //strip any imports
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var parser = new less.Parser({
|
compileLESS(bsLessSource, 'bootstrap', result)
|
||||||
paths: ['variables.less', 'mixins.less'],
|
|
||||||
optimization: 0,
|
|
||||||
filename: 'bootstrap.css'
|
|
||||||
}).parse(css, function (err, tree) {
|
|
||||||
if (err) {
|
|
||||||
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
|
||||||
}
|
|
||||||
result = {
|
|
||||||
'bootstrap.css' : cw + tree.toCSS(),
|
|
||||||
'bootstrap.min.css' : cw + tree.toCSS({ compress: true })
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
return showError('<strong>Ruh roh!</strong> Could not parse less files.', err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user