added find_import to import and renamed lib method to from_paths
This commit is contained in:
parent
33a2c7efbf
commit
ec5ab05330
46
src/lib.rs
46
src/lib.rs
@ -140,6 +140,13 @@ fn raw_to_parse_error(map: &CodeMap, err: Error) -> Box<Error> {
|
|||||||
#[cfg_attr(not(feature = "profiling"), inline)]
|
#[cfg_attr(not(feature = "profiling"), inline)]
|
||||||
#[cfg(not(feature = "wasm"))]
|
#[cfg(not(feature = "wasm"))]
|
||||||
pub fn from_path(p: &str) -> Result<String> {
|
pub fn from_path(p: &str) -> Result<String> {
|
||||||
|
from_paths(p, &Vec::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "profiling", inline(never))]
|
||||||
|
#[cfg_attr(not(feature = "profiling"), inline)]
|
||||||
|
#[cfg(not(feature = "wasm"))]
|
||||||
|
pub fn from_paths(p: &str, loadpaths: &[&Path]) -> Result<String> {
|
||||||
let mut map = CodeMap::new();
|
let mut map = CodeMap::new();
|
||||||
let file = map.add_file(p.into(), String::from_utf8(fs::read(p)?)?);
|
let file = map.add_file(p.into(), String::from_utf8(fs::read(p)?)?);
|
||||||
let empty_span = file.span.subspan(0, 0);
|
let empty_span = file.span.subspan(0, 0);
|
||||||
@ -161,7 +168,7 @@ pub fn from_path(p: &str) -> Result<String> {
|
|||||||
at_root_has_selector: false,
|
at_root_has_selector: false,
|
||||||
extender: &mut Extender::new(empty_span),
|
extender: &mut Extender::new(empty_span),
|
||||||
content_scopes: &mut Scopes::new(),
|
content_scopes: &mut Scopes::new(),
|
||||||
load_paths: &Vec::new(),
|
load_paths: loadpaths,
|
||||||
}
|
}
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e))?;
|
.map_err(|e| raw_to_parse_error(&map, *e))?;
|
||||||
@ -171,7 +178,6 @@ pub fn from_path(p: &str) -> Result<String> {
|
|||||||
.pretty_print(&map)
|
.pretty_print(&map)
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e))
|
.map_err(|e| raw_to_parse_error(&map, *e))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile CSS from a string
|
/// Compile CSS from a string
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
@ -249,39 +255,3 @@ pub fn from_string(p: String) -> std::result::Result<String, JsValue> {
|
|||||||
.pretty_print(&map)
|
.pretty_print(&map)
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?)
|
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "profiling", inline(never))]
|
|
||||||
#[cfg_attr(not(feature = "profiling"), inline)]
|
|
||||||
#[cfg(not(feature = "wasm"))]
|
|
||||||
pub fn from_path_with_load_paths(p: &str, loadpaths: &[&Path]) -> Result<String> {
|
|
||||||
let mut map = CodeMap::new();
|
|
||||||
let file = map.add_file(p.into(), String::from_utf8(fs::read(p)?)?);
|
|
||||||
let empty_span = file.span.subspan(0, 0);
|
|
||||||
|
|
||||||
let stmts = Parser {
|
|
||||||
toks: &mut Lexer::new(&file)
|
|
||||||
.collect::<Vec<Token>>()
|
|
||||||
.into_iter()
|
|
||||||
.peekmore(),
|
|
||||||
map: &mut map,
|
|
||||||
path: p.as_ref(),
|
|
||||||
scopes: &mut Scopes::new(),
|
|
||||||
global_scope: &mut Scope::new(),
|
|
||||||
super_selectors: &mut NeverEmptyVec::new(Selector::new(empty_span)),
|
|
||||||
span_before: empty_span,
|
|
||||||
content: &mut Vec::new(),
|
|
||||||
flags: ContextFlags::empty(),
|
|
||||||
at_root: true,
|
|
||||||
at_root_has_selector: false,
|
|
||||||
extender: &mut Extender::new(empty_span),
|
|
||||||
content_scopes: &mut Scopes::new(),
|
|
||||||
load_paths: loadpaths,
|
|
||||||
}
|
|
||||||
.parse()
|
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e))?;
|
|
||||||
|
|
||||||
Css::from_stmts(stmts, false)
|
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e))?
|
|
||||||
.pretty_print(&map)
|
|
||||||
.map_err(|e| raw_to_parse_error(&map, *e))
|
|
||||||
}
|
|
||||||
|
@ -7,7 +7,7 @@ use std::{
|
|||||||
use clap::{arg_enum, App, AppSettings, Arg};
|
use clap::{arg_enum, App, AppSettings, Arg};
|
||||||
|
|
||||||
#[cfg(not(feature = "wasm"))]
|
#[cfg(not(feature = "wasm"))]
|
||||||
use grass::from_path_with_load_paths;
|
use grass::from_paths;
|
||||||
|
|
||||||
arg_enum! {
|
arg_enum! {
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
@ -193,7 +193,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
if let Some(path) = matches.value_of("OUTPUT") {
|
if let Some(path) = matches.value_of("OUTPUT") {
|
||||||
let mut buf = BufWriter::new(File::open(path).unwrap_or(File::create(path)?));
|
let mut buf = BufWriter::new(File::open(path).unwrap_or(File::create(path)?));
|
||||||
buf.write_all(
|
buf.write_all(
|
||||||
from_path_with_load_paths(name, &vals)
|
from_paths(name, &vals)
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
@ -203,7 +203,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
} else {
|
} else {
|
||||||
let mut stdout = BufWriter::new(stdout());
|
let mut stdout = BufWriter::new(stdout());
|
||||||
stdout.write_all(
|
stdout.write_all(
|
||||||
from_path_with_load_paths(name, &vals)
|
from_paths(name, &vals)
|
||||||
.unwrap_or_else(|e| {
|
.unwrap_or_else(|e| {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
|
@ -7,6 +7,56 @@ use crate::{common::QuoteKind, error::SassResult, lexer::Lexer, value::Value, To
|
|||||||
|
|
||||||
use super::{Parser, Stmt};
|
use super::{Parser, Stmt};
|
||||||
|
|
||||||
|
/// Searches the current directory of the file then searches in load paths directories
|
||||||
|
/// if the import has not yet been found.
|
||||||
|
/// [https://sass-lang.com/documentation/at-rules/import#finding-the-file](finding a file)
|
||||||
|
/// [https://sass-lang.com/documentation/at-rules/import#load-paths](load path)
|
||||||
|
fn find_import(file_path: &PathBuf, name: &OsStr, load_paths: &[&Path]) -> Option<PathBuf> {
|
||||||
|
let paths = [
|
||||||
|
file_path.with_file_name(name).with_extension("scss"),
|
||||||
|
file_path
|
||||||
|
.with_file_name(format!("_{}", name.to_str().unwrap()))
|
||||||
|
.with_extension("scss"),
|
||||||
|
file_path.clone(),
|
||||||
|
file_path.join("index.scss"),
|
||||||
|
file_path.join("_index.scss"),
|
||||||
|
];
|
||||||
|
|
||||||
|
for name in &paths {
|
||||||
|
if name.is_file() {
|
||||||
|
return Some(name.to_path_buf());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for path in load_paths {
|
||||||
|
let paths: Vec<PathBuf> = if path.is_dir() {
|
||||||
|
vec![
|
||||||
|
path.join(format!("{}.scss", name.to_str().unwrap())),
|
||||||
|
path.join(format!("_{}.scss", name.to_str().unwrap())),
|
||||||
|
path.join("index.scss"),
|
||||||
|
path.join("_index.scss"),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec![
|
||||||
|
path.to_path_buf(),
|
||||||
|
path.with_file_name(name).with_extension("scss"),
|
||||||
|
path.with_file_name(format!("_{}", name.to_str().unwrap()))
|
||||||
|
.with_extension("scss"),
|
||||||
|
path.join("index.scss"),
|
||||||
|
path.join("_index.scss"),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
for name in paths {
|
||||||
|
if name.is_file() {
|
||||||
|
return Some(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> Parser<'a> {
|
impl<'a> Parser<'a> {
|
||||||
pub(super) fn import(&mut self) -> SassResult<Vec<Stmt>> {
|
pub(super) fn import(&mut self) -> SassResult<Vec<Stmt>> {
|
||||||
self.whitespace();
|
self.whitespace();
|
||||||
@ -57,95 +107,32 @@ impl<'a> Parser<'a> {
|
|||||||
|
|
||||||
let name = path_buf.file_name().unwrap_or_else(|| OsStr::new(".."));
|
let name = path_buf.file_name().unwrap_or_else(|| OsStr::new(".."));
|
||||||
|
|
||||||
let paths = [
|
if let Some(name) = find_import(&path_buf, name, self.load_paths) {
|
||||||
path_buf.with_file_name(name).with_extension("scss"),
|
let file = self.map.add_file(
|
||||||
path_buf
|
name.to_string_lossy().into(),
|
||||||
.with_file_name(format!("_{}", name.to_str().unwrap()))
|
String::from_utf8(fs::read(&name)?)?,
|
||||||
.with_extension("scss"),
|
);
|
||||||
path_buf.clone(),
|
|
||||||
path_buf.join("index.scss"),
|
|
||||||
path_buf.join("_index.scss"),
|
|
||||||
];
|
|
||||||
|
|
||||||
for name in &paths {
|
return Parser {
|
||||||
if name.is_file() {
|
toks: &mut Lexer::new(&file)
|
||||||
let file = self.map.add_file(
|
.collect::<Vec<Token>>()
|
||||||
name.to_string_lossy().into(),
|
.into_iter()
|
||||||
String::from_utf8(fs::read(name)?)?,
|
.peekmore(),
|
||||||
);
|
map: self.map,
|
||||||
|
path: &name,
|
||||||
return Parser {
|
scopes: self.scopes,
|
||||||
toks: &mut Lexer::new(&file)
|
global_scope: self.global_scope,
|
||||||
.collect::<Vec<Token>>()
|
super_selectors: self.super_selectors,
|
||||||
.into_iter()
|
span_before: file.span.subspan(0, 0),
|
||||||
.peekmore(),
|
content: self.content,
|
||||||
map: self.map,
|
flags: self.flags,
|
||||||
path: name.as_ref(),
|
at_root: self.at_root,
|
||||||
scopes: self.scopes,
|
at_root_has_selector: self.at_root_has_selector,
|
||||||
global_scope: self.global_scope,
|
extender: self.extender,
|
||||||
super_selectors: self.super_selectors,
|
content_scopes: self.content_scopes,
|
||||||
span_before: file.span.subspan(0, 0),
|
load_paths: self.load_paths,
|
||||||
content: self.content,
|
|
||||||
flags: self.flags,
|
|
||||||
at_root: self.at_root,
|
|
||||||
at_root_has_selector: self.at_root_has_selector,
|
|
||||||
extender: self.extender,
|
|
||||||
content_scopes: self.content_scopes,
|
|
||||||
load_paths: self.load_paths,
|
|
||||||
}
|
|
||||||
.parse();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for path in self.load_paths {
|
|
||||||
let paths: Vec<PathBuf> = if path.is_dir() {
|
|
||||||
vec![
|
|
||||||
path.join(format!("{}.scss", name.to_str().unwrap())),
|
|
||||||
path.join(format!("_{}.scss", name.to_str().unwrap())),
|
|
||||||
path.join("index.scss"),
|
|
||||||
path.join("_index.scss"),
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
vec![
|
|
||||||
path.to_path_buf(),
|
|
||||||
path.with_file_name(name).with_extension("scss"),
|
|
||||||
path.with_file_name(format!("_{}", name.to_str().unwrap()))
|
|
||||||
.with_extension("scss"),
|
|
||||||
path.join("index.scss"),
|
|
||||||
path.join("_index.scss"),
|
|
||||||
]
|
|
||||||
};
|
|
||||||
|
|
||||||
for name in &paths {
|
|
||||||
if name.is_file() {
|
|
||||||
println!("found file: {:?}", name);
|
|
||||||
let file = self.map.add_file(
|
|
||||||
name.to_string_lossy().into(),
|
|
||||||
String::from_utf8(fs::read(name)?)?,
|
|
||||||
);
|
|
||||||
|
|
||||||
return Parser {
|
|
||||||
toks: &mut Lexer::new(&file)
|
|
||||||
.collect::<Vec<Token>>()
|
|
||||||
.into_iter()
|
|
||||||
.peekmore(),
|
|
||||||
map: self.map,
|
|
||||||
path: name.as_ref(),
|
|
||||||
scopes: self.scopes,
|
|
||||||
global_scope: self.global_scope,
|
|
||||||
super_selectors: self.super_selectors,
|
|
||||||
span_before: file.span.subspan(0, 0),
|
|
||||||
content: self.content,
|
|
||||||
flags: self.flags,
|
|
||||||
at_root: self.at_root,
|
|
||||||
at_root_has_selector: self.at_root_has_selector,
|
|
||||||
extender: self.extender,
|
|
||||||
content_scopes: self.content_scopes,
|
|
||||||
load_paths: self.load_paths,
|
|
||||||
}
|
|
||||||
.parse();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
.parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(("Can't find stylesheet to import.", span).into())
|
Err(("Can't find stylesheet to import.", span).into())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user