[Pkg-javascript-commits] [less.js] 71/285: seperate out imports and tidy a few un-necessary optimisations
Jonas Smedegaard
dr at jones.dk
Mon Oct 26 23:23:39 UTC 2015
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag v2.0.0
in repository less.js.
commit 87203788a7ed0bfc9459bb14e86133484e9bf208
Author: Luke Page <luke.a.page at gmail.com>
Date: Mon Aug 25 09:28:17 2014 +0100
seperate out imports and tidy a few un-necessary optimisations
---
lib/less/parser/imports.js | 78 ++++++++++++++++++++++++++++++++++++++++
lib/less/parser/parser.js | 90 ++++------------------------------------------
2 files changed, 85 insertions(+), 83 deletions(-)
diff --git a/lib/less/parser/imports.js b/lib/less/parser/imports.js
new file mode 100644
index 0000000..bec1ce9
--- /dev/null
+++ b/lib/less/parser/imports.js
@@ -0,0 +1,78 @@
+var contexts = require("../contexts.js");
+module.exports = function(less, env, Parser) {
+ var rootFilename = env && env.filename;
+ return {
+ paths: env.paths || [], // Search paths, when importing
+ queue: [], // Files which haven't been imported yet
+ files: env.files, // Holds the imported parse trees
+ contents: env.contents, // Holds the imported file contents
+ contentsIgnoredChars: env.contentsIgnoredChars, // lines inserted, not in the original less
+ mime: env.mime, // MIME type of .less files
+ error: null, // Error in parsing/evaluating an import
+ push: function (path, currentFileInfo, importOptions, callback) {
+ var parserImports = this;
+ this.queue.push(path);
+
+ var fileParsedFunc = function (e, root, fullPath) {
+ parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
+
+ var importedPreviously = fullPath === rootFilename;
+
+ parserImports.files[fullPath] = root; // Store the root
+
+ if (e && !parserImports.error) { parserImports.error = e; }
+
+ callback(e, root, importedPreviously, fullPath);
+ };
+
+ var newFileInfo = {
+ relativeUrls: env.relativeUrls,
+ entryPath: currentFileInfo.entryPath,
+ rootpath: currentFileInfo.rootpath,
+ rootFilename: currentFileInfo.rootFilename
+ };
+
+ less.environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
+ if (e) {
+ fileParsedFunc(e);
+ return;
+ }
+
+ // Pass on an updated rootpath if path of imported file is relative and file
+ // is in a (sub|sup) directory
+ //
+ // Examples:
+ // - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
+ // then rootpath should become 'less/module/nav/'
+ // - If path of imported file is '../mixins.less' and rootpath is 'less/',
+ // then rootpath should become 'less/../'
+ newFileInfo.currentDirectory = less.environment.getPath(env, resolvedFilename);
+ if(newFileInfo.relativeUrls) {
+ newFileInfo.rootpath = less.environment.join((env.rootpath || ""), less.environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
+ if (!less.environment.isPathAbsolute(env, newFileInfo.rootpath) && less.environment.alwaysMakePathsAbsolute()) {
+ newFileInfo.rootpath = less.environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
+ }
+ }
+ newFileInfo.filename = resolvedFilename;
+
+ var newEnv = new contexts.parseEnv(env);
+
+ newEnv.currentFileInfo = newFileInfo;
+ newEnv.processImports = false;
+ newEnv.contents[resolvedFilename] = contents;
+
+ if (currentFileInfo.reference || importOptions.reference) {
+ newFileInfo.reference = true;
+ }
+
+ if (importOptions.inline) {
+ fileParsedFunc(null, contents, resolvedFilename);
+ } else {
+ new(Parser)(newEnv).parse(contents, function (e, root) {
+ fileParsedFunc(e, root, resolvedFilename);
+ });
+ }
+ });
+ }
+ };
+};
diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js
index 0ce0b4d..b26cf6b 100644
--- a/lib/less/parser/parser.js
+++ b/lib/less/parser/parser.js
@@ -2,7 +2,8 @@ var chunker = require('./chunker.js'),
LessError = require('../less-error.js'),
tree = require("../tree/index.js"),
visitor = require("../visitor/index.js"),
- contexts = require("../contexts.js");
+ contexts = require("../contexts.js"),
+ getImportManager = require("./imports.js");
module.exports = function(less) {
//
@@ -49,89 +50,16 @@ var Parser = function Parser(env) {
current, // current chunk
currentPos, // index of current chunk, in `input`
parser,
- parsers,
- rootFilename = env && env.filename;
+ parsers;
// Top parser on an import tree must be sure there is one "env"
// which will then be passed around by reference.
if (!(env instanceof contexts.parseEnv)) {
env = new contexts.parseEnv(env);
}
+ this.env = env;
- var imports = this.imports = {
- paths: env.paths || [], // Search paths, when importing
- queue: [], // Files which haven't been imported yet
- files: env.files, // Holds the imported parse trees
- contents: env.contents, // Holds the imported file contents
- contentsIgnoredChars: env.contentsIgnoredChars, // lines inserted, not in the original less
- mime: env.mime, // MIME type of .less files
- error: null, // Error in parsing/evaluating an import
- push: function (path, currentFileInfo, importOptions, callback) {
- var parserImports = this;
- this.queue.push(path);
-
- var fileParsedFunc = function (e, root, fullPath) {
- parserImports.queue.splice(parserImports.queue.indexOf(path), 1); // Remove the path from the queue
-
- var importedPreviously = fullPath === rootFilename;
-
- parserImports.files[fullPath] = root; // Store the root
-
- if (e && !parserImports.error) { parserImports.error = e; }
-
- callback(e, root, importedPreviously, fullPath);
- };
-
- var newFileInfo = {
- relativeUrls: env.relativeUrls,
- entryPath: currentFileInfo.entryPath,
- rootpath: currentFileInfo.rootpath,
- rootFilename: currentFileInfo.rootFilename
- };
-
- less.environment.loadFile(env, path, currentFileInfo.currentDirectory, function loadFileCallback(e, contents, resolvedFilename) {
- if (e) {
- fileParsedFunc(e);
- return;
- }
-
- // Pass on an updated rootpath if path of imported file is relative and file
- // is in a (sub|sup) directory
- //
- // Examples:
- // - If path of imported file is 'module/nav/nav.less' and rootpath is 'less/',
- // then rootpath should become 'less/module/nav/'
- // - If path of imported file is '../mixins.less' and rootpath is 'less/',
- // then rootpath should become 'less/../'
- newFileInfo.currentDirectory = less.environment.getPath(env, resolvedFilename);
- if(newFileInfo.relativeUrls) {
- newFileInfo.rootpath = less.environment.join((env.rootpath || ""), less.environment.pathDiff(newFileInfo.currentDirectory, newFileInfo.entryPath));
- if (!less.environment.isPathAbsolute(env, newFileInfo.rootpath) && less.environment.alwaysMakePathsAbsolute()) {
- newFileInfo.rootpath = less.environment.join(newFileInfo.entryPath, newFileInfo.rootpath);
- }
- }
- newFileInfo.filename = resolvedFilename;
-
- var newEnv = new contexts.parseEnv(env);
-
- newEnv.currentFileInfo = newFileInfo;
- newEnv.processImports = false;
- newEnv.contents[resolvedFilename] = contents;
-
- if (currentFileInfo.reference || importOptions.reference) {
- newFileInfo.reference = true;
- }
-
- if (importOptions.inline) {
- fileParsedFunc(null, contents, resolvedFilename);
- } else {
- new(less.Parser)(newEnv).parse(contents, function (e, root) {
- fileParsedFunc(e, root, resolvedFilename);
- });
- }
- });
- }
- };
+ var imports = this.imports = getImportManager(less, env, Parser);
function save() {
currentPos = i;
@@ -220,8 +148,6 @@ var Parser = function Parser(env) {
return m.length === 1 ? m[0] : m;
}
- var _$re = $re;
-
// Specialization of $(tok)
function $char(tok) {
if (input.charAt(i) !== tok) {
@@ -376,8 +302,6 @@ var Parser = function Parser(env) {
};
}
- this.env = env = env || {};
-
//
// The Parser
//
@@ -678,7 +602,7 @@ var Parser = function Parser(env) {
// block rule: at the root level.
//
primary: function () {
- var mixin = this.mixin, $re = _$re, root = [], node;
+ var mixin = this.mixin, root = [], node;
while (current)
{
@@ -1384,7 +1308,7 @@ var Parser = function Parser(env) {
// Selectors are made out of one or more Elements, see above.
//
selector: function (isLess) {
- var index = i, $re = _$re, elements, extendList, c, e, extend, when, condition;
+ var index = i, elements, extendList, c, e, extend, when, condition;
while ((isLess && (extend = this.extend())) || (isLess && (when = $re(/^when/))) || (e = this.element())) {
if (when) {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git
More information about the Pkg-javascript-commits
mailing list