Watch multiple directories using gulp-sass - path

I have .scss files in two different directories:
./blog/styles/
./common/styles/
There is a file in ./blog/styles called blog.scss and I'm importing .scss files from ./blog/styles/ (i.e. same directory) and ./common/styles/.
How should I create gulp task to watch both these directories and create final .css files somewhere else (say in ./dist)?
This is my current task:
gulp.task('sass', function () {
gulp.src(['./blog/styles/*.scss', './common/styles/*.scss'])
.pipe(sass())
.pipe(gulp.dest('./dist/style.css'));
});
I'm importing files in blog.scss only using the name like this:
#import "forms"; //from the same directory
#import "_mixins"; //from ./common/styles
#import "_common"; //from ./common/styles
Right now I'm getting the following error:
file to import not found or unreadable: _mixins
As it seems it can't see .scss files inside ./common/styles directory...

You need to provide the relative path to the sass files that you're importing. So change the import code to be something like this:
#import "forms";
#import "../../common/styles/_mixins";
#import "../../common/styles/_common";
Then, since you are importing the files from ./common/styles you should only need gulp to target the scss file in ./blog/styles. So your gulp function could look something like this:
gulp.task('sass', function() {
gulp.src('./blog/styles/blog.scss')
.pipe(sass())
.pipe(gulp.dest('./dist/style.css'));
});

You could also use sass load paths for this.
Rather than have relative paths in your blog.scss, you set your gulp task to have a load path pointing to your common/styles directory and just call the partial imports as you had done originally.
The compiler task will look inside the current src directory for the imports and then look in the load paths.

Related

Is there any way of loading local resource files in dart? (not flutter)

I want to load the bytes of a file into a variable while testing my flutter application.
I can't use the assets directory as those are bundled with the app and require WidgetsFlutterBinding.ensureInitialized();
I tried searching the file manually with the path package, but this did not seem to work and was rather hacky. That is why i'm searching for a more official approach.
I was thinking way to complicated ...
As Chuck Batson commented, you can just use the path from the projects root for passing it into the (dart:io) File:
File loadResource(String relativePath) {
final filePath = path.join("test", "resources", relativePath);
return File(filePath);
}
(Notice: The above code makes use of the path package for constructing a file path.)

Loading txt file as an asset for a dart package

In flutter it's easy to load a .txt asset at runtime by specifying it or its folder in the pubspec.yaml file and then loading it with rootBundle. However, i'm working on a pure dart package, and I'm struggling to work out how to get the package to load a .txt file relative to it's own directory structure.
When I use the package in a separate dart command line application i'm working on, the relative path that I specified in one of the package source code files causes an error to be thrown that the txt file doesn't exist. I understand why this error is being thrown, because the relative path is interpreted as being from the command line application's root directory instead of the package's root directory, but i'm unsure of how to solve this without specifying the absolute path for the .txt file. I'd rather not specify the absolute path as it makes the package less portable.
Is there anything similar to flutter's asset loading for a pure dart package?
I think you need the resolveSymbolicLinks or resolveSymbolicLinksSync methods to decode the relative path and then use the resolved path to read the txt file:
import 'dart:io';
void main() async {
String file = '../lib/main.dart';
var path = Uri.parse('.').resolveUri(Uri.file(file)).toFilePath();
print(path);
if (path == '') path = '.';
var resolved = await File(path).resolveSymbolicLinks();
print(resolved);
File(resolved).readAsString().then((String contents) {
print(contents);
});
}

Bridging header finds file only with full path

I have a project that requires a local framework.
The project has the frameworks set up in a frameworks folder.
The new local framework is placed in it as well. The result folder structure is like that:
project
-- frameworks
---- theFramework.framework
------Headers
--------TheFramework.h
--------file1.h
--------file2.h
--------subfolder
----------Subfolder.h
----------Another.h
In the Bridging header the TheFramework.h and the Subfolder.h need to be imported.
It looks like that:
#ifndef Bridging_Header_h
#define Bridging_Header_h
#import <TheFramework/TheFramework.h> //This one is working just fine
#import <subfolder/Subfolder.h> //This one fails with File not found
#endif /* Bridging_Header_h */
If I change #import <subfolder/Subfolder.h> to #import <TheFramework/subfolder/Subfolder.h> then it seems that the file is found because then it fails in Subfolder.h where it tries to do #import <subfolder/Another.h> and fails again with a Not found
If I change subfolder/Another.h to TheFramework/subfolder/Another.h then it works here as well.
Now in my real life scenario I have 4 header files in different subfolders all importing around 50 other files so changing all of the imports will be... annoying.
Also if later we want to update to a newer version of the framework, all the changed imports will be lost.
Does someone know how I can solve that issue? Either by having the project find the imports as already defined or by for example defining a value somewhere that makes the compiler understand that subfolder/file.h equals to TheFramework/subfolder/file.h ?
Thank you!
Found the solution I had to add
"$(SRCROOT)/frameworks/theFramework.framework/Headers"
to my targets User Header Search Paths (Target -> Build Settings -> User header search paths.
This seems to let the compiler know where additionally search for header files including subfolders.

Compiling scss files in ASP.NET MVC action

are any alternatives for compile .scss files in action in ASP.NET MVC controller?
I have /Content folder with two files: main.scss and external.scss. In my controller I'm using NSASS package:
public ActionResult GetCss(string scssPath)
{
string scss = #"#import """ + Server.MapPath(scssPath) + #""";";
var compiler = new SassCompiler();
string compiled = compiler.Compile(source: scss);
return Content(compiled.ToString(), "text/css");
}
my view:
<link href="#Url.Action("GetCss", "Theme", new { scssPath="~/Content/sass/main.scss" })" rel="stylesheet" type="text/css" />
main.scss file:
#import "external";
I have error
Error: File to import not found or unreadable: external
I tried to write #import "external.scss" but same problem.
We have the same (or similar problem). The problem I'm seeing is that SassCompiler is trying to find the #import files relative to the current working directory, instead of relative to the file which contains the #import.
There might be a few ways around this depending on what you're trying to do.
My workaround consisted of making a temporary copy of the directory structure and then updating all the #import statements in each file to make them relative to the working directory before compiling.
UPDATE
I got this working without this hack by passing in all the paths to the 'includePaths' parameter. I had tried this before without success because I was using relative paths. If you use absolute paths then it works.
NSass is outdated. It was updated last time in 2013 and can't compile many new scss syntaxes, but if you want to compile few simple lines and have rest precompiled, here's simplest solution I came up with.
string scss = System.IO.File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + #"\Design\Scss\custom.scss");
scss += "$primary: #f80;$secondary: #0f2;";
Btw, if you would like to import other scss files into your main scss file, you can use following code but note I'm not good at Regex and thins it might have flaws.
scss = Regex.Replace(scss, "import \"", m=> m + AppDomain.CurrentDomain.BaseDirectory + #"Design\Scss\");

TypeScript bundling and minification?

Assume I have two files
AFile.ts
/// <reference path="ZFile.ts" />
new Z().Foo();
ZFile.ts
class Z
{
Foo() { }
}
Is there a way to generate all scripts in a single js file in the order it requires (need ZFile before AFile to get the definition of Z)?
In post build events I added a call to TypeScript compiler
tsc "..\Content\Scripts\Start.ts" --out "..\Content\Scripts\all.js"
In the bundle configuration I added
bundles.Add(new ScriptBundle("~/scripts/all").Include("~/Content/Scripts/all.js"));
On the _Layout.cshtml file I added
#Scripts.Render("~/Scripts/all")
And with that I got
<script src="/Scripts/all?v=vsTcwLvB3b7F7Kv9GO8..."></script>
Which is all my script in a single file.
The compiler does not minify, you have to use bundles and compile on Release or set
BundleTable.EnableOptimizations = true;
You can also minify using Web Essentials or grabbing the contents and minifing somewhere else.
Now VS Typescript Extension supports merging to one file.
Make sure that you have installed the extension Tools -> Extensions and Updates (VS2015 has it by default)
Go to the project properties and check Combine JavaScript output into file:
Important to have /// <reference /> (as in question), it helps tsc order files by dependencies before the merge.
Then for minimisation bundle can be used as usual:
bundles.Add(new ScriptBundle("~/bundles/finale").Include("~/js/all.js"));
and in view
#Scripts.Render("~/bundles/finale")
Use the --out parameter.
tsc AFile.ts ZFile.ts --out single.js
The typescript compiler will do the dependency navigation for you automatically.
Assuming all of your ts files are directly or indirectly under a folder called say 'ts' you could write a tt script which merged all of .js files(but not min.js) into a file myApp.js and all of your min.js files into myApp.min.js.
To obtain the ordering of files you could process subfolders thus:
string[] FolderOrder =
{
#"libs\utils\",
#"libs\controls\",
#"app\models",
#"app\viewmodels",
#".",
};

Resources