Skip to content

Commit d9e3bf9

Browse files
committed
Initial commit of plugin files
0 parents  commit d9e3bf9

8 files changed

+182
-0
lines changed

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.DS_Store
2+
3+
*.js.map
4+
5+
coverage
6+
lib-cov
7+
*.seed
8+
*.log
9+
*.csv
10+
*.dat
11+
*.out
12+
*.pid
13+
*.gz
14+
*.tgz
15+
*.tmp
16+
*.sublime-workspace
17+
tscommand*.tmp.txt
18+
.tscache/
19+
/lib/.d.ts
20+
21+
pids
22+
logs
23+
results
24+
scratch/
25+
.idea/
26+
.settings/
27+
.vscode/
28+
test-reports.xml
29+
30+
npm-debug.log
31+
node_modules
32+
docs/html

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2015 Telerik AD
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
SASS CSS pre-processor for NativeScript projects
2+
=======================================
3+
(Based on [nativescript-dev-less plugin](https://github.com/NativeScript/nativescript-dev-less))
4+
5+
----------
6+
This plugin uses the [node-sass compiler](https://www.npmjs.com/package/node-sass) to transpile SCSS files to CSS files in [NativeScript](https://www.nativescript.org/) projects.
7+
8+
9+
How to use
10+
----------
11+
```
12+
$ tns install sass
13+
```
14+
15+
The above command installs this module and installs the necessary NativeScript hooks. SASS CSS pre-processing of all `.scss` files inside `app` folder happens when the project is prepared for build (including LiveSync, Emulate and Watch commands).
16+
17+
**NOTE: SASS @import syntax**
18+
In some cases, the current version of node-sass requires `@import` statements to explicitly include the filename extension (like `.scss`). This occurs if files with the same name exist in the same path.
19+
20+
Example:
21+
`variables.scss`
22+
`variables.css`
23+
`_variables.scss`
24+
25+
Node-sass will throw an error if the `@import variables;` syntax is used. As a workaround, use an explicit filename, like: `@import variables.scss;`
26+
27+
This will be fixed in node-sass 3.5. [See this issue for more detail](https://github.com/sass/node-sass/issues/1222).
28+

lib/before-prepare.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var converter = require('./converter');
2+
3+
module.exports = function ($logger, $projectData, $usbLiveSyncService) {
4+
return converter.convert($logger, $projectData.projectDir);
5+
}

lib/converter.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
exports.convert = convert;
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var sass = require('node-sass');
6+
var glob = require('glob');
7+
8+
function convert(logger, projectDir, options) {
9+
return new Promise(function (resolve, reject) {
10+
options = options || {};
11+
12+
var sassFilesPath = path.join(projectDir, 'app/**/*.scss');
13+
var sassImportPath = path.join(projectDir, 'app/');
14+
console.log("SASS Import Path", sassImportPath);
15+
16+
var sassFiles = glob.sync(sassFilesPath).filter(function(fileName){
17+
return fileName.indexOf("App_Resources") === -1;
18+
});
19+
20+
if(sassFiles.length === 0){
21+
//No sass files in project; skip parsing
22+
resolve();
23+
} else {
24+
var i = 0;
25+
var loopSassFilesAsync = function(sassFiles){
26+
parseSass(sassFiles[i], sassImportPath, function(e){
27+
if(e !== undefined){
28+
//Error in the LESS parser; Reject promise
29+
reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e));
30+
}
31+
32+
i++; //Increment loop counter
33+
34+
if(i < sassFiles.length){
35+
loopSassFilesAsync(sassFiles);
36+
} else {
37+
//All files have been processed; Resolve promise
38+
resolve();
39+
}
40+
});
41+
}
42+
43+
loopSassFilesAsync(sassFiles);
44+
}
45+
});
46+
}
47+
48+
function parseSass(filePath, importPath, callback){
49+
var sassFileContent = fs.readFileSync(filePath, { encoding: 'utf8'});
50+
var cssFilePath = filePath.replace('.scss', '.css');
51+
52+
sass.render({
53+
data: sassFileContent,
54+
includePaths: [importPath],
55+
outFile: cssFilePath,
56+
outputStyle: 'compressed'
57+
}, function (e, output) {
58+
if(e) {
59+
//Callback with error
60+
callback(e);
61+
}
62+
63+
if(output === null){
64+
//No CSS content in converted scss file; No need to write file
65+
callback();
66+
} else {
67+
fs.writeFile(cssFilePath, output.css, 'utf8', function(){
68+
//File done writing
69+
callback();
70+
});
71+
}
72+
});
73+
}

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "nativescript-dev-sass",
3+
"version": "0.1.0",
4+
"description": "SASS CSS pre-processor for NativeScript projects.",
5+
"scripts": {
6+
"test": "exit 0",
7+
"postinstall": "node postinstall.js",
8+
"preuninstall": "node preuninstall.js"
9+
},
10+
"nativescript": {
11+
"hooks": [
12+
{
13+
"type": "before-prepare",
14+
"script": "lib/before-prepare.js",
15+
"inject": true
16+
}
17+
]
18+
},
19+
"license": "Apache-2.0",
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/ToddAnglin/nativescript-dev-sass.git"
23+
},
24+
"dependencies": {
25+
"node-sass": "*",
26+
"glob": "*",
27+
"nativescript-hook": "^0.2.0"
28+
}
29+
}

postinstall.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('nativescript-hook')(__dirname).postinstall();

preuninstall.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('nativescript-hook')(__dirname).preuninstall();

0 commit comments

Comments
 (0)