kopia lustrzana https://github.com/c9/core
better caching for css in cdn-cli
rodzic
f9ece5fec1
commit
9b9baf3bc8
|
@ -4,7 +4,10 @@ var mkdirp = require("mkdirp");
|
|||
var moduleDeps = require("./module-deps");
|
||||
var path = require("path");
|
||||
|
||||
function build(config, opts, callback){
|
||||
function build(config, opts, callback) {
|
||||
if (opts.compileLess && opts.sources)
|
||||
return compileLess(opts, opts.sources, callback);
|
||||
|
||||
if (!opts.configFile) {
|
||||
opts.configFile = "\nrequire.plugins = "
|
||||
+ JSON.stringify(config, null, 4)
|
||||
|
@ -155,6 +158,7 @@ function createOutputFolder(opts, cb) {
|
|||
}
|
||||
|
||||
function compileLess(opts, sources, callback) {
|
||||
var libs = opts.lessLibs;
|
||||
var less = stripLess(sources);
|
||||
var cssCode = [];
|
||||
var code = [];
|
||||
|
@ -164,9 +168,23 @@ function compileLess(opts, sources, callback) {
|
|||
if (cache && !cache.images)
|
||||
cache.images = Object.create(null);
|
||||
|
||||
function readLibs(cb) {
|
||||
async.forEach(Object.keys(libs), function(key, next) {
|
||||
var lib = libs[key];
|
||||
if (typeof lib !== "string")
|
||||
return next();
|
||||
var path = moduleDeps.resolveModulePath(lib, opts.pathConfig.pathMap);
|
||||
fs.readFile(path, "utf8", function(e, code) {
|
||||
libs[key] = { code: code || "", id: lib };
|
||||
next();
|
||||
});
|
||||
}, function() {
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
function expandVariables(code, variables, plugin) {
|
||||
variables["base-path"] = "/static/" + libs.staticPrefix;
|
||||
variables["base-path"] = (plugin && plugin.staticPrefix || opts.staticPrefix);
|
||||
variables["icon-path"] = variables["base-path"] + "/icons";
|
||||
variables["image-path"] = variables["base-path"] + "/images";
|
||||
variables["plugin-path"] = plugin
|
||||
|
@ -179,42 +197,51 @@ function compileLess(opts, sources, callback) {
|
|||
});
|
||||
}
|
||||
|
||||
// function
|
||||
|
||||
var libs = opts.lessLibs;
|
||||
less.forEach(function(file) {
|
||||
var plugin = file.pkg.parent.plugin;
|
||||
|
||||
var id = file.pkg.id.replace(/^[^!]+!/, "");
|
||||
code.push(
|
||||
"/* @file " + id + " */\nß{"
|
||||
+ expandVariables(file.code, Object.create(null), plugin)
|
||||
+ "}"
|
||||
);
|
||||
});
|
||||
|
||||
var ctx = {
|
||||
paths: ["/"],
|
||||
filename: opts.basepath + '/unknown.less',
|
||||
compress: !!opts.compress
|
||||
};
|
||||
code = expandVariables(libs.join("\n"), Object.create(null)) + code.join("\n");
|
||||
|
||||
var lessParser = require("less");
|
||||
return lessParser.parse(code, ctx, function(err, tree, imports, options) {
|
||||
if (err) return callback(err);
|
||||
|
||||
toCss(tree, imports, options, function(err, css) {
|
||||
if (err) return callback(err);
|
||||
css = css.replace(/ß /g, "").replace(/^ +/gm, "\t");
|
||||
css = checkImages(css, libs, cache);
|
||||
css = addCssPrefixes(css);
|
||||
callback(null, { code: css });
|
||||
function preprocess() {
|
||||
less.forEach(function(file) {
|
||||
var plugin = file.pkg.parent.plugin;
|
||||
|
||||
var id = file.pkg.id.replace(/^[^!]+!/, "");
|
||||
code.push(
|
||||
"/* @file " + id + " */\nß{"
|
||||
+ expandVariables(file.code, Object.create(null), plugin)
|
||||
+ "}"
|
||||
);
|
||||
});
|
||||
code = code.join("\n")
|
||||
+ expandVariables(libs.map(function(l) {
|
||||
return l.code ? "/* @file " + l.id + " */\n" + l.code : "";
|
||||
}).join("\n"), Object.create(null));
|
||||
}
|
||||
|
||||
function compile() {
|
||||
var ctx = {
|
||||
paths: ["/"],
|
||||
filename: opts.basepath + '/unknown.less',
|
||||
compress: !!opts.compress
|
||||
};
|
||||
var lessParser = require("less");
|
||||
return lessParser.parse(code, ctx, function(err, tree, imports, options) {
|
||||
if (err) return callback(err);
|
||||
|
||||
toCss(tree, imports, options, function(err, css) {
|
||||
if (err) return callback(err);
|
||||
css = css.replace(/ß /g, "").replace(/^ +/gm, "\t");
|
||||
css = checkImages(css, opts, cache);
|
||||
css = addCssPrefixes(css);
|
||||
callback(null, { code: css });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
readLibs(function() {
|
||||
preprocess();
|
||||
compile();
|
||||
});
|
||||
}
|
||||
|
||||
function checkImages(css, libs, cache) {
|
||||
function checkImages(css, opts, cache) {
|
||||
var images = cache && cache.images || Object.create(null);
|
||||
var root = __dirname + "/../../";
|
||||
var t = Date.now();
|
||||
|
@ -231,7 +258,7 @@ function checkImages(css, libs, cache) {
|
|||
count++;
|
||||
|
||||
if (/^(images|icons)/.test(imagePath))
|
||||
imagePath = libs.staticPrefix + "/" + imagePath;
|
||||
imagePath = opts.staticPrefix + "/" + imagePath;
|
||||
|
||||
var dir = path.dirname(imagePath);
|
||||
var name = path.basename(imagePath);
|
||||
|
@ -305,6 +332,7 @@ function stripLess(sources) {
|
|||
var less = [];
|
||||
|
||||
function addLessFile(pkg, code, file) {
|
||||
pkg.cssSource = code;
|
||||
less.push({
|
||||
pkg: pkg,
|
||||
code: code,
|
||||
|
@ -313,7 +341,10 @@ function stripLess(sources) {
|
|||
}
|
||||
|
||||
sources.forEach(function(pkg) {
|
||||
if (pkg.id && (pkg.id.indexOf("text!") > -1) && pkg.id.match(/text\!.*\.(less|css)$/)) {
|
||||
if (pkg.cssSource != null) {
|
||||
addLessFile(pkg, pkg.cssSource);
|
||||
}
|
||||
else if (pkg.id && (pkg.id.indexOf("text!") > -1) && pkg.id.match(/text\!.*\.(less|css)$/)) {
|
||||
var source = pkg.source;
|
||||
pkg.source = "";
|
||||
|
||||
|
|
|
@ -50,6 +50,9 @@ function main(options, imports, register) {
|
|||
}
|
||||
|
||||
function readConfig(config) {
|
||||
if (typeof config != "string")
|
||||
return config;
|
||||
|
||||
if (config == "full") {
|
||||
var plugins = [];
|
||||
["default-local", "ssh", "default"].forEach(function(n) {
|
||||
|
@ -114,45 +117,29 @@ function main(options, imports, register) {
|
|||
var data = readConfig(config);
|
||||
if (data.error)
|
||||
return callback(data.error);
|
||||
|
||||
build(data.config, {
|
||||
sources: data.sources,
|
||||
cache: cache,
|
||||
pathConfig: pathConfig,
|
||||
enableBrowser: true,
|
||||
includeConfig: false,
|
||||
noArchitect: true,
|
||||
compress: compress,
|
||||
filter: [],
|
||||
ignore: [],
|
||||
withRequire: false,
|
||||
compileLess: true,
|
||||
lessLibs: [
|
||||
"plugins/c9.ide.layout.classic/less/lesshat.less",
|
||||
"plugins/c9.ide.layout.classic/themes/default-" + color + ".less",
|
||||
"plugins/c9.ide.layout.classic/themes/" + color + ".less"
|
||||
],
|
||||
staticPrefix: "plugins/c9.ide.layout.classic",
|
||||
lessLibCacheKey: color,
|
||||
basepath: pathConfig.root,
|
||||
}, callback);
|
||||
|
||||
var plugins = data.config.concat(sharedModules());
|
||||
var lessLibs = [];
|
||||
|
||||
fs.readFile(path.join(pathConfig.root, "plugins/c9.ide.layout.classic/less/lesshat.less"), "utf8", function(err, lesshat) {
|
||||
if (err) return callback(err);
|
||||
|
||||
lessLibs.push(lesshat);
|
||||
|
||||
fs.readFile(path.join(pathConfig.root, "plugins/c9.ide.layout.classic/themes/default-" + color + ".less"), "utf8", function(err, theme) {
|
||||
if (err) return callback(err);
|
||||
|
||||
lessLibs.push(theme);
|
||||
|
||||
lessLibs.staticPrefix = "plugins/c9.ide.layout.classic";
|
||||
|
||||
var themeCss = [{
|
||||
id: "text!" + "plugins/c9.ide.layout.classic/themes/" + color + ".less",
|
||||
parent: {}
|
||||
}];
|
||||
|
||||
build(plugins, {
|
||||
cache: cache,
|
||||
pathConfig: pathConfig,
|
||||
enableBrowser: true,
|
||||
includeConfig: false,
|
||||
noArchitect: true,
|
||||
compress: compress,
|
||||
filter: [],
|
||||
ignore: [],
|
||||
withRequire: false,
|
||||
compileLess: true,
|
||||
lessLibs: lessLibs,
|
||||
lessLibCacheKey: color,
|
||||
basepath: pathConfig.root,
|
||||
additional: themeCss
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function buildAce(module, pathConfig, callback, aceModuleIgnores) {
|
||||
|
|
|
@ -49,12 +49,17 @@ define(function(require, exports, module) {
|
|||
else
|
||||
skins = skins ? skins.split(/,\s*/) : [];
|
||||
|
||||
build.buildConfig(config, pathConfig, save(["config", path.basename(config) + ".js"], function next(err) {
|
||||
if (err) return done(err);
|
||||
var skin = skins.pop();
|
||||
if (!skin) return buildConfig();
|
||||
build.buildSkin(config, skin, pathConfig, save(["skin", config, skin + ".css"], next));
|
||||
}), function(configName, data) {
|
||||
build.buildConfig(config, pathConfig, function(err, result) {
|
||||
save(["config", path.basename(config) + ".js"], function next(err) {
|
||||
if (err) return done(err);
|
||||
var skin = skins.pop();
|
||||
if (!skin) return buildConfig();
|
||||
build.buildSkin({
|
||||
sources: result.sources,
|
||||
config: [],
|
||||
}, skin, pathConfig, save(["skin", config, skin + ".css"], next));
|
||||
})(err, result)
|
||||
}, function(configName, data) {
|
||||
var pluginPaths = data.map(function(p) {
|
||||
return typeof p == "string" ? p : p.packagePath;
|
||||
}).filter(Boolean).sort();
|
||||
|
|
Ładowanie…
Reference in New Issue