fis-parser-sass
Advanced tools
Comparing version 0.3.16 to 1.0.0
359
index.js
@@ -6,232 +6,261 @@ /* | ||
'use strict'; | ||
"use strict"; | ||
var path = require('path'); | ||
var sass = require('fis-sass'); | ||
var util = require('util'); | ||
var path = require("path"); | ||
var sass = require("sass"); | ||
var util = require("util"); | ||
var root; | ||
function resolve_and_load(filename, dir) { | ||
// Resolution order for ambiguous imports: | ||
// (1) filename as given | ||
// (2) underscore + given | ||
// (3) underscore + given + extension | ||
// (4) given + extension | ||
// | ||
// Resolution order for ambiguous imports: | ||
// (1) filename as given | ||
// (2) underscore + given | ||
// (3) underscore + given + extension | ||
// (4) given + extension | ||
// | ||
var basename = path.basename(filename); | ||
var dirname = path.dirname(filename); | ||
var files = []; | ||
var basename = path.basename(filename); | ||
var dirname = path.dirname(filename); | ||
var files = []; | ||
files.push(path.join(dirname, basename)); | ||
files.push(path.join(dirname, '_' + basename)); | ||
files.push(path.join(dirname, '_' + basename + '.scss')); | ||
files.push(path.join(dirname, '_' + basename + '.sass')); | ||
files.push(path.join(dirname, basename + '.scss')); | ||
files.push(path.join(dirname, basename + '.sass')); | ||
files.push(path.join(dirname, basename)); | ||
files.push(path.join(dirname, "_" + basename)); | ||
files.push(path.join(dirname, "_" + basename + ".scss")); | ||
files.push(path.join(dirname, "_" + basename + ".sass")); | ||
files.push(path.join(dirname, basename + ".scss")); | ||
files.push(path.join(dirname, basename + ".sass")); | ||
var found = null; | ||
var found = null; | ||
files.every(function(url) { | ||
var file = fis.util(dir, url); | ||
files.every(function (url) { | ||
var file = fis.util(dir, url); | ||
if( file && fis.util.isFile(file) ) { | ||
found = fis.file(file); | ||
return false; | ||
} | ||
if (file && fis.util.isFile(file)) { | ||
found = fis.file(file); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return true; | ||
}); | ||
return found; | ||
return found; | ||
} | ||
function find(filename, paths) { | ||
var found = null; | ||
var found = null; | ||
paths.every(function(dir) { | ||
var file; | ||
paths.every(function (dir) { | ||
var file; | ||
if ((file = resolve_and_load(filename, dir))) { | ||
found = file; | ||
return false; | ||
} | ||
if ((file = resolve_and_load(filename, dir))) { | ||
found = file; | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return true; | ||
}); | ||
return found; | ||
return found; | ||
} | ||
function fixSourcePath(content, file) { | ||
// 处理,解决资源引用路径问题。 | ||
content = fis.compile.extCss(content); | ||
// 处理,解决资源引用路径问题。 | ||
content = fis.compile.extCss(content); | ||
return content.replace(fis.compile.lang.reg, function(all, type, depth, value) { | ||
return content.replace(fis.compile.lang.reg, function ( | ||
all, | ||
type, | ||
depth, | ||
value | ||
) { | ||
// 判断是否为 fis2 | ||
if (!fis.match) { | ||
value = depth; | ||
} | ||
// 判断是否为 fis2 | ||
if (!fis.match) { | ||
value = depth; | ||
} | ||
var info = fis.uri(value, file.dirname); | ||
var info = fis.uri(value, file.dirname); | ||
if (info.file && info.file.subpath) { | ||
value = info.quote + info.file.subpath + info.query + info.quote; | ||
} | ||
if (info.file && info.file.subpath) { | ||
value = info.quote + info.file.subpath + info.query + info.quote; | ||
} | ||
return value; | ||
}); | ||
return value; | ||
}); | ||
} | ||
function fixImport(content) { | ||
var reg = /((?:\/\/.*?\n)|(?:\/\*[\s\S]*?\*\/))|(?:@import\s([\s\S]*?)(?:\n|$)(?!\s+[^{@]*\n))/ig; | ||
var reg = /((?:\/\/.*?\n)|(?:\/\*[\s\S]*?\*\/))|(?:@import\s([\s\S]*?)(?:\n|$)(?!\s+[^{@]*\n))/gi; | ||
return content.replace(reg, function(all, comments, value) { | ||
return content.replace(reg, function (all, comments, value) { | ||
if (!comments && value && !/;$/.test(value)) { | ||
all += ";"; | ||
} | ||
if (!comments && value && !/;$/.test(value)) { | ||
all += ';'; | ||
} | ||
return all; | ||
}); | ||
return all; | ||
}); | ||
} | ||
module.exports = function(content, file, conf){ | ||
module.exports = function (content, file, conf) { | ||
// console.log(file.basename) | ||
// console.log(file.basename) | ||
// 不处理空文件,处理空文件有人反馈报错。 | ||
// 不独立编译 _ 打头的文件。 | ||
if (!content || !content.trim() || file.basename[0] === "_") { | ||
return content; | ||
} | ||
// 不处理空文件,处理空文件有人反馈报错。 | ||
// 不独立编译 _ 打头的文件。 | ||
if (!content || !content.trim() || file.basename[0] === '_') { | ||
return content; | ||
} | ||
content = fixImport(content); | ||
content = fixImport(content); | ||
root = root || fis.project.getProjectPath(); | ||
var opts = fis.util.clone(conf); | ||
root = root || fis.project.getProjectPath(); | ||
var opts = fis.util.clone(conf); | ||
// 读取私有配置。 | ||
if (file.sass) { | ||
fis.util.map(fis.sass, opts, true); | ||
} | ||
// 读取私有配置。 | ||
if (file.sass) { | ||
fis.util.map(fis.sass, opts, true); | ||
opts.includePaths = opts.include_paths || opts.includePaths || []; | ||
file.dirname !== root && opts.includePaths.unshift(file.dirname); | ||
opts.includePaths.push(root); | ||
opts.includePaths = opts.includePaths.map(function (dir) { | ||
if ( | ||
path.resolve(dir) != path.normalize(dir) || | ||
fis.util.exists(path.join(root, dir)) | ||
) { | ||
dir = path.join(root, dir); | ||
} | ||
opts.includePaths = opts.include_paths || opts.includePaths || []; | ||
file.dirname !== root && opts.includePaths.unshift(file.dirname); | ||
opts.includePaths.push(root); | ||
return dir; | ||
}); | ||
opts.includePaths = opts.includePaths.map(function( dir ) { | ||
opts.file = file.subpath; | ||
opts.data = content; | ||
if (path.resolve( dir ) != path.normalize( dir ) || fis.util.exists(path.join(root, dir))) { | ||
dir = path.join(root, dir); | ||
} | ||
if (file.ext === ".sass") { | ||
opts.indentedSyntax = true; | ||
} | ||
return dir; | ||
}); | ||
var includePaths = opts.includePaths.concat(); | ||
var stacks = []; | ||
var sources = [file.subpath]; | ||
opts.importer = [ | ||
function (url, prev, done) { | ||
prev = prev.replace(/^\w+\:/, ""); // windows 里面莫名加个盘符。 | ||
var prevFile = find(prev, stacks.concat(includePaths)); | ||
opts.file = file.subpath; | ||
opts.data = content; | ||
if (!prevFile) { | ||
throw new Error("Can't find `" + prev + "`"); | ||
} | ||
if (file.ext === '.sass') { | ||
opts.indentedSyntax = true; | ||
} | ||
var dirname = prevFile.dirname; | ||
var includePaths = opts.includePaths.concat(); | ||
var stacks = []; | ||
var sources = [file.subpath]; | ||
opts.importer = function(url, prev, done) { | ||
prev = prev.replace(/^\w+\:/, ''); // windows 里面莫名加个盘符。 | ||
var prevFile = find(prev, stacks.concat(includePaths)); | ||
// 如果已经在里面 | ||
var idx = stacks.indexOf(dirname); | ||
if (~idx) { | ||
stacks.splice(idx, 1); | ||
} | ||
stacks.unshift(dirname); | ||
if (!prevFile) { | ||
throw new Error('Can\'t find `' + prev +'`'); | ||
} | ||
var target = find(url, stacks.concat(includePaths)); | ||
var dirname = prevFile.dirname; | ||
if (!target) { | ||
throw new Error("Can't find `" + url + "` in `" + prev + "`"); | ||
} | ||
// 如果已经在里面 | ||
var idx = stacks.indexOf(dirname); | ||
if (~idx) { | ||
stacks.splice(idx, 1); | ||
} | ||
stacks.unshift(dirname); | ||
var content = target.getContent(); | ||
content = fixSourcePath(content, target); | ||
var target = find(url, stacks.concat(includePaths)); | ||
if (file.cache) { | ||
file.cache.addDeps(target.realpath); | ||
} | ||
//解决include_path 内import导致subpath为空报错问题 | ||
if (!target.subpath) { | ||
target.subpath = path.relative(root, target.realpath); | ||
} | ||
~sources.indexOf(target.subpath) || sources.push(target.subpath); | ||
if (!target) { | ||
throw new Error('Can\'t find `' + url +'` in `' + prev + '`'); | ||
} | ||
var content = target.getContent(); | ||
content = fixSourcePath(content, target); | ||
if (file.cache) { | ||
file.cache.addDeps(target.realpath); | ||
} | ||
//解决include_path 内import导致subpath为空报错问题 | ||
if(!target.subpath){ | ||
target.subpath = path.relative(root, target.realpath); | ||
} | ||
~sources.indexOf(target.subpath) || sources.push(target.subpath); | ||
if (!done) { | ||
return { | ||
file: target.subpath, | ||
contents: content, | ||
}; | ||
} else { | ||
done({ | ||
file: target.subpath, | ||
contents: content | ||
file: target.subpath, | ||
contents: content, | ||
}); | ||
}; | ||
} | ||
}, | ||
]; | ||
if (opts.sourceMap) { | ||
var mapping = fis.file.wrap(file.dirname + '/' + file.filename + file.rExt + '.map'); | ||
if (opts.sourceMap) { | ||
var mapping = fis.file.wrap( | ||
file.dirname + "/" + file.filename + file.rExt + ".map" | ||
); | ||
mapping.useDomain = true; | ||
mapping.useHash = false; | ||
mapping.useDomain = true; | ||
mapping.useHash = false; | ||
opts.sourceMap = mapping.getUrl(fis.compile.settings.hash, fis.compile.settings.domain); | ||
file.release && (opts.outFile = file.getUrl(fis.compile.settings.hash, fis.compile.settings.domain)); | ||
} | ||
opts.sourceMap = mapping.getUrl( | ||
fis.compile.settings.hash, | ||
fis.compile.settings.domain | ||
); | ||
file.release && | ||
(opts.outFile = file.getUrl( | ||
fis.compile.settings.hash, | ||
fis.compile.settings.domain | ||
)); | ||
} | ||
var ret; | ||
try { | ||
ret = sass.renderSync(opts); | ||
} catch (e) { | ||
e = JSON.parse(e); | ||
fis.log.error(util.format("%s".red + " [`%s` %s:%s]".yellow, e.message, e.file, e.line, e.column)); | ||
} | ||
var ret; | ||
try { | ||
ret = sass.renderSync(opts); | ||
} catch (e) { | ||
console.log(e); | ||
// e = JSON.parse(e); | ||
fis.log.error( | ||
util.format( | ||
// "%s".red + " [`%s` %s:%s]".yellow, | ||
e.message, | ||
e.file, | ||
e.line, | ||
e.column | ||
) | ||
); | ||
} | ||
// if (file.cache && ret.stats.includedFiles.length) { | ||
// ret.stats.includedFiles.forEach(function(dep) { | ||
// file.cache.addDeps(dep); | ||
// }); | ||
// } | ||
// if (file.cache && ret.stats.includedFiles.length) { | ||
// ret.stats.includedFiles.forEach(function(dep) { | ||
// file.cache.addDeps(dep); | ||
// }); | ||
// } | ||
if (mapping && ret.map) { | ||
// var sourceMap = ret.sourceMap; | ||
// console.log(ret); | ||
if (mapping) { | ||
var sourceMap = ret.sourceMap; | ||
// // 修复 sourceMap 中文件路径错误问题 | ||
// // 等 node-sass 修复后,可以删除。 | ||
// // --------------------------------------------- | ||
// var sourceMapObj = JSON.parse(sourceMap); | ||
// sourceMapObj.sources = sources; | ||
// sourceMap = JSON.stringify(sourceMapObj, null, 4); | ||
// // ----------------------------------------------- | ||
// 修复 sourceMap 中文件路径错误问题 | ||
// 等 node-sass 修复后,可以删除。 | ||
// --------------------------------------------- | ||
var sourceMapObj = JSON.parse(sourceMap); | ||
sourceMapObj.sources = sources; | ||
sourceMap = JSON.stringify(sourceMapObj, null, 4); | ||
// ----------------------------------------------- | ||
mapping.setContent(ret.map); | ||
file.extras = file.extras || {}; | ||
file.extras.derived = file.extras.derived || []; | ||
file.extras.derived.push(mapping); | ||
} | ||
mapping.setContent(sourceMap); | ||
file.extras = file.extras || {}; | ||
file.extras.derived = file.extras.derived || []; | ||
file.extras.derived.push(mapping); | ||
} | ||
return ret.css; | ||
return ret.css; | ||
}; | ||
module.exports.defaultOptions = { | ||
outputStyle: 'nested', | ||
sourceMapContents: true, | ||
sourceMap: false, | ||
omitSourceMapUrl: false | ||
outputStyle: "expanded", | ||
sourceMapContents: true, | ||
sourceMap: false, | ||
omitSourceMapUrl: false, | ||
}; |
{ | ||
"name": "fis-parser-sass", | ||
"description": "A parser plugin for fis to compile sass file.", | ||
"version": "0.3.16", | ||
"version": "1.0.0", | ||
"author": "FIS Team <fis@baidu.com>", | ||
@@ -26,3 +26,3 @@ "homepage": "http://fis.baidu.com/", | ||
"dependencies": { | ||
"fis-sass": "^3.1.0" | ||
"sass": "1.27.0" | ||
}, | ||
@@ -29,0 +29,0 @@ "devDependencies": { |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
397
0
0
16468
5
+ Addedsass@1.27.0
+ Addedanymatch@3.1.3(transitive)
+ Addedbinary-extensions@2.3.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedchokidar@3.6.0(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedis-binary-path@2.1.0(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addednormalize-path@3.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedreaddirp@3.6.0(transitive)
+ Addedsass@1.27.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
- Removedfis-sass@^3.1.0
- Removedansi-regex@0.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
- Removedchalk@0.5.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfis-sass@3.1.3(transitive)
- Removedhas-ansi@0.1.0(transitive)
- Removednan@1.9.0(transitive)
- Removedstep@0.0.5(transitive)
- Removedstrip-ansi@0.3.0(transitive)
- Removedsupports-color@0.2.0(transitive)